From 49f757965d4ccd8acadfd37eb20f7cf72535943b Mon Sep 17 00:00:00 2001 From: karim-en Date: Tue, 15 Nov 2022 00:20:25 +0000 Subject: [PATCH] Eth-client: replace `admin_controlled` with near plugins --- contracts/near/Cargo.lock | 88 ++---------------------- contracts/near/eth-client/Cargo.toml | 5 +- contracts/near/eth-client/rust-toolchain | 4 +- contracts/near/eth-client/src/lib.rs | 15 ++-- contracts/near/eth-client/src/tests.rs | 2 +- contracts/near/rust-toolchain | 4 +- 6 files changed, 24 insertions(+), 94 deletions(-) diff --git a/contracts/near/Cargo.lock b/contracts/near/Cargo.lock index d517fefe7..feeac7ed5 100644 --- a/contracts/near/Cargo.lock +++ b/contracts/near/Cargo.lock @@ -963,14 +963,16 @@ dependencies = [ name = "eth-client" version = "0.1.0" dependencies = [ - "admin-controlled", "arrutil", + "borsh", "eth-types", "ethash", "futures", "hex 0.4.2", "indicatif", "lazy_static", + "near-plugins", + "near-plugins-derive", "near-sdk", "primal", "rlp", @@ -978,7 +980,6 @@ dependencies = [ "serde", "serde_json", "web3", - "wee_alloc", ] [[package]] @@ -992,11 +993,12 @@ dependencies = [ "near-crypto 0.14.0", "near-plugins", "near-plugins-derive", - "near-primitives 0.14.0", + "near-primitives 0.15.0", "near-sdk", "rlp", "serde", "serde_json", + "tokio 1.20.1", "workspaces", ] @@ -2376,35 +2378,6 @@ dependencies = [ "thiserror", ] -[[package]] -name = "near-primitives" -version = "0.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ad1a9a1640539c81f065425c31bffcfbf6b31ef1aeaade59ce905f5df6ac860" -dependencies = [ - "borsh", - "byteorder", - "bytesize", - "chrono", - "derive_more 0.99.17", - "easy-ext", - "hex 0.4.2", - "near-crypto 0.14.0", - "near-primitives-core 0.14.0", - "near-rpc-error-macro 0.14.0", - "near-vm-errors 0.14.0", - "num-rational", - "once_cell", - "primitive-types 0.10.1", - "rand 0.7.3", - "reed-solomon-erasure", - "serde", - "serde_json", - "smart-default", - "strum", - "thiserror", -] - [[package]] name = "near-primitives" version = "0.15.0" @@ -2452,23 +2425,6 @@ dependencies = [ "strum", ] -[[package]] -name = "near-primitives-core" -version = "0.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91d508f0fc340f6461e4e256417685720d3c4c00bb5a939b105160e49137caba" -dependencies = [ - "base64 0.11.0", - "borsh", - "bs58", - "derive_more 0.99.17", - "near-account-id 0.14.0", - "num-rational", - "serde", - "sha2 0.10.2", - "strum", -] - [[package]] name = "near-primitives-core" version = "0.15.0" @@ -2498,17 +2454,6 @@ dependencies = [ "syn 1.0.96", ] -[[package]] -name = "near-rpc-error-core" -version = "0.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93ee0b41c75ef859c193a8ff1dadfa0c8207bc0ac447cc22259721ad769a1408" -dependencies = [ - "quote 1.0.18", - "serde", - "syn 1.0.96", -] - [[package]] name = "near-rpc-error-core" version = "0.15.0" @@ -2531,17 +2476,6 @@ dependencies = [ "syn 1.0.96", ] -[[package]] -name = "near-rpc-error-macro" -version = "0.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e837bd4bacd807073ec5ceb85708da7f721b46a4c2a978de86027fb0034ce31" -dependencies = [ - "near-rpc-error-core 0.14.0", - "serde", - "syn 1.0.96", -] - [[package]] name = "near-rpc-error-macro" version = "0.15.0" @@ -2650,18 +2584,6 @@ dependencies = [ "serde", ] -[[package]] -name = "near-vm-errors" -version = "0.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0da466a30f0446639cbd788c30865086fac3e8dcb07a79e51d2b0775ed4261e" -dependencies = [ - "borsh", - "near-account-id 0.14.0", - "near-rpc-error-macro 0.14.0", - "serde", -] - [[package]] name = "near-vm-errors" version = "0.15.0" diff --git a/contracts/near/eth-client/Cargo.toml b/contracts/near/eth-client/Cargo.toml index af19ae59c..4a1385623 100644 --- a/contracts/near/eth-client/Cargo.toml +++ b/contracts/near/eth-client/Cargo.toml @@ -9,9 +9,7 @@ crate-type = ["cdylib", "rlib"] [dependencies] eth-types = { path = "../eth-types", default-features = false } -admin-controlled = { path = "../admin-controlled" } near-sdk = "4.0.0" -wee_alloc = { version = "0.4.5", default-features = false, features = [] } rlp = "0.4.2" futures = "0.1.26" primal = "0.2.3" @@ -19,6 +17,9 @@ arrutil = "0.1.2" ethash = { git = "https://github.com/nearprotocol/rust-ethash" } hex = "0.4.0" rustc-hex = "2.1.0" +borsh = "0.9.3" +near-plugins = { git = "https://github.com/aurora-is-near/near-plugins", rev = "5579de43c28984d00fa9aebc0a8e755258bba6c2" } +near-plugins-derive = { git = "https://github.com/aurora-is-near/near-plugins", rev = "5579de43c28984d00fa9aebc0a8e755258bba6c2" } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] # serde's Serialize and Deserialize traits are required for `near_bindgen` macro for non-wasm32 targets diff --git a/contracts/near/eth-client/rust-toolchain b/contracts/near/eth-client/rust-toolchain index d5a27893d..973115b24 100644 --- a/contracts/near/eth-client/rust-toolchain +++ b/contracts/near/eth-client/rust-toolchain @@ -1 +1,3 @@ -stable-2022-07-19 +[toolchain] +channel = "1.64.0" +components = ["clippy", "rustfmt"] diff --git a/contracts/near/eth-client/src/lib.rs b/contracts/near/eth-client/src/lib.rs index 6a479cad8..805559b4d 100644 --- a/contracts/near/eth-client/src/lib.rs +++ b/contracts/near/eth-client/src/lib.rs @@ -1,9 +1,10 @@ -use admin_controlled::Mask; use borsh::{BorshDeserialize, BorshSerialize}; use eth_types::*; use near_sdk::collections::UnorderedMap; use near_sdk::{assert_self, AccountId}; use near_sdk::{env, near_bindgen, PanicOnDefault}; +use near_plugins::{Ownable, Pausable}; +use near_plugins_derive::pause; #[cfg(not(target_arch = "wasm32"))] use serde::{Deserialize, Serialize}; @@ -59,10 +60,10 @@ pub struct HeaderInfo { pub number: u64, } -const PAUSE_ADD_BLOCK_HEADER: Mask = 1; +pub type Mask = u128; #[near_bindgen] -#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)] +#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault, Ownable, Pausable)] 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 @@ -102,6 +103,7 @@ pub struct EthClient { /// others will be immediately rejected, used in PoA testnets trusted_signer: Option, /// Mask determining all paused functions + #[deprecated] paused: Mask, } @@ -122,6 +124,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, @@ -150,6 +153,8 @@ impl EthClient { number: header_number, }, ); + + res.owner_set(Some(near_sdk::env::predecessor_account_id())); res } @@ -198,6 +203,7 @@ 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(owner, self))] #[result_serializer(borsh)] pub fn add_block_header( &mut self, @@ -205,7 +211,6 @@ impl EthClient { #[serializer(borsh)] dag_nodes: Vec, ) { 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 { @@ -454,5 +459,3 @@ impl EthClient { (H256(pair.0), H256(pair.1)) } } - -admin_controlled::impl_admin_controlled!(EthClient, paused); diff --git a/contracts/near/eth-client/src/tests.rs b/contracts/near/eth-client/src/tests.rs index 600cbba57..799ee17c6 100644 --- a/contracts/near/eth-client/src/tests.rs +++ b/contracts/near/eth-client/src/tests.rs @@ -171,7 +171,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, diff --git a/contracts/near/rust-toolchain b/contracts/near/rust-toolchain index d5a27893d..973115b24 100644 --- a/contracts/near/rust-toolchain +++ b/contracts/near/rust-toolchain @@ -1 +1,3 @@ -stable-2022-07-19 +[toolchain] +channel = "1.64.0" +components = ["clippy", "rustfmt"]