deth
Version:
Ethereum node focused on Developer Experience
35 lines (34 loc) • 1.6 kB
JavaScript
import VM from 'ethereumts-vm';
import Common from 'ethereumjs-common';
import Account from 'ethereumjs-account';
import { toBuffer } from 'ethereumjs-util';
import { Wallet } from 'ethers';
import BN from 'bn.js';
import { putGenesisBlock } from './putGenesisBlock';
import Blockchain from 'ethereumjs-blockchain';
import { BlockchainAdapter } from './storage/BlockchainAdapter';
import { StateManagerAdapter } from './storage/StateManagerAdapter';
export async function initializeVM(options, stateManager, blockchain) {
const common = Common.forCustomChain('mainnet', {
chainId: options.chainId,
networkId: options.chainId,
name: options.chainName,
}, options.hardfork);
const callbackBlockchain = blockchain
? new BlockchainAdapter(blockchain)
: new Blockchain({ common, validate: false });
const stateManger = stateManager ? new StateManagerAdapter(stateManager) : undefined;
const vm = new VM({ common, stateManager: stateManger, blockchain: callbackBlockchain });
await initAccounts(vm, options);
await putGenesisBlock(vm, options);
return vm;
}
// @TODO extract this. VM should not be aware of any private keys etc. TestChain should provide data for genesis block
async function initAccounts(vm, options) {
const psm = vm.pStateManager;
const balance = new BN(options.accounts.initialBalance.toString()).toBuffer();
for (const privateKey of options.accounts.privateKeys) {
const { address } = new Wallet(privateKey);
await psm.putAccount(toBuffer(address), new Account({ balance }));
}
}