Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Fork choice and metadata framework for Engine #8401

Merged
merged 73 commits into from
May 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
b07ad5f
Add light client TODO item
sorpaas Apr 13, 2018
3c891b9
Move existing total-difficulty-based fork choice check to Engine
sorpaas Apr 13, 2018
d272376
Abstract total difficulty and block provider as Machine::BlockMetadat…
sorpaas Apr 14, 2018
c4d349c
Decouple "generate_metadata" logic to Engine
sorpaas Apr 14, 2018
e1c0252
Merge branch 'master' into fork-choice-no-metadata
sorpaas Apr 14, 2018
1d8149a
Use fixed BlockMetadata and BlockProvider type for null and instantseal
sorpaas Apr 14, 2018
d3ef877
Extend blockdetails with metadatas and finalized info
sorpaas Apr 16, 2018
f325298
Extra data update: mark_finalized and update_metadatas
sorpaas Apr 16, 2018
6756cef
Check finalized block in Blockchain
sorpaas Apr 16, 2018
7c958b4
Fix a test constructor in verification mod
sorpaas Apr 16, 2018
e491abf
Add total difficulty trait
sorpaas Apr 16, 2018
5459695
Fix type import
sorpaas Apr 16, 2018
7fe0d79
Db migration to V13 with metadata column
sorpaas Apr 16, 2018
ce105c6
Address grumbles
sorpaas Apr 16, 2018
45b8ce7
metadatas -> metadata
sorpaas Apr 16, 2018
f86d4cf
Use generic type for update_metadata to avoid passing HashMap all around
sorpaas Apr 16, 2018
24b6316
Merge branch 'finalize' into fork-choice-no-metadata
sorpaas Apr 17, 2018
e797462
Merge branch 'master' of https://github.com/paritytech/parity into fo…
sorpaas Apr 17, 2018
c6dd811
Remove metadata in blockdetails
sorpaas Apr 17, 2018
fb1cc21
[WIP] Implement a generic metadata architecture
sorpaas Apr 17, 2018
5cb6813
[WIP] Metadata insertion logic in BlockChain
sorpaas Apr 17, 2018
f972609
typo: Value -> Self::Value
sorpaas Apr 17, 2018
493e6f0
[WIP] Temporarily remove Engine::is_new_best interface
sorpaas Apr 17, 2018
cb5bfcf
[WIP] Fix more type errors
sorpaas Apr 17, 2018
fc809c7
[WIP] ExtendedHeader::PartialEq
sorpaas Apr 17, 2018
d517ba3
[WIP] Change metadata type Option<Vec<u8>> to Vec<u8>
sorpaas Apr 17, 2018
ac37c31
[WIP] Remove Metadata Error
sorpaas Apr 17, 2018
0f36e50
[WIP] Clean up error conversion
sorpaas Apr 17, 2018
ef3aa63
[WIP] finalized -> is_finalized
sorpaas Apr 17, 2018
3957c49
[WIP] Mark all fields in ExtrasInsert as pub
sorpaas Apr 17, 2018
bc9cc87
[WIP] Remove unused import
sorpaas Apr 17, 2018
4983b68
[WIP] Keep only local metadata info
sorpaas Apr 17, 2018
6009dfa
Mark metadata as optional
sorpaas Apr 17, 2018
13b1b5b
[WIP] Revert metadata db change in BlockChain
sorpaas Apr 17, 2018
723e8a9
[WIP] Put finalization in unclosed state
sorpaas Apr 17, 2018
cf34f0d
Use metadata interface in BlockDetail
sorpaas Apr 17, 2018
5861acb
[WIP] Fix current build failures
sorpaas Apr 17, 2018
6d68222
[WIP] Remove unused blockmetadata struct
sorpaas Apr 17, 2018
a492830
Remove DB migration info
sorpaas Apr 17, 2018
8a06c4e
[WIP] Typo
sorpaas Apr 17, 2018
e673a98
Use ExtendedHeader to implement fork choice check
sorpaas Apr 17, 2018
700779e
Implement is_new_best using Ancestry iterator
sorpaas Apr 17, 2018
143277f
Use expect instead of panic
sorpaas Apr 17, 2018
1dd2ea1
[WIP] Add ancestry Engine support via on_new_block
sorpaas Apr 18, 2018
5d4bd67
Fix tests
sorpaas Apr 18, 2018
704e697
Emission of ancestry actions
sorpaas Apr 18, 2018
fdadd42
use_short_version should take account of metadata
sorpaas Apr 18, 2018
60a7a17
Engine::is_new_best -> Engine::fork_choice
sorpaas Apr 18, 2018
78569a0
Use proper expect format as defined in #1026
sorpaas Apr 18, 2018
8d6629b
panic -> expect
sorpaas Apr 18, 2018
fda5e75
ancestry_header -> ancestry_with_metadata
sorpaas Apr 18, 2018
45f144d
Boxed iterator -> &mut iterator
sorpaas Apr 18, 2018
86d1ef2
Fix tests
sorpaas Apr 18, 2018
7281726
is_new_best -> primitive_fork_choice
sorpaas Apr 18, 2018
d86a056
Document how fork_choice works
sorpaas Apr 18, 2018
e7a83f4
Engine::fork_choice -> Engine::primitive_fork_choice
sorpaas Apr 18, 2018
6b6b042
comment: clarify types of finalization where Engine::primitive_fork_c…
sorpaas Apr 19, 2018
d36e41f
Merge branch 'master' of https://github.com/paritytech/parity into fo…
sorpaas Apr 19, 2018
c593e17
Expose FinalizationInfo to Engine
sorpaas Apr 21, 2018
4900e86
Merge branch 'master' of https://github.com/paritytech/parity into fo…
sorpaas Apr 21, 2018
0b8e55d
Fix tests due to merging
sorpaas Apr 21, 2018
4512b09
Merge branch 'master' into fork-choice-no-metadata
sorpaas Apr 27, 2018
69cc856
Remove TotalDifficulty trait
sorpaas May 1, 2018
4df05f0
Merge branch 'master' of https://github.com/paritytech/parity into fo…
sorpaas May 8, 2018
60b6def
Do not pass FinalizationInfo to Engine
sorpaas May 8, 2018
fca4ca1
Merge branch 'master' of https://github.com/paritytech/parity into fo…
sorpaas May 9, 2018
15a967b
Fix compile
sorpaas May 9, 2018
8d1d98b
Fix unused import
sorpaas May 9, 2018
63a3f54
Remove is_to_route_finalized
sorpaas May 11, 2018
7b0cd66
Address format grumbles
sorpaas May 11, 2018
2c17071
Fix docs: mark_finalized returns None if block hash is not found
sorpaas May 11, 2018
819b3d8
Fix inaccurate tree_route None expect description
sorpaas May 11, 2018
58756e4
Merge branch 'master' of https://github.com/paritytech/parity into fo…
sorpaas May 14, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions ethcore/light/src/client/header_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ impl HeaderChain {
let raw = header.encoded().into_inner();
transaction.put_vec(self.col, &hash[..], raw);

// TODO: For engines when required, use cryptoeconomic guarantees.
let (best_num, is_new_best) = {
let cur_best = self.best_block.read();
if cur_best.total_difficulty < total_difficulty {
Expand Down
51 changes: 44 additions & 7 deletions ethcore/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use vm::{EnvInfo, LastHashes};
use engines::EthEngine;
use error::{Error, BlockError};
use factory::Factories;
use header::Header;
use header::{Header, ExtendedHeader};
use receipt::{Receipt, TransactionOutcome};
use state::State;
use state_db::StateDB;
Expand Down Expand Up @@ -94,6 +94,8 @@ pub struct ExecutedBlock {
state: State<StateDB>,
traces: Tracing,
last_hashes: Arc<LastHashes>,
is_finalized: bool,
metadata: Option<Vec<u8>>,
}

impl ExecutedBlock {
Expand All @@ -112,6 +114,8 @@ impl ExecutedBlock {
Tracing::Disabled
},
last_hashes: last_hashes,
is_finalized: false,
metadata: None,
}
}

Expand Down Expand Up @@ -206,6 +210,26 @@ impl ::parity_machine::Transactions for ExecutedBlock {
}
}

impl ::parity_machine::Finalizable for ExecutedBlock {
fn is_finalized(&self) -> bool {
self.is_finalized
}

fn mark_finalized(&mut self) {
self.is_finalized = true;
}
}

impl ::parity_machine::WithMetadata for ExecutedBlock {
fn metadata(&self) -> Option<&[u8]> {
self.metadata.as_ref().map(|v| v.as_ref())
}

fn set_metadata(&mut self, value: Option<Vec<u8>>) {
self.metadata = value;
}
}

/// Block that is ready for transactions to be added.
///
/// It's a bit like a Vec<Transaction>, except that whenever a transaction is pushed, we execute it and
Expand All @@ -224,6 +248,8 @@ pub struct ClosedBlock {
block: ExecutedBlock,
uncle_bytes: Bytes,
unclosed_state: State<StateDB>,
unclosed_finalization_state: bool,
unclosed_metadata: Option<Vec<u8>>,
}

/// Just like `ClosedBlock` except that we can't reopen it and it's faster.
Expand All @@ -245,7 +271,7 @@ pub struct SealedBlock {

impl<'x> OpenBlock<'x> {
/// Create a new `OpenBlock` ready for transaction pushing.
pub fn new(
pub fn new<'a>(
engine: &'x EthEngine,
factories: Factories,
tracing: bool,
Expand All @@ -256,6 +282,7 @@ impl<'x> OpenBlock<'x> {
gas_range_target: (U256, U256),
extra_data: Bytes,
is_epoch_begin: bool,
ancestry: &mut Iterator<Item=ExtendedHeader>,
) -> Result<Self, Error> {
let number = parent.number() + 1;
let state = State::from_existing(db, parent.state_root().clone(), engine.account_start_nonce(number), factories)?;
Expand All @@ -277,7 +304,7 @@ impl<'x> OpenBlock<'x> {
engine.populate_from_parent(&mut r.block.header, parent);

engine.machine().on_new_block(&mut r.block)?;
engine.on_new_block(&mut r.block, is_epoch_begin)?;
engine.on_new_block(&mut r.block, is_epoch_begin, ancestry)?;

Ok(r)
}
Expand Down Expand Up @@ -387,6 +414,8 @@ impl<'x> OpenBlock<'x> {
let mut s = self;

let unclosed_state = s.block.state.clone();
let unclosed_metadata = s.block.metadata.clone();
let unclosed_finalization_state = s.block.is_finalized;

if let Err(e) = s.engine.on_close_block(&mut s.block) {
warn!("Encountered error on closing the block: {}", e);
Expand All @@ -410,6 +439,8 @@ impl<'x> OpenBlock<'x> {
block: s.block,
uncle_bytes,
unclosed_state,
unclosed_metadata,
unclosed_finalization_state,
}
}

Expand Down Expand Up @@ -483,6 +514,8 @@ impl ClosedBlock {
// revert rewards (i.e. set state back at last transaction's state).
let mut block = self.block;
block.state = self.unclosed_state;
block.metadata = self.unclosed_metadata;
block.is_finalized = self.unclosed_finalization_state;
OpenBlock {
block: block,
engine: engine,
Expand Down Expand Up @@ -588,6 +621,7 @@ fn enact(
last_hashes: Arc<LastHashes>,
factories: Factories,
is_epoch_begin: bool,
ancestry: &mut Iterator<Item=ExtendedHeader>,
) -> Result<LockedBlock, Error> {
{
if ::log::max_log_level() >= ::log::LogLevel::Trace {
Expand All @@ -608,6 +642,7 @@ fn enact(
(3141562.into(), 31415620.into()),
vec![],
is_epoch_begin,
ancestry,
)?;

b.populate_from(&header);
Expand All @@ -630,6 +665,7 @@ pub fn enact_verified(
last_hashes: Arc<LastHashes>,
factories: Factories,
is_epoch_begin: bool,
ancestry: &mut Iterator<Item=ExtendedHeader>,
) -> Result<LockedBlock, Error> {
let view = view!(BlockView, &block.bytes);

Expand All @@ -644,6 +680,7 @@ pub fn enact_verified(
last_hashes,
factories,
is_epoch_begin,
ancestry,
)
}

Expand Down Expand Up @@ -701,6 +738,7 @@ mod tests {
(3141562.into(), 31415620.into()),
vec![],
false,
&mut Vec::new().into_iter(),
)?;

b.populate_from(&header);
Expand Down Expand Up @@ -734,7 +772,7 @@ mod tests {
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 b = OpenBlock::new(&*spec.engine, Default::default(), false, db, &genesis_header, last_hashes, Address::zero(), (3141562.into(), 31415620.into()), vec![], false).unwrap();
let b = OpenBlock::new(&*spec.engine, Default::default(), false, db, &genesis_header, last_hashes, Address::zero(), (3141562.into(), 31415620.into()), vec![], false, &mut Vec::new().into_iter()).unwrap();
let b = b.close_and_lock();
let _ = b.seal(&*spec.engine, vec![]);
}
Expand All @@ -748,7 +786,7 @@ mod tests {

let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
let last_hashes = Arc::new(vec![genesis_header.hash()]);
let b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes.clone(), Address::zero(), (3141562.into(), 31415620.into()), vec![], false).unwrap()
let b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes.clone(), Address::zero(), (3141562.into(), 31415620.into()), vec![], false, &mut Vec::new().into_iter()).unwrap()
.close_and_lock().seal(engine, vec![]).unwrap();
let orig_bytes = b.rlp_bytes();
let orig_db = b.drain();
Expand All @@ -772,7 +810,7 @@ mod tests {

let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
let last_hashes = Arc::new(vec![genesis_header.hash()]);
let mut open_block = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes.clone(), Address::zero(), (3141562.into(), 31415620.into()), vec![], false).unwrap();
let mut open_block = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes.clone(), Address::zero(), (3141562.into(), 31415620.into()), vec![], false, &mut Vec::new().into_iter()).unwrap();
let mut uncle1_header = Header::new();
uncle1_header.set_extra_data(b"uncle1".to_vec());
let mut uncle2_header = Header::new();
Expand All @@ -797,4 +835,3 @@ mod tests {
assert!(orig_db.journal_db().keys().iter().filter(|k| orig_db.journal_db().get(k.0) != db.journal_db().get(k.0)).next() == None);
}
}