Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup anchor for ink! smart contracts #30

Merged
merged 67 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
105edad
Merge pull request #1 from Phala-Network/main
GuiGou12358 Jun 20, 2023
a9cc209
rollup anchor for ink smart contract
GuiGou12358 Jul 14, 2023
87dc18e
rollup anchor for ink smart contract
GuiGou12358 Jul 14, 2023
94f34c6
Merge remote-tracking branch 'origin/main'
GuiGou12358 Jul 14, 2023
8a6966b
rollup anchor for ink smart contract
GuiGou12358 Jul 14, 2023
385ace8
Add artifacts
GuiGou12358 Jul 15, 2023
2e350ab
Uodsate documentation
GuiGou12358 Jul 15, 2023
5fb1408
remove useless digit in the version number
GuiGou12358 Jul 21, 2023
d56c302
Refactoring:
GuiGou12358 Jul 21, 2023
860e654
reduce the visibility of 'Internal Traits' to only the crate
GuiGou12358 Jul 21, 2023
7491c97
allow by default clippy::inline_fn_without_body required in the attri…
GuiGou12358 Jul 21, 2023
8a598a7
refactor to use stable rust
GuiGou12358 Jul 21, 2023
78db124
refactor to use stable rust
GuiGou12358 Jul 28, 2023
8a4146f
refactor to use stable rust
GuiGou12358 Jul 28, 2023
d798b75
refactor to use stable rust
GuiGou12358 Jul 28, 2023
92cc283
move the integration tests in the tests directory
GuiGou12358 Jul 28, 2023
b3ee38b
update readme
GuiGou12358 Jul 28, 2023
fc67f1f
improvements
GuiGou12358 Jul 28, 2023
54d6449
improve the separation between rollup and meta transaction
GuiGou12358 Jul 28, 2023
73874a4
rename meta transaction
GuiGou12358 Jul 28, 2023
e049df1
improvement
GuiGou12358 Jul 28, 2023
10dd288
cargo fmt
GuiGou12358 Jul 31, 2023
efe5872
Update readme after the changes
GuiGou12358 Jul 31, 2023
92306ef
Update contract test after the changes
GuiGou12358 Jul 31, 2023
61016e7
Update crate for the phat contract after the changes (stable rust ver…
GuiGou12358 Jul 31, 2023
48f54d3
Update readme after the changes (stable rust version for ink! smart c…
GuiGou12358 Jul 31, 2023
d883605
Update the tests after the changes (stable rust version for ink! smar…
GuiGou12358 Jul 31, 2023
39ab8a3
Update the readme after the changes (stable rust version for ink! sma…
GuiGou12358 Jul 31, 2023
4a808f8
Update the artifacts after the changes (stable rust version for ink! …
GuiGou12358 Jul 31, 2023
9c1aa57
Merge pull request #2 from GuiGou12358/stable-rust
GuiGou12358 Aug 1, 2023
6aed3b3
Improve the readme
GuiGou12358 Aug 2, 2023
ef8e601
Check the tests
GuiGou12358 Aug 2, 2023
f1a4791
Delete .env file committed by mistake
GuiGou12358 Aug 7, 2023
a7d71e5
Update readme
GuiGou12358 Aug 7, 2023
ad88ecb
Use cargo contract 3.2.0 and rust toolchain 1.72.0
GuiGou12358 Sep 5, 2023
1a32aab
Use cargo contract 3.2.0 and rust toolchain 1.72.0
GuiGou12358 Sep 5, 2023
f6ede45
Use last version of ink!
GuiGou12358 Sep 5, 2023
3d9309c
Use last version of ink! and update dependency
GuiGou12358 Sep 5, 2023
e7a7e45
Update artifacts with last version of rust toolchain and ink!
GuiGou12358 Sep 5, 2023
6575d29
Update artifacts with last version of rust toolchain and ink!
GuiGou12358 Sep 5, 2023
280d342
Use last version of ink! and update dependencies
GuiGou12358 Sep 5, 2023
6d63ff8
Update pallet id for local substrate contract node
GuiGou12358 Sep 5, 2023
c2d8088
Update dependencies
GuiGou12358 Sep 5, 2023
d958620
fix cargo clippy issue
GuiGou12358 Sep 5, 2023
01ed1b1
fix typo
GuiGou12358 Sep 14, 2023
d6e51c3
Improve readme
GuiGou12358 Sep 14, 2023
9785772
reuse the encoded value
GuiGou12358 Sep 14, 2023
d3d8fe1
Remove the useless boolean in the return of some methods
GuiGou12358 Sep 14, 2023
105db1a
Fix a typo in some names containing 'rollup' (missing l)
GuiGou12358 Sep 14, 2023
3277426
remove useless field
GuiGou12358 Sep 14, 2023
8c6705b
Improve the error handling of method get_ecdsa_public_key
GuiGou12358 Sep 14, 2023
5c8892d
Use an enum instead of struct for implementing the actions
GuiGou12358 Sep 14, 2023
97c8396
Fix tests after the different updates
GuiGou12358 Sep 14, 2023
7acd72a
Updated artifacts after the different changes
GuiGou12358 Sep 14, 2023
e680f39
rename use_meta_tx by ensure_meta_tx_valid
GuiGou12358 Sep 15, 2023
fbc5889
rename use_meta_tx by ensure_meta_tx_valid
GuiGou12358 Sep 15, 2023
3a18b8b
refactor a little bit the test
GuiGou12358 Sep 15, 2023
1707c44
refactor a little bit the code
GuiGou12358 Sep 15, 2023
c33cff5
strict comparison between nonces in meta transaction
GuiGou12358 Sep 16, 2023
2ed56fd
update artifacts
GuiGou12358 Sep 16, 2023
4fd7174
use the last released pink-subrpc version
GuiGou12358 Sep 18, 2023
3494f55
remove commented dependency
GuiGou12358 Sep 18, 2023
670be39
Merge the traits KvStore, MessageQueue and RollupAnchor for simplific…
GuiGou12358 Sep 18, 2023
56b09c6
Merge the traits KvStore, MessageQueue and RollupAnchor for simplific…
GuiGou12358 Sep 18, 2023
1d7ebfa
Add the consumer contract address to add a domain separation and repl…
GuiGou12358 Sep 21, 2023
9f6db49
Get the attestor address used in direct, without using meta transaction
GuiGou12358 Sep 21, 2023
c244a2e
rename the method pub_key_to_ss58 to get_ecdsa_account_id
GuiGou12358 Sep 22, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ink/artifacts/test_oracle/test_oracle.contract

Large diffs are not rendered by default.

667 changes: 207 additions & 460 deletions ink/artifacts/test_oracle/test_oracle.json

Large diffs are not rendered by default.

Binary file modified ink/artifacts/test_oracle/test_oracle.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions ink/contracts/test_oracle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ phat_rollup_anchor_ink = { path = "../../crates/phat_rollup_anchor_ink", default
[dev-dependencies]
ink_e2e = { version = "4.3.0" }
hex-literal = { version = "0.4.1" }
subxt-signer = { version = "0.31.0" }

[lib]
path = "lib.rs"
Expand Down
83 changes: 25 additions & 58 deletions ink/contracts/test_oracle/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ pub mod test_oracle {
use scale::{Decode, Encode};

use phat_rollup_anchor_ink::traits::{
kv_store, kv_store::*, message_queue, message_queue::*, meta_transaction,
meta_transaction::*, rollup_anchor, rollup_anchor::*,
meta_transaction, meta_transaction::*, rollup_anchor, rollup_anchor::*,
};

pub type TradingPairId = u32;

pub const MANAGER_ROLE: RoleType = ink::selector_id!("MANAGER_ROLE");

/// Events emitted when a price is received
#[ink(event)]
pub struct PriceReceived {
Expand All @@ -38,22 +39,22 @@ pub mod test_oracle {
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum ContractError {
AccessControlError(AccessControlError),
MessageQueueError(MessageQueueError),
RollupAnchorError(RollupAnchorError),
MetaTransactionError(MetaTransactionError),
MissingTradingPair,
}
/// convertor from MessageQueueError to ContractError
impl From<MessageQueueError> for ContractError {
fn from(error: MessageQueueError) -> Self {
ContractError::MessageQueueError(error)
}
}
/// convertor from MessageQueueError to ContractError
impl From<AccessControlError> for ContractError {
fn from(error: AccessControlError) -> Self {
ContractError::AccessControlError(error)
}
}
/// convertor from RollupAnchorError to ContractError
impl From<RollupAnchorError> for ContractError {
fn from(error: RollupAnchorError) -> Self {
ContractError::RollupAnchorError(error)
}
}
/// convertor from MetaTxError to ContractError
impl From<MetaTransactionError> for ContractError {
fn from(error: MetaTransactionError) -> Self {
Expand Down Expand Up @@ -118,7 +119,7 @@ pub mod test_oracle {
#[storage_field]
access: access_control::Data,
#[storage_field]
kv_store: kv_store::Data,
rollup_anchor: rollup_anchor::Data,
#[storage_field]
meta_transaction: meta_transaction::Data,
trading_pairs: Mapping<TradingPairId, TradingPair>,
Expand Down Expand Up @@ -187,13 +188,8 @@ pub mod test_oracle {
}

#[ink(message)]
pub fn register_attestor(
&mut self,
account_id: AccountId,
ecdsa_public_key: [u8; 33],
) -> Result<(), ContractError> {
pub fn register_attestor(&mut self, account_id: AccountId) -> Result<(), ContractError> {
AccessControl::grant_role(self, ATTESTOR_ROLE, Some(account_id))?;
self.register_ecdsa_public_key(account_id, ecdsa_public_key)?;
Ok(())
}

Expand All @@ -208,8 +204,6 @@ pub mod test_oracle {
}
}

impl KvStore for TestOracle {}
impl MessageQueue for TestOracle {}
impl RollupAnchor for TestOracle {}
impl MetaTransaction for TestOracle {}

Expand Down Expand Up @@ -266,7 +260,7 @@ pub mod test_oracle {
pub id: u32,
}

impl message_queue::EventBroadcaster for TestOracle {
impl rollup_anchor::EventBroadcaster for TestOracle {
fn emit_event_message_queued(&self, id: u32, data: Vec<u8>) {
self.env().emit_event(MessageQueued { id, data });
}
Expand Down Expand Up @@ -794,22 +788,12 @@ pub mod test_oracle {
.expect("instantiate failed")
.account_id;

// register the ecda public key because I am not able to retrieve if from the account id
// Alice
// Alice is the attestor
// use the ecsda account because we are not able to verify the sr25519 signature
let from = ink::primitives::AccountId::from(
Signer::<PolkadotConfig>::account_id(&ink_e2e::alice()).0,
);
let ecdsa_public_key: [u8; 33] = hex_literal::hex!(
"037051bed73458951b45ca6376f4096c85bf1a370da94d5336d04867cfaaad019e"
Signer::<PolkadotConfig>::account_id(&subxt_signer::ecdsa::dev::alice()).0,
);

let register_ecdsa_public_key = build_message::<TestOracleRef>(contract_acc_id.clone())
.call(|oracle| oracle.register_ecdsa_public_key(from, ecdsa_public_key));
client
.call(&ink_e2e::bob(), register_ecdsa_public_key, 0, None)
.await
.expect("We should be able to register the ecdsa public key");

// prepare the meta transaction
let data = u8::encode(&5);
let prepare_meta_tx = build_message::<TestOracleRef>(contract_acc_id.clone())
Expand All @@ -819,19 +803,15 @@ pub mod test_oracle {
.await
.expect("We should be able to prepare the meta tx");

let (request, hash) = result
let (request, _hash) = result
.return_value()
.expect("Expected value when preparing meta tx");

assert_eq!(0, request.nonce);
assert_eq!(from, request.from);
assert_eq!(contract_acc_id, request.to);
assert_eq!(&data, &request.data);

let expected_hash = hex_literal::hex!(
"17cb4f6eae2f95ba0fbaee9e0e51dc790fe752e7386b72dcd93b9669450c2ccf"
);
assert_eq!(&expected_hash, &hash.as_ref());

Ok(())
}

Expand All @@ -850,22 +830,12 @@ pub mod test_oracle {
.expect("instantiate failed")
.account_id;

// register the ecda public key because I am not able to retrieve if from the account id
// Alice is the attestor
// use the ecsda account because we are not able to verify the sr25519 signature
let from = ink::primitives::AccountId::from(
Signer::<PolkadotConfig>::account_id(&ink_e2e::alice()).0,
);
let ecdsa_public_key: [u8; 33] = hex_literal::hex!(
"037051bed73458951b45ca6376f4096c85bf1a370da94d5336d04867cfaaad019e"
Signer::<PolkadotConfig>::account_id(&subxt_signer::ecdsa::dev::alice()).0,
);

let register_ecdsa_public_key = build_message::<TestOracleRef>(contract_acc_id.clone())
.call(|oracle| oracle.register_ecdsa_public_key(from, ecdsa_public_key));
client
.call(&ink_e2e::charlie(), register_ecdsa_public_key, 0, None)
.await
.expect("We should be able to register the ecdsa public key");

// add the role => it should be succeed
let grant_role = build_message::<TestOracleRef>(contract_acc_id.clone())
.call(|oracle| oracle.grant_role(ATTESTOR_ROLE, Some(from)));
Expand All @@ -883,21 +853,18 @@ pub mod test_oracle {
.await
.expect("We should be able to prepare the meta tx");

let (request, hash) = result
let (request, _hash) = result
.return_value()
.expect("Expected value when preparing meta tx");

assert_eq!(0, request.nonce);
assert_eq!(from, request.from);
assert_eq!(contract_acc_id, request.to);
assert_eq!(&data, &request.data);

let expected_hash = hex_literal::hex!(
"c91f57305dc05a66f1327352d55290a250eb61bba8e3cf8560a4b8e7d172bb54"
);
assert_eq!(&expected_hash, &hash.as_ref());

// signature by Alice of previous hash
let signature : [u8; 65] = hex_literal::hex!("c9a899bc8daa98fd1e819486c57f9ee889d035e8d0e55c04c475ca32bb59389b284d18d785a9db1bdd72ce74baefe6a54c0aa2418b14f7bc96232fa4bf42946600");
// Alice signs the message
let keypair = subxt_signer::ecdsa::dev::alice();
let signature = keypair.sign(&scale::Encode::encode(&request)).0;

// do the meta tx
let meta_tx_rollup_cond_eq = build_message::<TestOracleRef>(contract_acc_id.clone())
Expand Down
3 changes: 2 additions & 1 deletion ink/crates/phat_rollup_anchor_ink/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ scale = { package = "parity-scale-codec", version = "3", default-features = fals
scale-info = { version = "2.6", default-features = false, features = ["derive"], optional = true }
kv-session = { package = "pink-kv-session", version = "0.2" }

#openbrush = { git = "https://github.com/727-Ventures/openbrush-contracts", tag = "4.0.0-beta", features = ["ownable", "access_control"], default-features = false }
openbrush = { git = "https://github.com/Brushfam/openbrush-contracts", version = "4.0.0-beta", features = ["ownable", "access_control"], default-features = false }

[dev-dependencies]
hex-literal = "0.4.1"
ink_e2e = { version = "4.3.0" }
subxt-signer = { version = "0.31.0" }

[lib]
path = "src/lib.rs"
Expand Down
47 changes: 19 additions & 28 deletions ink/crates/phat_rollup_anchor_ink/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Library for Ink! smart contract to help you build [Phat Rollup Anchor ](https://github.com/Phala-Network/phat-offchain-rollup/
)deployed on the Substrate pallet Contracts.
This library uses the [OpenBrush](https://learn.brushfam.io/docs/OpenBrush) library with the features `ownable` and `access_control`
It provides the following traits for:
It provides the following features for:
- `KvStore`: key-value store that allows offchain Phat Contracts to perform read/write operations.
- `MessageQueue`: Message Queue, enabling a request-response programming model for the smart-contract while ensuring that each request received exactly one response. It uses the KV Store to save the messages.
- `RollupAnchor`: Use the kv-store and the message queue to allow offchain's rollup transactions.
Expand Down Expand Up @@ -58,7 +58,7 @@ std = [
### Add imports

Use `openbrush::contract` macro instead of `ink::contract`.
Import everything from `openbrush::contracts::access_control`, `openbrush::contracts::ownable`, `phat_rollup_anchor_ink::traits::kv_store`, `phat_rollup_anchor_ink::traits::message_queue`, `phat_rollup_anchor_ink::traits::meta_transaction`, `phat_rollup_anchor_ink::traits::rollup_anchor`.
Import everything from `openbrush::contracts::access_control`, `openbrush::contracts::ownable`, `phat_rollup_anchor_ink::traits::meta_transaction`, `phat_rollup_anchor_ink::traits::rollup_anchor`.

```rust
#![cfg_attr(not(feature = "std"), no_std, no_main)]
Expand All @@ -73,8 +73,6 @@ pub mod test_oracle {
use scale::{Decode, Encode};

use phat_rollup_anchor_ink::traits::{
kv_store, kv_store::*,
message_queue, message_queue::*,
meta_transaction, meta_transaction::*,
rollup_anchor, rollup_anchor::*
};
Expand All @@ -94,7 +92,7 @@ pub struct TestOracle {
#[storage_field]
access: access_control::Data,
#[storage_field]
kv_store: kv_store::Data,
rollup_anchor: rollup_anchor::Data,
#[storage_field]
meta_transaction: meta_transaction::Data,
...
Expand All @@ -105,8 +103,6 @@ pub struct TestOracle {
Inherit implementation of the traits. You can customize (override) methods in this `impl` block.

```rust
impl KvStore for TestOracle {}
impl MessageQueue for TestOracle {}
impl RollupAnchor for TestOracle {}
impl MetaTransaction for TestOracle {}
```
Expand All @@ -129,8 +125,8 @@ impl TestOracle {

### Traits to implement

### Trait for the message queue
Implement the `message_queue::EventBroadcaster` trait to emit the events when a message is pushed in the queue and when a message is proceeded.
### Trait for the rollup anchor
Implement the `rollup_anchor::EventBroadcaster` trait to emit the events when a message is pushed in the queue and when a message is proceeded.
If you don't want to emit the events, you can put an empty block in the methods `emit_event_message_queued` and `emit_event_message_processed_to`.

```rust
Expand All @@ -147,7 +143,7 @@ pub struct MessageProcessedTo {
pub id: u32,
}

impl message_queue::EventBroadcaster for TestOracle {
impl rollup_anchor::EventBroadcaster for TestOracle {

fn emit_event_message_queued(&self, id: u32, data: Vec<u8>){
self.env().emit_event(MessageQueued { id, data });
Expand All @@ -159,7 +155,7 @@ impl message_queue::EventBroadcaster for TestOracle {

}
```
### Traits for the rollup anchor

Implement the `rollup_anchor::MessageHandler` trait to put your business logic when a message is received.
Here an example when the Oracle receives a message with the price feed.

Expand Down Expand Up @@ -239,8 +235,7 @@ pub mod test_oracle {
use scale::{Decode, Encode};

use phat_rollup_anchor_ink::traits::{
kv_store, kv_store::*, message_queue, message_queue::*, meta_transaction,
meta_transaction::*, rollup_anchor, rollup_anchor::*,
meta_transaction, meta_transaction::*, rollup_anchor, rollup_anchor::*,
};

pub type TradingPairId = u32;
Expand All @@ -264,25 +259,25 @@ pub mod test_oracle {
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum ContractError {
AccessControlError(AccessControlError),
MessageQueueError(MessageQueueError),
RollupAnchorError(RollupAnchorError),
MetaTransactionError(MetaTransactionError),
MissingTradingPair,
}

/// convertor from MessageQueueError to ContractError
impl From<MessageQueueError> for ContractError {
fn from(error: MessageQueueError) -> Self {
ContractError::MessageQueueError(error)
}
}

/// convertor from MessageQueueError to ContractError
impl From<AccessControlError> for ContractError {
fn from(error: AccessControlError) -> Self {
ContractError::AccessControlError(error)
}
}

/// convertor from RollupAnchorError to ContractError
impl From<RollupAnchorError> for ContractError {
fn from(error: RollupAnchorError) -> Self {
ContractError::RollupAnchorError(error)
}
}

/// convertor from MetaTxError to ContractError
impl From<MetaTransactionError> for ContractError {
fn from(error: MetaTransactionError) -> Self {
Expand Down Expand Up @@ -312,7 +307,7 @@ pub mod test_oracle {
trading_pair_id: TradingPairId,
/// price of the trading pair
price: Option<u128>,
/// when the price is read
/// error when the price is read
err_no: Option<u128>,
}

Expand Down Expand Up @@ -348,7 +343,7 @@ pub mod test_oracle {
#[storage_field]
access: access_control::Data,
#[storage_field]
kv_store: kv_store::Data,
rollup_anchor: rollup_anchor::Data,
#[storage_field]
meta_transaction: meta_transaction::Data,
trading_pairs: Mapping<TradingPairId, TradingPair>,
Expand Down Expand Up @@ -438,10 +433,6 @@ pub mod test_oracle {
}
}

impl KvStore for TestOracle {}

impl MessageQueue for TestOracle {}

impl RollupAnchor for TestOracle {}

impl MetaTransaction for TestOracle {}
Expand Down Expand Up @@ -499,7 +490,7 @@ pub mod test_oracle {
pub id: u32,
}

impl message_queue::EventBroadcaster for TestOracle {
impl rollup_anchor::EventBroadcaster for TestOracle {
fn emit_event_message_queued(&self, id: u32, data: Vec<u8>) {
self.env().emit_event(MessageQueued { id, data });
}
Expand Down
28 changes: 0 additions & 28 deletions ink/crates/phat_rollup_anchor_ink/src/traits/kv_store.rs

This file was deleted.

Loading