openethereum/crates/ethcore/src/tests/trace.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

248 lines
7.0 KiB
Rust
Raw Normal View History

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, *};
use crypto::publickey::KeyPair;
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::*;
use std::{str::FromStr, sync::Arc};
2017-07-25 12:04:37 +02:00
use test_helpers::{self, get_temp_state_db};
use trace::{trace::Action::Reward, LocalizedTrace, RewardType};
use types::{
header::Header,
transaction::{Action, Transaction, TypedTransaction},
2020-08-05 06:08:03 +02:00
view,
2017-07-27 19:15:25 +02:00
views::BlockView,
};
use verification::queue::kind::blocks::Unverified;
2017-07-25 12:04:37 +02:00
#[test]
fn can_trace_block_and_uncle_reward() {
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
// 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,
db,
New Transaction Queue implementation (#8074) * Implementation of Verifier, Scoring and Ready. * Queue in progress. * TransactionPool. * Prepare for txpool release. * Miner refactor [WiP] * WiP reworking miner. * Make it compile. * Add some docs. * Split blockchain access to a separate file. * Work on miner API. * Fix ethcore tests. * Refactor miner interface for sealing/work packages. * Implement next nonce. * RPC compiles. * Implement couple of missing methdods for RPC. * Add transaction queue listeners. * Compiles! * Clean-up and parallelize. * Get rid of RefCell in header. * Revert "Get rid of RefCell in header." This reverts commit 0f2424c9b7319a786e1565ea2a8a6d801a21b4fb. * Override Sync requirement. * Fix status display. * Unify logging. * Extract some cheap checks. * Measurements and optimizations. * Fix scoring bug, heap size of bug and add cache * Disable tx queueing and parallel verification. * Make ethcore and ethcore-miner compile again. * Make RPC compile again. * Bunch of txpool tests. * Migrate transaction queue tests. * Nonce Cap * Nonce cap cache and tests. * Remove stale future transactions from the queue. * Optimize scoring and write some tests. * Simple penalization. * Clean up and support for different scoring algorithms. * Add CLI parameters for the new queue. * Remove banning queue. * Disable debug build. * Change per_sender limit to be 1% instead of 5% * Avoid cloning when propagating transactions. * Remove old todo. * Post-review fixes. * Fix miner options default. * Implement back ready transactions for light client. * Get rid of from_pending_block * Pass rejection reason. * Add more details to drop. * Rollback heap size of. * Avoid cloning hashes when propagating and include more details on rejection. * Fix tests. * Introduce nonces cache. * Remove uneccessary hashes allocation. * Lower the mem limit. * Re-enable parallel verification. * Add miner log. Don't check the type if not below min_gas_price. * Add more traces, fix disabling miner. * Fix creating pending blocks twice on AuRa authorities. * Fix tests. * re-use pending blocks in AuRa * Use reseal_min_period to prevent too frequent update_sealing. * Fix log to contain hash not sender. * Optimize local transactions. * Fix aura tests. * Update locks comments. * Get rid of unsafe Sync impl. * Review fixes. * Remove excessive matches. * Fix compilation errors. * Use new pool in private transactions. * Fix private-tx test. * Fix secret store tests. * Actually use gas_floor_target * Fix config tests. * Fix pool tests. * Address grumbles.
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
// 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();
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();
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
let kp = KeyPair::from_secret_slice(keccak("").as_bytes()).unwrap();
let author = kp.address();
2020-08-05 06:08:03 +02:00
// Add root block first
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,
None,
2017-08-10 12:36:29 +02:00
)
.unwrap();
rolling_timestamp += 10;
root_block.set_timestamp(rolling_timestamp);
2020-08-05 06:08:03 +02:00
let root_block = root_block
.close_and_lock()
.unwrap()
.seal(engine, vec![])
.unwrap();
2020-08-05 06:08:03 +02:00
if let Err(e) = client.import_block(Unverified::from_rlp(root_block.rlp_bytes()).unwrap()) {
panic!(
"error importing block which is valid by definition: {:?}",
e
);
}
2020-08-05 06:08:03 +02:00
last_header = view!(BlockView, &root_block.rlp_bytes()).header();
let root_header = last_header.clone();
db = root_block.drain().state.drop().1;
2020-08-05 06:08:03 +02:00
last_hashes.push(last_header.hash());
2020-08-05 06:08:03 +02:00
// Add parent block
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,
None,
2017-08-10 12:36:29 +02:00
)
.unwrap();
rolling_timestamp += 10;
parent_block.set_timestamp(rolling_timestamp);
2020-08-05 06:08:03 +02:00
let parent_block = parent_block
.close_and_lock()
.unwrap()
.seal(engine, vec![])
.unwrap();
2020-08-05 06:08:03 +02:00
if let Err(e) = client.import_block(Unverified::from_rlp(parent_block.rlp_bytes()).unwrap()) {
panic!(
"error importing block which is valid by definition: {:?}",
e
);
}
2020-08-05 06:08:03 +02:00
last_header = view!(BlockView, &parent_block.rlp_bytes()).header();
db = parent_block.drain().state.drop().1;
2020-08-05 06:08:03 +02:00
last_hashes.push(last_header.hash());
2020-08-05 06:08:03 +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,
Arc::new(last_hashes.clone()),
author.clone(),
2017-08-31 11:53:26 +02:00
(3141562.into(), 31415620.into()),
vec![],
Fork choice and metadata framework for Engine (#8401) * Add light client TODO item * Move existing total-difficulty-based fork choice check to Engine * Abstract total difficulty and block provider as Machine::BlockMetadata and Machine::BlockProvider * Decouple "generate_metadata" logic to Engine * Use fixed BlockMetadata and BlockProvider type for null and instantseal In this way they can use total difficulty fork choice check * Extend blockdetails with metadatas and finalized info * Extra data update: mark_finalized and update_metadatas * Check finalized block in Blockchain * Fix a test constructor in verification mod * Add total difficulty trait * Fix type import * Db migration to V13 with metadata column * Address grumbles * metadatas -> metadata * Use generic type for update_metadata to avoid passing HashMap all around * Remove metadata in blockdetails * [WIP] Implement a generic metadata architecture * [WIP] Metadata insertion logic in BlockChain * typo: Value -> Self::Value * [WIP] Temporarily remove Engine::is_new_best interface So that we don't have too many type errors. * [WIP] Fix more type errors * [WIP] ExtendedHeader::PartialEq * [WIP] Change metadata type Option<Vec<u8>> to Vec<u8> * [WIP] Remove Metadata Error * [WIP] Clean up error conversion * [WIP] finalized -> is_finalized * [WIP] Mark all fields in ExtrasInsert as pub * [WIP] Remove unused import * [WIP] Keep only local metadata info * Mark metadata as optional * [WIP] Revert metadata db change in BlockChain * [WIP] Put finalization in unclosed state * Use metadata interface in BlockDetail * [WIP] Fix current build failures * [WIP] Remove unused blockmetadata struct * Remove DB migration info * [WIP] Typo * Use ExtendedHeader to implement fork choice check * Implement is_new_best using Ancestry iterator * Use expect instead of panic * [WIP] Add ancestry Engine support via on_new_block * Fix tests * Emission of ancestry actions * use_short_version should take account of metadata * Engine::is_new_best -> Engine::fork_choice * Use proper expect format as defined in #1026 * panic -> expect * ancestry_header -> ancestry_with_metadata * Boxed iterator -> &mut iterator * Fix tests * is_new_best -> primitive_fork_choice * Document how fork_choice works * Engine::fork_choice -> Engine::primitive_fork_choice * comment: clarify types of finalization where Engine::primitive_fork_choice works * Expose FinalizationInfo to Engine * Fix tests due to merging * Remove TotalDifficulty trait * Do not pass FinalizationInfo to Engine If there's finalized blocks in from route, choose the old branch without calling `Engine::fork_choice`. * Fix compile * Fix unused import * Remove is_to_route_finalized When no block reorg passes a finalized block, this variable is always false. * Address format grumbles * Fix docs: mark_finalized returns None if block hash is not found `blockchain` mod does not yet have an Error type, so we still temporarily use None here. * Fix inaccurate tree_route None expect description
2018-05-16 08:58:01 +02:00
false,
None,
)
.unwrap();
rolling_timestamp += 10;
block.set_timestamp(rolling_timestamp);
2020-08-05 06:08:03 +02:00
let mut n = 0;
for _ in 0..1 {
block
.push_transaction(
TypedTransaction::Legacy(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,
2020-08-05 06:08:03 +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();
let uncle_author = Address::from_str("ef2d6d194084c2de36e0dabfce45d046b37d1106").unwrap();
2017-07-31 12:23:47 +02:00
uncle.set_author(uncle_author);
uncle.set_parent_hash(root_header.hash());
uncle.set_gas_limit(genesis_gas);
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
let block = block
.close_and_lock()
.unwrap()
.seal(engine, vec![])
.unwrap();
2020-08-05 06:08:03 +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
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
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);
assert!(traces.is_some(), "Filtered 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);
2020-08-05 06:08:03 +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
}