diff --git a/bin/node-template/node/src/chain_spec.rs b/bin/node-template/node/src/chain_spec.rs index 4efb26e1a0..94aed90a23 100644 --- a/bin/node-template/node/src/chain_spec.rs +++ b/bin/node-template/node/src/chain_spec.rs @@ -241,8 +241,33 @@ fn testnet_genesis( }), darwinia_ethereum_relay: Some(EthereumRelayConfig { genesis_header_info: ( - 0, - b"A\x94\x10#h\t#\xe0\xfeMt\xa3K\xda\xc8\x14\x1f%@\xe3\xae\x90b7\x18\xe4}f\xd1\xcaJ-".into(), + vec![ + 249, 2, 20, 160, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 160, 29, 204, 77, 232, 222, 199, 93, 122, 171, + 133, 181, 103, 182, 204, 212, 26, 211, 18, 69, 27, 148, 138, 116, 19, 240, 161, + 66, 253, 64, 212, 147, 71, 148, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 160, 215, 248, 151, 79, 181, 172, 120, 217, 172, 9, 155, 154, 213, + 1, 139, 237, 194, 206, 10, 114, 218, 209, 130, 122, 23, 9, 218, 48, 88, 15, 5, + 68, 160, 86, 232, 31, 23, 27, 204, 85, 166, 255, 131, 69, 230, 146, 192, 248, + 110, 91, 72, 224, 27, 153, 108, 173, 192, 1, 98, 47, 181, 227, 99, 180, 33, + 160, 86, 232, 31, 23, 27, 204, 85, 166, 255, 131, 69, 230, 146, 192, 248, 110, + 91, 72, 224, 27, 153, 108, 173, 192, 1, 98, 47, 181, 227, 99, 180, 33, 185, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 133, 4, 0, + 0, 0, 0, 128, 130, 19, 136, 128, 128, 160, 17, 187, 232, 219, 78, 52, 123, 78, + 140, 147, 124, 28, 131, 112, 228, 181, 237, 51, 173, 179, 219, 105, 203, 219, + 122, 56, 225, 229, 11, 27, 130, 250, 160, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 136, 0, 0, 0, 0, 0, + 0, 0, 66, + ], b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".into() ), dags_merkle_roots_loader: DagsMerkleRootsLoaderR::from_file( diff --git a/bin/node-template/runtime/darwinia_types.json b/bin/node-template/runtime/darwinia_types.json index b7aa6c9c8b..7152dc4a53 100644 --- a/bin/node-template/runtime/darwinia_types.json +++ b/bin/node-template/runtime/darwinia_types.json @@ -185,7 +185,11 @@ "header_hash": "H256" }, "EthereumReceiptProofThing": "(EthereumHeader, EthereumReceiptProof, MMRProof)", - "MMRProof": "Vec", + "MMRProof": { + "member_leaf_index": "u64", + "last_leaf_index": "u64", + "proof": "Vec" + }, "__[pallet.claims]__": {}, "OtherSignature": { "_enum": { diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index d49e8f7ab0..3a4802b98c 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -842,10 +842,12 @@ type EnsureRootOrHalfTechnicalComittee = EnsureOneOf< >; parameter_types! { pub const EthereumRelayModuleId: ModuleId = ModuleId(*b"da/ethrl"); + pub const EthereumNetwork: ethereum_primitives::EthereumNetworkType = ethereum_primitives::EthereumNetworkType::Ropsten; } impl darwinia_ethereum_relay::Trait for Runtime { type ModuleId = EthereumRelayModuleId; type Event = Event; + type EthereumNetwork = EthereumNetwork; type Call = Call; type Currency = Ring; type RelayerGame = EthereumRelayerGame; diff --git a/docs/CODEOWNERS b/docs/CODEOWNERS index aec3705ed8..f958e53bfc 100644 --- a/docs/CODEOWNERS +++ b/docs/CODEOWNERS @@ -28,11 +28,12 @@ /frame/balances/rpc/ @AurevoirXavier /frame/balances/rpc/runtime-api/ @AurevoirXavier -/frame/bridge/relayer-game/ @AurevoirXavier @yanganto +/frame/bridge/crab/issuing @AurevoirXavier @HackFisher /frame/bridge/ethereum/backing/ @AurevoirXavier @HackFisher /frame/bridge/ethereum/linear-relay/ @AurevoirXavier @clearloop @HackFisher /frame/bridge/ethereum/offchain/ @AurevoirXavier @yanganto /frame/bridge/ethereum/relay/ @AurevoirXavier @HackFisher @yanganto +/frame/bridge/relayer-game/ @AurevoirXavier @yanganto /frame/claims/ @AurevoirXavier @HackFisher diff --git a/frame/bridge/ethereum/backing/src/test_with_linear_relay.rs b/frame/bridge/ethereum/backing/src/test_with_linear_relay.rs index 5ab93d98c1..71fe72547d 100644 --- a/frame/bridge/ethereum/backing/src/test_with_linear_relay.rs +++ b/frame/bridge/ethereum/backing/src/test_with_linear_relay.rs @@ -7,10 +7,9 @@ use sp_runtime::{traits::Dispatchable, AccountId32}; // --- darwinia --- use crate::*; use array_bytes::hex_bytes_unchecked; -use darwinia_ethereum_linear_relay::EthereumNetworkType; use darwinia_staking::{RewardDestination, StakingBalance, StakingLedger, TimeDepositItem}; use darwinia_support::balance::lock::StakingLock; -use ethereum_primitives::{header::EthereumHeader, receipt::EthereumReceiptProof}; +use ethereum_primitives::{EthereumNetworkType, header::EthereumHeader, receipt::EthereumReceiptProof}; type EthereumRelay = darwinia_ethereum_linear_relay::Module; diff --git a/frame/bridge/ethereum/backing/src/test_with_relay.rs b/frame/bridge/ethereum/backing/src/test_with_relay.rs index 8d572ab7dc..2b69a6bdf6 100644 --- a/frame/bridge/ethereum/backing/src/test_with_relay.rs +++ b/frame/bridge/ethereum/backing/src/test_with_relay.rs @@ -7,11 +7,11 @@ use sp_runtime::{traits::Dispatchable, AccountId32}; // --- darwinia --- use crate::*; use array_bytes::hex_bytes_unchecked; -use darwinia_ethereum_relay::{EthereumHeaderThing, EthereumHeaderThingWithProof}; +use darwinia_ethereum_relay::{EthereumHeaderThing, EthereumHeaderThingWithProof, MMRProof}; use darwinia_relay_primitives::*; use darwinia_staking::{RewardDestination, StakingBalance, StakingLedger, TimeDepositItem}; use darwinia_support::balance::lock::StakingLock; -use ethereum_primitives::{header::EthereumHeader, receipt::EthereumReceiptProof, H256}; +use ethereum_primitives::{header::EthereumHeader, receipt::EthereumReceiptProof, EthereumNetworkType}; type EthereumRelay = darwinia_ethereum_relay::Module; @@ -19,10 +19,12 @@ decl_tests!(); parameter_types! { pub const EthereumRelayModuleId: ModuleId = ModuleId(*b"da/ethrl"); + pub const EthereumNetwork: EthereumNetworkType = EthereumNetworkType::Ropsten; } impl darwinia_ethereum_relay::Trait for Test { type ModuleId = EthereumRelayModuleId; type Event = (); + type EthereumNetwork = EthereumNetwork; type RelayerGame = UnusedRelayerGame; type ApproveOrigin = EnsureRoot; type RejectOrigin = EnsureRoot; @@ -110,7 +112,7 @@ impl ExtBuilder { pub struct TestReceiptProofThing { pub header: EthereumHeader, pub receipt_proof: EthereumReceiptProof, - pub mmr_proof: Vec, + pub mmr_proof: MMRProof, } #[test] @@ -155,19 +157,23 @@ fn verify_parse_token_redeem_proof() { "proof": "0xf90654f90651b873f871a08905d6a9a81124e73b632ff8e0ac638331d4aa0f89bc5b296b5132ab1e6db295a0296366ce16b627f71457cefa27e7cbd6aa3f13ce1e2225bb06236089d5363667808080808080a0c73f3d756add498b44b70ae5d5b917fcc8c3adb72f10cc5cd245a862d9a2a17d8080808080808080b873f871a039af78839760433d410ab11ef453e8656451a51575e0be71fa7a72b54bfc296aa098d3ce8768b102d89494e55dde408e28d1ec148affca70a955d2b30bd9fcf008a078eacce43297ddfa328b51d92ccd8666abf1df855cef7ab3b1f4dbd16874ff658080808080808080808080808080b90564f9056120b9055df9055a01830e921db9010000000000008000000000002000000004400000000000001000000000000000000000000000000000000010000000000000000000000000400000000000000000000000000000000010000008000000000010000000000000000000000000000000000000020000000104000000000800080000000000000000000010000000400000000000000001000000000000008000000002004000000010000000200000000000000000000000000000800000000000000000000200080000000000000000000002000000000000000000040000000001000000000800000000000020000000000000000000000000000010000000000000000080000000000000000000f9044ff89b94b52fbe2b925ab79a821b261c82c5ba0814aaa5e0f863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa0000000000000000000000000cc5e48beb33b83b8bd0d9d9a85a8f6a27c51f5c5a000000000000000000000000049262b932e439271d05634c32978294c7ea15d0ca00000000000000000000000000000000000000000000000001121d33597384000f89b94b52fbe2b925ab79a821b261c82c5ba0814aaa5e0f863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa0000000000000000000000000cc5e48beb33b83b8bd0d9d9a85a8f6a27c51f5c5a00000000000000000000000007f5b598827359939606b3525712fb124a1c7851da00000000000000000000000000000000000000000000000001bc16d674ec80000f87a94b52fbe2b925ab79a821b261c82c5ba0814aaa5e0f842a0cc16f5dbb4873280815c1ee09dbd06736cffcc184412cf7a71a0fdb75d397ca5a000000000000000000000000049262b932e439271d05634c32978294c7ea15d0ca00000000000000000000000000000000000000000000000001121d33597384000f89b94b52fbe2b925ab79a821b261c82c5ba0814aaa5e0f863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa000000000000000000000000049262b932e439271d05634c32978294c7ea15d0ca00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000001121d33597384000f8fc9449262b932e439271d05634c32978294c7ea15d0cf863a0c9dcda609937876978d7e0aa29857cb187aea06ad9e843fd23fd32108da73f10a0000000000000000000000000b52fbe2b925ab79a821b261c82c5ba0814aaa5e0a0000000000000000000000000cc5e48beb33b83b8bd0d9d9a85a8f6a27c51f5c5b8800000000000000000000000000000000000000000000000001121d3359738400000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000020e44664996ab7b5d86c12e9d5ac3093f5b2efc9172cb7ce298cd6c3c51002c318f8fc94b52fbe2b925ab79a821b261c82c5ba0814aaa5e0f863a09bfafdc2ae8835972d7b64ef3f8f307165ac22ceffde4a742c52da5487f45fd1a0000000000000000000000000cc5e48beb33b83b8bd0d9d9a85a8f6a27c51f5c5a000000000000000000000000049262b932e439271d05634c32978294c7ea15d0cb8800000000000000000000000000000000000000000000000001121d3359738400000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000020e44664996ab7b5d86c12e9d5ac3093f5b2efc9172cb7ce298cd6c3c51002c318", "header_hash": "0xabf627ce77d9f92a40f34e3cace721c3f089000dae820d00d3e99314c263a0c3" }, - "mmr_proof": [ - "0x36f3d834cbe12a5a20b063c432b88f5506bdce03b93fa3aa035a5d82fd50177c", - "0x541f71c116b054b7cf0f35461b41abdc0bc6e461ae54862c8dc3af6b48742a35", - "0x095f1b66aed2db73567706e227981454a80a442aa69b00aedaef787fd5401a78", - "0x942cfd846d464575b520554dd07fd4b07ba2cb8bee6234530db1c1b3a69e1c50", - "0x3174f9fdf5f7c9df6ac422a0a9e2aec05ccb7968bb1ff19b6cc0b64fbc930e77", - "0x5624f44ff885dcdc971303281bb76c6b6a957a6cc2d244b30f736fc6220f9b89", - "0xa3adc2ee02f7ceda7e63df8fb58094769afd3f7657c8f9f45cdc69d5c868b468", - "0xe588926a1643dc04b197a7ed30dd47d42d35fdb01f7aa80b2e293b058c11c6a1", - "0x25d7ac48be9565512424ef559b7e0ec7a91bfe9b57440913712a174e35eb47d8", - "0xf4505f26b22ae59f5560003a68a66e8c07a9f77f212e05ea060018bd0acd1f55", - "0xd55ce7660d0161c38b34015ce5468e1661f1c77865f23415e246ac9ccf7b2b22" - ] + "mmr_proof": { + "member_leaf_index": 8610261, + "last_leaf_index": 8610261, + "proof": [ + "0x36f3d834cbe12a5a20b063c432b88f5506bdce03b93fa3aa035a5d82fd50177c", + "0x541f71c116b054b7cf0f35461b41abdc0bc6e461ae54862c8dc3af6b48742a35", + "0x095f1b66aed2db73567706e227981454a80a442aa69b00aedaef787fd5401a78", + "0x942cfd846d464575b520554dd07fd4b07ba2cb8bee6234530db1c1b3a69e1c50", + "0x3174f9fdf5f7c9df6ac422a0a9e2aec05ccb7968bb1ff19b6cc0b64fbc930e77", + "0x5624f44ff885dcdc971303281bb76c6b6a957a6cc2d244b30f736fc6220f9b89", + "0xa3adc2ee02f7ceda7e63df8fb58094769afd3f7657c8f9f45cdc69d5c868b468", + "0xe588926a1643dc04b197a7ed30dd47d42d35fdb01f7aa80b2e293b058c11c6a1", + "0x25d7ac48be9565512424ef559b7e0ec7a91bfe9b57440913712a174e35eb47d8", + "0xf4505f26b22ae59f5560003a68a66e8c07a9f77f212e05ea060018bd0acd1f55", + "0xd55ce7660d0161c38b34015ce5468e1661f1c77865f23415e246ac9ccf7b2b22" + ] + } } "# ).unwrap(); @@ -250,19 +256,23 @@ fn verify_redeem_ring() { "proof": "0xf90654f90651b873f871a08905d6a9a81124e73b632ff8e0ac638331d4aa0f89bc5b296b5132ab1e6db295a0296366ce16b627f71457cefa27e7cbd6aa3f13ce1e2225bb06236089d5363667808080808080a0c73f3d756add498b44b70ae5d5b917fcc8c3adb72f10cc5cd245a862d9a2a17d8080808080808080b873f871a039af78839760433d410ab11ef453e8656451a51575e0be71fa7a72b54bfc296aa098d3ce8768b102d89494e55dde408e28d1ec148affca70a955d2b30bd9fcf008a078eacce43297ddfa328b51d92ccd8666abf1df855cef7ab3b1f4dbd16874ff658080808080808080808080808080b90564f9056120b9055df9055a01830e921db9010000000000008000000000002000000004400000000000001000000000000000000000000000000000000010000000000000000000000000400000000000000000000000000000000010000008000000000010000000000000000000000000000000000000020000000104000000000800080000000000000000000010000000400000000000000001000000000000008000000002004000000010000000200000000000000000000000000000800000000000000000000200080000000000000000000002000000000000000000040000000001000000000800000000000020000000000000000000000000000010000000000000000080000000000000000000f9044ff89b94b52fbe2b925ab79a821b261c82c5ba0814aaa5e0f863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa0000000000000000000000000cc5e48beb33b83b8bd0d9d9a85a8f6a27c51f5c5a000000000000000000000000049262b932e439271d05634c32978294c7ea15d0ca00000000000000000000000000000000000000000000000001121d33597384000f89b94b52fbe2b925ab79a821b261c82c5ba0814aaa5e0f863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa0000000000000000000000000cc5e48beb33b83b8bd0d9d9a85a8f6a27c51f5c5a00000000000000000000000007f5b598827359939606b3525712fb124a1c7851da00000000000000000000000000000000000000000000000001bc16d674ec80000f87a94b52fbe2b925ab79a821b261c82c5ba0814aaa5e0f842a0cc16f5dbb4873280815c1ee09dbd06736cffcc184412cf7a71a0fdb75d397ca5a000000000000000000000000049262b932e439271d05634c32978294c7ea15d0ca00000000000000000000000000000000000000000000000001121d33597384000f89b94b52fbe2b925ab79a821b261c82c5ba0814aaa5e0f863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa000000000000000000000000049262b932e439271d05634c32978294c7ea15d0ca00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000001121d33597384000f8fc9449262b932e439271d05634c32978294c7ea15d0cf863a0c9dcda609937876978d7e0aa29857cb187aea06ad9e843fd23fd32108da73f10a0000000000000000000000000b52fbe2b925ab79a821b261c82c5ba0814aaa5e0a0000000000000000000000000cc5e48beb33b83b8bd0d9d9a85a8f6a27c51f5c5b8800000000000000000000000000000000000000000000000001121d3359738400000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000020e44664996ab7b5d86c12e9d5ac3093f5b2efc9172cb7ce298cd6c3c51002c318f8fc94b52fbe2b925ab79a821b261c82c5ba0814aaa5e0f863a09bfafdc2ae8835972d7b64ef3f8f307165ac22ceffde4a742c52da5487f45fd1a0000000000000000000000000cc5e48beb33b83b8bd0d9d9a85a8f6a27c51f5c5a000000000000000000000000049262b932e439271d05634c32978294c7ea15d0cb8800000000000000000000000000000000000000000000000001121d3359738400000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000020e44664996ab7b5d86c12e9d5ac3093f5b2efc9172cb7ce298cd6c3c51002c318", "header_hash": "0xabf627ce77d9f92a40f34e3cace721c3f089000dae820d00d3e99314c263a0c3" }, - "mmr_proof": [ - "0x36f3d834cbe12a5a20b063c432b88f5506bdce03b93fa3aa035a5d82fd50177c", - "0x541f71c116b054b7cf0f35461b41abdc0bc6e461ae54862c8dc3af6b48742a35", - "0x095f1b66aed2db73567706e227981454a80a442aa69b00aedaef787fd5401a78", - "0x942cfd846d464575b520554dd07fd4b07ba2cb8bee6234530db1c1b3a69e1c50", - "0x3174f9fdf5f7c9df6ac422a0a9e2aec05ccb7968bb1ff19b6cc0b64fbc930e77", - "0x5624f44ff885dcdc971303281bb76c6b6a957a6cc2d244b30f736fc6220f9b89", - "0xa3adc2ee02f7ceda7e63df8fb58094769afd3f7657c8f9f45cdc69d5c868b468", - "0xe588926a1643dc04b197a7ed30dd47d42d35fdb01f7aa80b2e293b058c11c6a1", - "0x25d7ac48be9565512424ef559b7e0ec7a91bfe9b57440913712a174e35eb47d8", - "0xf4505f26b22ae59f5560003a68a66e8c07a9f77f212e05ea060018bd0acd1f55", - "0xd55ce7660d0161c38b34015ce5468e1661f1c77865f23415e246ac9ccf7b2b22" - ] + "mmr_proof": { + "member_leaf_index": 8610261, + "last_leaf_index": 8610261, + "proof": [ + "0x36f3d834cbe12a5a20b063c432b88f5506bdce03b93fa3aa035a5d82fd50177c", + "0x541f71c116b054b7cf0f35461b41abdc0bc6e461ae54862c8dc3af6b48742a35", + "0x095f1b66aed2db73567706e227981454a80a442aa69b00aedaef787fd5401a78", + "0x942cfd846d464575b520554dd07fd4b07ba2cb8bee6234530db1c1b3a69e1c50", + "0x3174f9fdf5f7c9df6ac422a0a9e2aec05ccb7968bb1ff19b6cc0b64fbc930e77", + "0x5624f44ff885dcdc971303281bb76c6b6a957a6cc2d244b30f736fc6220f9b89", + "0xa3adc2ee02f7ceda7e63df8fb58094769afd3f7657c8f9f45cdc69d5c868b468", + "0xe588926a1643dc04b197a7ed30dd47d42d35fdb01f7aa80b2e293b058c11c6a1", + "0x25d7ac48be9565512424ef559b7e0ec7a91bfe9b57440913712a174e35eb47d8", + "0xf4505f26b22ae59f5560003a68a66e8c07a9f77f212e05ea060018bd0acd1f55", + "0xd55ce7660d0161c38b34015ce5468e1661f1c77865f23415e246ac9ccf7b2b22" + ] + } } "# ).unwrap(); @@ -361,19 +371,23 @@ fn verify_redeem_kton() { "proof": "0xf90654f90651b853f851a0924e7317d57b9cb7ebf90321fdc9f800b94b64adbaae8da31dab0142e8c079ea80808080808080a0e58215be848c1293dd381210359d84485553000a82b67410406d183b42adbbdd8080808080808080b893f89180a0d1d9123dac06536f593ff89d28ac2373b3bc603fbee756e6054d6b2162e99337a01d2879f862c4f4f818f91e74fa43188c36a344c93132783006864e506a656076a0002cd3adf59d2aaa8313a0bbaa8fd411921077bfa1edcbe04018f7339bd273faa03bddfc128660298a289de46a9301b7f03cbe5364c22aedbc28f472bdbc318778808080808080808080808080b90564f9056120b9055df9055a0183032c33b901000000000000800000000000000000000440000000000000100000000000000000000000000000000000001000000000000000000000000040000000000000000000000000000040001020000800000000001000000000000000000004000000000000000002000000010400000000080008000000000000000000001000000040000000000000000000000000000000c000000002004000000010000000200000000000000000000000000000800000000000000000000202080000000000000000000002000000000000000000040000000001000000000000000000000020000000000000000000000000010010000000000000000080000000000000000000f9044ff89b941994100c58753793d52c6f457f189aa3ce9cee94f863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa0000000000000000000000000cc5e48beb33b83b8bd0d9d9a85a8f6a27c51f5c5a000000000000000000000000049262b932e439271d05634c32978294c7ea15d0ca00000000000000000000000000000000000000000000000000000703b4d2a5000f89b94b52fbe2b925ab79a821b261c82c5ba0814aaa5e0f863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa0000000000000000000000000cc5e48beb33b83b8bd0d9d9a85a8f6a27c51f5c5a00000000000000000000000007f5b598827359939606b3525712fb124a1c7851da00000000000000000000000000000000000000000000000001bc16d674ec80000f87a941994100c58753793d52c6f457f189aa3ce9cee94f842a0cc16f5dbb4873280815c1ee09dbd06736cffcc184412cf7a71a0fdb75d397ca5a000000000000000000000000049262b932e439271d05634c32978294c7ea15d0ca00000000000000000000000000000000000000000000000000000703b4d2a5000f89b941994100c58753793d52c6f457f189aa3ce9cee94f863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa000000000000000000000000049262b932e439271d05634c32978294c7ea15d0ca00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000703b4d2a5000f8fc9449262b932e439271d05634c32978294c7ea15d0cf863a0c9dcda609937876978d7e0aa29857cb187aea06ad9e843fd23fd32108da73f10a00000000000000000000000001994100c58753793d52c6f457f189aa3ce9cee94a0000000000000000000000000cc5e48beb33b83b8bd0d9d9a85a8f6a27c51f5c5b8800000000000000000000000000000000000000000000000000000703b4d2a500000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000020e44664996ab7b5d86c12e9d5ac3093f5b2efc9172cb7ce298cd6c3c51002c318f8fc941994100c58753793d52c6f457f189aa3ce9cee94f863a09bfafdc2ae8835972d7b64ef3f8f307165ac22ceffde4a742c52da5487f45fd1a0000000000000000000000000cc5e48beb33b83b8bd0d9d9a85a8f6a27c51f5c5a000000000000000000000000049262b932e439271d05634c32978294c7ea15d0cb8800000000000000000000000000000000000000000000000000000703b4d2a500000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000020e44664996ab7b5d86c12e9d5ac3093f5b2efc9172cb7ce298cd6c3c51002c318", "header_hash": "0x5f80ee45d62872fa4b0dbf779a8eb380e166ac80616d05875dd3e80e5fc40839" }, - "mmr_proof": [ - "0x36f3d834cbe12a5a20b063c432b88f5506bdce03b93fa3aa035a5d82fd50177c", - "0x541f71c116b054b7cf0f35461b41abdc0bc6e461ae54862c8dc3af6b48742a35", - "0x095f1b66aed2db73567706e227981454a80a442aa69b00aedaef787fd5401a78", - "0x942cfd846d464575b520554dd07fd4b07ba2cb8bee6234530db1c1b3a69e1c50", - "0x3174f9fdf5f7c9df6ac422a0a9e2aec05ccb7968bb1ff19b6cc0b64fbc930e77", - "0x5624f44ff885dcdc971303281bb76c6b6a957a6cc2d244b30f736fc6220f9b89", - "0xa3adc2ee02f7ceda7e63df8fb58094769afd3f7657c8f9f45cdc69d5c868b468", - "0xe588926a1643dc04b197a7ed30dd47d42d35fdb01f7aa80b2e293b058c11c6a1", - "0x25d7ac48be9565512424ef559b7e0ec7a91bfe9b57440913712a174e35eb47d8", - "0xddce19308c3a42831e215c55aa42af46b09d98ccb2c48fc25a74aebc929f4b5d", - "0x734ea7bd03f510e7dd0acc85a7ceb777d5d7d5ad5650785536fc09179a250143" - ] + "mmr_proof": { + "member_leaf_index": 8610265, + "last_leaf_index": 8610265, + "proof": [ + "0x36f3d834cbe12a5a20b063c432b88f5506bdce03b93fa3aa035a5d82fd50177c", + "0x541f71c116b054b7cf0f35461b41abdc0bc6e461ae54862c8dc3af6b48742a35", + "0x095f1b66aed2db73567706e227981454a80a442aa69b00aedaef787fd5401a78", + "0x942cfd846d464575b520554dd07fd4b07ba2cb8bee6234530db1c1b3a69e1c50", + "0x3174f9fdf5f7c9df6ac422a0a9e2aec05ccb7968bb1ff19b6cc0b64fbc930e77", + "0x5624f44ff885dcdc971303281bb76c6b6a957a6cc2d244b30f736fc6220f9b89", + "0xa3adc2ee02f7ceda7e63df8fb58094769afd3f7657c8f9f45cdc69d5c868b468", + "0xe588926a1643dc04b197a7ed30dd47d42d35fdb01f7aa80b2e293b058c11c6a1", + "0x25d7ac48be9565512424ef559b7e0ec7a91bfe9b57440913712a174e35eb47d8", + "0xddce19308c3a42831e215c55aa42af46b09d98ccb2c48fc25a74aebc929f4b5d", + "0x734ea7bd03f510e7dd0acc85a7ceb777d5d7d5ad5650785536fc09179a250143" + ] + } } "# ).unwrap(); @@ -486,18 +500,22 @@ fn verify_redeem_deposit() { "proof": "0xf9065ff9065cb8b3f8b1a00465eebe6e5de09530e54a14732f416c6dd3df3e3d4de7e224057775e176005fa0364264753139e9b26d4caa5550e780af82744f1fa0212fb4d944843f40941767a0d5e53b63048540c8faf6b8cb035d471603e21ed03641b2750fae5b6029968928a0a2c1de87659c963a8a8970108e0087f59b102253748e0655f60005fc5f21463780808080a0985911552c5e2f0c8f1d3b43d8c68cb6cd0e23292e7a01ed65865d9c35b5364c8080808080808080b90214f90211a09a09b69760a6f7754adb10479eed2baa872b4994161458511eda43e19ada21d1a0778f7b69876965bfc8363c672c21912ca5f92f93980999e1da19b9849c075d45a0c120f0350037e907148309c3a2e5b07bfe220e630f67c1bd7d834b40e0c6e484a0138d6d6f0e5bcf7a46b8881303caeb3bbbd3cb32d654c0294172d44d3fad3ad2a0e0665be49fab75e8c174394d55813a6fc460d64e396ef4881a55a44b89e1fae3a00227d0ab9af8d74eddbf2f8ee9bb66f7ba856ae09571f9a24272f88854d9a771a00b40ca0d746f1610bcd9d1733cdbb4a03a495cc03e6b36586e78b7f752bf8908a02c5ae2bf60a79068b949a1a5cc831f468e8092615aa61b9c8e42f21449bad981a0d6fb9b1a137c24ab6aeadfd96385eeeeebf93061285e3d2e72c5227532d2f50ba0f8206f39008acc6ce0b550424a79d05daabe982f12aaaa499ebf6165c271721ba07e1caebbd285e75cceecfa73f76dc0d5c9b3a4471d37249a1f0c8f9f9f08404aa044e197285d11b10d4d1db722bc28eb33904014c70e1f0a48dc2557be28ee836da0d2255e960373f9d0a8af01d7a2eaa2ef61d4626be9a2ea25b92cd00b7eb2d370a0ac6e37eb951f28e057c20f2be5ec4929b03f523d6fb7728701dea74be9f7c7cba06929256f0c8f74646539b0407b77c905451e1b74bc1f968a73b4c51b4889c537a0fa4f77e7a759b28b652a748dff3f136fa4ed34458fd7e212af8614235a14b27380b9038df9038a20b90386f90383018371c62eb9010000000000000000400000200000000000000000000000001000000000000000000000000140000000000000000400000000000000000000000000000000000000000000000000008000000008000000000000000000000000000000000000000000000000020000000000000000000800080000000000000000000010000000001000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000800008000000208000000000000000000000002000000000000000000040000000001000000000000000000020020000000000000000000080000000000000000000000000000000000000000000000f90278f87a94b52fbe2b925ab79a821b261c82c5ba0814aaa5e0f842a0cc16f5dbb4873280815c1ee09dbd06736cffcc184412cf7a71a0fdb75d397ca5a00000000000000000000000006ef538314829efa8386fc43386cb13b4e0a67d1ea000000000000000000000000000000000000000000000003643aa647986040000f89b94b52fbe2b925ab79a821b261c82c5ba0814aaa5e0f863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa00000000000000000000000006ef538314829efa8386fc43386cb13b4e0a67d1ea00000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000003643aa647986040000f9015c946ef538314829efa8386fc43386cb13b4e0a67d1ef842a0e77bf2fa8a25e63c1e5e29e1b2fcb6586d673931e020c4e3ffede453b830fb12a0000000000000000000000000000000000000000000000000000000000000001eb90100000000000000000000000000cc5e48beb33b83b8bd0d9d9a85a8f6a27c51f5c5000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000005f50b7de00000000000000000000000000000000000000000000000000000000000003e800000000000000000000000000000000000000000000003643aa64798604000000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000020e44664996ab7b5d86c12e9d5ac3093f5b2efc9172cb7ce298cd6c3c51002c318", "header_hash": "0x202591d2a7bff469ec186e3583e37b9c4bce2db847612ff975180436b5a4a1f1" }, - "mmr_proof": [ - "0x36f3d834cbe12a5a20b063c432b88f5506bdce03b93fa3aa035a5d82fd50177c", - "0x541f71c116b054b7cf0f35461b41abdc0bc6e461ae54862c8dc3af6b48742a35", - "0x095f1b66aed2db73567706e227981454a80a442aa69b00aedaef787fd5401a78", - "0x942cfd846d464575b520554dd07fd4b07ba2cb8bee6234530db1c1b3a69e1c50", - "0x3174f9fdf5f7c9df6ac422a0a9e2aec05ccb7968bb1ff19b6cc0b64fbc930e77", - "0xf50c2f20115cfdc7ae6c3bf3b3bf42f386ce517310a9ae985da2ef2c9f72b54e", - "0x4b2db3a5e7dafa1627b3ec6816bb6aa4b1212c5291ca1eeaa4f13a0e140fb814", - "0x07d9c0ef21208dbc3ef29d44293b77811e1c4937cb4d3c099d7a66d3d692ab2f", - "0xa733e5990271f7c5a68f159c23c58f5bc5fa5187b173697230ff1637077ce9df", - "0x870d2655fe393a3d12f595bce56ce5115907af9735cc8de8d95c05e7467d1321" - ] + "mmr_proof": { + "member_leaf_index": 8610453, + "last_leaf_index": 8610453, + "proof": [ + "0x36f3d834cbe12a5a20b063c432b88f5506bdce03b93fa3aa035a5d82fd50177c", + "0x541f71c116b054b7cf0f35461b41abdc0bc6e461ae54862c8dc3af6b48742a35", + "0x095f1b66aed2db73567706e227981454a80a442aa69b00aedaef787fd5401a78", + "0x942cfd846d464575b520554dd07fd4b07ba2cb8bee6234530db1c1b3a69e1c50", + "0x3174f9fdf5f7c9df6ac422a0a9e2aec05ccb7968bb1ff19b6cc0b64fbc930e77", + "0xf50c2f20115cfdc7ae6c3bf3b3bf42f386ce517310a9ae985da2ef2c9f72b54e", + "0x4b2db3a5e7dafa1627b3ec6816bb6aa4b1212c5291ca1eeaa4f13a0e140fb814", + "0x07d9c0ef21208dbc3ef29d44293b77811e1c4937cb4d3c099d7a66d3d692ab2f", + "0xa733e5990271f7c5a68f159c23c58f5bc5fa5187b173697230ff1637077ce9df", + "0x870d2655fe393a3d12f595bce56ce5115907af9735cc8de8d95c05e7467d1321" + ] + } } "# ).unwrap(); diff --git a/frame/bridge/ethereum/linear-relay/src/lib.rs b/frame/bridge/ethereum/linear-relay/src/lib.rs index 3d0c98dcc6..f2e8a503a3 100644 --- a/frame/bridge/ethereum/linear-relay/src/lib.rs +++ b/frame/bridge/ethereum/linear-relay/src/lib.rs @@ -74,7 +74,7 @@ use ethereum_primitives::{ header::EthereumHeader, pow::EthashPartial, receipt::{EthereumReceipt, EthereumReceiptProof, EthereumTransactionIndex}, - EthereumBlockNumber, H256, U256, + EthereumBlockNumber, H256, U256, EthereumNetworkType }; use types::*; @@ -817,17 +817,6 @@ impl SignedExtension for CheckEthereumRelayHeaderHash } } -#[derive(Clone, PartialEq, Encode, Decode)] -pub enum EthereumNetworkType { - Mainnet, - Ropsten, -} -impl Default for EthereumNetworkType { - fn default() -> EthereumNetworkType { - EthereumNetworkType::Mainnet - } -} - /// Familial details concerning a block #[derive(Clone, Default, PartialEq, Encode, Decode)] pub struct EthereumHeaderBrief { diff --git a/frame/bridge/ethereum/relay/src/lib.rs b/frame/bridge/ethereum/relay/src/lib.rs index 2a9ec634ad..89618f2242 100644 --- a/frame/bridge/ethereum/relay/src/lib.rs +++ b/frame/bridge/ethereum/relay/src/lib.rs @@ -1,7 +1,52 @@ -//! # Darwinia-ethereum-relay Module +//! # Darwinia Ethereum Relay Module #![cfg_attr(not(feature = "std"), no_std)] +mod migration { + // --- substrate --- + use frame_support::migration::*; + // --- darwinia --- + use crate::*; + + pub fn migrate() { + sp_runtime::print("Migrating DarwiniaEthereumRelay..."); + + let module_name: &[u8] = b"DarwiniaEthereumRelay"; + let value_items: &[&[u8]] = &[ + // pub LastConfirmedHeaderInfo get(fn last_confirmed_header_info): (EthereumBlockNumber, H256, H256); + b"LastConfirmedHeaderInfo", + // LastConfirmedBlockCycle: EthereumBlockNumber; + b"LastConfirmedBlockCycle", + // pub ConfirmBlocksInCycle get(fn confirm_block_cycle): EthereumBlockNumber = 185142; + b"ConfirmBlocksInCycle", + // pub ConfirmBlockKeepInMonth get(fn confirm_block_keep_in_mounth): EthereumBlockNumber = 3; + b"ConfirmBlockKeepInMonth", + ]; + let map_items: &[&[u8]] = &[ + // pub ConfirmedHeadersDoubleMap + // get(fn confirmed_header) + // : double_map hasher(identity) EthereumBlockNumber, hasher(identity) EthereumBlockNumber => EthereumHeader; + b"ConfirmedHeadersDoubleMap", + ]; + let hash: &[u8] = &[]; + + for item in value_items { + // --- substrate --- + use frame_support::{storage::unhashed::kill, StorageHasher, Twox128}; + + let mut key = vec![0u8; 32 + hash.len()]; + key[0..16].copy_from_slice(&Twox128::hash(module_name)); + key[16..32].copy_from_slice(&Twox128::hash(item)); + key[32..].copy_from_slice(hash); + kill(&key); + } + + for item in map_items { + remove_storage_prefix(module_name, item, hash); + } + } +} + mod mmr; #[cfg(test)] mod mock; @@ -15,8 +60,6 @@ mod types { pub type AccountId = ::AccountId; pub type RingBalance = as Currency>>::Balance; - pub type MMRProof = Vec; - type CurrencyT = ::Currency; } @@ -56,7 +99,7 @@ use ethereum_primitives::{ header::EthereumHeader, pow::EthashPartial, receipt::{EthereumReceipt, EthereumReceiptProof, EthereumTransactionIndex}, - EthereumBlockNumber, H256, + EthereumBlockNumber, EthereumNetworkType, H256, }; use types::*; @@ -69,6 +112,8 @@ pub trait Trait: frame_system::Trait { type Event: From> + Into<::Event>; + type EthereumNetwork: Get; + type Call: Dispatchable + From> + IsSubType> + Clone; type Currency: LockableCurrency @@ -97,19 +142,6 @@ decl_event! { /// The specific confirmed block is removed. [block height] RemoveConfirmedBlock(EthereumBlockNumber), - /// The range of confirmed blocks are removed. [block height, block height] - RemoveConfirmedBlockRang(EthereumBlockNumber, EthereumBlockNumber), - - /// The block confimed block parameters are changed. [block height, block height] - UpdateConfrimedBlockCleanCycle(EthereumBlockNumber, EthereumBlockNumber), - - /// This Error event is caused by unreasonable Confirm block delete parameter set. - /// - /// ConfirmBlockKeepInMonth should be greator then 1 to avoid the relayer game cross the - /// month. - /// [block height] - ConfirmBlockManagementError(EthereumBlockNumber), - /// EthereumReceipt Verification. [account, receipt, header] VerifyReceipt(AccountId, EthereumReceipt, EthereumHeader), } @@ -117,10 +149,6 @@ decl_event! { decl_error! { pub enum Error for Module { - /// Block Number - UNDERFLOW - BlockNumberUF, - /// Target Header - ALREADY EXISTED - TargetHeaderAE, /// Header - INVALID HeaderI, /// Confirmed Blocks - CONFLICT @@ -131,8 +159,8 @@ decl_error! { MMRI, /// Header Hash - MISMATCHED HeaderHashMis, - /// Last Header - NOT EXISTED - LastHeaderNE, + /// Confirmed Header - NOT EXISTED + ConfirmedHeaderNE, /// EthereumReceipt Proof - INVALID ReceiptProofI, } @@ -146,35 +174,24 @@ darwinia_support::impl_genesis! { } decl_storage! { trait Store for Module as DarwiniaEthereumRelay { - /// Ethereum last confrimed header info including ethereum block number, hash, and mmr - pub - LastConfirmedHeaderInfo - get(fn last_confirmed_header_info) - : (EthereumBlockNumber, H256, H256); - - /// The Ethereum headers confrimed by relayer game - /// The actural storage needs to be defined - pub - ConfirmedHeadersDoubleMap + /// Confirmed Ethereum Headers + pub ConfirmedHeaders get(fn confirmed_header) - : double_map hasher(identity) EthereumBlockNumber, hasher(identity) EthereumBlockNumber - => EthereumHeader; + : map hasher(identity) EthereumBlockNumber => Option; - /// Dags merkle roots of ethereum epoch (each epoch is 30000) - pub DagsMerkleRoots get(fn dag_merkle_root): map hasher(identity) u64 => H128; + /// Confirmed Ethereum Block Numbers + /// The orders are from small to large + pub ConfirmedBlockNumbers get(fn confirmed_header_numbers): Vec; - /// The current confirm block cycle nubmer (default is one month one cycle) - LastConfirmedBlockCycle: EthereumBlockNumber; + pub ConfirmedDepth get(fn confirmed_depth) config(): u32 = 10; - /// The number of ehtereum blocks in a month - pub ConfirmBlocksInCycle get(fn confirm_block_cycle): EthereumBlockNumber = 185142; - /// The confirm blocks keep in month - pub ConfirmBlockKeepInMonth get(fn confirm_block_keep_in_mounth): EthereumBlockNumber = 3; + /// Dags merkle roots of ethereum epoch (each epoch is 30000) + pub DagsMerkleRoots get(fn dag_merkle_root): map hasher(identity) u64 => H128; pub ReceiptVerifyFee get(fn receipt_verify_fee) config(): RingBalance; } add_extra_genesis { - config(genesis_header_info): (EthereumBlockNumber, H256, H256); + config(genesis_header_info): (Vec, H256); config(dags_merkle_roots_loader): DagsMerkleRootsLoader; build(|config| { let GenesisConfig { @@ -183,13 +200,25 @@ decl_storage! { .. } = config; - LastConfirmedHeaderInfo::put(genesis_header_info); + if let Ok(header) = rlp::decode(&genesis_header_info.0) { + let header_info = ConfirmedEthereumHeaderInfo { + header, + mmr_root : genesis_header_info.1 + }; + + ConfirmedBlockNumbers::mutate(|numbers| { + numbers.push(header_info.header.number); + + ConfirmedHeaders::insert(header_info.header.number, header_info); + }); + } let dags_merkle_roots = if dags_merkle_roots_loader.dags_merkle_roots.is_empty() { DagsMerkleRootsLoader::from_str(DAGS_MERKLE_ROOTS_STR).dags_merkle_roots.clone() } else { dags_merkle_roots_loader.dags_merkle_roots.clone() }; + for (i, dag_merkle_root) in dags_merkle_roots.into_iter().enumerate() { DagsMerkleRoots::insert(i as u64, dag_merkle_root); } @@ -206,6 +235,11 @@ decl_module! { fn deposit_event() = default; + fn on_runtime_upgrade() -> frame_support::weights::Weight { + migration::migrate::(); + 0 + } + #[weight = 0] pub fn submit_proposal(origin, proposal: Vec) { let relayer = ensure_signed(origin)?; @@ -276,7 +310,7 @@ decl_module! { /// # #[weight = 10_000_000] pub fn set_receipt_verify_fee(origin, #[compact] new: RingBalance) { - T::RejectOrigin::ensure_origin(origin)?; + T::ApproveOrigin::ensure_origin(origin)?; >::put(new); } @@ -284,39 +318,44 @@ decl_module! { /// Remove the specific malicous block #[weight = 100_000_000] pub fn remove_confirmed_block(origin, number: EthereumBlockNumber) { - T::RejectOrigin::ensure_origin(origin)?; + T::ApproveOrigin::ensure_origin(origin)?; - ConfirmedHeadersDoubleMap::take(number / ConfirmBlocksInCycle::get(), number); + ::mutate(|numbers| { + let index = numbers.iter().position(|x| *x == number).unwrap(); + numbers.remove(index); + + ::remove(number); + }); Self::deposit_event(RawEvent::RemoveConfirmedBlock(number)); } - /// Remove the blocks in particular month (month is calculated as cycle) - #[weight = 100_000_000] - pub fn remove_confirmed_blocks_in_month(origin, cycle: EthereumBlockNumber) { - T::RejectOrigin::ensure_origin(origin)?; + // --- root call --- - let c = ConfirmBlocksInCycle::get(); - - ConfirmedHeadersDoubleMap::remove_prefix(cycle); + #[weight = 10_000_000] + pub fn clean_confirmeds(origin) { + T::ApproveOrigin::ensure_origin(origin)?; - Self::deposit_event(RawEvent::RemoveConfirmedBlockRang(cycle * c, cycle.saturating_add(1) * c)); + for block_number in Self::confirmed_header_numbers() { + ConfirmedHeaders::remove(block_number); + } + ConfirmedBlockNumbers::put(>::new()); } - /// Setup the parameters to delete the confirmed blocks after month * blocks_in_month - #[weight = 100_000_000] - pub fn set_confirmed_blocks_clean_parameters(origin, month: EthereumBlockNumber, blocks_in_month: EthereumBlockNumber) { - T::RejectOrigin::ensure_origin(origin)?; + #[weight = 10_000_000] + pub fn set_confirmed(origin, header_thing: EthereumHeaderThing) { + T::ApproveOrigin::ensure_origin(origin)?; - if month < 2 { - // read the doc string of of event - Self::deposit_event(RawEvent::ConfirmBlockManagementError(month)); - } else { - ConfirmBlocksInCycle::put(blocks_in_month); - ConfirmBlockKeepInMonth::put(month); + let EthereumHeaderThing { header, mmr_root } = header_thing; - Self::deposit_event(RawEvent::UpdateConfrimedBlockCleanCycle(month, blocks_in_month)); - } + ConfirmedBlockNumbers::mutate(|numbers| { + numbers.push(header.number); + + ConfirmedHeaders::insert( + header.number, + ConfirmedEthereumHeaderInfo { header, mmr_root }, + ); + }); } } } @@ -326,55 +365,23 @@ impl Module { /// /// This actually does computation. If you need to keep using it, then make sure you cache the /// value and only call this once. - fn account_id() -> AccountId { + pub fn account_id() -> AccountId { T::ModuleId::get().into_account() } - /// validate block with the hash, difficulty of confirmed headers - fn verify_block_with_confrim_blocks(header: &EthereumHeader) -> bool { - let eth_partial = EthashPartial::production(); - let confirm_blocks_in_cycle = ConfirmBlocksInCycle::get(); - if ConfirmedHeadersDoubleMap::contains_key( - (header.number.saturating_sub(1)) / confirm_blocks_in_cycle, - header.number.saturating_sub(1), - ) { - let previous_header = ConfirmedHeadersDoubleMap::get( - (header.number.saturating_sub(1)) / confirm_blocks_in_cycle, - header.number.saturating_sub(1), - ); - if header.parent_hash != previous_header.hash.unwrap_or_default() - || *header.difficulty() - != eth_partial.calculate_difficulty(header, &previous_header) - { - return false; - } - } - - if ConfirmedHeadersDoubleMap::contains_key( - (header.number.saturating_add(1)) / confirm_blocks_in_cycle, - header.number.saturating_add(1), - ) { - let subsequent_header = ConfirmedHeadersDoubleMap::get( - (header.number.saturating_add(1)) / confirm_blocks_in_cycle, - header.number.saturating_add(1), - ); - if header.hash.unwrap_or_default() != subsequent_header.parent_hash - || *subsequent_header.difficulty() - != eth_partial.calculate_difficulty(&subsequent_header, header) - { - return false; - } + pub fn ethash_params() -> EthashPartial { + match T::EthereumNetwork::get() { + EthereumNetworkType::Mainnet => EthashPartial::production(), + EthereumNetworkType::Ropsten => EthashPartial::ropsten_testnet(), } - - true } - fn verify_block_seal(header: &EthereumHeader, ethash_proof: &[EthashProof]) -> bool { + pub fn verify_header(header: &EthereumHeader, ethash_proof: &[EthashProof]) -> bool { if header.hash() != header.re_compute_hash() { return false; } - let eth_partial = EthashPartial::production(); + let eth_partial = Self::ethash_params(); if eth_partial.verify_block_basic(header).is_err() { return false; @@ -395,26 +402,16 @@ impl Module { true } - fn verify_basic(header: &EthereumHeader, ethash_proof: &[EthashProof]) -> DispatchResult { - ensure!( - Self::verify_block_seal(header, ethash_proof), - >::HeaderI - ); - ensure!( - Self::verify_block_with_confrim_blocks(header), - >::ConfirmebBlocksC - ); - - Ok(()) - } + // TODO + // pub fn verify_continuous() -> bool {} // Verify the MMR root // NOTE: leaves are (block_number, H256) // block_number will transform to position in this function - fn verify_mmr( + pub fn verify_mmr( last_leaf: u64, mmr_root: H256, - mmr_proof: MMRProof, + mmr_proof: Vec, leaves: Vec<(u64, H256)>, ) -> bool { let p = MerkleProof::<[u8; 32], MMRMerge>::new( @@ -437,7 +434,7 @@ impl Relayable for Module { type HeaderThingWithProof = EthereumHeaderThingWithProof; type HeaderThing = EthereumHeaderThing; - fn basic_verify( + fn verify( proposal_with_proof: Vec, ) -> Result, DispatchError> { let proposal_len = proposal_with_proof.len(); @@ -466,10 +463,22 @@ impl Relayable for Module { last_leaf = header.number - 1; if proposal_len == 1 { - Self::verify_basic(&header, ðash_proof)?; + ensure!( + Self::verify_header(&header, ðash_proof), + >::HeaderI + ); - let (last_confirmed_block_number, last_confirmed_hash, _) = - LastConfirmedHeaderInfo::get(); + let ConfirmedEthereumHeaderInfo { + header: last_confirmed_header, + mmr_root: _, + } = Self::confirmed_header(Self::best_block_number()) + .ok_or(>::ConfirmedHeaderNE)?; + + // last_confirmed_header.hash should not be None. + let (last_confirmed_block_number, last_confirmed_hash) = ( + last_confirmed_header.number, + last_confirmed_header.hash.unwrap_or_default(), + ); trace!( target: "ethereum-relay", @@ -506,7 +515,10 @@ impl Relayable for Module { ); } } else if i == proposal_len - 1 { - Self::verify_basic(&header, ðash_proof)?; + ensure!( + Self::verify_header(&header, ðash_proof), + >::HeaderI + ); // last confirm no exsit the mmr verification will be passed // @@ -540,13 +552,16 @@ impl Relayable for Module { } fn best_block_number() -> ::Number { - Self::last_confirmed_header_info().0 + Self::confirmed_header_numbers() + .last() + .copied() + .unwrap_or(0) } fn on_chain_arbitrate(proposal: Vec) -> DispatchResult { // Currently Ethereum samples function is continuously sampling - let eth_partial = EthashPartial::production(); + let eth_partial = Self::ethash_params(); for i in 1..proposal.len() - 1 { let header = &proposal[i].header; @@ -567,31 +582,25 @@ impl Relayable for Module { } fn store_header(header_thing: Self::HeaderThing) -> DispatchResult { - let last_comfirmed_block_number = LastConfirmedHeaderInfo::get().0; + let last_comfirmed_block_number = Self::best_block_number(); let EthereumHeaderThing { header, mmr_root } = header_thing; // Not allow to relay genesis header - ensure!(header.number > 0, >::HeaderI); - - if header.number > last_comfirmed_block_number { - LastConfirmedHeaderInfo::put(( - header.number, - H256::from(array_unchecked!(header.hash.unwrap_or_default(), 0, 32)), - mmr_root, - )); - }; + ensure!( + header.number > last_comfirmed_block_number, + >::HeaderI + ); - let confirm_cycle = header.number / ConfirmBlocksInCycle::get(); - let last_confirmed_block_cycle = LastConfirmedBlockCycle::get(); + ConfirmedBlockNumbers::mutate(|numbers| { + numbers.push(header.number); - ConfirmedHeadersDoubleMap::insert(confirm_cycle, header.number, header); + // TODO: remove old numbers according to ConfirmedDepth - if confirm_cycle > last_confirmed_block_cycle { - ConfirmedHeadersDoubleMap::remove_prefix( - confirm_cycle.saturating_sub(ConfirmBlockKeepInMonth::get()), + ConfirmedHeaders::insert( + header.number, + ConfirmedEthereumHeaderInfo { header, mmr_root }, ); - LastConfirmedBlockCycle::put(confirm_cycle); - } + }); Ok(()) } @@ -622,23 +631,23 @@ impl EthereumReceiptT, RingBalance> for Module { >::HeaderHashMis, ); - // Verify header member to last confirmed block using mmr proof - let (last_leaf, mmr_root) = { - let (block_number, _, mmr_root) = Self::last_confirmed_header_info(); + ensure!( + eth_header.number == mmr_proof.member_leaf_index, + >::MMRI, + ); - ( - block_number - .checked_sub(1) - .ok_or(>::BlockNumberUF)?, - mmr_root, - ) - }; + // Verify header member to last confirmed block using mmr proof + let ConfirmedEthereumHeaderInfo { + header: _, + mmr_root, + } = Self::confirmed_header(mmr_proof.last_leaf_index + 1) + .ok_or(>::ConfirmedHeaderNE)?; ensure!( Self::verify_mmr( - last_leaf, + mmr_proof.last_leaf_index, mmr_root, - mmr_proof.to_vec(), + mmr_proof.proof.to_vec(), vec![( eth_header.number, array_unchecked!(eth_header.hash.unwrap_or_default(), 0, 32).into(), @@ -675,7 +684,7 @@ pub struct EthereumHeaderThingWithProof { } #[cfg_attr(any(feature = "deserialize", test), derive(serde::Deserialize))] -#[derive(Clone, PartialEq, Encode, Decode, Default, RuntimeDebug)] +#[derive(Clone, PartialEq, Eq, Encode, Decode, Default, RuntimeDebug)] pub struct EthereumHeaderThing { pub header: EthereumHeader, pub mmr_root: H256, @@ -693,6 +702,22 @@ impl HeaderThing for EthereumHeaderThing { } } +#[derive(Clone, Default, PartialEq, Encode, Decode, RuntimeDebug)] +pub struct ConfirmedEthereumHeaderInfo { + /// Ethereum Block Number + pub header: EthereumHeader, + /// MMR root of all previous headers. + pub mmr_root: H256, +} + +#[cfg_attr(any(feature = "deserialize", test), derive(serde::Deserialize))] +#[derive(Clone, Default, PartialEq, Encode, Decode, RuntimeDebug)] +pub struct MMRProof { + pub member_leaf_index: u64, + pub last_leaf_index: u64, + pub proof: Vec, +} + #[derive(Encode, Decode, Clone, Eq, PartialEq)] pub struct CheckEthereumRelayHeaderHash(PhantomData); impl CheckEthereumRelayHeaderHash { diff --git a/frame/bridge/ethereum/relay/src/mock.rs b/frame/bridge/ethereum/relay/src/mock.rs index 9aae7b9f98..14469079b3 100644 --- a/frame/bridge/ethereum/relay/src/mock.rs +++ b/frame/bridge/ethereum/relay/src/mock.rs @@ -48,10 +48,12 @@ darwinia_support::impl_account_data! { pub struct Test; parameter_types! { pub const EthereumRelayModuleId: ModuleId = ModuleId(*b"da/ethrl"); + pub const EthereumNetwork: EthereumNetworkType = EthereumNetworkType::Mainnet; } impl Trait for Test { type ModuleId = EthereumRelayModuleId; type Event = (); + type EthereumNetwork = EthereumNetwork; type Call = Call; type Currency = Ring; type RelayerGame = UnusedRelayerGame; @@ -119,8 +121,33 @@ impl ExtBuilder { GenesisConfig:: { genesis_header_info: ( - 0, - b"\xd4\xe5g@\xf8v\xae\xf8\xc0\x10\xb8j@\xd5\xf5gE\xa1\x18\xd0\x90j4\xe6\x9a\xec\x8c\r\xb1\xcb\x8f\xa3".into(), + vec![ + 249, 2, 20, 160, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 160, 29, 204, 77, 232, 222, 199, 93, 122, 171, + 133, 181, 103, 182, 204, 212, 26, 211, 18, 69, 27, 148, 138, 116, 19, 240, 161, + 66, 253, 64, 212, 147, 71, 148, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 160, 215, 248, 151, 79, 181, 172, 120, 217, 172, 9, 155, 154, 213, + 1, 139, 237, 194, 206, 10, 114, 218, 209, 130, 122, 23, 9, 218, 48, 88, 15, 5, + 68, 160, 86, 232, 31, 23, 27, 204, 85, 166, 255, 131, 69, 230, 146, 192, 248, + 110, 91, 72, 224, 27, 153, 108, 173, 192, 1, 98, 47, 181, 227, 99, 180, 33, + 160, 86, 232, 31, 23, 27, 204, 85, 166, 255, 131, 69, 230, 146, 192, 248, 110, + 91, 72, 224, 27, 153, 108, 173, 192, 1, 98, 47, 181, 227, 99, 180, 33, 185, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 133, 4, 0, + 0, 0, 0, 128, 130, 19, 136, 128, 128, 160, 17, 187, 232, 219, 78, 52, 123, 78, + 140, 147, 124, 28, 131, 112, 228, 181, 237, 51, 173, 179, 219, 105, 203, 219, + 122, 56, 225, 229, 11, 27, 130, 250, 160, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 136, 0, 0, 0, 0, 0, + 0, 0, 66, + ], b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".into() ), dags_merkle_roots_loader: DagsMerkleRootsLoader::from_file( diff --git a/frame/bridge/ethereum/relay/src/tests.rs b/frame/bridge/ethereum/relay/src/tests.rs index a82ad6f315..e48cdf4d92 100644 --- a/frame/bridge/ethereum/relay/src/tests.rs +++ b/frame/bridge/ethereum/relay/src/tests.rs @@ -26,7 +26,7 @@ fn proposal_basic_verification_should_sucess() { for id in 0..=2 { // eprintln!("{}, {}", game, id); - assert_ok!(::basic_verify( + assert_ok!(::verify( proposal_of_game_with_id(game, id) )); } diff --git a/frame/bridge/relayer-game/src/lib.rs b/frame/bridge/relayer-game/src/lib.rs index e39c47fe0e..bc86a01993 100644 --- a/frame/bridge/relayer-game/src/lib.rs +++ b/frame/bridge/relayer-game/src/lib.rs @@ -666,7 +666,7 @@ impl, I: Instance> RelayerGameProtocol for Module { proposal ); - let verified_proposal = T::TargetChain::basic_verify(proposal)?; + let verified_proposal = T::TargetChain::verify(proposal)?; let proposed_header = verified_proposal .get(0) .ok_or(>::ProposalI)? diff --git a/frame/bridge/relayer-game/src/mock.rs b/frame/bridge/relayer-game/src/mock.rs index 1558bfc0a5..001670b5b5 100644 --- a/frame/bridge/relayer-game/src/mock.rs +++ b/frame/bridge/relayer-game/src/mock.rs @@ -62,7 +62,7 @@ pub mod mock_relay { type HeaderThingWithProof = MockTcHeader; type HeaderThing = MockTcHeader; - fn basic_verify( + fn verify( proposal_with_proof: Vec, ) -> Result, DispatchError> { let verify = |header: &Self::HeaderThing| -> DispatchResult { diff --git a/primitives/ethereum-primitives/src/lib.rs b/primitives/ethereum-primitives/src/lib.rs index 6b9d5794e9..11852f864e 100644 --- a/primitives/ethereum-primitives/src/lib.rs +++ b/primitives/ethereum-primitives/src/lib.rs @@ -16,7 +16,19 @@ pub use ethereum_types::H64; pub use primitive_types::{H160, H256, U128, U256, U512}; use sp_std::prelude::*; +use codec::{Decode, Encode}; pub type Bytes = Vec; pub type EthereumAddress = H160; pub type EthereumBlockNumber = u64; + +#[derive(Clone, PartialEq, Encode, Decode)] +pub enum EthereumNetworkType { + Mainnet, + Ropsten, +} +impl Default for EthereumNetworkType { + fn default() -> EthereumNetworkType { + EthereumNetworkType::Mainnet + } +} diff --git a/primitives/relay/src/lib.rs b/primitives/relay/src/lib.rs index ca84ee21e0..85adaac79d 100644 --- a/primitives/relay/src/lib.rs +++ b/primitives/relay/src/lib.rs @@ -24,7 +24,7 @@ pub trait Relayable { // type BlockNumber: Clone + Copy + Debug + Default + AtLeast32BitUnsigned + FullCodec; // type HeaderHash: Clone + Debug + Default + PartialEq + FullCodec; - fn basic_verify( + fn verify( proposal_with_proof: Vec, ) -> Result, DispatchError>;