Transaction address decoding.

This commit is contained in:
Gav Wood 2016-01-11 20:47:19 +01:00
parent 845ac87f88
commit 7239acc451
4 changed files with 20 additions and 12 deletions

View File

@ -97,6 +97,12 @@ impl From<ExecutionError> for Error {
}
}
impl From<CryptoError> for Error {
fn from(err: CryptoError) -> Error {
Error::Util(UtilError::Crypto(err))
}
}
// TODO: uncomment below once https://github.com/rust-lang/rust/issues/27336 sorted.
/*#![feature(concat_idents)]
macro_rules! assimilate {

View File

@ -94,24 +94,24 @@ impl<'a> Executive<'a> {
}
/// This funtion should be used to execute transaction.
pub fn transact(&mut self, t: &Transaction) -> ExecutionResult {
pub fn transact(&mut self, t: &Transaction) -> Result<Executed, Error> {
// TODO: validate transaction signature ?/ sender
let sender = t.sender();
let sender = try!(t.sender());
let nonce = self.state.nonce(&sender);
// validate transaction nonce
if t.nonce != nonce {
return Err(ExecutionError::InvalidNonce { expected: nonce, is: t.nonce });
return Err(From::from(ExecutionError::InvalidNonce { expected: nonce, is: t.nonce }));
}
// validate if transaction fits into given block
if self.info.gas_used + t.gas > self.info.gas_limit {
return Err(ExecutionError::BlockGasLimitReached {
return Err(From::from(ExecutionError::BlockGasLimitReached {
gas_limit: self.info.gas_limit,
gas_used: self.info.gas_used,
gas: t.gas
});
}));
}
// TODO: we might need bigints here, or at least check overflows.
@ -121,7 +121,7 @@ impl<'a> Executive<'a> {
// avoid unaffordable transactions
if balance < total_cost {
return Err(ExecutionError::NotEnoughCash { required: total_cost, is: balance });
return Err(From::from(ExecutionError::NotEnoughCash { required: total_cost, is: balance }));
}
// NOTE: there can be no invalid transactions from this point.
@ -160,7 +160,7 @@ impl<'a> Executive<'a> {
};
// finalize here!
self.finalize(t, substate, backup, res)
Ok(try!(self.finalize(t, substate, backup, res)))
}
/// Calls contract function with given contract params.
@ -232,7 +232,7 @@ impl<'a> Executive<'a> {
// real ammount to refund
let refund = cmp::min(sstore_refunds + suicide_refunds, (t.gas - gas_left) / U256::from(2)) + gas_left;
let refund_value = refund * t.gas_price;
self.state.add_balance(&t.sender(), &refund_value);
self.state.add_balance(&t.sender().unwrap(), &refund_value);
// fees earned by author
let fees = (t.gas - refund) * t.gas_price;

View File

@ -2,7 +2,7 @@ use common::*;
use engine::Engine;
use executive::Executive;
pub type ApplyResult = Result<Receipt, ExecutionError>;
pub type ApplyResult = Result<Receipt, Error>;
/// Representation of the entire state of all accounts in the system.
pub struct State {

View File

@ -1,5 +1,6 @@
use util::*;
use basic_types::*;
use error::Error;
pub enum Action {
Create,
@ -48,7 +49,7 @@ impl Transaction {
s.out()
}
pub fn rlp_sha3_opt(&self, with_seal: Seal) -> H256 { self.rlp_bytes_opt(with_seal).sha3() }
pub fn rlp_sha3_opt(&self, with_seal: Seal) -> H256 { self.rlp_bytes_opt(with_seal).sha3() }
}
impl RlpStandard for Transaction {
@ -77,8 +78,9 @@ impl Transaction {
pub fn action(&self) -> &Action { &self.action }
/// Returns transaction sender.
pub fn sender(&self) -> Address {
Address::new()
pub fn sender(&self) -> Result<Address, Error> {
let p = try!(ec::recover(&self.signature, &self.rlp_sha3_opt(Seal::Without)));
Ok(From::from(p.sha3()))
}
}