diff --git a/Cargo.lock b/Cargo.lock index 4f7718c6..03d55acd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -873,7 +873,7 @@ dependencies = [ [[package]] name = "frame-benchmarking" version = "4.0.0-dev" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "frame-support", "frame-support-procedural", @@ -910,7 +910,7 @@ dependencies = [ [[package]] name = "frame-support" version = "4.0.0-dev" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "bitflags 1.3.2", "environmental", @@ -943,7 +943,7 @@ dependencies = [ [[package]] name = "frame-support-procedural" version = "4.0.0-dev" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "Inflector", "cfg-expr", @@ -959,7 +959,7 @@ dependencies = [ [[package]] name = "frame-support-procedural-tools" version = "4.0.0-dev" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "frame-support-procedural-tools-derive", "proc-macro-crate", @@ -971,7 +971,7 @@ dependencies = [ [[package]] name = "frame-support-procedural-tools-derive" version = "3.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "proc-macro2", "quote", @@ -981,7 +981,7 @@ dependencies = [ [[package]] name = "frame-system" version = "4.0.0-dev" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "frame-support", "log", @@ -1786,7 +1786,7 @@ checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" [[package]] name = "pallet-balances" version = "4.0.0-dev" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "frame-benchmarking", "frame-support", @@ -1955,7 +1955,7 @@ dependencies = [ [[package]] name = "pallet-timestamp" version = "4.0.0-dev" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "frame-benchmarking", "frame-support", @@ -1973,7 +1973,7 @@ dependencies = [ [[package]] name = "pallet-vesting" version = "4.0.0-dev" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "frame-benchmarking", "frame-support", @@ -2813,7 +2813,7 @@ checksum = "4dccd0940a2dcdf68d092b8cbab7dc0ad8fa938bf95787e1b916b0e3d0e8e970" [[package]] name = "sp-api" version = "4.0.0-dev" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "hash-db", "log", @@ -2833,7 +2833,7 @@ dependencies = [ [[package]] name = "sp-api-proc-macro" version = "4.0.0-dev" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "Inflector", "blake2", @@ -2847,7 +2847,7 @@ dependencies = [ [[package]] name = "sp-application-crypto" version = "7.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "parity-scale-codec", "scale-info", @@ -2860,7 +2860,7 @@ dependencies = [ [[package]] name = "sp-arithmetic" version = "6.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "integer-sqrt", "num-traits", @@ -2874,7 +2874,7 @@ dependencies = [ [[package]] name = "sp-authority-discovery" version = "4.0.0-dev" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "parity-scale-codec", "scale-info", @@ -2887,7 +2887,7 @@ dependencies = [ [[package]] name = "sp-consensus-slots" version = "0.10.0-dev" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "parity-scale-codec", "scale-info", @@ -2899,7 +2899,7 @@ dependencies = [ [[package]] name = "sp-core" version = "7.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "array-bytes", "bitflags 1.3.2", @@ -2943,7 +2943,7 @@ dependencies = [ [[package]] name = "sp-core-hashing" version = "5.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "blake2b_simd", "byteorder", @@ -2957,7 +2957,7 @@ dependencies = [ [[package]] name = "sp-core-hashing-proc-macro" version = "5.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "proc-macro2", "quote", @@ -2968,7 +2968,7 @@ dependencies = [ [[package]] name = "sp-debug-derive" version = "5.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "proc-macro2", "quote", @@ -2978,7 +2978,7 @@ dependencies = [ [[package]] name = "sp-externalities" version = "0.13.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "environmental", "parity-scale-codec", @@ -2989,7 +2989,7 @@ dependencies = [ [[package]] name = "sp-inherents" version = "4.0.0-dev" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "async-trait", "impl-trait-for-tuples", @@ -3004,7 +3004,7 @@ dependencies = [ [[package]] name = "sp-io" version = "7.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "bytes", "ed25519", @@ -3030,7 +3030,7 @@ dependencies = [ [[package]] name = "sp-keyring" version = "7.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "lazy_static", "sp-core", @@ -3041,7 +3041,7 @@ dependencies = [ [[package]] name = "sp-keystore" version = "0.13.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "futures", "parity-scale-codec", @@ -3055,7 +3055,7 @@ dependencies = [ [[package]] name = "sp-metadata-ir" version = "0.1.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "frame-metadata", "parity-scale-codec", @@ -3066,7 +3066,7 @@ dependencies = [ [[package]] name = "sp-panic-handler" version = "5.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "backtrace", "lazy_static", @@ -3076,7 +3076,7 @@ dependencies = [ [[package]] name = "sp-runtime" version = "7.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "either", "hash256-std-hasher", @@ -3098,7 +3098,7 @@ dependencies = [ [[package]] name = "sp-runtime-interface" version = "7.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "bytes", "impl-trait-for-tuples", @@ -3116,7 +3116,7 @@ dependencies = [ [[package]] name = "sp-runtime-interface-proc-macro" version = "6.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "Inflector", "proc-macro-crate", @@ -3128,7 +3128,7 @@ dependencies = [ [[package]] name = "sp-staking" version = "4.0.0-dev" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "parity-scale-codec", "scale-info", @@ -3141,7 +3141,7 @@ dependencies = [ [[package]] name = "sp-state-machine" version = "0.13.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "hash-db", "log", @@ -3161,12 +3161,12 @@ dependencies = [ [[package]] name = "sp-std" version = "5.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" [[package]] name = "sp-storage" version = "7.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "impl-serde", "parity-scale-codec", @@ -3179,7 +3179,7 @@ dependencies = [ [[package]] name = "sp-timestamp" version = "4.0.0-dev" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "async-trait", "futures-timer", @@ -3194,7 +3194,7 @@ dependencies = [ [[package]] name = "sp-tracing" version = "6.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "parity-scale-codec", "sp-std", @@ -3206,7 +3206,7 @@ dependencies = [ [[package]] name = "sp-trie" version = "7.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "ahash 0.8.6", "hash-db", @@ -3229,7 +3229,7 @@ dependencies = [ [[package]] name = "sp-version" version = "5.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "impl-serde", "parity-scale-codec", @@ -3246,7 +3246,7 @@ dependencies = [ [[package]] name = "sp-version-proc-macro" version = "4.0.0-dev" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "parity-scale-codec", "proc-macro2", @@ -3257,7 +3257,7 @@ dependencies = [ [[package]] name = "sp-wasm-interface" version = "7.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "anyhow", "impl-trait-for-tuples", @@ -3271,7 +3271,7 @@ dependencies = [ [[package]] name = "sp-weights" version = "4.0.0" -source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.42#ff24c60ac7d9f87727ecdd0ded9a80c56e4f4b65" dependencies = [ "parity-scale-codec", "scale-info", diff --git a/primitives/sidechain/src/lib.rs b/primitives/sidechain/src/lib.rs index a5faac04..86a82554 100644 --- a/primitives/sidechain/src/lib.rs +++ b/primitives/sidechain/src/lib.rs @@ -24,9 +24,10 @@ use sp_core::H256; #[cfg(feature = "std")] use serde::{Deserialize, Serialize}; +pub type SidechainBlockNumber = u64; #[derive(PartialEq, Eq, Clone, Encode, Decode, Debug, Copy, Default, TypeInfo)] #[cfg_attr(feature = "std", derive(Serialize, Deserialize))] pub struct SidechainBlockConfirmation { - pub block_number: u64, + pub block_number: SidechainBlockNumber, pub block_header_hash: H256, } diff --git a/sidechain/src/lib.rs b/sidechain/src/lib.rs index 5624d062..e0f5a141 100644 --- a/sidechain/src/lib.rs +++ b/sidechain/src/lib.rs @@ -21,7 +21,7 @@ use enclave_bridge_primitives::ShardIdentifier; use frame_support::dispatch::DispatchResultWithPostInfo; use frame_system::{self}; use pallet_enclave_bridge::Pallet as EnclaveBridge; -use sidechain_primitives::SidechainBlockConfirmation; +use sidechain_primitives::{SidechainBlockConfirmation, SidechainBlockNumber}; use sp_core::H256; use sp_std::{prelude::*, str, vec}; @@ -29,7 +29,7 @@ pub use crate::weights::WeightInfo; // Disambiguate associated types pub type AccountId = ::AccountId; -pub type ShardBlockNumber = (ShardIdentifier, u64); +pub type ShardAndBlockNumber = (ShardIdentifier, SidechainBlockNumber); pub use pallet::*; @@ -60,6 +60,7 @@ pub mod pallet { /// a sidechain block has been finalized FinalizedSidechainBlock { shard: ShardIdentifier, + block_number: SidechainBlockNumber, block_header_hash: H256, validateer: T::AccountId, }, @@ -76,12 +77,7 @@ pub mod pallet { #[pallet::storage] #[pallet::getter(fn latest_sidechain_block_confirmation)] pub type LatestSidechainBlockConfirmation = - StorageMap<_, Blake2_128Concat, ShardIdentifier, SidechainBlockConfirmation, ValueQuery>; - - #[pallet::storage] - #[pallet::getter(fn sidechain_block_finalization_candidate)] - pub type SidechainBlockFinalizationCandidate = - StorageMap<_, Blake2_128Concat, ShardIdentifier, u64, ValueQuery>; + StorageMap<_, Blake2_128Concat, ShardIdentifier, SidechainBlockConfirmation, OptionQuery>; #[pallet::call] impl Pallet { @@ -92,7 +88,7 @@ pub mod pallet { origin: OriginFor, shard: ShardIdentifier, block_number: u64, - next_finalization_candidate_block_number: u64, + _next_finalization_candidate_block_number: u64, //fixme: can be removed next time we introduce breaking changes block_header_hash: H256, ) -> DispatchResultWithPostInfo { let sender = ensure_signed(origin)?; @@ -103,7 +99,8 @@ pub mod pallet { >::block_number(), )?; - // TODO: Simple logic for now: only accept blocks from first registered enclave. + // TODO: Simple but robust logic for now: + // accept all blocks from first registered enclave for shard as long as blocknumber monotonically increases. if sender != shard_status[0].signer { log::debug!( "Ignore block confirmation from registered enclave with index > 1: {:?}", @@ -111,22 +108,12 @@ pub mod pallet { ); return Ok(().into()) } - let finalization_candidate_block_number = - >::try_get(shard).unwrap_or(1); - - ensure!( - block_number == finalization_candidate_block_number, - >::ReceivedUnexpectedSidechainBlock - ); - ensure!( - next_finalization_candidate_block_number > finalization_candidate_block_number, - >::InvalidNextFinalizationCandidateBlockNumber - ); - - >::insert( - shard, - next_finalization_candidate_block_number, - ); + if let Some(ancestor) = Self::latest_sidechain_block_confirmation(shard) { + ensure!( + ancestor.block_number < block_number, + >::ReceivedUnexpectedSidechainBlock + ); + } Self::finalize_block( shard, SidechainBlockConfirmation { block_number, block_header_hash }, @@ -145,13 +132,16 @@ impl Pallet { ) { >::insert(shard, confirmation); let block_header_hash = confirmation.block_header_hash; + let block_number = confirmation.block_number; log::debug!( - "Imported sidechain block confirmed with shard {:?}, block header hash {:?}", + "Imported sidechain block {} confirmed with shard {:?}, block header hash {:?}", + block_number, shard, block_header_hash ); Self::deposit_event(Event::FinalizedSidechainBlock { shard, + block_number, block_header_hash, validateer: sender.clone(), }); diff --git a/sidechain/src/tests.rs b/sidechain/src/tests.rs index b9d0c9bc..8e63db75 100644 --- a/sidechain/src/tests.rs +++ b/sidechain/src/tests.rs @@ -33,31 +33,6 @@ fn get_signer(pubkey: &[u8; 32]) -> AccountId { test_utils::get_signer(pubkey) } -#[test] -fn confirm_imported_sidechain_block_invalid_next_finalization_candidate() { - new_test_ext().execute_with(|| { - Timestamp::set_timestamp(TEST7_TIMESTAMP); - let hash = H256::default(); - let signer7 = get_signer(TEST7_SIGNER_PUB); - let shard7 = H256::from_slice(&TEST7_MRENCLAVE); - - let block_number = 1; - - register_ias_enclave7(); - - assert_err!( - Sidechain::confirm_imported_sidechain_block( - RuntimeOrigin::signed(signer7.clone()), - shard7, - block_number, - block_number, - hash - ), - Error::::InvalidNextFinalizationCandidateBlockNumber, - ); - }) -} - #[test] fn confirm_imported_sidechain_block_works_for_correct_shard() { new_test_ext().execute_with(|| { @@ -81,6 +56,7 @@ fn confirm_imported_sidechain_block_works_for_correct_shard() { let expected_event = RuntimeEvent::Sidechain(SidechainEvent::FinalizedSidechainBlock { shard: shard7, + block_number, block_header_hash: hash, validateer: signer7, }); @@ -122,72 +98,90 @@ fn confirm_imported_sidechain_block_correct_order() { register_ias_enclave7(); assert_ok!(confirm_sidechain_block7(1, 2, H256::random(), true)); - assert_eq!(Sidechain::latest_sidechain_block_confirmation(shard7).block_number, 1); - assert_eq!(Sidechain::sidechain_block_finalization_candidate(shard7), 2); + assert_eq!( + Sidechain::latest_sidechain_block_confirmation(shard7) + .unwrap_or_default() + .block_number, + 1 + ); assert_ok!(confirm_sidechain_block7(2, 3, H256::random(), true)); - assert_eq!(Sidechain::latest_sidechain_block_confirmation(shard7).block_number, 2); - assert_eq!(Sidechain::sidechain_block_finalization_candidate(shard7), 3); + assert_eq!( + Sidechain::latest_sidechain_block_confirmation(shard7) + .unwrap_or_default() + .block_number, + 2 + ); assert_ok!(confirm_sidechain_block7(3, 4, H256::random(), true)); - assert_eq!(Sidechain::latest_sidechain_block_confirmation(shard7).block_number, 3); - assert_eq!(Sidechain::sidechain_block_finalization_candidate(shard7), 4); + assert_eq!( + Sidechain::latest_sidechain_block_confirmation(shard7) + .unwrap_or_default() + .block_number, + 3 + ); assert_ok!(confirm_sidechain_block7(4, 5, H256::random(), true)); - assert_eq!(Sidechain::latest_sidechain_block_confirmation(shard7).block_number, 4); - assert_eq!(Sidechain::sidechain_block_finalization_candidate(shard7), 5); + assert_eq!( + Sidechain::latest_sidechain_block_confirmation(shard7) + .unwrap_or_default() + .block_number, + 4 + ); assert_ok!(confirm_sidechain_block7(5, 6, H256::random(), true)); - assert_eq!(Sidechain::latest_sidechain_block_confirmation(shard7).block_number, 5); - assert_eq!(Sidechain::sidechain_block_finalization_candidate(shard7), 6); + assert_eq!( + Sidechain::latest_sidechain_block_confirmation(shard7) + .unwrap_or_default() + .block_number, + 5 + ); }) } #[test] -fn confirm_imported_sidechain_block_wrong_next() { +fn confirm_imported_sidechain_block_outdated_successor_fails() { new_test_ext().execute_with(|| { Timestamp::set_timestamp(TEST7_TIMESTAMP); let shard7 = H256::from_slice(&TEST7_MRENCLAVE); register_ias_enclave7(); - assert_ok!(confirm_sidechain_block7(1, 2, H256::random(), true)); - assert_eq!(Sidechain::latest_sidechain_block_confirmation(shard7).block_number, 1); - assert_eq!(Sidechain::sidechain_block_finalization_candidate(shard7), 2); - assert_ok!(confirm_sidechain_block7(2, 4, H256::random(), true)); - assert_eq!(Sidechain::latest_sidechain_block_confirmation(shard7).block_number, 2); - assert_eq!(Sidechain::sidechain_block_finalization_candidate(shard7), 4); + assert_ok!(confirm_sidechain_block7(11, 42, H256::random(), true)); + assert_eq!( + Sidechain::latest_sidechain_block_confirmation(shard7) + .unwrap_or_default() + .block_number, + 11 + ); + assert_ok!(confirm_sidechain_block7(22, 42, H256::random(), true)); + assert_eq!( + Sidechain::latest_sidechain_block_confirmation(shard7) + .unwrap_or_default() + .block_number, + 22 + ); + + // resubmission must fail assert_err!( - confirm_sidechain_block7(3, 4, H256::random(), true), + confirm_sidechain_block7(22, 42, H256::random(), true), Error::::ReceivedUnexpectedSidechainBlock ); - assert_eq!(Sidechain::latest_sidechain_block_confirmation(shard7).block_number, 2); - assert_eq!(Sidechain::sidechain_block_finalization_candidate(shard7), 4); - assert_ok!(confirm_sidechain_block7(4, 5, H256::random(), true)); - assert_eq!(Sidechain::latest_sidechain_block_confirmation(shard7).block_number, 4); - assert_eq!(Sidechain::sidechain_block_finalization_candidate(shard7), 5); - }) -} - -#[test] -fn confirm_imported_sidechain_block_outdated() { - new_test_ext().execute_with(|| { - Timestamp::set_timestamp(TEST7_TIMESTAMP); - let shard7 = H256::from_slice(&TEST7_MRENCLAVE); - - register_ias_enclave7(); - - assert_ok!(confirm_sidechain_block7(1, 2, H256::random(), true)); - assert_eq!(Sidechain::latest_sidechain_block_confirmation(shard7).block_number, 1); - assert_eq!(Sidechain::sidechain_block_finalization_candidate(shard7), 2); - assert_ok!(confirm_sidechain_block7(2, 4, H256::random(), true)); - assert_eq!(Sidechain::latest_sidechain_block_confirmation(shard7).block_number, 2); - assert_eq!(Sidechain::sidechain_block_finalization_candidate(shard7), 4); + // outdated blocks must fail assert_err!( - confirm_sidechain_block7(2, 4, H256::random(), true), + confirm_sidechain_block7(21, 42, H256::random(), true), Error::::ReceivedUnexpectedSidechainBlock ); - assert_eq!(Sidechain::latest_sidechain_block_confirmation(shard7).block_number, 2); - assert_eq!(Sidechain::sidechain_block_finalization_candidate(shard7), 4); - assert_ok!(confirm_sidechain_block7(4, 5, H256::random(), true)); - assert_eq!(Sidechain::latest_sidechain_block_confirmation(shard7).block_number, 4); - assert_eq!(Sidechain::sidechain_block_finalization_candidate(shard7), 5); + + assert_eq!( + Sidechain::latest_sidechain_block_confirmation(shard7) + .unwrap_or_default() + .block_number, + 22 + ); + assert_ok!(confirm_sidechain_block7(44, 42, H256::random(), true)); + assert_eq!( + Sidechain::latest_sidechain_block_confirmation(shard7) + .unwrap_or_default() + .block_number, + 44 + ); }) } @@ -199,13 +193,19 @@ fn dont_process_confirmation_of_second_registered_enclave() { register_ias_enclave(TEST7_SIGNER_PUB, TEST7_CERT); assert_ok!(confirm_sidechain_block(shard7, TEST7_SIGNER_PUB, 1, 2, H256::default(), true)); - assert_eq!(Sidechain::latest_sidechain_block_confirmation(shard7).block_number, 1); + assert_eq!( + Sidechain::latest_sidechain_block_confirmation(shard7) + .unwrap_or_default() + .block_number, + 1 + ); register_ias_enclave(TEST6_SIGNER_PUB, TEST6_CERT); System::reset_events(); assert_ok!(confirm_sidechain_block(shard7, TEST6_SIGNER_PUB, 1, 2, H256::default(), false)); let expected_event = RuntimeEvent::Sidechain(SidechainEvent::FinalizedSidechainBlock { shard: shard7, + block_number: 1, block_header_hash: H256::default(), validateer: get_signer(TEST6_SIGNER_PUB), }); @@ -241,6 +241,7 @@ fn confirm_imported_sidechain_block_works_for_correct_shard_with_updated_fingerp let expected_event = RuntimeEvent::Sidechain(SidechainEvent::FinalizedSidechainBlock { shard, + block_number: 1, block_header_hash: hash, validateer: enclave_signer.clone(), }); @@ -266,6 +267,7 @@ fn confirm_imported_sidechain_block_works_for_correct_shard_with_updated_fingerp let expected_event = RuntimeEvent::Sidechain(SidechainEvent::FinalizedSidechainBlock { shard, + block_number: 2, block_header_hash: hash, validateer: enclave_signer.clone(), }); @@ -287,6 +289,7 @@ fn confirm_imported_sidechain_block_works_for_correct_shard_with_updated_fingerp let expected_event = RuntimeEvent::Sidechain(SidechainEvent::FinalizedSidechainBlock { shard, + block_number: 3, block_header_hash: hash, validateer: enclave_signer, }); @@ -314,6 +317,7 @@ fn two_sidechains_with_different_fingerprint_works() { let expected_event = RuntimeEvent::Sidechain(SidechainEvent::FinalizedSidechainBlock { shard: shard1, + block_number: 1, block_header_hash: hash1, validateer: enclave_signer1.clone(), }); @@ -337,6 +341,7 @@ fn two_sidechains_with_different_fingerprint_works() { let expected_event = RuntimeEvent::Sidechain(SidechainEvent::FinalizedSidechainBlock { shard: shard2, + block_number: 1, block_header_hash: hash2, validateer: enclave_signer2.clone(), }); @@ -356,14 +361,12 @@ fn two_sidechains_with_different_fingerprint_works() { assert_eq!(shard_status2[0].last_activity, 2u32); assert_eq!( Sidechain::latest_sidechain_block_confirmation(shard1), - SidechainBlockConfirmation { block_number: 1, block_header_hash: hash1 } + Some(SidechainBlockConfirmation { block_number: 1, block_header_hash: hash1 }) ); assert_eq!( Sidechain::latest_sidechain_block_confirmation(shard2), - SidechainBlockConfirmation { block_number: 1, block_header_hash: hash2 } + Some(SidechainBlockConfirmation { block_number: 1, block_header_hash: hash2 }) ); - assert_eq!(Sidechain::sidechain_block_finalization_candidate(shard1), 2); - assert_eq!(Sidechain::sidechain_block_finalization_candidate(shard2), 10); }) } @@ -424,6 +427,7 @@ fn confirm_sidechain_block( if assert_event { let expected_event = RuntimeEvent::Sidechain(SidechainEvent::FinalizedSidechainBlock { shard, + block_number, block_header_hash, validateer: signer, });