Skip to content

Commit

Permalink
Add support for PreLog in pallet-ethereum (paritytech#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorpaas authored Mar 8, 2021
1 parent b76c2f6 commit d990d20
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 29 deletions.
4 changes: 2 additions & 2 deletions client/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use fp_rpc::EthereumRuntimeRPCApi;
use sc_client_api::{BlockOf, backend::AuxStore};
use sp_blockchain::{HeaderBackend, ProvideCache, well_known_cache_keys::Id as CacheKeyId};
use sp_block_builder::BlockBuilder as BlockBuilderApi;
use sp_runtime::traits::Block as BlockT;
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
use sp_api::ProvideRuntimeApi;
use sp_consensus::{
BlockImportParams, Error as ConsensusError, BlockImport,
Expand Down Expand Up @@ -129,7 +129,7 @@ impl<B, I, C> BlockImport<B> for FrontierBlockImport<B, I, C> where
// We validate that there are only one frontier log. No other
// actions are needed and mapping syncing is delegated to a separate
// worker.
ensure_log::<B>(&block.header).map_err(|e| Error::from(e))?;
ensure_log(&block.header.digest()).map_err(|e| Error::from(e))?;

self.inner.import_block(block, new_cache).map_err(Into::into)
}
Expand Down
2 changes: 1 addition & 1 deletion client/mapping-sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn sync_block<Block: BlockT>(
backend: &fc_db::Backend<Block>,
header: &Block::Header,
) -> Result<(), String> {
let log = fp_consensus::find_log::<Block>(&header).map_err(|e| format!("{:?}", e))?;
let log = fp_consensus::find_log(header.digest()).map_err(|e| format!("{:?}", e))?;
let post_hashes = log.into_hashes();

let mapping_commitment = fc_db::MappingCommitment {
Expand Down
41 changes: 31 additions & 10 deletions frame/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ use frame_support::{
dispatch::DispatchResultWithPostInfo,
};
use sp_std::prelude::*;
use frame_system::ensure_none;
use frame_system::{ensure_none, RawOrigin};
use frame_support::{ensure, traits::UnfilteredDispatchable};
use ethereum_types::{H160, H64, H256, U256, Bloom, BloomInput};
use sp_runtime::{
transaction_validity::{
Expand All @@ -42,7 +43,7 @@ use fp_evm::CallOrCreateInfo;
use pallet_evm::{Runner, GasWeightMapping, FeeCalculator};
use sha3::{Digest, Keccak256};
use codec::{Encode, Decode};
use fp_consensus::{FRONTIER_ENGINE_ID, PostLog};
use fp_consensus::{FRONTIER_ENGINE_ID, PostLog, PreLog};

pub use fp_rpc::TransactionStatus;
pub use ethereum::{Transaction, Log, Block, Receipt, TransactionAction, TransactionMessage};
Expand Down Expand Up @@ -97,7 +98,7 @@ decl_storage! {
}
add_extra_genesis {
build(|_config: &GenesisConfig| {
<Module<T>>::store_block();
<Module<T>>::store_block(false);
});
}
}
Expand All @@ -116,6 +117,8 @@ decl_error! {
pub enum Error for Module<T: Config> {
/// Signature is invalid.
InvalidSignature,
/// Pre-log is present, therefore transact is not allowed.
PreLogExists,
}
}

Expand All @@ -130,6 +133,11 @@ decl_module! {
fn transact(origin, transaction: ethereum::Transaction) -> DispatchResultWithPostInfo {
ensure_none(origin)?;

ensure!(
fp_consensus::find_pre_log(&frame_system::Module::<T>::digest()).is_err(),
Error::<T>::PreLogExists,
);

let source = Self::recover_signer(&transaction)
.ok_or_else(|| Error::<T>::InvalidSignature)?;

Expand Down Expand Up @@ -207,11 +215,22 @@ decl_module! {
}

fn on_finalize(n: T::BlockNumber) {
<Module<T>>::store_block();
<Module<T>>::store_block(
fp_consensus::find_pre_log(&frame_system::Module::<T>::digest()).is_err(),
);
}

fn on_initialize(n: T::BlockNumber) -> Weight {
Pending::kill();

if let Ok(log) = fp_consensus::find_pre_log(&frame_system::Module::<T>::digest()) {
let PreLog::Block(block) = log;

for transaction in block.transactions {
let _ = Call::<T>::transact(transaction).dispatch_bypass_filter(RawOrigin::None.into());
}
}

0
}
}
Expand Down Expand Up @@ -284,7 +303,7 @@ impl<T: Config> Module<T> {
Some(H160::from(H256::from_slice(Keccak256::digest(&pubkey).as_slice())))
}

fn store_block() {
fn store_block(post_log: bool) {
let mut transactions = Vec::new();
let mut statuses = Vec::new();
let mut receipts = Vec::new();
Expand Down Expand Up @@ -331,11 +350,13 @@ impl<T: Config> Module<T> {
CurrentReceipts::put(receipts.clone());
CurrentTransactionStatuses::put(statuses.clone());

let digest = DigestItem::<T::Hash>::Consensus(
FRONTIER_ENGINE_ID,
PostLog::Hashes(fp_consensus::Hashes::from_block(block)).encode(),
);
frame_system::Module::<T>::deposit_log(digest.into());
if post_log {
let digest = DigestItem::<T::Hash>::Consensus(
FRONTIER_ENGINE_ID,
PostLog::Hashes(fp_consensus::Hashes::from_block(block)).encode(),
);
frame_system::Module::<T>::deposit_log(digest.into());
}
}

fn logs_bloom(logs: Vec<Log>, bloom: &mut Bloom) {
Expand Down
29 changes: 14 additions & 15 deletions primitives/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ use codec::{Encode, Decode};
use sp_std::vec::Vec;
use sp_core::H256;
use sp_runtime::{
ConsensusEngineId, generic::OpaqueDigestItemId,
traits::{Block as BlockT, Header as HeaderT},
ConsensusEngineId, generic::{Digest, OpaqueDigestItemId},
};
use sha3::{Digest, Keccak256};
use sha3::{Digest as Sha3Digest, Keccak256};

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

Expand Down Expand Up @@ -89,12 +88,12 @@ pub enum FindLogError {
MultipleLogs,
}

pub fn find_pre_log<Block: BlockT>(
header: &Block::Header,
pub fn find_pre_log<Hash>(
digest: &Digest<Hash>,
) -> Result<PreLog, FindLogError> {
let mut found = None;

for log in header.digest().logs() {
for log in digest.logs() {
let log = log.try_to::<PreLog>(OpaqueDigestItemId::PreRuntime(&FRONTIER_ENGINE_ID));
match (log, found.is_some()) {
(Some(_), true) => return Err(FindLogError::MultipleLogs),
Expand All @@ -106,12 +105,12 @@ pub fn find_pre_log<Block: BlockT>(
found.ok_or(FindLogError::NotFound)
}

pub fn find_post_log<Block: BlockT>(
header: &Block::Header,
pub fn find_post_log<Hash>(
digest: &Digest<Hash>,
) -> Result<PostLog, FindLogError> {
let mut found = None;

for log in header.digest().logs() {
for log in digest.logs() {
let log = log.try_to::<PostLog>(OpaqueDigestItemId::Consensus(&FRONTIER_ENGINE_ID));
match (log, found.is_some()) {
(Some(_), true) => return Err(FindLogError::MultipleLogs),
Expand All @@ -123,12 +122,12 @@ pub fn find_post_log<Block: BlockT>(
found.ok_or(FindLogError::NotFound)
}

pub fn find_log<Block: BlockT>(
header: &Block::Header,
pub fn find_log<Hash>(
digest: &Digest<Hash>,
) -> Result<Log, FindLogError> {
let mut found = None;

for log in header.digest().logs() {
for log in digest.logs() {
let pre_log = log.try_to::<PreLog>(OpaqueDigestItemId::PreRuntime(&FRONTIER_ENGINE_ID));
match (pre_log, found.is_some()) {
(Some(_), true) => return Err(FindLogError::MultipleLogs),
Expand All @@ -147,12 +146,12 @@ pub fn find_log<Block: BlockT>(
found.ok_or(FindLogError::NotFound)
}

pub fn ensure_log<Block: BlockT>(
header: &Block::Header,
pub fn ensure_log<Hash>(
digest: &Digest<Hash>,
) -> Result<(), FindLogError> {
let mut found = false;

for log in header.digest().logs() {
for log in digest.logs() {
let pre_log = log.try_to::<PreLog>(OpaqueDigestItemId::PreRuntime(&FRONTIER_ENGINE_ID));
match (pre_log, found) {
(Some(_), true) => return Err(FindLogError::MultipleLogs),
Expand Down
2 changes: 1 addition & 1 deletion template/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ pub fn new_full(
if let Ok(locked) = &mut pending_transactions.clone().unwrap().lock() {
// As pending transactions have a finite lifespan anyway
// we can ignore MultiplePostRuntimeLogs error checks.
let log = fp_consensus::find_log::<Block>(&notification.header).ok();
let log = fp_consensus::find_log(&notification.header.digest).ok();
let post_hashes = log.map(|log| log.into_hashes());

if let Some(post_hashes) = post_hashes {
Expand Down

0 comments on commit d990d20

Please sign in to comment.