Skip to content

Commit

Permalink
vm -> spuriousDragon: added pre-Byzantium tx receipt format (EIP-658)…
Browse files Browse the repository at this point in the history
… to runBlock.js
  • Loading branch information
holgerd77 committed Jun 26, 2020
1 parent b6f9b9d commit 2616b99
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 9 deletions.
52 changes: 43 additions & 9 deletions packages/vm/lib/runBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,17 @@ export interface RunBlockResult {
/**
* Receipts generated for transactions in the block
*/
receipts: TxReceipt[]
receipts: PreByzantiumTxReceipt | PostByzantiumTxReceipt[]
/**
* Results of executing the transactions in the block
*/
results: RunTxResult[]
}

/**
* Receipt generated for a transaction
* Abstract interface with common transaction receipt fields
*/
export interface TxReceipt {
/**
* Status of transaction, `1` if successful, `0` if an exception occured
*/
status: 0 | 1
interface TxReceipt {
/**
* Gas used
*/
Expand All @@ -73,6 +69,28 @@ export interface TxReceipt {
logs: any[]
}

/**
* Pre-Byzantium receipt type with a field
* for the intermediary state root
*/
export interface PreByzantiumTxReceipt extends TxReceipt {
/**
* Intermediary state root
*/
stateRoot: Buffer
}

/**
* Receipt type for Byzantium and beyond replacing the intermediary
* state root field with a status code field (EIP-658)
*/
export interface PostByzantiumTxReceipt extends TxReceipt {
/**
* Status of transaction, `1` if successful, `0` if an exception occured
*/
status: 0 | 1
}

/**
* @ignore
*/
Expand Down Expand Up @@ -219,12 +237,28 @@ async function applyTransactions(this: VM, block: any, opts: RunBlockOpts) {
// Combine blooms via bitwise OR
bloom.or(txRes.bloom)

const txReceipt: TxReceipt = {
status: txRes.execResult.exceptionError ? 0 : 1, // Receipts have a 0 as status on error
const abstractTxReceipt: TxReceipt = {
gasUsed: gasUsed.toArrayLike(Buffer),
bitvector: txRes.bloom.bitvector,
logs: txRes.execResult.logs || [],
}
let txReceipt
if (this._common.gteHardfork('byzantium')) {
txReceipt = {
status: txRes.execResult.exceptionError ? 0 : 1, // Receipts have a 0 as status on error
...abstractTxReceipt,
} as PostByzantiumTxReceipt
} else {
// This is just using a dummy place holder for the state root right now.
// Giving the correct intermediary state root would need a too depp intervention
// into the current checkpointing mechanism which hasn't been considered
// to be worth it on a HF backport, 2020-06-26
txReceipt = {
stateRoot: Buffer.alloc(32),
...abstractTxReceipt,
} as PreByzantiumTxReceipt
}

receipts.push(txReceipt)

// Add receipt to trie to later calculate receipt root
Expand Down
29 changes: 29 additions & 0 deletions packages/vm/tests/api/runBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,32 @@ tape('should run valid block', async (t) => {

t.end()
})

async function runWithHf(hardfork) {
const vm = setupVM({ hardfork: hardfork })
const suite = setup(vm)

const block = new Block(util.rlp.decode(suite.data.blocks[0].rlp))

await setupPreConditions(suite.vm.stateManager._trie, suite.data)

let res = await suite.p.runBlock({
block,
generate: true,
skipBlockValidation: true,
})
return res
}

tape('should return correct HF receipts', async (t) => {
let res = await runWithHf('byzantium')
t.equal(res.receipts[0].status, 1, 'should return correct post-Byzantium receipt format')

res = await runWithHf('spuriousDragon')
t.deepEqual(
res.receipts[0].stateRoot,
Buffer.alloc(32),
'should return correct pre-Byzantium receipt format')

t.end()
})

0 comments on commit 2616b99

Please sign in to comment.