Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing balance in generateCanonicalGenesis #589

Merged
merged 3 commits into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/state/stateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,11 @@ export default class StateManager {
addresses,
(address: string, done: any) => {
const account = new Account()
account.balance = new BN(initState[address]).toArrayLike(Buffer)
if (initState[address].slice(0, 2) === '0x') {
account.balance = new BN(initState[address].slice(2), 16).toArrayLike(Buffer)
} else {
account.balance = new BN(initState[address]).toArrayLike(Buffer)
}
const addressBuffer = utils.toBuffer(address)
this._trie.put(addressBuffer, account.serialize(), done)
},
Expand Down
19 changes: 19 additions & 0 deletions tests/api/state/stateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,25 @@ tape('StateManager', (t) => {
st.end()
})

t.test('should generate correct genesis state root for all chains', async (st) => {
const chains = ['mainnet', 'ropsten', 'rinkeby', 'kovan', 'goerli']
for (const chain of chains) {
const common = new Common(chain, 'petersburg')
const expectedStateRoot = Buffer.from(common.genesis().stateRoot.slice(2), 'hex')
const stateManager = new StateManager({ common: common })

const generateCanonicalGenesis = promisify((...args) => stateManager.generateCanonicalGenesis(...args))
const getStateRoot = promisify((...args) => stateManager.getStateRoot(...args))

await generateCanonicalGenesis()
let stateRoot = await getStateRoot()

st.true(stateRoot.equals(expectedStateRoot), `generateCanonicalGenesis should produce correct state root for ${chain}`)
}

st.end()
})

t.test('should dump storage', async st => {
const stateManager = new StateManager()
const addressBuffer = Buffer.from('a94f5374fce5edbc8e2a8697c15331677e6ebf0b', 'hex')
Expand Down