Skip to content

Commit

Permalink
Fix ethereum block author (#286)
Browse files Browse the repository at this point in the history
* Fix ethereum block author

* Move some structs to the common folder

* Fix CI test

* Clean
  • Loading branch information
boundless-forest authored Feb 17, 2023
1 parent 62c77fd commit 19f643c
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 172 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.

12 changes: 11 additions & 1 deletion runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ codec = { package = "parity-scale-codec", workspace = true }
smallvec = { version = "1.10" }

# darwinia
dc-primitives = { workspace = true }
darwinia-precompile-assets = { workspace = true }
dc-primitives = { workspace = true }

# darwinia-messages-substrate
bp-darwinia-core = { workspace = true }

# frontier
pallet-evm = { workspace = true }

# moonbeam
account = { workspace = true }

Expand All @@ -32,6 +36,7 @@ pallet-assets = { workspace = true }
pallet-balances = { workspace = true }
pallet-collective = { workspace = true }
pallet-treasury = { workspace = true }
sp-core = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }
Expand All @@ -43,11 +48,15 @@ std = [
"codec/std",

# darwinia
"darwinia-precompile-assets/std",
"dc-primitives/std",

# darwinia-messages-substrate
"bp-darwinia-core/std",

# frontier
"pallet-evm/std",

# moonbeam
"account/std",

Expand All @@ -63,6 +72,7 @@ std = [
"pallet-balances/std",
"pallet-collective/std",
"pallet-treasury/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
Expand Down
27 changes: 27 additions & 0 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,33 @@ macro_rules! impl_self_contained_call {
};
}

pub struct DarwiniaFindAuthor<Inner>(sp_std::marker::PhantomData<Inner>);
impl<Inner> frame_support::traits::FindAuthor<sp_core::H160> for DarwiniaFindAuthor<Inner>
where
Inner: frame_support::traits::FindAuthor<AccountId>,
{
fn find_author<'a, I>(digests: I) -> Option<sp_core::H160>
where
I: 'a + IntoIterator<Item = (frame_support::ConsensusEngineId, &'a [u8])>,
{
Inner::find_author(digests).map(Into::into)
}
}

pub struct FixedGasPrice;
impl pallet_evm::FeeCalculator for FixedGasPrice {
fn min_gas_price() -> (sp_core::U256, frame_support::weights::Weight) {
(sp_core::U256::from(GWEI), frame_support::weights::Weight::zero())
}
}

pub struct AssetIdConverter;
impl darwinia_precompile_assets::AccountToAssetId<AccountId, AssetId> for AssetIdConverter {
fn account_to_asset_id(account_id: AccountId) -> AssetId {
let addr: sp_core::H160 = account_id.into();
addr.to_low_u64_be()
}
}
/// Helper for pallet-assets benchmarking.
#[cfg(feature = "runtime-benchmarks")]
pub struct AssetsBenchmarkHelper;
Expand Down
42 changes: 1 addition & 41 deletions runtime/crab/src/pallets/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,6 @@ frame_support::parameter_types! {
pub WeightPerGas: frame_support::weights::Weight = frame_support::weights::Weight::from_ref_time(WEIGHT_PER_GAS);
}

pub struct FindAuthorTruncated<F>(sp_std::marker::PhantomData<F>);
impl<F: frame_support::traits::FindAuthor<u32>> frame_support::traits::FindAuthor<sp_core::H160>
for FindAuthorTruncated<F>
{
fn find_author<'a, I>(digests: I) -> Option<sp_core::H160>
where
I: 'a + IntoIterator<Item = (frame_support::ConsensusEngineId, &'a [u8])>,
{
// substrate
use sp_core::crypto::ByteArray;

F::find_author(digests).and_then(|i| {
Aura::authorities().get(i as usize).and_then(|id| {
let raw = id.to_raw_vec();

if raw.len() >= 24 {
Some(sp_core::H160::from_slice(&raw[4..24]))
} else {
None
}
})
})
}
}

pub struct FixedGasPrice;
impl pallet_evm::FeeCalculator for FixedGasPrice {
fn min_gas_price() -> (sp_core::U256, frame_support::weights::Weight) {
(sp_core::U256::from(GWEI), frame_support::weights::Weight::zero())
}
}

// TODO: Integrate to the upstream repo
pub struct FromH160;
impl<T> pallet_evm::AddressMapping<T> for FromH160
Expand All @@ -72,14 +40,6 @@ where
}
}

pub struct AssetIdConverter;
impl darwinia_precompile_assets::AccountToAssetId<AccountId, AssetId> for AssetIdConverter {
fn account_to_asset_id(account_id: AccountId) -> AssetId {
let addr: sp_core::H160 = account_id.into();
addr.to_low_u64_be()
}
}

pub struct CrabPrecompiles<R>(sp_std::marker::PhantomData<R>);
impl<R> CrabPrecompiles<R>
where
Expand Down Expand Up @@ -178,7 +138,7 @@ impl pallet_evm::Config for Runtime {
type ChainId = ConstU64<44>;
type Currency = Balances;
type FeeCalculator = FixedGasPrice;
type FindAuthor = FindAuthorTruncated<Aura>;
type FindAuthor = DarwiniaFindAuthor<pallet_session::FindAccountFromAuthorIndex<Self, Aura>>;
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type OnChargeTransaction = ();
type OnCreate = ();
Expand Down
41 changes: 1 addition & 40 deletions runtime/darwinia/src/pallets/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,6 @@ frame_support::parameter_types! {
pub WeightPerGas: frame_support::weights::Weight = frame_support::weights::Weight::from_ref_time(WEIGHT_PER_GAS);
}

pub struct FindAuthorTruncated<F>(sp_std::marker::PhantomData<F>);
impl<F: frame_support::traits::FindAuthor<u32>> frame_support::traits::FindAuthor<sp_core::H160>
for FindAuthorTruncated<F>
{
fn find_author<'a, I>(digests: I) -> Option<sp_core::H160>
where
I: 'a + IntoIterator<Item = (frame_support::ConsensusEngineId, &'a [u8])>,
{
// substrate
use sp_core::crypto::ByteArray;

F::find_author(digests).and_then(|i| {
Aura::authorities().get(i as usize).and_then(|id| {
let raw = id.to_raw_vec();

if raw.len() >= 24 {
Some(sp_core::H160::from_slice(&raw[4..24]))
} else {
None
}
})
})
}
}

pub struct FixedGasPrice;
impl pallet_evm::FeeCalculator for FixedGasPrice {
fn min_gas_price() -> (sp_core::U256, frame_support::weights::Weight) {
(sp_core::U256::from(GWEI), frame_support::weights::Weight::zero())
}
}

// TODO: Integrate to the upstream repo
pub struct FromH160;
impl<T> pallet_evm::AddressMapping<T> for FromH160
Expand All @@ -72,13 +40,6 @@ where
}
}

pub struct AssetIdConverter;
impl darwinia_precompile_assets::AccountToAssetId<AccountId, AssetId> for AssetIdConverter {
fn account_to_asset_id(account_id: AccountId) -> AssetId {
let addr: sp_core::H160 = account_id.into();
addr.to_low_u64_be()
}
}

pub struct DarwiniaPrecompiles<R>(sp_std::marker::PhantomData<R>);
impl<R> DarwiniaPrecompiles<R>
Expand Down Expand Up @@ -178,7 +139,7 @@ impl pallet_evm::Config for Runtime {
type ChainId = ConstU64<46>;
type Currency = Balances;
type FeeCalculator = FixedGasPrice;
type FindAuthor = FindAuthorTruncated<Aura>;
type FindAuthor = DarwiniaFindAuthor<pallet_session::FindAccountFromAuthorIndex<Self, Aura>>;
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type OnChargeTransaction = ();
type OnCreate = ();
Expand Down
42 changes: 1 addition & 41 deletions runtime/pangolin/src/pallets/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,6 @@ frame_support::parameter_types! {
pub WeightPerGas: frame_support::weights::Weight = frame_support::weights::Weight::from_ref_time(WEIGHT_PER_GAS);
}

pub struct FindAuthorTruncated<F>(sp_std::marker::PhantomData<F>);
impl<F: frame_support::traits::FindAuthor<u32>> frame_support::traits::FindAuthor<sp_core::H160>
for FindAuthorTruncated<F>
{
fn find_author<'a, I>(digests: I) -> Option<sp_core::H160>
where
I: 'a + IntoIterator<Item = (frame_support::ConsensusEngineId, &'a [u8])>,
{
// substrate
use sp_core::crypto::ByteArray;

F::find_author(digests).and_then(|i| {
Aura::authorities().get(i as usize).and_then(|id| {
let raw = id.to_raw_vec();

if raw.len() >= 24 {
Some(sp_core::H160::from_slice(&raw[4..24]))
} else {
None
}
})
})
}
}

pub struct FixedGasPrice;
impl pallet_evm::FeeCalculator for FixedGasPrice {
fn min_gas_price() -> (sp_core::U256, frame_support::weights::Weight) {
(sp_core::U256::from(GWEI), frame_support::weights::Weight::zero())
}
}

// TODO: Integrate to the upstream repo
pub struct FromH160;
impl<T> pallet_evm::AddressMapping<T> for FromH160
Expand All @@ -72,14 +40,6 @@ where
}
}

pub struct AssetIdConverter;
impl darwinia_precompile_assets::AccountToAssetId<AccountId, AssetId> for AssetIdConverter {
fn account_to_asset_id(account_id: AccountId) -> AssetId {
let addr: sp_core::H160 = account_id.into();
addr.to_low_u64_be()
}
}

pub struct PangolinPrecompiles<R>(sp_std::marker::PhantomData<R>);
impl<R> PangolinPrecompiles<R>
where
Expand Down Expand Up @@ -178,7 +138,7 @@ impl pallet_evm::Config for Runtime {
type ChainId = ConstU64<43>;
type Currency = Balances;
type FeeCalculator = FixedGasPrice;
type FindAuthor = FindAuthorTruncated<Aura>;
type FindAuthor = DarwiniaFindAuthor<pallet_session::FindAccountFromAuthorIndex<Self, Aura>>;
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type OnChargeTransaction = ();
type OnCreate = ();
Expand Down
44 changes: 1 addition & 43 deletions runtime/pangoro/src/pallets/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,6 @@ frame_support::parameter_types! {
pub PrecompilesValue: PangoroPrecompiles<Runtime> = PangoroPrecompiles::<_>::new();
pub WeightPerGas: frame_support::weights::Weight = frame_support::weights::Weight::from_ref_time(WEIGHT_PER_GAS);
}

pub struct FindAuthorTruncated<F>(sp_std::marker::PhantomData<F>);
impl<F: frame_support::traits::FindAuthor<u32>> frame_support::traits::FindAuthor<sp_core::H160>
for FindAuthorTruncated<F>
{
fn find_author<'a, I>(digests: I) -> Option<sp_core::H160>
where
I: 'a + IntoIterator<Item = (frame_support::ConsensusEngineId, &'a [u8])>,
{
// substrate
use sp_core::crypto::ByteArray;

F::find_author(digests).and_then(|i| {
Aura::authorities().get(i as usize).and_then(|id| {
let raw = id.to_raw_vec();

if raw.len() >= 24 {
Some(sp_core::H160::from_slice(&raw[4..24]))
} else {
None
}
})
})
}
}

pub struct FixedGasPrice;
impl pallet_evm::FeeCalculator for FixedGasPrice {
fn min_gas_price() -> (sp_core::U256, frame_support::weights::Weight) {
(sp_core::U256::from(GWEI), frame_support::weights::Weight::zero())
}
}

// TODO: Integrate to the upstream repo
pub struct FromH160;
impl<T> pallet_evm::AddressMapping<T> for FromH160
Expand All @@ -71,15 +38,6 @@ where
address.into()
}
}

pub struct AssetIdConverter;
impl darwinia_precompile_assets::AccountToAssetId<AccountId, AssetId> for AssetIdConverter {
fn account_to_asset_id(account_id: AccountId) -> AssetId {
let addr: sp_core::H160 = account_id.into();
addr.to_low_u64_be()
}
}

pub struct PangoroPrecompiles<R>(sp_std::marker::PhantomData<R>);
impl<R> PangoroPrecompiles<R>
where
Expand Down Expand Up @@ -178,7 +136,7 @@ impl pallet_evm::Config for Runtime {
type ChainId = ConstU64<45>;
type Currency = Balances;
type FeeCalculator = FixedGasPrice;
type FindAuthor = FindAuthorTruncated<Aura>;
type FindAuthor = DarwiniaFindAuthor<pallet_session::FindAccountFromAuthorIndex<Self, Aura>>;
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type OnChargeTransaction = ();
type OnCreate = ();
Expand Down
1 change: 1 addition & 0 deletions tests/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const DEFAULT_GAS = 4000000;
export const EXTRINSIC_GAS_LIMIT = 9059375;

// Accounts builtin
export const ALITH = "0xf24ff3a9cf04c71dbc94d0b566f7a27b94566cac";
export const FAITH = "0xC0F0f4ab324C46e55D02D0033343B4Be8A55532d";
export const FAITH_P = "0xb9d2ea9a615f3165812e8d44de0d24da9bbd164b65c4f0573e1ce2c8dbd9c8df";

Expand Down
9 changes: 3 additions & 6 deletions tests/ethereum/test-constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Web3 from "web3";
import { describe } from "mocha";
import { expect } from "chai";
import { HOST_HTTP_URL, CHAIN_ID } from "../config";
import { HOST_HTTP_URL, CHAIN_ID, ALITH } from "../config";

const web3 = new Web3(HOST_HTTP_URL);
describe("Test constants RPC", () => {
Expand All @@ -13,10 +13,7 @@ describe("Test constants RPC", () => {
expect(await web3.eth.getChainId()).to.equal(CHAIN_ID);
});

// TODO: FIX ME
it.skip("block author should be 0x0000000000000000000000000000000000000000", async () => {
// This address `0x1234567890` is hardcoded into the runtime find_author
// as we are running manual sealing consensus.
expect(await web3.eth.getCoinbase()).to.equal("0x0000000000000000000000000000000000000000");
it("block author should be ALITH", async () => {
expect(await web3.eth.getCoinbase()).to.equal(ALITH);
});
});

0 comments on commit 19f643c

Please sign in to comment.