Skip to content

Commit

Permalink
Merge pull request #94 from ethereumjs/genesis-contracts
Browse files Browse the repository at this point in the history
Fixed geth params parser to handle contracts
  • Loading branch information
vpulim authored Feb 21, 2019
2 parents dfd3797 + fcfcc4d commit 22e5bd9
Show file tree
Hide file tree
Showing 4 changed files with 873 additions and 4 deletions.
35 changes: 32 additions & 3 deletions lib/util/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
const Account = require('ethereumjs-account')
const Block = require('ethereumjs-block')
const Trie = require('merkle-patricia-tree/secure')
const BN = require('ethereumjs-util').BN
const util = require('ethereumjs-util')
const url = require('url')
const path = require('path')

function toBuffer (string) {
return Buffer.from(util.stripHexPrefix(string), 'hex')
}

function parseBootnodes (string) {
if (!string) {
return
Expand Down Expand Up @@ -39,13 +43,38 @@ function parseTransports (transports) {
})
}

async function parseStorage (storage) {
const trie = new Trie()
const promises = []
for (let [address, value] of Object.entries(storage)) {
address = toBuffer(address)
value = util.rlp.encode(util.unpad(toBuffer(value)))
promises.push(new Promise((resolve, reject) => {
trie.put(address, value, (err) => {
if (err) return reject(err)
resolve()
})
}))
}
await Promise.all(promises)
return trie
}

async function parseGethState (alloc) {
const trie = new Trie()
const promises = []
for (let [key, value] of Object.entries(alloc)) {
const address = Buffer.from(key, 'hex')
const address = toBuffer(key)
const account = new Account()
account.balance = new BN(value.balance.slice(2), 16)
if (value.balance) {
account.balance = new util.BN(value.balance.slice(2), 16)
}
if (value.code) {
account.codeHash = util.keccak(value.code)
}
if (value.storage) {
account.stateRoot = (await parseStorage(value.storage)).root
}
promises.push(new Promise((resolve, reject) => {
trie.put(address, account.serialize(), (err) => {
if (err) return reject(err)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"ethereumjs-blockchain": "^3.4.0",
"ethereumjs-common": "^1.1.0",
"ethereumjs-devp2p": "^2.5.1",
"ethereumjs-util": "^6.0.0",
"ethereumjs-util": "^6.1.0",
"fs-extra": "^6.0.1",
"jayson": "^2.0.6",
"level": "^4.0.0",
Expand Down
Loading

0 comments on commit 22e5bd9

Please sign in to comment.