2020-09-22 14:53:52 +02:00
|
|
|
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of OpenEthereum.
|
2017-07-25 12:04:37 +02:00
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is free software: you can redistribute it and/or modify
|
2017-07-25 12:04:37 +02:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is distributed in the hope that it will be useful,
|
2017-07-25 12:04:37 +02:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2020-09-22 14:53:52 +02:00
|
|
|
// along with OpenEthereum. If not, see <http://www.gnu.org/licenses/>.
|
2017-07-25 12:04:37 +02:00
|
|
|
|
|
|
|
//! Client tests of tracing
|
|
|
|
|
|
|
|
use block::*;
|
|
|
|
use client::{BlockChainClient, Client, ClientConfig, *};
|
2021-03-12 10:12:42 +01:00
|
|
|
use crypto::publickey::KeyPair;
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::{Address, U256};
|
2017-08-31 11:53:26 +02:00
|
|
|
use hash::keccak;
|
2017-07-25 12:04:37 +02:00
|
|
|
use io::*;
|
|
|
|
use miner::Miner;
|
|
|
|
use spec::*;
|
2021-03-12 10:12:42 +01:00
|
|
|
use std::{str::FromStr, sync::Arc};
|
2017-07-25 12:04:37 +02:00
|
|
|
use test_helpers::{self, get_temp_state_db};
|
2019-01-04 14:05:46 +01:00
|
|
|
use trace::{trace::Action::Reward, LocalizedTrace, RewardType};
|
|
|
|
use types::{
|
|
|
|
header::Header,
|
2020-12-10 16:42:05 +01:00
|
|
|
transaction::{Action, Transaction, TypedTransaction},
|
2020-08-05 06:08:03 +02:00
|
|
|
view,
|
2017-07-27 19:15:25 +02:00
|
|
|
views::BlockView,
|
|
|
|
};
|
2018-08-02 11:20:46 +02:00
|
|
|
use verification::queue::kind::blocks::Unverified;
|
2017-07-25 12:04:37 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_trace_block_and_uncle_reward() {
|
2018-06-20 15:13:07 +02:00
|
|
|
let db = test_helpers::new_db();
|
2017-07-31 12:23:47 +02:00
|
|
|
let spec = Spec::new_test_with_reward();
|
|
|
|
let engine = &*spec.engine;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-07-28 13:41:51 +02:00
|
|
|
// Create client
|
2017-07-25 12:04:37 +02:00
|
|
|
let mut client_config = ClientConfig::default();
|
|
|
|
client_config.tracing.enabled = true;
|
2017-07-31 12:23:47 +02:00
|
|
|
let client = Client::new(
|
2017-07-25 12:04:37 +02:00
|
|
|
client_config,
|
|
|
|
&spec,
|
2018-06-20 15:13:07 +02:00
|
|
|
db,
|
2018-04-13 17:34:27 +02:00
|
|
|
Arc::new(Miner::new_for_tests(&spec, None)),
|
2017-07-25 12:04:37 +02:00
|
|
|
IoChannel::disconnected(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-07-28 13:41:51 +02:00
|
|
|
// Create test data:
|
|
|
|
// genesis
|
|
|
|
// |
|
|
|
|
// root_block
|
|
|
|
// |
|
|
|
|
// parent_block
|
|
|
|
// |
|
|
|
|
// block with transaction and uncle
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-08-31 11:53:26 +02:00
|
|
|
let genesis_header = spec.genesis_header();
|
2017-09-26 14:19:08 +02:00
|
|
|
let genesis_gas = genesis_header.gas_limit().clone();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-07-31 12:23:47 +02:00
|
|
|
let mut db = spec
|
|
|
|
.ensure_db_good(get_temp_state_db(), &Default::default())
|
|
|
|
.unwrap();
|
2017-07-26 19:36:09 +02:00
|
|
|
let mut rolling_timestamp = 40;
|
|
|
|
let mut last_hashes = vec![];
|
|
|
|
let mut last_header = genesis_header.clone();
|
|
|
|
last_hashes.push(last_header.hash());
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2021-03-12 10:12:42 +01:00
|
|
|
let kp = KeyPair::from_secret_slice(keccak("").as_bytes()).unwrap();
|
2017-07-26 19:36:09 +02:00
|
|
|
let author = kp.address();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-07-28 13:41:51 +02:00
|
|
|
// Add root block first
|
2017-07-26 19:36:09 +02:00
|
|
|
let mut root_block = OpenBlock::new(
|
2017-08-10 12:36:29 +02:00
|
|
|
engine,
|
|
|
|
Default::default(),
|
|
|
|
false,
|
|
|
|
db,
|
|
|
|
&last_header,
|
|
|
|
Arc::new(last_hashes.clone()),
|
|
|
|
author.clone(),
|
|
|
|
(3141562.into(), 31415620.into()),
|
|
|
|
vec![],
|
|
|
|
false,
|
2019-03-15 15:43:54 +01:00
|
|
|
None,
|
2017-08-10 12:36:29 +02:00
|
|
|
)
|
|
|
|
.unwrap();
|
2017-07-26 19:36:09 +02:00
|
|
|
rolling_timestamp += 10;
|
|
|
|
root_block.set_timestamp(rolling_timestamp);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-07-16 13:53:55 +02:00
|
|
|
let root_block = root_block
|
|
|
|
.close_and_lock()
|
|
|
|
.unwrap()
|
|
|
|
.seal(engine, vec![])
|
|
|
|
.unwrap();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-08-02 11:20:46 +02:00
|
|
|
if let Err(e) = client.import_block(Unverified::from_rlp(root_block.rlp_bytes()).unwrap()) {
|
2017-07-26 19:36:09 +02:00
|
|
|
panic!(
|
|
|
|
"error importing block which is valid by definition: {:?}",
|
|
|
|
e
|
|
|
|
);
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-04-16 15:52:12 +02:00
|
|
|
last_header = view!(BlockView, &root_block.rlp_bytes()).header();
|
2017-07-26 19:36:09 +02:00
|
|
|
let root_header = last_header.clone();
|
2018-07-15 11:01:47 +02:00
|
|
|
db = root_block.drain().state.drop().1;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-07-26 19:36:09 +02:00
|
|
|
last_hashes.push(last_header.hash());
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-07-28 13:41:51 +02:00
|
|
|
// Add parent block
|
2017-07-26 19:36:09 +02:00
|
|
|
let mut parent_block = OpenBlock::new(
|
2017-08-10 12:36:29 +02:00
|
|
|
engine,
|
|
|
|
Default::default(),
|
|
|
|
false,
|
|
|
|
db,
|
|
|
|
&last_header,
|
|
|
|
Arc::new(last_hashes.clone()),
|
|
|
|
author.clone(),
|
|
|
|
(3141562.into(), 31415620.into()),
|
|
|
|
vec![],
|
|
|
|
false,
|
2019-03-15 15:43:54 +01:00
|
|
|
None,
|
2017-08-10 12:36:29 +02:00
|
|
|
)
|
|
|
|
.unwrap();
|
2017-07-26 19:36:09 +02:00
|
|
|
rolling_timestamp += 10;
|
|
|
|
parent_block.set_timestamp(rolling_timestamp);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-07-16 13:53:55 +02:00
|
|
|
let parent_block = parent_block
|
|
|
|
.close_and_lock()
|
|
|
|
.unwrap()
|
|
|
|
.seal(engine, vec![])
|
|
|
|
.unwrap();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-08-02 11:20:46 +02:00
|
|
|
if let Err(e) = client.import_block(Unverified::from_rlp(parent_block.rlp_bytes()).unwrap()) {
|
2017-07-26 19:36:09 +02:00
|
|
|
panic!(
|
|
|
|
"error importing block which is valid by definition: {:?}",
|
|
|
|
e
|
|
|
|
);
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-04-16 15:52:12 +02:00
|
|
|
last_header = view!(BlockView, &parent_block.rlp_bytes()).header();
|
2018-07-15 11:01:47 +02:00
|
|
|
db = parent_block.drain().state.drop().1;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-07-26 19:36:09 +02:00
|
|
|
last_hashes.push(last_header.hash());
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-07-28 13:41:51 +02:00
|
|
|
// Add testing block with transaction and uncle
|
2017-07-31 12:23:47 +02:00
|
|
|
let mut block = OpenBlock::new(
|
2017-08-31 11:53:26 +02:00
|
|
|
engine,
|
|
|
|
Default::default(),
|
|
|
|
true,
|
|
|
|
db,
|
|
|
|
&last_header,
|
2017-07-26 19:36:09 +02:00
|
|
|
Arc::new(last_hashes.clone()),
|
|
|
|
author.clone(),
|
2017-08-31 11:53:26 +02:00
|
|
|
(3141562.into(), 31415620.into()),
|
2017-07-26 19:36:09 +02:00
|
|
|
vec![],
|
2018-05-16 08:58:01 +02:00
|
|
|
false,
|
2019-03-15 15:43:54 +01:00
|
|
|
None,
|
2017-07-26 19:36:09 +02:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
rolling_timestamp += 10;
|
|
|
|
block.set_timestamp(rolling_timestamp);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-07-26 19:36:09 +02:00
|
|
|
let mut n = 0;
|
|
|
|
for _ in 0..1 {
|
|
|
|
block
|
|
|
|
.push_transaction(
|
2020-12-10 16:42:05 +01:00
|
|
|
TypedTransaction::Legacy(Transaction {
|
2017-07-26 19:36:09 +02:00
|
|
|
nonce: n.into(),
|
|
|
|
gas_price: 10000.into(),
|
|
|
|
gas: 100000.into(),
|
|
|
|
action: Action::Create,
|
|
|
|
data: vec![],
|
|
|
|
value: U256::zero(),
|
2020-12-10 16:42:05 +01:00
|
|
|
})
|
2017-07-26 19:36:09 +02:00
|
|
|
.sign(kp.secret(), Some(spec.network_id())),
|
|
|
|
None,
|
2020-08-05 06:08:03 +02:00
|
|
|
)
|
2017-07-26 19:36:09 +02:00
|
|
|
.unwrap();
|
|
|
|
n += 1;
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-07-31 12:23:47 +02:00
|
|
|
let mut uncle = Header::new();
|
2021-03-12 10:12:42 +01:00
|
|
|
let uncle_author = Address::from_str("ef2d6d194084c2de36e0dabfce45d046b37d1106").unwrap();
|
2017-07-31 12:23:47 +02:00
|
|
|
uncle.set_author(uncle_author);
|
2017-07-26 19:36:09 +02:00
|
|
|
uncle.set_parent_hash(root_header.hash());
|
2017-09-26 14:19:08 +02:00
|
|
|
uncle.set_gas_limit(genesis_gas);
|
2017-07-26 19:36:09 +02:00
|
|
|
uncle.set_number(root_header.number() + 1);
|
|
|
|
uncle.set_timestamp(rolling_timestamp);
|
2017-07-31 12:23:47 +02:00
|
|
|
block.push_uncle(uncle).unwrap();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-07-16 13:53:55 +02:00
|
|
|
let block = block
|
|
|
|
.close_and_lock()
|
|
|
|
.unwrap()
|
|
|
|
.seal(engine, vec![])
|
|
|
|
.unwrap();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-08-02 11:20:46 +02:00
|
|
|
let res = client.import_block(Unverified::from_rlp(block.rlp_bytes()).unwrap());
|
2017-07-31 12:23:47 +02:00
|
|
|
if res.is_err() {
|
2017-08-31 11:53:26 +02:00
|
|
|
panic!("error importing block: {:#?}", res.err().unwrap());
|
2017-07-25 12:04:37 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-07-26 19:36:09 +02:00
|
|
|
block.drain();
|
2017-07-25 12:04:37 +02:00
|
|
|
client.flush_queue();
|
|
|
|
client.import_verified_blocks();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-07-31 12:23:47 +02:00
|
|
|
// Test0. Check overall filter
|
2017-07-26 19:36:09 +02:00
|
|
|
let filter = TraceFilter {
|
2017-08-10 12:36:29 +02:00
|
|
|
range: (BlockId::Number(1)..BlockId::Number(3)),
|
|
|
|
from_address: vec![],
|
|
|
|
to_address: vec![],
|
2017-10-03 11:59:48 +02:00
|
|
|
after: None,
|
|
|
|
count: None,
|
2017-08-10 12:36:29 +02:00
|
|
|
};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-07-31 12:23:47 +02:00
|
|
|
let traces = client.filter_traces(filter);
|
2017-07-28 13:41:51 +02:00
|
|
|
assert!(traces.is_some(), "Filtered traces should be present");
|
2017-07-26 19:36:09 +02:00
|
|
|
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);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-07-28 13:41:51 +02:00
|
|
|
// Test1. Check block filter
|
|
|
|
let traces = client.block_traces(BlockId::Number(3));
|
2017-08-31 11:53:26 +02:00
|
|
|
assert_eq!(traces.unwrap().len(), 3);
|
2017-07-31 12:23:47 +02:00
|
|
|
}
|