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

Generate receipt roots in runBlock #1037

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion packages/vm/lib/runBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export default async function runBlock(this: VM, opts: RunBlockOpts): Promise<Ru
const bloom = result.bloom.bitvector
block = Block.fromBlockData({
...block,
header: { ...block.header, stateRoot, bloom },
header: { ...block.header, stateRoot, bloom, receiptTrie: result.receiptRoot },
})
} else {
if (result.receiptRoot && !result.receiptRoot.equals(block.header.receiptTrie)) {
Expand Down
45 changes: 43 additions & 2 deletions packages/vm/tests/api/runBlock.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import tape from 'tape'
import { Address, BN, rlp } from 'ethereumjs-util'
import { Address, BN, rlp, KECCAK256_RLP } from 'ethereumjs-util'
import Common from '@ethereumjs/common'
import { Block } from '@ethereumjs/block'
import { Transaction } from '@ethereumjs/tx'
import { DefaultStateManager } from '../../lib/state'
import runBlock from '../../lib/runBlock'
import runBlock, { RunBlockOpts } from '../../lib/runBlock'
import { setupPreConditions, getDAOCommon } from '../util'
import { setupVM, createAccount } from './utils'
import VM from '../../lib/index'
import { AfterBlockEvent } from '../../lib/runBlock'

const testData = require('./testdata.json')

Expand Down Expand Up @@ -264,6 +265,46 @@ tape(
t.end()
}
)

async function runBlockAndGetAfterBlockEvent(
vm: VM,
runBlockOpts: RunBlockOpts
): Promise<AfterBlockEvent> {
let results: AfterBlockEvent
function handler(event: AfterBlockEvent) {
results = event
}

try {
vm.once('afterBlock', handler)
await vm.runBlock(runBlockOpts)
} finally {
// We need this in case `runBlock` throws before emitting the event.
// Otherwise we'd be leaking the listener until the next call to runBlock.
vm.removeListener('afterBlock', handler)
}

return results!
}

tape('should generate a receipt root', async (t) => {
const vm = new VM()

// We create a block with a receiptTrie filled with 0s and no txs.
// Once we run it we should get a receipt trie root of for the empty receipts set,
// which is a well known constant.
const block = Block.fromBlockData({ header: { receiptTrie: Buffer.alloc(32, 0) } })

const results = await runBlockAndGetAfterBlockEvent(vm, {
block,
generate: true,
skipBlockValidation: true,
})

t.ok(results.block.header.receiptTrie.equals(KECCAK256_RLP))

t.end()
})
/*
async function runWithHf(hardfork: string) {
const vm = setupVM({ common: new Common({ chain: 'mainnet', hardfork }) })
Expand Down