Skip to content

Commit

Permalink
fix: add --skip-copy to avoid copy state db
Browse files Browse the repository at this point in the history
  • Loading branch information
shenghu committed May 24, 2019
1 parent 45cd0c3 commit 34069a5
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*.tsbuildinfo
/.coverage
/.db
/.ethstatedb*
/.nyc_output
/.test-data
/bin
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@ COMMANDS
help display help for ethstatedb
```
<!-- commandsstop -->

# More information
1. When playing with data generated by geth, make sure to stop geth so that it flushes all data to database. This command by default copies the data to a temp location, i.e. `.ethstatedb`, to avoid messing up the original database. However, you can use `--skip-copy` to use original database.
2. To get stateRoot, use `geth attach ipc://path/to/geth.ipc` to connect to a node. Then use `eth.getBlock(eth.blockNumber).stateRoot` to get stateRoot for the latest block. You can change `eth.blockNumber` to be any one.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,11 @@
"lines": 90,
"statements": 90,
"functions": 90,
"branches": 50,
"branches": 55,
"reporter": [
"lcov",
"text",
"text-summary",
"cobertura"
"text-summary"
],
"report-dir": ".coverage",
"exclude": [
Expand Down
20 changes: 19 additions & 1 deletion src/commands/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Command, flags} from '@oclif/command'
import cli from 'cli-ux'
import EAccount from 'ethereumjs-account'
import {BN, KECCAK256_NULL_S, KECCAK256_RLP_S} from 'ethereumjs-util'
import * as fs from 'fs-extra'

export default class Account extends Command {
static description = 'load information of an account'
Expand All @@ -23,6 +24,15 @@ export default class Account extends Command {
required: true,
description: 'value of stateRoot',
}),
'skip-copy': flags.boolean({
required: false,
description:
'skip copying database to .ethstatedb. By default, database is copied to avoid messing the original database.',
}),
'work-dir': flags.string({
required: false,
description: 'directory to pull down database',
}),
}

static args = [
Expand All @@ -39,7 +49,15 @@ export default class Account extends Command {
const level = require('level')

const {args, flags} = this.parse(Account)
const db = level(flags.dbdir)

let workDir = flags.dbdir
if (!flags['skip-copy']) {
workDir = flags['work-dir'] || '.ethstatedb'
await fs.emptyDir(workDir)
await fs.copy(flags.dbdir, workDir)
}

const db = level(workDir)
const trie = new Trie(db, flags.root)

const found = await new Promise((resolve, reject) => {
Expand Down
66 changes: 49 additions & 17 deletions test/commands/account.test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,65 @@
import {expect, test} from '@oclif/test'
import * as fs from 'fs-extra'
import * as os from 'os'

import {BlockChainConfig, runBlockchain, SupportedHardforkType} from '../utils/run-blockchain'
import {runBlockchain, SupportedHardforkType} from '../utils/run-blockchain'

const testData = require('../fixtures/test-blockchain-data.json')

const testOnEthereum = test.register('runEVM', (config: BlockChainConfig) => {
return {
async run(ctx: any) {
ctx.evm = await runBlockchain(config)
}
}
})

describe('account', () => {
testOnEthereum
.runEVM({dbPath: '.db/chaindata',
before(async () => {
const evm = await runBlockchain({
dbPath: '.db/chaindata',
cachePath: '.db/cachedata',
validate: true,
data: testData,
hardfork: SupportedHardforkType.byzantium,
reset: true})
reset: true
})

await new Promise((resolve, reject) => {
evm.blockchain.db.close((err: Error, result: any) => {
if (err) reject(err)

resolve(result)
})
})
})

test
.stdout()
.do(() => {
fs.copySync('.db/chaindata', '.db/chaindata-1')
})
.command(['account',
'--dbdir', '.db/chaindata-1',
'--root', '0xecc60e00b3fe5ce9f6e1a10e5469764daf51f1fe93c22ec3f9a7583a80357217',
'--work-dir', '.ethstatedb-1',
'0x095e7baea6a6c7c4c2dfeb977efac326af552d87'])
.it('runs account', async ctx => {
const result: any = {}
ctx.stdout.split(os.EOL).map((element: string) => {
const tokens = element.trim().split(/\s+/)
if (tokens.length > 1) {
result[tokens[0]] = tokens[1]
}
})

expect(result.Balance).to.equal('10')
})

test
.stdout()
.do(ctx => {
ctx.evm.blockchain.db.close()
.do(() => {
fs.copySync('.db/chaindata', '.db/chaindata-2')
fs.removeSync('.ethstatedb')
})
.command(['account',
'--dbdir', '.db/chaindata',
'--root', '0xecc60e00b3fe5ce9f6e1a10e5469764daf51f1fe93c22ec3f9a7583a80357217', '0x095e7baea6a6c7c4c2dfeb977efac326af552d87'])
.it('runs account', ctx => {
'--dbdir', '.db/chaindata-2',
'--root', '0xecc60e00b3fe5ce9f6e1a10e5469764daf51f1fe93c22ec3f9a7583a80357217',
'--skip-copy',
'0x095e7baea6a6c7c4c2dfeb977efac326af552d87'])
.it('runs account --skip-copy', async ctx => {
const result: any = {}
ctx.stdout.split(os.EOL).map((element: string) => {
const tokens = element.trim().split(/\s+/)
Expand All @@ -38,5 +69,6 @@ describe('account', () => {
})

expect(result.Balance).to.equal('10')
expect(await fs.pathExists('.ethstatedb')).to.be.false
})
})
2 changes: 1 addition & 1 deletion test/utils/run-blockchain.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Account from 'ethereumjs-account'
import * as ethereumUtil from 'ethereumjs-util'
import * as fs from 'fs-extra'
import leveldown from 'leveldown'
import levelup from 'levelup'
import * as util from 'util'

const fs = require('fs-extra')
const ethjsUtil = require('ethjs-util')
const Trie = require('merkle-patricia-tree/secure')
const Block = require('ethereumjs-block')
Expand Down

0 comments on commit 34069a5

Please sign in to comment.