Skip to content
This repository has been archived by the owner on Aug 24, 2023. It is now read-only.

Commit

Permalink
chore(meta): ethereum chain id (#105)
Browse files Browse the repository at this point in the history
<!--  Thanks for sending a pull request! -->

#### What this PR does / why we need it:

#### Which issue(s) does this PR fixes?:

<!--
(Optional) Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->

Fixes #

#### Additional comments?:
  • Loading branch information
canonbrother authored Oct 3, 2022
1 parent 4d2973e commit 8c7da99
Show file tree
Hide file tree
Showing 13 changed files with 187 additions and 21 deletions.
3 changes: 0 additions & 3 deletions .vscode/settings.json

This file was deleted.

12 changes: 12 additions & 0 deletions Cargo.lock

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

24 changes: 24 additions & 0 deletions meta/meta-ethereum-chain-id/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "meta-ethereum-chain-id"
version = "0.1.0"
edition = "2021"

[dependencies]
serde = { version = "1.0.141", optional = true, features = ["derive"] }

# Substrate
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.25" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.25" }
parity-scale-codec = { default-features = false, version = "3.1.5", features = ["derive"] }
scale-info = { default-features = false, version = "2.1.2", features = ["derive"] }

[features]
default = ["std"]
std = [
"frame-support/std",
"frame-system/std",
"parity-scale-codec/std",
"scale-info/std",
"serde",
]
try-runtime = ["frame-support/try-runtime"]
42 changes: 42 additions & 0 deletions meta/meta-ethereum-chain-id/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::pallet;

pub use pallet::*;

#[pallet]
pub mod pallet {

use frame_support::pallet_prelude::*;

/// The Ethereum Chain Id Pallet
#[pallet::pallet]
pub struct Pallet<T>(PhantomData<T>);

/// Configuration trait of this pallet.
#[pallet::config]
pub trait Config: frame_system::Config {}

impl<T: Config> Get<u64> for Pallet<T> {
fn get() -> u64 {
Self::chain_id()
}
}

#[pallet::storage]
#[pallet::getter(fn chain_id)]
pub type ChainId<T> = StorageValue<_, u64, ValueQuery>;

#[pallet::genesis_config]
#[derive(Default)]
pub struct GenesisConfig {
pub chain_id: u64,
}

#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig {
fn build(&self) {
ChainId::<T>::put(self.chain_id);
}
}
}
59 changes: 59 additions & 0 deletions meta/meta-node/specs/meta.json

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions meta/meta-node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ pub fn development_config() -> Result<ChainSpec, String> {
move || {
testnet_genesis(
wasm_binary,
// TODO(): Manual-seal: Initial PoA authorities
// vec![authority_keys_from_seed("Alice")],
// Sudo account
get_account_id_from_seed::<sr25519::Public>("Alice"),
// Pre-funded accounts
Expand All @@ -51,6 +49,7 @@ pub fn development_config() -> Result<ChainSpec, String> {
get_account_id_from_seed::<sr25519::Public>("Bob//stash"),
],
true,
1988,
)
},
// Bootnodes
Expand All @@ -63,7 +62,7 @@ pub fn development_config() -> Result<ChainSpec, String> {
// Properties
Some(
serde_json::from_str(
"{\"tokenDecimals\": 8, \"tokenSymbol\": \"DFI\", \"SS58Prefix\": 988}",
"{\"tokenDecimals\": 8, \"tokenSymbol\": \"DFI\", \"SS58Prefix\": 1988}",
)
.expect("Provided valid json map"),
),
Expand All @@ -84,7 +83,6 @@ pub fn local_testnet_config() -> Result<ChainSpec, String> {
move || {
testnet_genesis(
wasm_binary,
// TODO(): Manual-seal: Initial PoA authorities
// Sudo account
get_account_id_from_seed::<sr25519::Public>("Alice"),
// Pre-funded accounts
Expand All @@ -103,6 +101,7 @@ pub fn local_testnet_config() -> Result<ChainSpec, String> {
get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),
],
true,
1988,
)
},
// Bootnodes
Expand All @@ -115,7 +114,7 @@ pub fn local_testnet_config() -> Result<ChainSpec, String> {
// Properties
Some(
serde_json::from_str(
"{\"tokenDecimals\": 8, \"tokenSymbol\": \"DFI\", \"SS58Prefix\": 988}",
"{\"tokenDecimals\": 8, \"tokenSymbol\": \"DFI\", \"SS58Prefix\": 1988}",
)
.expect("Provided valid json map"),
),
Expand All @@ -127,12 +126,14 @@ pub fn local_testnet_config() -> Result<ChainSpec, String> {
/// Configure initial storage state for FRAME modules.
pub fn testnet_genesis(
wasm_binary: &[u8],
// TODO(): Manual-seal: Initial PoA authorities
root_key: AccountId,
endowed_accounts: Vec<AccountId>,
_enable_println: bool,
chain_id: u64,
) -> GenesisConfig {
use meta_runtime::{BalancesConfig, EVMConfig, SudoConfig, SystemConfig};
use meta_runtime::{
BalancesConfig, EVMConfig, EthereumChainIdConfig, SudoConfig, SystemConfig,
};
GenesisConfig {
system: SystemConfig {
// Add Wasm runtime to storage.
Expand All @@ -151,6 +152,7 @@ pub fn testnet_genesis(
// Assign network admin rights.
key: Some(root_key),
},
ethereum_chain_id: EthereumChainIdConfig { chain_id },
evm: EVMConfig {
accounts: {
let mut map = BTreeMap::new();
Expand Down
3 changes: 3 additions & 0 deletions meta/meta-node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ impl SubstrateCli for Cli {

fn load_spec(&self, id: &str) -> Result<Box<dyn sc_service::ChainSpec>, String> {
Ok(match id {
"meta" => Box::new(chain_spec::ChainSpec::from_json_bytes(
&include_bytes!("../specs/meta.json")[..],
)?),
"dev" => Box::new(chain_spec::development_config()?),
"" | "local" => Box::new(chain_spec::local_testnet_config()?),
path => Box::new(chain_spec::ChainSpec::from_json_file(
Expand Down
2 changes: 2 additions & 0 deletions meta/meta-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pallet-base-fee = { default-features = false, git = "https://github.com/p
pallet-dynamic-fee = { default-features = false, git = "https://github.com/paritytech/frontier.git", branch = "polkadot-v0.9.25" }
pallet-ethereum = { default-features = false, git = "https://github.com/paritytech/frontier.git", branch = "polkadot-v0.9.25" }
pallet-evm = { default-features = false, git = "https://github.com/paritytech/frontier.git", branch = "polkadot-v0.9.25" }
# Local
meta-ethereum-chain-id = { default-features = false, path = "../meta-ethereum-chain-id", package = "meta-ethereum-chain-id"}

[build-dependencies]
substrate-wasm-builder = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.25" }
Expand Down
9 changes: 5 additions & 4 deletions meta/meta-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use sp_version::RuntimeVersion;
use fp_rpc::TransactionStatus;
use frame_support::{
construct_runtime, parameter_types,
traits::{ConstU16, ConstU32, ConstU8},
traits::{ConstU16, ConstU32, ConstU8, Get},
weights::{
constants::{RocksDbWeight, WEIGHT_PER_SECOND},
ConstantMultiplier, IdentityFee, Weight,
Expand Down Expand Up @@ -238,8 +238,6 @@ impl GasWeightMapping for FixedGasWeightMapping {
}

parameter_types! {
// after looking the https://chainlist.org/, `988` is a good number since no other public network are using it
pub const ChainId: u64 = 988;
pub BlockGasLimit: U256 = U256::from(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT / WEIGHT_PER_GAS);
}

Expand All @@ -255,7 +253,7 @@ impl pallet_evm::Config for Runtime {
type Runner = pallet_evm::runner::stack::Runner<Self>;
type PrecompilesType = ();
type PrecompilesValue = ();
type ChainId = ChainId;
type ChainId = EthereumChainId;
type BlockGasLimit = BlockGasLimit;
type OnChargeTransaction = ();
type FindAuthor = ();
Expand All @@ -266,6 +264,8 @@ impl pallet_ethereum::Config for Runtime {
type StateRoot = pallet_ethereum::IntermediateStateRoot<Self>;
}

impl meta_ethereum_chain_id::Config for Runtime {}

frame_support::parameter_types! {
pub BoundDivision: U256 = U256::from(1024);
}
Expand Down Expand Up @@ -323,6 +323,7 @@ construct_runtime!(
TransactionPayment: pallet_transaction_payment,
Sudo: pallet_sudo,
Ethereum: pallet_ethereum,
EthereumChainId: meta_ethereum_chain_id,
EVM: pallet_evm,
DynamicFee: pallet_dynamic_fee,
BaseFee: pallet_base_fee,
Expand Down
7 changes: 5 additions & 2 deletions packages/network/src/NetworkConfig.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
export interface NetworkConfig {
chainId: 988;
chainId: 988 | 1988;
ports: {
p2p: number;
rpc: number;
ws: number;
};
spec: string;
}

export const MainNet: NetworkConfig = {
Expand All @@ -14,13 +15,15 @@ export const MainNet: NetworkConfig = {
rpc: 9333,
ws: 9944,
},
spec: 'meta',
};

export const TestNet: NetworkConfig = {
chainId: 988,
chainId: 1988,
ports: {
p2p: 39333,
rpc: 19933,
ws: 19944,
},
spec: 'dev',
};
4 changes: 3 additions & 1 deletion packages/network/src/NetworkConfig.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ it('MainNet config should not drift', () => {
rpc: 9333,
ws: 9944,
},
spec: 'meta',
});
});

it('TestNet config should not drift', () => {
expect(TestNet).toStrictEqual({
chainId: 988,
chainId: 1988,
ports: {
p2p: 39333,
rpc: 19933,
ws: 19944,
},
spec: 'dev',
});
});
5 changes: 3 additions & 2 deletions packages/testcontainers/src/MetaChainContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { GenericContainer, Network, StartedTestContainer } from 'testcontainers'
import { AbstractStartedContainer } from 'testcontainers/dist/modules/abstract-started-container';

export class MetaChainContainer extends GenericContainer {
constructor(image: string = MetaChainContainer.image, protected readonly config: NetworkConfig = TestNet) {
constructor(protected readonly config: NetworkConfig = TestNet, image: string = MetaChainContainer.image) {
super(image);
}

static get image(): string {
if (process?.env?.METACHAIN_DOCKER_IMAGE !== undefined) {
return process.env.METACHAIN_DOCKER_IMAGE;
}
return 'ghcr.io/defich/metachain:af2e7d03b061352491d550c8923d1dfac4f65095';
return 'ghcr.io/defich/metachain:cc77218f794ac2c05e76007ca2c8b4e890686903';
}

protected getCmd(): string[] {
Expand All @@ -21,6 +21,7 @@ export class MetaChainContainer extends GenericContainer {
'--no-telemetry', // disable connecting to substrate telemetry server
'--no-prometheus', // do not expose a Prometheus exporter endpoint
'--no-grandpa',
`--chain=${this.config.spec}`,
`--port=${this.config.ports.p2p}`,
`--rpc-port=${this.config.ports.rpc}`,
`--ws-port=${this.config.ports.ws}`,
Expand Down
22 changes: 20 additions & 2 deletions packages/testcontainers/src/MetaChainContainer.unit.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { TestNet } from '@defimetachain/network';
import { MainNet, TestNet } from '@defimetachain/network';
import { ethers } from 'ethers';

import { MetaChainContainer, StartedMetaChainContainer } from '.';

describe('ethers.providers.JsonRpcProvider', () => {
describe('Testnet ethers.providers.JsonRpcProvider', () => {
let container: StartedMetaChainContainer;
let rpc: ethers.providers.JsonRpcProvider;

Expand Down Expand Up @@ -33,6 +33,24 @@ describe('ethers.providers.JsonRpcProvider', () => {
});
});

describe('MainNet ethers.providers.JsonRpcProvider', () => {
let container: StartedMetaChainContainer;
let rpc: ethers.providers.JsonRpcProvider;

beforeAll(async () => {
container = await new MetaChainContainer(MainNet).start();
rpc = container.getEthersHttpProvider();
});

afterAll(async () => {
await container.stop();
});

it('should have chainId', async () => {
expect(Number(await rpc.send('net_version', []))).toStrictEqual(MainNet.chainId);
});
});

describe('utility method', () => {
let container: StartedMetaChainContainer;
let rpc: ethers.providers.JsonRpcProvider;
Expand Down

0 comments on commit 8c7da99

Please sign in to comment.