deth
Version:
Ethereum node focused on Developer Experience
41 lines (40 loc) • 2.22 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ethereumts_vm_1 = __importDefault(require("ethereumts-vm"));
const ethereumjs_common_1 = __importDefault(require("ethereumjs-common"));
const ethereumjs_account_1 = __importDefault(require("ethereumjs-account"));
const ethereumjs_util_1 = require("ethereumjs-util");
const ethers_1 = require("ethers");
const bn_js_1 = __importDefault(require("bn.js"));
const putGenesisBlock_1 = require("./putGenesisBlock");
const ethereumjs_blockchain_1 = __importDefault(require("ethereumjs-blockchain"));
const BlockchainAdapter_1 = require("./storage/BlockchainAdapter");
const StateManagerAdapter_1 = require("./storage/StateManagerAdapter");
async function initializeVM(options, stateManager, blockchain) {
const common = ethereumjs_common_1.default.forCustomChain('mainnet', {
chainId: options.chainId,
networkId: options.chainId,
name: options.chainName,
}, options.hardfork);
const callbackBlockchain = blockchain
? new BlockchainAdapter_1.BlockchainAdapter(blockchain)
: new ethereumjs_blockchain_1.default({ common, validate: false });
const stateManger = stateManager ? new StateManagerAdapter_1.StateManagerAdapter(stateManager) : undefined;
const vm = new ethereumts_vm_1.default({ common, stateManager: stateManger, blockchain: callbackBlockchain });
await initAccounts(vm, options);
await putGenesisBlock_1.putGenesisBlock(vm, options);
return vm;
}
exports.initializeVM = initializeVM;
// @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_js_1.default(options.accounts.initialBalance.toString()).toBuffer();
for (const privateKey of options.accounts.privateKeys) {
const { address } = new ethers_1.Wallet(privateKey);
await psm.putAccount(ethereumjs_util_1.toBuffer(address), new ethereumjs_account_1.default({ balance }));
}
}