From e3fa730dc78cdec410232ccb31d81edd370793be Mon Sep 17 00:00:00 2001 From: debris Date: Tue, 12 Jan 2016 18:31:47 +0100 Subject: [PATCH] executive tests in progress --- src/executive.rs | 80 +++++++++++++++++++++++++++++++++++++++++++--- src/transaction.rs | 15 +++++++++ 2 files changed, 90 insertions(+), 5 deletions(-) diff --git a/src/executive.rs b/src/executive.rs index 4e829cdd5..0fdb46335 100644 --- a/src/executive.rs +++ b/src/executive.rs @@ -55,7 +55,6 @@ pub struct Executed { pub cumulative_gas_used: U256, /// Vector of logs generated by transaction. pub logs: Vec, - /// Execution ended running out of gas. pub out_of_gas: bool, /// Addresses of contracts created during execution of transaction. @@ -102,8 +101,6 @@ impl<'a> Executive<'a> { /// This funtion should be used to execute transaction. pub fn transact(&mut self, t: &Transaction) -> Result { - // TODO: validate transaction signature ?/ sender - let sender = try!(t.sender()); let nonce = self.state.nonce(&sender); @@ -244,9 +241,10 @@ impl<'a> Executive<'a> { self.state.add_balance(&t.sender().unwrap(), &refund_value); // fees earned by author - let fees = (t.gas - refund) * t.gas_price; + let fees = t.gas - refund; + let fees_value = fees * t.gas_price; let author = &self.info.author; - self.state.add_balance(author, &fees); + self.state.add_balance(author, &fees_value); // perform suicides for address in substate.suicides.iter() { @@ -742,4 +740,76 @@ mod tests { assert_eq!(state.storage_at(&address, &H256::from(&U256::zero())), H256::from(&U256::from(1))); assert_eq!(state.storage_at(&address, &H256::from(&U256::one())), H256::from(&U256::from(1))); } + + #[test] + fn test_transact_simple() { + let mut t = Transaction::new(U256::zero(), U256::zero(), U256::from(100_000), Action::Create, U256::from(17), "3331600055".from_hex().unwrap()); + t.r = U256::from_str("98ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4a").unwrap(); + t.s = U256::from_str("8887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a3").unwrap(); + t.v = 0x1c; + let sender = t.sender().unwrap(); + + let mut state = State::new_temp(); + state.add_balance(&sender, &U256::from(17)); + let mut info = EnvInfo::new(); + info.gas_limit = U256::from(100_000); + let engine = TestEngine::new(0); + + let executed = { + let mut ex = Executive::new(&mut state, &info, &engine); + ex.transact(&t).unwrap() + }; + + assert_eq!(executed.gas, U256::from(100_000)); + assert_eq!(executed.gas_used, U256::from(5025)); + assert_eq!(executed.refunded, U256::from(94_975)); + assert_eq!(executed.cumulative_gas_used, U256::from(5025)); + assert_eq!(executed.logs.len(), 0); + assert_eq!(executed.out_of_gas, false); + assert_eq!(executed.contracts_created.len(), 0); + } + + #[test] + fn test_transact_invalid_sender() { + let mut t = Transaction::new(U256::zero(), U256::zero(), U256::from(100_000), Action::Create, U256::from(17), "3331600055".from_hex().unwrap()); + t.r = U256::from_str("98ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4a").unwrap(); + t.s = U256::from_str("8887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a3").unwrap(); + t.v = 0x1d; + + let mut state = State::new_temp(); + let info = EnvInfo::new(); + let engine = TestEngine::new(0); + + let res = { + let mut ex = Executive::new(&mut state, &info, &engine); + ex.transact(&t) + }; + + match res { + Err(Error::Util(UtilError::Crypto(CryptoError::InvalidSignature))) => (), + _ => assert!(false, "expected signature error") + } + } + + #[test] + fn test_transact_invalid_nonce() { + let mut t = Transaction::new(U256::one(), U256::zero(), U256::from(100_000), Action::Create, U256::from(17), "3331600055".from_hex().unwrap()); + t.r = U256::from_str("98ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4a").unwrap(); + t.s = U256::from_str("8887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a3").unwrap(); + t.v = 0x1c; + + let mut state = State::new_temp(); + let info = EnvInfo::new(); + let engine = TestEngine::new(0); + + let res = { + let mut ex = Executive::new(&mut state, &info, &engine); + ex.transact(&t) + }; + + match res { + Err(Error::Execution(ExecutionError::InvalidNonce { expected, is })) if expected == U256::zero() && is == U256::one() => (), + _ => assert!(false, "expected signature error") + } + } } diff --git a/src/transaction.rs b/src/transaction.rs index 661c1355f..df7da7da2 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -27,6 +27,21 @@ pub struct Transaction { } impl Transaction { + pub fn new(nonce: U256, gas_price: U256, gas: U256, action: Action, value: U256, data: Bytes) -> Transaction { + Transaction { + nonce: nonce, + gas_price: gas_price, + gas: gas, + action: action, + value: value, + data: data, + v: 0, + r: U256::zero(), + s: U256::zero(), + hash: RefCell::new(None) + } + } + /// Append object into RLP stream, optionally with or without the signature. pub fn rlp_append_opt(&self, s: &mut RlpStream, with_seal: Seal) { s.append_list(6 + match with_seal { Seal::With => 3, _ => 0 });