Skip to content

Commit

Permalink
Near-plugins integration (#845)
Browse files Browse the repository at this point in the history
* Eth-prover: replace `admin_controlled` with near plugins

* Eth2-client: replace `admin_controlled` with near plugins

* Eth-client: replace `admin_controlled` with near plugins

* Fix prover tests

* Add prover test to CI

* Update wasm builds

* Fix prover tests after merge

* Prover: update acl plugins

* Eth2Client: update acl plugins

* EthClient: Update near plugins

* Revert script changes

* Update contracts builds

* Update contracts builds

* Update near plugins and it's roles

* Update contracts builds

* Update contract build

* Clean roles for eth-client contracts

* Update contract build

* Fix test

* Remove unused deps

* Update contract build

* Minor roles fixes

* Update contracts builds
  • Loading branch information
karim-en authored Nov 1, 2023
1 parent e38a17a commit bf7cc1e
Show file tree
Hide file tree
Showing 20 changed files with 407 additions and 356 deletions.
72 changes: 65 additions & 7 deletions contracts/near/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions contracts/near/eth-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ edition = "2021"
crate-type = ["cdylib", "rlib"]

[dependencies]
eth-types = { path = "../eth-types", default-features = false }
admin-controlled = { path = "../admin-controlled" }
eth-types = { path = "../eth-types", default-features = false }
near-sdk = "4.1.1"
borsh = "0.9.3"
rlp = "0.5.2"
Expand All @@ -18,19 +17,22 @@ arrutil = "0.1.2"
ethash = { git = "https://github.com/aurora-is-near/rust-ethash", tag = "0.2.0", default-features = false }
hex = "0.4.0"
rustc-hex = "2.1.0"
near-plugins = { git = "https://github.com/aurora-is-near/near-plugins", tag = "v0.1.0" }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
# serde's Serialize and Deserialize traits are required for `near_bindgen` macro for non-wasm32 targets
serde = { version = "1.0", features = ["derive"] }

[dev-dependencies]
tokio = "1.19"
primitive-types = { version = "0.7.2", default-features = false, features = ["rlp"] }
primitive-types = { version = "0.7.2", default-features = false, features = [
"rlp",
] }
web3 = "0.18.0"
lazy_static = "1.4.0"
serde_json = "1.0"
indicatif = "0.14"

[features]
default = []
expensive_tests = []
expensive_tests = []
1 change: 1 addition & 0 deletions contracts/near/eth-client/src/data/12965004.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions contracts/near/eth-client/src/data/12965005.json

Large diffs are not rendered by default.

49 changes: 34 additions & 15 deletions contracts/near/eth-client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use admin_controlled::Mask;
use borsh::{BorshDeserialize, BorshSerialize};
use eth_types::*;
use near_plugins::{
access_control, access_control_any, pause, AccessControlRole, AccessControllable, Pausable,
Upgradable,
};
use near_sdk::collections::UnorderedMap;
use near_sdk::{assert_self, AccountId};
use near_sdk::{env, near_bindgen, PanicOnDefault};

#[cfg(not(target_arch = "wasm32"))]
use serde::{Deserialize, Serialize};
use near_sdk::serde::{Deserialize, Serialize};
use near_sdk::{env, near_bindgen, AccountId, PanicOnDefault};

#[cfg(not(target_arch = "wasm32"))]
#[cfg(test)]
Expand Down Expand Up @@ -59,10 +59,27 @@ pub struct HeaderInfo {
pub number: u64,
}

const PAUSE_ADD_BLOCK_HEADER: Mask = 1;
#[derive(AccessControlRole, Deserialize, Serialize, Copy, Clone)]
#[serde(crate = "near_sdk::serde")]
pub enum Role {
PauseManager,
UpgradableCodeStager,
UpgradableCodeDeployer,
UnrestrictedAddBlockHeader,
DAO,
}

#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)]
#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault, Pausable, Upgradable)]
#[access_control(role_type(Role))]
#[pausable(manager_roles(Role::PauseManager))]
#[upgradable(access_control_roles(
code_stagers(Role::UpgradableCodeStager, Role::DAO),
code_deployers(Role::UpgradableCodeDeployer, Role::DAO),
duration_initializers(Role::DAO),
duration_update_stagers(Role::DAO),
duration_update_appliers(Role::DAO),
))]
pub struct EthClient {
/// Whether client validates the PoW when accepting the header. Should only be set to `false`
/// for debugging, testing, diagnostic purposes when used with Ganache or in PoA testnets
Expand Down Expand Up @@ -102,7 +119,8 @@ pub struct EthClient {
/// others will be immediately rejected, used in PoA testnets
trusted_signer: Option<AccountId>,
/// Mask determining all paused functions
paused: Mask,
#[deprecated]
paused: u128,
}

#[near_bindgen]
Expand All @@ -122,6 +140,7 @@ impl EthClient {
let header: BlockHeader = rlp::decode(first_header.as_slice()).unwrap();
let header_hash = header.hash.unwrap().clone();
let header_number = header.number;
#[allow(deprecated)]
let mut res = Self {
validate_ethash,
dags_start_epoch,
Expand All @@ -135,7 +154,7 @@ impl EthClient {
headers: UnorderedMap::new(b"h".to_vec()),
infos: UnorderedMap::new(b"i".to_vec()),
trusted_signer,
paused: Mask::default(),
paused: 0,
};
res.canonical_header_hashes
.insert(&header_number, &header_hash);
Expand All @@ -150,6 +169,8 @@ impl EthClient {
number: header_number,
},
);

res.acl_init_super_admin(near_sdk::env::predecessor_account_id());
res
}

Expand Down Expand Up @@ -198,14 +219,14 @@ impl EthClient {
/// Add the block header to the client.
/// `block_header` -- RLP-encoded Ethereum header;
/// `dag_nodes` -- dag nodes with their merkle proofs.
#[pause(except(roles(Role::UnrestrictedAddBlockHeader, Role::DAO)))]
#[result_serializer(borsh)]
pub fn add_block_header(
&mut self,
#[serializer(borsh)] block_header: Vec<u8>,
#[serializer(borsh)] dag_nodes: Vec<DoubleNodeWithMerkleProof>,
) {
env::log_str("Add block header");
self.check_not_paused(PAUSE_ADD_BLOCK_HEADER);
let header: BlockHeader = rlp::decode(block_header.as_slice()).unwrap();

if let Some(trusted_signer) = &self.trusted_signer {
Expand All @@ -229,21 +250,21 @@ impl EthClient {
self.record_header(header);
}

#[access_control_any(roles(Role::DAO))]
pub fn update_trusted_signer(&mut self, trusted_signer: Option<AccountId>) {
assert_self();
self.trusted_signer = trusted_signer;
}

pub fn get_trusted_signer(&self) -> Option<AccountId> {
self.trusted_signer.clone()
}

#[access_control_any(roles(Role::DAO))]
pub fn update_dags_merkle_roots(
&mut self,
#[serializer(borsh)] dags_start_epoch: u64,
#[serializer(borsh)] dags_merkle_roots: Vec<H128>,
) {
assert_self();
self.dags_start_epoch = dags_start_epoch;
self.dags_merkle_roots = dags_merkle_roots;
}
Expand Down Expand Up @@ -454,5 +475,3 @@ impl EthClient {
(H256(pair.0), H256(pair.1))
}
}

admin_controlled::impl_admin_controlled!(EthClient, paused);
5 changes: 4 additions & 1 deletion contracts/near/eth-client/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ fn get_context() -> VMContext {
signer_account_pk: "ed25519:6E8sCci9badyRkXb3JoRpBj5p8C6Tw41ELDZoiihKEtp"
.parse()
.unwrap(),
predecessor_account_id: "carol.near".parse().unwrap(),
predecessor_account_id: "alice.near".parse().unwrap(),
input: vec![],
block_index: 0,
block_timestamp: 0,
Expand Down Expand Up @@ -278,6 +278,8 @@ fn add_dags_merkle_roots() {

#[test]
fn update_dags_merkle_roots() {
use near_plugins::AccessControllable;

let block = read_block(format!("./src/data/{}.json", 12_965_000).to_string());
let mut context = get_context();
context.predecessor_account_id = context.current_account_id.clone();
Expand All @@ -295,6 +297,7 @@ fn update_dags_merkle_roots() {
None,
);

contract.acl_grant_role(crate::Role::DAO.into(), context.predecessor_account_id);
contract.update_dags_merkle_roots(0, dmr.dag_merkle_roots.clone());

for i in 0..699 {
Expand Down
8 changes: 5 additions & 3 deletions contracts/near/eth-prover/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
[package]
name = "eth-prover"
version = "0.1.0"
version = "0.2.0"
authors = ["Near Inc <[email protected]>"]
edition = "2021"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
eth-types = { path = "../eth-types", default-features = false }
admin-controlled = { path = "../admin-controlled" }
eth-types = { path = "../eth-types", default-features = false }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
near-sdk = "4.1.1"
borsh = "0.9.3"
rlp = "0.5.2"
hex = "0.4.2"
near-plugins = { git = "https://github.com/aurora-is-near/near-plugins", tag = "v0.1.0" }

[dev-dependencies]
hex = { version = "0.4.3", features = ["serde"] }
indicatif = "0.14"
lazy_static = "*"
near-crypto = "0.16.0"
near-primitives = "0.16.0"
workspaces = "0.6.0"
tokio = { version = "1.18.1", features = ["full"] }

[features]
default = []
Expand Down
2 changes: 1 addition & 1 deletion contracts/near/eth-prover/rust-toolchain
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[toolchain]
channel = "1.68.0"
channel = "1.69.0"
Loading

0 comments on commit bf7cc1e

Please sign in to comment.