Skip to content

Commit

Permalink
Init block reward from pallet (ParaState#38)
Browse files Browse the repository at this point in the history
Implement block reward
  • Loading branch information
yanganto authored Jun 24, 2021
1 parent e90136f commit 20e64b1
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 0 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.

4 changes: 4 additions & 0 deletions frame/vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ frame-support = { version = "3.0.0", default-features = false, git = "https://gi
frame-system = { version = "3.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
pallet-timestamp = { version = "3.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
pallet-balances = { version = "3.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
pallet-authorship = { version = "3.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
pallet-session = { version = "3.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
sp-core = { version = "3.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
sp-runtime = { version = "3.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
sp-std = { version = "3.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
Expand Down Expand Up @@ -46,6 +48,8 @@ std = [
"frame-support/std",
"frame-system/std",
"pallet-balances/std",
"pallet-authorship/std",
"pallet-session/std",
"sp-io/std",
"sp-std/std",
"fp-vm/std",
Expand Down
26 changes: 26 additions & 0 deletions frame/vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ pub mod pallet {
pub enum Event<T: Config> {
/// Setup the etherem address for block reward
EthAddrSet((T::AccountId, H160)),
/// Ethereum Reward to miner fail
EthRewardFailed(H160),
/// Ethereum events from contracts.
Log(Log),
/// A contract has been created at given \[address\].
Expand Down Expand Up @@ -369,6 +371,8 @@ pub mod pallet {
/// EVM is forbidden for the call from pallet,
/// and only allow from traditional Ethereum client
Forbidden,
/// Reward miner failed
RewardFailed,
}

#[pallet::genesis_config]
Expand Down Expand Up @@ -761,3 +765,25 @@ impl<T> OnChargeEVMTransaction<T> for ()
EVMCurrencyAdapter::<<T as Config>::Currency, ()>::correct_and_deposit_fee(who, corrected_fee, already_withdrawn)
}
}

impl<T> pallet_authorship::EventHandler<T::AccountId, T::BlockNumber> for Module<T>
where
T: Config + pallet_authorship::Config + pallet_session::Config,
{
fn note_author(author: T::AccountId) {
if let Some(eth_addr) = <EthAddrOf<T>>::get(author) {
Self::reward(eth_addr);
}
}
fn note_uncle(_author: T::AccountId, _age: T::BlockNumber) {}
}

impl<T: Config> Module<T> {
pub(crate) fn reward(
eth_address: H160,
) {
if T::Runner::mint(eth_address, U256::from(1000000000000000000u128), T::config()).is_err() {
Pallet::<T>::deposit_event(Event::<T>::EthRewardFailed(eth_address));
}
}
}
6 changes: 6 additions & 0 deletions frame/vm/src/runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,10 @@ pub trait Runner<T: Config> {
nonce: Option<U256>,
config: &evm::Config,
) -> Result<CreateInfo, Self::Error>;

fn mint(
miner: H160,
value: U256,
config: &evm::Config,
) -> Result<(), Self::Error>;
}
15 changes: 15 additions & 0 deletions frame/vm/src/runner/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,21 @@ impl<T: Config> RunnerT<T> for Runner<T> {
},
)
}
fn mint(
miner: H160,
value: U256,
config: &evm::Config,
) -> Result<(), Self::Error> {
if_std! {
let account_id = T::AddressMapping::into_account_id(miner);
T::Currency::issue(value.low_u128().unique_saturated_into());
T::Currency::deposit_creating(&account_id, value.low_u128().unique_saturated_into());
log::debug!(target: "ssvm", "reward {:?} to {:?} [{:?}]", value, miner, account_id);
return Ok(())
}
log::warn!(target: "ssvm", "SSVM only works with native code, you will not get the reward");
Ok(())
}
}

pub fn create_address(caller: H160, nonce: U256) -> H160 {
Expand Down
1 change: 1 addition & 0 deletions template/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pallet-vm-precompile-simple = { default-features = false, path = "../../frame/vm
pallet-vm-precompile-sha3fips = { default-features = false, path = "../../frame/vm/precompile/sha3fips" }
pallet-vm-precompile-modexp = { default-features = false, path = "../../frame/vm/precompile/modexp" }
pallet-aura = { version = "3.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
pallet-authorship = { version = "3.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
pallet-balances = { version = "3.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
pallet-grandpa = { version = "3.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
pallet-randomness-collective-flip = { version = "3.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
Expand Down
13 changes: 13 additions & 0 deletions template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,18 @@ impl pallet_session::Config for Runtime {
type WeightInfo = ();
}

parameter_types! {
pub const UncleGenerations: BlockNumber = 5;
}

impl pallet_authorship::Config for Runtime {
type FindAuthor = pallet_session::FindAccountFromAuthorIndex<Self, Aura>;
type UncleGenerations = UncleGenerations;
type FilterUncle = ();
type EventHandler = EVM;
}


impl pallet_aura::Config for Runtime {
type AuthorityId = AuraId;
}
Expand Down Expand Up @@ -356,6 +368,7 @@ construct_runtime!(
System: frame_system::{Module, Call, Config, Storage, Event<T>},
RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Module, Call, Storage},
Timestamp: pallet_timestamp::{Module, Call, Storage, Inherent},
Authorship: pallet_authorship::{Module, Call, Storage, Inherent},
Balances: pallet_balances::{Module, Call, Storage, Config<T>, Event<T>},
Session: pallet_session::{Module, Call, Storage, Event, Config<T>},
ValidatorSet: pallet_validator_set::{Module, Call, Storage, Event<T>, Config<T>},
Expand Down

0 comments on commit 20e64b1

Please sign in to comment.