Skip to content

Commit

Permalink
Introduce ConsensusLog::PostBlock and ConsensusLog::PreBlock (parityt…
Browse files Browse the repository at this point in the history
  • Loading branch information
sorpaas authored Feb 23, 2021
1 parent 8f43100 commit 57bca6b
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 41 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 18 additions & 17 deletions client/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,24 +136,25 @@ impl<B, I, C> BlockImport<B> for FrontierBlockImport<B, I, C> where
if self.enabled {
let log = find_frontier_log::<B>(&block.header)?;
let hash = block.post_hash();
match log {
ConsensusLog::EndBlock {
block_hash, transaction_hashes,
} => {
let res = aux_schema::write_block_hash(client.as_ref(), block_hash, hash, insert_closure!());
if res.is_err() { trace!(target: "frontier-consensus", "{:?}", res); }

for (index, transaction_hash) in transaction_hashes.into_iter().enumerate() {
let res = aux_schema::write_transaction_metadata(
client.as_ref(),
transaction_hash,
(block_hash, index as u32),
insert_closure!(),
);
if res.is_err() { trace!(target: "frontier-consensus", "{:?}", res); }
}
},
let post_hashes = match log {
ConsensusLog::PostHashes(post_hashes) => post_hashes,
ConsensusLog::PreBlock(block) => fp_consensus::PostHashes::from_block(block),
ConsensusLog::PostBlock(block) => fp_consensus::PostHashes::from_block(block),
};

let res = aux_schema::write_block_hash(client.as_ref(), post_hashes.block_hash, hash, insert_closure!());
if res.is_err() { trace!(target: "frontier-consensus", "{:?}", res); }

for (index, transaction_hash) in post_hashes.transaction_hashes.into_iter().enumerate() {
let res = aux_schema::write_transaction_metadata(
client.as_ref(),
transaction_hash,
(post_hashes.block_hash, index as u32),
insert_closure!(),
);
if res.is_err() { trace!(target: "frontier-consensus", "{:?}", res); }
}

// On importing block 1 we also map the genesis block in the auxiliary.
if block.header.number().clone() == One::one() {
let id = BlockId::Number(Zero::zero());
Expand Down
14 changes: 1 addition & 13 deletions frame/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,25 +323,13 @@ impl<T: Config> Module<T> {
let mut block = ethereum::Block::new(partial_header, transactions.clone(), ommers);
block.header.state_root = T::StateRoot::get();

let mut transaction_hashes = Vec::new();

for t in &transactions {
let transaction_hash = H256::from_slice(
Keccak256::digest(&rlp::encode(t)).as_slice()
);
transaction_hashes.push(transaction_hash);
}

CurrentBlock::put(block.clone());
CurrentReceipts::put(receipts.clone());
CurrentTransactionStatuses::put(statuses.clone());

let digest = DigestItem::<T::Hash>::Consensus(
FRONTIER_ENGINE_ID,
ConsensusLog::EndBlock {
block_hash: block.header.hash(),
transaction_hashes,
}.encode(),
ConsensusLog::PostHashes(fp_consensus::PostHashes::from_block(block)).encode(),
);
frame_system::Module::<T>::deposit_log(digest.into());
}
Expand Down
6 changes: 6 additions & 0 deletions primitives/consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ sp-std = { version = "2.0.0", default-features = false, git = "https://github.co
sp-runtime = { version = "2.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
sp-core = { version = "2.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
codec = { package = "parity-scale-codec", version = "1.3.1", default-features = false, features = ["derive"] }
ethereum = { version = "0.6.0", default-features = false, features = ["with-codec"] }
rlp = { version = "0.5", default-features = false }
sha3 = { version = "0.8", default-features = false }

[features]
default = ["std"]
Expand All @@ -21,4 +24,7 @@ std = [
"sp-runtime/std",
"sp-core/std",
"codec/std",
"ethereum/std",
"rlp/std",
"sha3/std",
]
37 changes: 31 additions & 6 deletions primitives/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,41 @@ use codec::{Encode, Decode};
use sp_std::vec::Vec;
use sp_core::H256;
use sp_runtime::ConsensusEngineId;
use sha3::{Digest, Keccak256};

pub const FRONTIER_ENGINE_ID: ConsensusEngineId = [b'f', b'r', b'o', b'n'];

#[derive(Decode, Encode, Clone, PartialEq, Eq)]
pub enum ConsensusLog {
#[codec(index = "1")]
EndBlock {
/// Ethereum block hash.
block_hash: H256,
/// Transaction hashes of the Ethereum block.
transaction_hashes: Vec<H256>,
},
PostHashes(PostHashes),
#[codec(index = "2")]
PostBlock(ethereum::Block),
#[codec(index = "3")]
PreBlock(ethereum::Block),
}

#[derive(Decode, Encode, Clone, PartialEq, Eq)]
pub struct PostHashes {
/// Ethereum block hash.
pub block_hash: H256,
/// Transaction hashes of the Ethereum block.
pub transaction_hashes: Vec<H256>,
}

impl PostHashes {
pub fn from_block(block: ethereum::Block) -> Self {
let mut transaction_hashes = Vec::new();

for t in &block.transactions {
let transaction_hash = H256::from_slice(
Keccak256::digest(&rlp::encode(t)).as_slice()
);
transaction_hashes.push(transaction_hash);
}

let block_hash = block.header.hash();

PostHashes { transaction_hashes, block_hash }
}
}
15 changes: 10 additions & 5 deletions template/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ pub fn new_full(
task_manager.spawn_essential_handle().spawn(
"frontier-pending-transactions",
client.import_notification_stream().for_each(move |notification| {

if let Ok(locked) = &mut pending_transactions.clone().unwrap().lock() {
// As pending transactions have a finite lifespan anyway
// we can ignore MultiplePostRuntimeLogs error checks.
Expand All @@ -281,12 +280,18 @@ pub fn new_full(

let imported_number: u64 = notification.header.number as u64;

if let Some(ConsensusLog::EndBlock {
block_hash: _, transaction_hashes,
}) = frontier_log {
let post_hashes = frontier_log.map(|l| {
match l {
ConsensusLog::PostHashes(post_hashes) => post_hashes,
ConsensusLog::PreBlock(block) => fp_consensus::PostHashes::from_block(block),
ConsensusLog::PostBlock(block) => fp_consensus::PostHashes::from_block(block),
}
});

if let Some(post_hashes) = post_hashes {
// Retain all pending transactions that were not
// processed in the current block.
locked.retain(|&k, _| !transaction_hashes.contains(&k));
locked.retain(|&k, _| !post_hashes.transaction_hashes.contains(&k));
}
locked.retain(|_, v| {
// Drop all the transactions that exceeded the given lifespan.
Expand Down

0 comments on commit 57bca6b

Please sign in to comment.