Bugs fixed for last_hashes.

This commit is contained in:
Gav Wood 2016-01-15 22:55:04 +01:00
parent 2eead090d5
commit e876a04afb
5 changed files with 46 additions and 18 deletions

View File

@ -255,6 +255,11 @@ impl IsBlock for SealedBlock {
/// Enact the block given by `block_bytes` using `engine` on the database `db` with given `parent` block header /// Enact the block given by `block_bytes` using `engine` on the database `db` with given `parent` block header
pub fn enact<'x, 'y>(block_bytes: &[u8], engine: &'x Engine, db: OverlayDB, parent: &Header, last_hashes: &'y LastHashes) -> Result<ClosedBlock<'x, 'y>, Error> { pub fn enact<'x, 'y>(block_bytes: &[u8], engine: &'x Engine, db: OverlayDB, parent: &Header, last_hashes: &'y LastHashes) -> Result<ClosedBlock<'x, 'y>, Error> {
{
let header = BlockView::new(block_bytes).header_view();
let s = State::from_existing(db.clone(), parent.state_root().clone(), engine.account_start_nonce());
// flush(format!("enact(): root={}, author={}, author_balance={}\n", s.root(), header.author(), s.balance(&header.author())));
}
let block = BlockView::new(block_bytes); let block = BlockView::new(block_bytes);
let header = block.header_view(); let header = block.header_view();
let mut b = OpenBlock::new(engine, db, parent, last_hashes, header.author(), header.extra_data()); let mut b = OpenBlock::new(engine, db, parent, last_hashes, header.author(), header.extra_data());

View File

@ -142,14 +142,13 @@ impl Client {
}, },
}; };
// build last hashes // build last hashes
let mut last = self.chain.read().unwrap().best_block_hash();
let mut last_hashes = LastHashes::new(); let mut last_hashes = LastHashes::new();
last_hashes.resize(256, H256::new()); last_hashes.resize(256, H256::new());
last_hashes[0] = header.parent_hash.clone();
for i in 0..255 { for i in 0..255 {
match self.chain.read().unwrap().block_details(&last) { match self.chain.read().unwrap().block_details(&last_hashes[i]) {
Some(details) => { Some(details) => {
last_hashes[i + 1] = details.parent.clone(); last_hashes[i + 1] = details.parent.clone();
last = details.parent.clone();
}, },
None => break, None => break,
} }

View File

@ -196,6 +196,8 @@ impl<'a> Executive<'a> {
// part of substate that may be reverted // part of substate that may be reverted
let mut unconfirmed_substate = Substate::new(); let mut unconfirmed_substate = Substate::new();
info!("exec: call; env_info={:?}", self.info);
let res = { let res = {
let mut ext = self.to_externalities(params, &mut unconfirmed_substate, OutputPolicy::Return(output)); let mut ext = self.to_externalities(params, &mut unconfirmed_substate, OutputPolicy::Return(output));
let evm = Factory::create(); let evm = Factory::create();
@ -257,12 +259,12 @@ impl<'a> Executive<'a> {
let refund_value = gas_left * t.gas_price; let refund_value = gas_left * t.gas_price;
let fees_value = gas_used * t.gas_price; let fees_value = gas_used * t.gas_price;
flush(format!("exec::finalize: t.gas={}, sstore_refunds={}, suicide_refunds={}, refunds_bound={}, gas_left_prerefund={}, refunded={}, gas_left={}, gas_used={}, refund_value={}, fees_value={}\n", // flush(format!("exec::finalize: t.gas={}, sstore_refunds={}, suicide_refunds={}, refunds_bound={}, gas_left_prerefund={}, refunded={}, gas_left={}, gas_used={}, refund_value={}, fees_value={}\n",
t.gas, sstore_refunds, suicide_refunds, refunds_bound, gas_left_prerefund, refunded, gas_left, gas_used, refund_value, fees_value)); // t.gas, sstore_refunds, suicide_refunds, refunds_bound, gas_left_prerefund, refunded, gas_left, gas_used, refund_value, fees_value));
flush(format!("exec::finalize: Refunding sender={}\n", t.sender().unwrap())); // flush(format!("exec::finalize: Refunding refund_value={}, sender={}\n", refund_value, t.sender().unwrap()));
self.state.add_balance(&t.sender().unwrap(), &refund_value); self.state.add_balance(&t.sender().unwrap(), &refund_value);
flush(format!("exec::finalize: Compensating author: fees_value: {}, author: {}", fees_value, &self.info.author)); // flush(format!("exec::finalize: Compensating author: fees_value={}, author={}\n", fees_value, &self.info.author));
self.state.add_balance(&self.info.author, &fees_value); self.state.add_balance(&self.info.author, &fees_value);
// perform suicides // perform suicides

View File

@ -84,9 +84,14 @@ impl<'a> Ext for Externalities<'a> {
match *number < U256::from(self.info.number) && number.low_u64() >= cmp::max(256, self.info.number) - 256 { match *number < U256::from(self.info.number) && number.low_u64() >= cmp::max(256, self.info.number) - 256 {
true => { true => {
let index = self.info.number - number.low_u64() - 1; let index = self.info.number - number.low_u64() - 1;
self.info.last_hashes[index as usize].clone() let r = self.info.last_hashes[index as usize].clone();
// flush(format!("ext: blockhash({}) -> {} self.info.number={}\n", number, r, self.info.number));
r
},
false => {
// flush(format!("ext: blockhash({}) -> null self.info.number={}\n", number, self.info.number));
H256::from(&U256::zero())
}, },
false => H256::from(&U256::zero()),
} }
} }
@ -142,6 +147,8 @@ impl<'a> Ext for Externalities<'a> {
call_gas = call_gas + U256::from(self.schedule.call_stipend); call_gas = call_gas + U256::from(self.schedule.call_stipend);
} }
// flush(format!("Externalities::call(gas={}, call_gas={}, recv={}, value={}, data={}, code={})\n", gas, call_gas, receive_address, value, data.pretty(), code_address));
if gas_cost > *gas { if gas_cost > *gas {
return Err(evm::Error::OutOfGas); return Err(evm::Error::OutOfGas);
} }
@ -165,8 +172,13 @@ impl<'a> Ext for Externalities<'a> {
data: Some(data.to_vec()), data: Some(data.to_vec()),
}; };
let mut ex = Executive::from_parent(self.state, self.info, self.engine, self.depth);
match ex.call(&params, self.substate, BytesRef::Fixed(output)) { // flush(format!("Externalities::call: BEFORE: bal({})={}, bal({})={}\n", params.sender, self.state.balance(&params.sender), params.address, self.state.balance(&params.address)));
// flush(format!("Externalities::call: CALLING: params={:?}\n", params));
let r = Executive::from_parent(self.state, self.info, self.engine, self.depth).call(&params, self.substate, BytesRef::Fixed(output));
// flush(format!("Externalities::call: AFTER: bal({})={}, bal({})={}\n", params.sender, self.state.balance(&params.sender), params.address, self.state.balance(&params.address)));
match r {
Ok(gas_left) => Ok((gas + gas_left, true)), Ok(gas_left) => Ok((gas + gas_left, true)),
_ => Ok((gas, false)) _ => Ok((gas, false))
} }

View File

@ -107,14 +107,16 @@ impl State {
/// Add `incr` to the balance of account `a`. /// Add `incr` to the balance of account `a`.
pub fn add_balance(&mut self, a: &Address, incr: &U256) { pub fn add_balance(&mut self, a: &Address, incr: &U256) {
flush(format!("state: add_balance({}, {})\n", a, incr)); // let old = self.balance(a);
self.require(a, false).add_balance(incr) self.require(a, false).add_balance(incr);
// flush(format!("state: add_balance({}, {}): {} -> {}\n", a, incr, old, self.balance(a)));
} }
/// Subtract `decr` from the balance of account `a`. /// Subtract `decr` from the balance of account `a`.
pub fn sub_balance(&mut self, a: &Address, decr: &U256) { pub fn sub_balance(&mut self, a: &Address, decr: &U256) {
flush(format!("state: sub_balance({}, {})\n", a, decr)); // let old = self.balance(a);
self.require(a, false).sub_balance(decr) self.require(a, false).sub_balance(decr);
// flush(format!("state: sub_balance({}, {}): {} -> {}\n", a, decr, old, self.balance(a)));
} }
/// Subtracts `by` from the balance of `from` and adds it to that of `to`. /// Subtracts `by` from the balance of `from` and adds it to that of `to`.
@ -148,7 +150,7 @@ impl State {
let e = try!(Executive::new(self, env_info, engine).transact(t)); let e = try!(Executive::new(self, env_info, engine).transact(t));
//println!("Executed: {:?}", e); //println!("Executed: {:?}", e);
flush(format!("Applied transaction. Diff:\n{}\n", StateDiff::diff_pod(&old, &self.to_pod()))); // flush(format!("Applied transaction. Diff:\n{}\n", StateDiff::diff_pod(&old, &self.to_pod())));
self.commit(); self.commit();
let receipt = Receipt::new(self.root().clone(), e.cumulative_gas_used, e.logs); let receipt = Receipt::new(self.root().clone(), e.cumulative_gas_used, e.logs);
debug!("Transaction receipt: {:?}", receipt); debug!("Transaction receipt: {:?}", receipt);
@ -227,8 +229,16 @@ impl State {
/// Pull account `a` in our cache from the trie DB and return it. /// Pull account `a` in our cache from the trie DB and return it.
/// `require_code` requires that the code be cached, too. /// `require_code` requires that the code be cached, too.
fn get(&self, a: &Address, require_code: bool) -> Ref<Option<Account>> { fn get(&self, a: &Address, require_code: bool) -> Ref<Option<Account>> {
self.cache.borrow_mut().entry(a.clone()).or_insert_with(|| self.cache.borrow_mut().entry(a.clone()).or_insert_with(|| {
SecTrieDB::new(&self.db, &self.root).get(&a).map(|rlp| Account::from_rlp(rlp))); /*{
let orlp = SecTrieDB::new(&self.db, &self.root).get(&a).map(|rlp| rlp.to_vec());
if let Some(rlp) = orlp {
let acc = Account::from_rlp(&rlp);
flush(format!("State::get({}) -> {} (bal={})\n", a, rlp.pretty(), acc.balance()));
}
}*/
SecTrieDB::new(&self.db, &self.root).get(&a).map(|rlp| Account::from_rlp(rlp))
});
if require_code { if require_code {
if let Some(ref mut account) = self.cache.borrow_mut().get_mut(a).unwrap().as_mut() { if let Some(ref mut account) = self.cache.borrow_mut().get_mut(a).unwrap().as_mut() {
account.cache_code(&self.db); account.cache_code(&self.db);