Skip to content

Commit

Permalink
Clean up some pallet-ethereum comments (#88)
Browse files Browse the repository at this point in the history
* Clean up some pallet-ethereum comments

* Fix template compiles
  • Loading branch information
sorpaas authored Jul 27, 2020
1 parent b8d0072 commit fa42345
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 117 deletions.
47 changes: 30 additions & 17 deletions Cargo.lock

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

123 changes: 25 additions & 98 deletions frame/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

use frame_support::{
decl_module, decl_storage, decl_error, decl_event, ensure,
weights::Weight, traits::Get, traits::FindAuthor
traits::Get, traits::FindAuthor
};
use sp_std::prelude::*;
use frame_system::ensure_none;
Expand All @@ -48,30 +48,26 @@ mod mock;
/// A type alias for the balance type from this pallet's point of view.
pub type BalanceOf<T> = <T as pallet_balances::Trait>::Balance;

/// Our pallet's configuration trait. All our types and constants go in here. If the
/// pallet is dependent on specific other pallets, then their configuration traits
/// should be added to our implied traits list.
///
/// `frame_system::Trait` should always be included in our implied traits.
/// Trait for Ethereum pallet.
pub trait Trait: frame_system::Trait<Hash=H256> + pallet_balances::Trait + pallet_timestamp::Trait + pallet_evm::Trait {
/// The overarching event type.
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;
type Event: From<Event> + Into<<Self as frame_system::Trait>::Event>;
/// Find author for Ethereum.
type FindAuthor: FindAuthor<H160>;
}

decl_storage! {
// A macro for the Storage trait, and its implementation, for this pallet.
// This allows for type-safe usage of the Substrate storage database, so you can
// keep things around between blocks.
//
// It is important to update your storage name so that your pallet's
// storage items are isolated from other pallets.
// ---------------------------------vvvvvvv
trait Store for Module<T: Trait> as Example {
BlocksAndReceipts: map hasher(blake2_128_concat) H256 => Option<(ethereum::Block, Vec<ethereum::Receipt>)>;
/// Block hash to block body and block receipt map.
BlocksAndReceipts: map hasher(blake2_128_concat)
H256 => Option<(ethereum::Block, Vec<ethereum::Receipt>)>;
/// Block number to block hash map.
BlockNumbers: map hasher(blake2_128_concat) T::BlockNumber => H256;
/// Current building block's transactions and receipts.
PendingTransactionsAndReceipts: Vec<(ethereum::Transaction, ethereum::Receipt)>;
/// Transaction hash to transaction status map.
TransactionStatuses: map hasher(blake2_128_concat) H256 => Option<TransactionStatus>;
/// Transaction hash to transaction map.
Transactions: map hasher(blake2_128_concat) H256 => Option<(H256, u32)>;
}
add_extra_genesis {
Expand All @@ -82,66 +78,23 @@ decl_storage! {
}

decl_event!(
/// Events are a simple means of reporting specific conditions and
/// circumstances that have happened that users, Dapps and/or chain explorers would find
/// interesting and otherwise difficult to detect.
pub enum Event<T> where B = <T as pallet_balances::Trait>::Balance {
// Just a normal `enum`, here's a dummy event to ensure it compiles.
/// Dummy event, just here so there's a generic type that's used.
Dummy(B),
}
/// Ethereum pallet events.
pub enum Event { }
);

// The module declaration. This states the entry points that we handle. The
// macro takes care of the marshalling of arguments and dispatch.
//
// Anyone can have these functions execute by signing and submitting
// an extrinsic. Ensure that calls into each of these execute in a time, memory and
// using storage space proportional to any costs paid for by the caller or otherwise the
// difficulty of forcing the call to happen.
//
// Generally you'll want to split these into three groups:
// - Public calls that are signed by an external account.
// - Root calls that are allowed to be made only by the governance system.
// - Unsigned calls that can be of two kinds:
// * "Inherent extrinsics" that are opinions generally held by the block
// authors that build child blocks.
// * Unsigned Transactions that are of intrinsic recognizable utility to the
// network, and are validated by the runtime.
//
// Information about where this dispatch initiated from is provided as the first argument
// "origin". As such functions must always look like:
//
// `fn foo(origin, bar: Bar, baz: Baz) -> Result;`
//
// The `Result` is required as part of the syntax (and expands to the conventional dispatch
// result of `Result<(), &'static str>`).
//
// When you come to `impl` them later in the pallet, you must specify the full type for `origin`:
//
// `fn foo(origin: T::Origin, bar: Bar, baz: Baz) { ... }`
//
// There are three entries in the `frame_system::Origin` enum that correspond
// to the above bullets: `::Signed(AccountId)`, `::Root` and `::None`. You should always match
// against them as the first thing you do in your function. There are three convenience calls
// in system that do the matching for you and return a convenient result: `ensure_signed`,
// `ensure_root` and `ensure_none`.


decl_error! {
/// Ethereum pallet errors.
pub enum Error for Module<T: Trait> {
/// Transaction signed with wrong chain id
InvalidChainId,
}
}

decl_module! {
// Simple declaration of the `Module` type. Lets the macro know what its working on.
/// Ethereum pallet module.
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
/// Deposit one of this pallet's events by using the default implementation.
/// It is also possible to provide a custom implementation.
/// For non-generic events, the generic parameter just needs to be dropped, so that it
/// looks like: `fn deposit_event() = default;`.
fn deposit_event() = default;

/// Transact an Ethereum transaction.
Expand All @@ -167,29 +120,9 @@ decl_module! {
Self::execute(source, transaction);
}

// The signature could also look like: `fn on_initialize()`.
// This function could also very well have a weight annotation, similar to any other. The
// only difference is that it mut be returned, not annotated.
fn on_initialize(_n: T::BlockNumber) -> Weight {
// Anything that needs to be done at the start of the block.
// We don't do anything here.

0
}

// The signature could also look like: `fn on_finalize()`
fn on_finalize(n: T::BlockNumber) {
<Module<T>>::store_block(n);
}

// A runtime code run after every block and have access to extended set of APIs.
//
// For instance you can generate extrinsics for the upcoming produced block.
fn offchain_worker(_n: T::BlockNumber) {
// We don't do anything here.
// but we could dispatch extrinsic (transaction/unsigned/inherent) using
// sp_io::submit_extrinsic
}
}
}

Expand All @@ -203,13 +136,7 @@ impl<T: Trait> frame_support::unsigned::ValidateUnsigned for Module<T> {
}
}

// The main implementation block for the pallet. Functions here fall into three broad
// categories:
// - Public interface. These are functions that are `pub` and generally fall into inspector
// functions that do not write to storage and operation functions that do.
// - Private functions. These are your usual private utilities unavailable to other pallets.
impl<T: Trait> Module<T> {

fn store_block(n: T::BlockNumber) {
let transactions_and_receipts = PendingTransactionsAndReceipts::take();
let (transactions, receipts): (Vec<_>, Vec<_>) =
Expand Down Expand Up @@ -269,25 +196,20 @@ impl<T: Trait> Module<T> {
BlockNumbers::<T>::insert(n, hash);
}

/// Get the author using the FindAuthor trait.
pub fn find_author() -> H160 {
let digest = <frame_system::Module<T>>::digest();
let pre_runtime_digests = digest.logs.iter().filter_map(|d| d.as_pre_runtime());
if let Some(authority_id) = T::FindAuthor::find_author(pre_runtime_digests) {
authority_id
} else {
H160::default()
}

T::FindAuthor::find_author(pre_runtime_digests).unwrap_or_default()
}

/// Get the transaction status with given transaction hash.
pub fn transaction_status(hash: H256) -> Option<TransactionStatus> {
TransactionStatuses::get(hash)
}

// Requires returning a Vec<Receipt> to enable cumulative calculations in the rpc-end.
//
// - `cumulative_gas_used`: the sum of `used_gas` for this and all previous transactions
// in the block.
// - `log_index`: each Log's index, block wise.
/// Get the transaction with given transaction hash.
pub fn transaction_by_hash(hash: H256) -> Option<(
ethereum::Transaction,
ethereum::Block,
Expand All @@ -301,6 +223,7 @@ impl<T: Trait> Module<T> {
Some((transaction.clone(), block, transaction_status, receipts))
}

/// Get transaction by block hash and index.
pub fn transaction_by_block_hash_and_index(
hash: H256,
index: u32
Expand All @@ -322,6 +245,7 @@ impl<T: Trait> Module<T> {
}
}

/// Get transaction by block number and index.
pub fn transaction_by_block_number_and_index(
number: T::BlockNumber,
index: u32
Expand All @@ -337,6 +261,7 @@ impl<T: Trait> Module<T> {
None
}

/// Get block by number.
pub fn block_by_number(number: T::BlockNumber) -> Option<ethereum::Block> {
if <BlockNumbers<T>>::contains_key(number) {
let hash = <BlockNumbers<T>>::get(number);
Expand All @@ -347,13 +272,15 @@ impl<T: Trait> Module<T> {
None
}

/// Get block by hash.
pub fn block_by_hash(hash: H256) -> Option<ethereum::Block> {
if let Some((block, _receipt)) = BlocksAndReceipts::get(hash) {
return Some(block)
}
None
}

/// Get block transaction status of the given block.
pub fn block_transaction_statuses(
block: &Block
) -> Vec<Option<TransactionStatus>> {
Expand Down
2 changes: 1 addition & 1 deletion template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ construct_runtime!(
Balances: balances::{Module, Call, Storage, Config<T>, Event<T>},
TransactionPayment: transaction_payment::{Module, Storage},
Sudo: sudo::{Module, Call, Config<T>, Storage, Event<T>},
Ethereum: ethereum::{Module, Call, Storage, Event<T>, Config, ValidateUnsigned},
Ethereum: ethereum::{Module, Call, Storage, Event, Config, ValidateUnsigned},
EVM: evm::{Module, Config, Call, Storage, Event<T>},
}
);
Expand Down
2 changes: 1 addition & 1 deletion ts-tests/frontier-test-node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ construct_runtime!(
Balances: balances::{Module, Call, Storage, Config<T>, Event<T>},
TransactionPayment: transaction_payment::{Module, Storage},
Sudo: sudo::{Module, Call, Config<T>, Storage, Event<T>},
Ethereum: ethereum::{Module, Call, Storage, Event<T>, ValidateUnsigned},
Ethereum: ethereum::{Module, Call, Storage, Event, ValidateUnsigned},
EVM: evm::{Module, Config, Call, Storage, Event<T>},
}
);
Expand Down

0 comments on commit fa42345

Please sign in to comment.