Complete version of tracing test with reward

This commit is contained in:
Anton Gavrilov 2017-07-26 19:36:09 +02:00
parent 5086dc3024
commit 3fdb912db6
3 changed files with 149 additions and 33 deletions

View File

@ -453,7 +453,7 @@ impl<'x> OpenBlock<'x> {
s.block.header.set_transactions_root(ordered_trie_root(s.block.transactions.iter().map(|e| e.rlp_bytes().into_vec())));
}
let uncle_bytes = s.block.uncles.iter().fold(RlpStream::new_list(s.block.uncles.len()), |mut s, u| {s.append_raw(&u.rlp(Seal::With), 1); s} ).out();
if s.block.header.uncles_hash().is_zero() {
if s.block.header.uncles_hash().is_zero() || s.block.header.receipts_root() == &SHA3_NULL_RLP {
s.block.header.set_uncles_hash(uncle_bytes.sha3());
}
if s.block.header.receipts_root().is_zero() || s.block.header.receipts_root() == &SHA3_NULL_RLP {

View File

@ -76,6 +76,7 @@ impl Engine for NullEngine {
return Ok(CloseOutcome{trace: None});
}
/// Block reward
let fields = block.fields_mut();
let mut tracer = ExecutiveTracer::default();
@ -89,6 +90,18 @@ impl Engine for NullEngine {
let block_miner = fields.header.author().clone();
tracer.trace_reward(block_miner, result_block_reward, RewardType::Block);
/// Uncle rewards
let result_uncle_reward = U256::from(10000000);
for u in fields.uncles.iter() {
let uncle_miner = u.author().clone();
fields.state.add_balance(
u.author(),
&(result_uncle_reward),
CleanupMode::NoEmpty
)?;
tracer.trace_reward(uncle_miner, result_uncle_reward, RewardType::Uncle);
}
fields.state.commit()?;
match *fields.tracing_enabled {
true => Ok(CloseOutcome{trace: Some(tracer.traces())}),

View File

@ -30,38 +30,19 @@ use std::sync::Arc;
use header::Header;
use miner::Miner;
use transaction::{Action, Transaction};
use views::BlockView;
use types::trace_types::trace::RewardType;
use types::trace_types::trace::Action::Reward;
use types::trace_types::localized::LocalizedTrace;
#[test]
fn can_trace_block_and_uncle_reward() {
let dir = RandomTempPath::new();
let spec = Spec::new_test_with_reward();
let engine = &*spec.engine;
let genesis_header = spec.genesis_header();
let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
let last_hashes = Arc::new(vec![genesis_header.hash()]);
let mut b = OpenBlock::new(engine, Default::default(), true, db, &genesis_header, last_hashes, Address::zero(), (3141562.into(), 31415620.into()), vec![], false).unwrap();
let kp = KeyPair::from_secret_slice(&"".sha3()).unwrap();
let mut n = 0;
for _ in 0..1 {
b.push_transaction(Transaction {
nonce: n.into(),
gas_price: 0.into(),
gas: 100000.into(),
action: Action::Create,
data: vec![],
value: U256::zero(),
}.sign(kp.secret(), Some(spec.network_id())), None).unwrap();
n += 1;
}
let mut uncle = Header::new();
let uncle_author: Address = "ef2d6d194084c2de36e0dabfce45d046b37d1106".into();
uncle.set_author(uncle_author);
//b.push_uncle(uncle).unwrap();
let b = b.close_and_lock().seal(engine, vec![]).unwrap();
let db_config = DatabaseConfig::with_columns(::db::NUM_COLUMNS);
/// Create client
let db_config = DatabaseConfig::with_columns(::db::NUM_COLUMNS);
let mut client_config = ClientConfig::default();
client_config.tracing.enabled = true;
let client_db = Arc::new(Database::open(&db_config, dir.as_path().to_str().unwrap()).unwrap());
@ -72,22 +53,144 @@ fn can_trace_block_and_uncle_reward() {
Arc::new(Miner::with_spec(&spec)),
IoChannel::disconnected(),
).unwrap();
let res = client.import_block(b.rlp_bytes());
/// Create test data
let genesis_header = spec.genesis_header();
let mut db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
let mut rolling_timestamp = 40;
let mut last_hashes = vec![];
let mut last_header = genesis_header.clone();
last_hashes.push(last_header.hash());
let kp = KeyPair::from_secret_slice(&"".sha3()).unwrap();
let author = kp.address();
/// Add root block first
let mut root_block = OpenBlock::new(
engine,
Default::default(),
false,
db,
&last_header,
Arc::new(last_hashes.clone()),
author.clone(),
(3141562.into(), 31415620.into()),
vec![],
false,
).unwrap();
root_block.set_difficulty(U256::from(0x20000));
rolling_timestamp += 10;
root_block.set_timestamp(rolling_timestamp);
let root_block = root_block.close_and_lock().seal(engine, vec![]).unwrap();
if let Err(e) = client.import_block(root_block.rlp_bytes()) {
panic!("error importing block which is valid by definition: {:?}", e);
}
last_header = BlockView::new(&root_block.rlp_bytes()).header();
let root_header = last_header.clone();
db = root_block.drain();
last_hashes.push(last_header.hash());
/// Add parent block
let mut parent_block = OpenBlock::new(
engine,
Default::default(),
false,
db,
&last_header,
Arc::new(last_hashes.clone()),
author.clone(),
(3141562.into(), 31415620.into()),
vec![],
false,
).unwrap();
parent_block.set_difficulty(U256::from(0x20000));
rolling_timestamp += 10;
parent_block.set_timestamp(rolling_timestamp);
let parent_block = parent_block.close_and_lock().seal(engine, vec![]).unwrap();
if let Err(e) = client.import_block(parent_block.rlp_bytes()) {
panic!("error importing block which is valid by definition: {:?}", e);
}
last_header = BlockView::new(&parent_block.rlp_bytes()).header();
db = parent_block.drain();
last_hashes.push(last_header.hash());
/// Add testing block with transactions and uncles
let mut block = OpenBlock::new(
engine,
Default::default(),
true,
db,
&last_header,
Arc::new(last_hashes.clone()),
author.clone(),
(3141562.into(), 31415620.into()),
vec![],
false
).unwrap();
block.set_difficulty(U256::from(0x20000));
rolling_timestamp += 10;
block.set_timestamp(rolling_timestamp);
let mut n = 0;
for _ in 0..1 {
block.push_transaction(Transaction {
nonce: n.into(),
gas_price: 10000.into(),
gas: 100000.into(),
action: Action::Create,
data: vec![],
value: U256::zero(),
}.sign(kp.secret(), Some(spec.network_id())), None).unwrap();
n += 1;
}
let mut uncle = Header::new();
let uncle_author: Address = "ef2d6d194084c2de36e0dabfce45d046b37d1106".into();
uncle.set_author(uncle_author);
uncle.set_parent_hash(root_header.hash());
uncle.set_gas_limit(U256::from(50_000));
uncle.set_number(root_header.number() + 1);
uncle.set_timestamp(rolling_timestamp);
block.push_uncle(uncle).unwrap();
let block = block.close_and_lock().seal(engine, vec![]).unwrap();
let res = client.import_block(block.rlp_bytes());
if res.is_err() {
panic!("error importing block: {:#?}", res.err().unwrap());
}
block.drain();
client.flush_queue();
client.import_verified_blocks();
let filter = TraceFilter {
range: (BlockId::Number(1)..BlockId::Number(1)),
/// Filter the resuliting data
let filter = TraceFilter {
range: (BlockId::Number(1)..BlockId::Number(3)),
from_address: vec![],
to_address: vec![],
};
let traces = client.filter_traces(filter);
assert!(traces.is_some(), "Genesis trace should be always present.");
//panic!("Traces size is: {:#?}", traces.unwrap().len());
let traces = client.filter_traces(filter);
assert!(traces.is_some(), "Traces should be present");
let traces_vec = traces.unwrap();
let block_reward_traces: Vec<LocalizedTrace> = traces_vec.clone().into_iter().filter(|trace| match (trace).action {
Reward(ref a) => a.reward_type == RewardType::Block,
_ => false,
}).collect();
assert_eq!(block_reward_traces.len(), 3);
let uncle_reward_traces: Vec<LocalizedTrace> = traces_vec.clone().into_iter().filter(|trace| match (trace).action {
Reward(ref a) => a.reward_type == RewardType::Uncle,
_ => false,
}).collect();
assert_eq!(uncle_reward_traces.len(), 1);
}