Skip to content

Commit 138dec4

Browse files
evm: allow setting pre-defined accounts in genesis (#6086)
* evm: allow setting pre-defined accounts in genesis * Only build GenesisAccount in std
1 parent 21a1da5 commit 138dec4

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/lib.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ mod backend;
2525
pub use crate::backend::{Account, Log, Vicinity, Backend};
2626

2727
use sp_std::{vec::Vec, marker::PhantomData};
28+
#[cfg(feature = "std")]
29+
use codec::{Encode, Decode};
30+
#[cfg(feature = "std")]
31+
use serde::{Serialize, Deserialize};
2832
use frame_support::{ensure, decl_module, decl_storage, decl_event, decl_error};
2933
use frame_support::weights::{Weight, DispatchClass, FunctionOf, Pays};
3034
use frame_support::traits::{Currency, WithdrawReason, ExistenceRequirement, Get};
@@ -137,12 +141,43 @@ pub trait Trait: frame_system::Trait + pallet_timestamp::Trait {
137141
}
138142
}
139143

144+
#[cfg(feature = "std")]
145+
#[derive(Clone, Eq, PartialEq, Encode, Decode, Debug, Serialize, Deserialize)]
146+
/// Account definition used for genesis block construction.
147+
pub struct GenesisAccount {
148+
/// Account nonce.
149+
pub nonce: U256,
150+
/// Account balance.
151+
pub balance: U256,
152+
/// Full account storage.
153+
pub storage: std::collections::BTreeMap<H256, H256>,
154+
/// Account code.
155+
pub code: Vec<u8>,
156+
}
157+
140158
decl_storage! {
141159
trait Store for Module<T: Trait> as EVM {
142-
Accounts get(fn accounts) config(): map hasher(blake2_128_concat) H160 => Account;
160+
Accounts get(fn accounts): map hasher(blake2_128_concat) H160 => Account;
143161
AccountCodes: map hasher(blake2_128_concat) H160 => Vec<u8>;
144162
AccountStorages: double_map hasher(blake2_128_concat) H160, hasher(blake2_128_concat) H256 => H256;
145163
}
164+
165+
add_extra_genesis {
166+
config(accounts): std::collections::BTreeMap<H160, GenesisAccount>;
167+
build(|config: &GenesisConfig| {
168+
for (address, account) in &config.accounts {
169+
Accounts::insert(address, Account {
170+
balance: account.balance,
171+
nonce: account.nonce,
172+
});
173+
AccountCodes::insert(address, &account.code);
174+
175+
for (index, value) in &account.storage {
176+
AccountStorages::insert(address, index, value);
177+
}
178+
}
179+
});
180+
}
146181
}
147182

148183
decl_event! {

0 commit comments

Comments
 (0)