From a3c1eea6f30bed1a9c006e70bc5900a0ea584f4f Mon Sep 17 00:00:00 2001 From: Xiliang Chen Date: Thu, 17 Sep 2020 00:18:07 +1200 Subject: [PATCH] fix nonce hanlding and emit event on success execution (#130) --- frame/ethereum/src/lib.rs | 20 +++++++++++++------- frame/ethereum/src/mock.rs | 16 +++++----------- frame/ethereum/src/tests.rs | 24 +++++++++++++++++++----- 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/frame/ethereum/src/lib.rs b/frame/ethereum/src/lib.rs index 9d08d8f00..f605575a1 100644 --- a/frame/ethereum/src/lib.rs +++ b/frame/ethereum/src/lib.rs @@ -81,7 +81,10 @@ decl_storage! { decl_event!( /// Ethereum pallet events. - pub enum Event { } + pub enum Event { + /// An ethereum transaction was successfully executed. [from, transaction_hash] + Executed(H160, H256), + } ); @@ -149,7 +152,11 @@ decl_module! { let source = Self::recover_signer(&transaction).ok_or_else(|| Error::::InvalidSignature)?; + let hash = transaction.message_hash(Some(T::ChainId::get())); + Self::execute(source, transaction)?; + + Self::deposit_event(Event::Executed(source, hash)); } fn on_finalize(n: T::BlockNumber) { @@ -183,9 +190,6 @@ impl frame_support::unsigned::ValidateUnsigned for Module { if transaction.nonce < account_data.nonce { return InvalidTransaction::Stale.into(); } - if transaction.nonce > account_data.nonce { - return InvalidTransaction::Future.into(); - } let fee = transaction.gas_price.saturating_mul(transaction.gas_limit); @@ -194,10 +198,12 @@ impl frame_support::unsigned::ValidateUnsigned for Module { } let mut builder = ValidTransaction::with_tag_prefix("Ethereum") - .and_provides((&account_data, account_data.nonce)); + .and_provides((&origin, transaction.nonce)); - if let Some(prev_nonce) = account_data.nonce.checked_sub(1.into()) { - builder = builder.and_requires((account_data, prev_nonce)) + if transaction.nonce > account_data.nonce { + if let Some(prev_nonce) = transaction.nonce.checked_sub(1.into()) { + builder = builder.and_requires((origin, prev_nonce)) + } } builder.build() diff --git a/frame/ethereum/src/mock.rs b/frame/ethereum/src/mock.rs index 2f68b007d..e312201b3 100644 --- a/frame/ethereum/src/mock.rs +++ b/frame/ethereum/src/mock.rs @@ -235,7 +235,7 @@ impl UnsignedTransaction { s.append(&self.action); s.append(&self.value); s.append(&self.input); - s.append(&42u8); // TODO: move this chain id into the frame ethereum configuration + s.append(&ChainId::get()); s.append(&0u8); s.append(&0u8); } @@ -246,20 +246,14 @@ impl UnsignedTransaction { H256::from_slice(&Keccak256::digest(&stream.drain()).as_slice()) } - pub fn sign(self, key: &H256) -> Transaction { + pub fn sign(&self, key: &H256) -> Transaction { let hash = self.signing_hash(); - let msg = { - let mut a = [0u8; 32]; - for i in 0..32 { - a[i] = hash[i]; - } - secp256k1::Message::parse(&a) - }; + let msg = secp256k1::Message::parse(hash.as_fixed_bytes()); let s = secp256k1::sign(&msg, &secp256k1::SecretKey::parse_slice(&key[..]).unwrap()); let sig = s.0.serialize(); let sig = TransactionSignature::new( - 0x78, + s.1.serialize() as u64 % 2 + ChainId::get() * 2 + 35, H256::from_slice(&sig[0..32]), H256::from_slice(&sig[32..64]), ) @@ -271,7 +265,7 @@ impl UnsignedTransaction { gas_limit: self.gas_limit, action: self.action, value: self.value, - input: self.input, + input: self.input.clone(), signature: sig, } } diff --git a/frame/ethereum/src/tests.rs b/frame/ethereum/src/tests.rs index 0670cf30e..21266213f 100644 --- a/frame/ethereum/src/tests.rs +++ b/frame/ethereum/src/tests.rs @@ -37,7 +37,7 @@ use sp_runtime::transaction_validity::{TransactionSource, InvalidTransaction}; // } const ERC20_CONTRACT_BYTECODE: &str = "608060405234801561001057600080fd5b50610041337fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff61004660201b60201c565b610291565b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1614156100e9576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601f8152602001807f45524332303a206d696e7420746f20746865207a65726f20616464726573730081525060200191505060405180910390fd5b6101028160025461020960201b610c7c1790919060201c565b60028190555061015d816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205461020960201b610c7c1790919060201c565b6000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508173ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040518082815260200191505060405180910390a35050565b600080828401905083811015610287576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601b8152602001807f536166654d6174683a206164646974696f6e206f766572666c6f77000000000081525060200191505060405180910390fd5b8091505092915050565b610e3a806102a06000396000f3fe608060405234801561001057600080fd5b50600436106100885760003560e01c806370a082311161005b57806370a08231146101fd578063a457c2d714610255578063a9059cbb146102bb578063dd62ed3e1461032157610088565b8063095ea7b31461008d57806318160ddd146100f357806323b872dd146101115780633950935114610197575b600080fd5b6100d9600480360360408110156100a357600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610399565b604051808215151515815260200191505060405180910390f35b6100fb6103b7565b6040518082815260200191505060405180910390f35b61017d6004803603606081101561012757600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803590602001909291905050506103c1565b604051808215151515815260200191505060405180910390f35b6101e3600480360360408110156101ad57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291908035906020019092919050505061049a565b604051808215151515815260200191505060405180910390f35b61023f6004803603602081101561021357600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061054d565b6040518082815260200191505060405180910390f35b6102a16004803603604081101561026b57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610595565b604051808215151515815260200191505060405180910390f35b610307600480360360408110156102d157600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610662565b604051808215151515815260200191505060405180910390f35b6103836004803603604081101561033757600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610680565b6040518082815260200191505060405180910390f35b60006103ad6103a6610707565b848461070f565b6001905092915050565b6000600254905090565b60006103ce848484610906565b61048f846103da610707565b61048a85604051806060016040528060288152602001610d7060289139600160008b73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000610440610707565b73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610bbc9092919063ffffffff16565b61070f565b600190509392505050565b60006105436104a7610707565b8461053e85600160006104b8610707565b73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008973ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610c7c90919063ffffffff16565b61070f565b6001905092915050565b60008060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b60006106586105a2610707565b8461065385604051806060016040528060258152602001610de160259139600160006105cc610707565b73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008a73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610bbc9092919063ffffffff16565b61070f565b6001905092915050565b600061067661066f610707565b8484610906565b6001905092915050565b6000600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905092915050565b600033905090565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff161415610795576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526024815260200180610dbd6024913960400191505060405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16141561081b576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526022815260200180610d286022913960400191505060405180910390fd5b80600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925836040518082815260200191505060405180910390a3505050565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff16141561098c576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526025815260200180610d986025913960400191505060405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff161415610a12576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526023815260200180610d056023913960400191505060405180910390fd5b610a7d81604051806060016040528060268152602001610d4a602691396000808773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610bbc9092919063ffffffff16565b6000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550610b10816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610c7c90919063ffffffff16565b6000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040518082815260200191505060405180910390a3505050565b6000838311158290610c69576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825283818151815260200191508051906020019080838360005b83811015610c2e578082015181840152602081019050610c13565b50505050905090810190601f168015610c5b5780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b5060008385039050809150509392505050565b600080828401905083811015610cfa576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601b8152602001807f536166654d6174683a206164646974696f6e206f766572666c6f77000000000081525060200191505060405180910390fd5b809150509291505056fe45524332303a207472616e7366657220746f20746865207a65726f206164647265737345524332303a20617070726f766520746f20746865207a65726f206164647265737345524332303a207472616e7366657220616d6f756e7420657863656564732062616c616e636545524332303a207472616e7366657220616d6f756e74206578636565647320616c6c6f77616e636545524332303a207472616e736665722066726f6d20746865207a65726f206164647265737345524332303a20617070726f76652066726f6d20746865207a65726f206164647265737345524332303a2064656372656173656420616c6c6f77616e63652062656c6f77207a65726fa265627a7a72315820c7a5ffabf642bda14700b2de42f8c57b36621af020441df825de45fd2b3e1c5c64736f6c63430005100032"; -fn default_erc20_creation_transaction(account: &AccountInfo) -> Transaction { +fn default_erc20_creation_unsigned_transaction() -> UnsignedTransaction { UnsignedTransaction { nonce: U256::zero(), gas_price: U256::from(1), @@ -45,7 +45,11 @@ fn default_erc20_creation_transaction(account: &AccountInfo) -> Transaction { action: ethereum::TransactionAction::Create, value: U256::zero(), input: FromHex::from_hex(ERC20_CONTRACT_BYTECODE).unwrap(), - }.sign(&account.private_key) + } +} + +fn default_erc20_creation_transaction(account: &AccountInfo) -> Transaction { + default_erc20_creation_unsigned_transaction().sign(&account.private_key) } #[test] @@ -100,10 +104,18 @@ fn transaction_with_invalid_nonce_should_not_work() { ext.execute_with(|| { // nonce is 0 - let mut transaction = default_erc20_creation_transaction(alice); + let mut transaction = default_erc20_creation_unsigned_transaction(); transaction.nonce = U256::from(1); - assert_err!(Ethereum::validate_unsigned(TransactionSource::External, &Call::transact(transaction.clone())), InvalidTransaction::Future); + let signed = transaction.sign(&alice.private_key); + + assert_eq!( + Ethereum::validate_unsigned(TransactionSource::External, &Call::transact(signed)), + ValidTransaction::with_tag_prefix("Ethereum") + .and_provides((&alice.address, U256::from(1))) + .and_requires((&alice.address, U256::from(0))) + .build() + ); // nonce is 1 assert_ok!(Ethereum::execute( @@ -113,7 +125,9 @@ fn transaction_with_invalid_nonce_should_not_work() { transaction.nonce = U256::from(0); - assert_err!(Ethereum::validate_unsigned(TransactionSource::External, &Call::transact(transaction)), InvalidTransaction::Stale); + let signed2 = transaction.sign(&alice.private_key); + + assert_err!(Ethereum::validate_unsigned(TransactionSource::External, &Call::transact(signed2)), InvalidTransaction::Stale); }); }