Skip to content

Commit

Permalink
Merge c236a35 into 97c30f1
Browse files Browse the repository at this point in the history
  • Loading branch information
jochem-brouwer authored Feb 12, 2021
2 parents 97c30f1 + c236a35 commit 9fac0e2
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/client/lib/sync/execution/vmexecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class VMExecution extends Execution {
const blockNumber = headBlock.header.number.toNumber()
this.config.execCommon.setHardforkByBlockNumber(blockNumber)
this.hardfork = this.config.execCommon.hardfork()
this.vm._updateOpcodes()
this.config.logger.info(`Initializing VM execution hardfork=${this.hardfork}`)
}

Expand Down Expand Up @@ -112,6 +113,7 @@ export class VMExecution extends Execution {
`Execution hardfork switch on block number=${blockNumber} hash=${hash} old=${this.hardfork} new=${hardfork}`
)
this.hardfork = this.config.execCommon.setHardforkByBlockNumber(blockNumber)
this.vm._updateOpcodes()
}
await this.vm.runBlock({ block, root: parentState })
txCounter += block.transactions.length
Expand Down
18 changes: 18 additions & 0 deletions packages/vm/lib/evm/opcodes/codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,29 @@ const opcodes = {

// Array of hard forks in order. These changes are repeatedly applied to `opcodes` until the hard fork is in the future based upon the common
// TODO: All gas price changes should be moved to common
// If the base gas cost of any of the operations change, then these should also be added to this list.
// If there are context variables changed (such as "warm slot reads") which are not the base gas fees,
// Then this does not have to be added.
const hardforkOpcodes = [
{
hardforkName: 'homestead',
opcodes: {
0xf4: { name: 'DELEGATECALL', isAsync: true }, // EIP 7
},
},
{
hardforkName: 'tangerineWhistle',
opcodes: {
0x54: { name: 'SLOAD', isAsync: true },
0xf1: { name: 'CALL', isAsync: true },
0xf2: { name: 'CALLCODE', isAsync: true },
0x3b: { name: 'EXTCODESIZE', isAsync: true },
0x3c: { name: 'EXTCODECOPY', isAsync: true },
0xf4: { name: 'DELEGATECALL', isAsync: true }, // EIP 7
0xff: { name: 'SELFDESTRUCT', isAsync: true },
0x31: { name: 'BALANCE', isAsync: true },
},
},
{
hardforkName: 'byzantium',
opcodes: {
Expand Down Expand Up @@ -267,7 +283,9 @@ function createOpcodes(opcodes: {
export function getOpcodesForHF(common: Common): OpcodeList {
let opcodeBuilder: any = { ...opcodes }

console.log('get hardforks, my hardfork: ', common.hardfork())
for (let fork = 0; fork < hardforkOpcodes.length; fork++) {
console.log('check: ', hardforkOpcodes[fork].hardforkName)
if (common.gteHardfork(hardforkOpcodes[fork].hardforkName)) {
opcodeBuilder = { ...opcodeBuilder, ...hardforkOpcodes[fork].opcodes }
}
Expand Down
41 changes: 41 additions & 0 deletions packages/vm/tests/api/consensus.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import tape from 'tape'
const level = require('level')
import Common from '@ethereumjs/common'
import { Block } from '@ethereumjs/block'
import VM from '../../lib'
import { SecureTrie as Trie } from '@ethereumjs/trie'
import { DefaultStateManager } from '../../lib/state'
import Blockchain from '@ethereumjs/blockchain'
const trace = require('../../tools/0xa92cf6853417158711a0021612cf200338cf58166fb3494076999f5cdf00eb27.json')
import { compareTrace } from '../../tools/compareTrace'

tape('consensus bugs', (t) => {
t.test('Rinkeby block 14182', async (st) => {
const common = new Common({ chain: 'rinkeby', hardfork: 'spuriousDragon' })
const RLP = Buffer.from("f91692f9025ba0b477241819c2222fe45f63300cc2b55760e08f626461f7d37deddcfcc6055defa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940000000000000000000000000000000000000000a071dd74d3fcf62ff5e8aa5043ed0184895daca26b00f2b2d18f19463e601c569ca054c74fc926f7414e4508721bc7770cba5451557f4ba3f248275d98223ded1903a00ce26afff75c0735ae1a517a2790a34e37221b934c980e5e62cb1a823a44b26cb9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000018237668347e7c48314720a8458f184c6b861d783010600846765746887676f312e372e33856c696e757800000000000000003025b299d8c3d198b3b0cdf8b8527186b829d63feb0a7d92f8c3ba002f48c91d69d1a7f4bf906513916f743a064f021840d1356afacd34cc65c7b75ed3421e2501a00000000000000000000000000000000000000000000000000000000000000000880000000000000000f91430f9142d808504a817c8008316e3608080b913da60606040819052600080546001604060020a03191667486974636861696e17905561131a3881900390819083398101604052805160805190820191018160016000509080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a257805160ff19168380011785555b506100d29291505b8082111561012b576000815560010161008e565b82800160010185558215610086579182015b828111156100865782518260005055916020019190600101906100b4565b50508060026000509080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061012f57805160ff19168380011785555b5061015f92915061008e565b5090565b8280016001018555821561011f579182015b8281111561011f578251826000505591602001919060010190610141565b505060008054680100000000000000003302604060020a60e060020a03199091161790555050611187806101936000396000f3606060405236156100da5760e060020a6000350463085270e481146100df57806312ab3c271461021757806314e971da1461031957806317d7de7c14610340578063266386c6146103ad578063451ee32d146103da5780635a22aecc146103e9578063806dec841461042c57806383197ef01461043c578063893d20e81461046c57806390b98a1114610491578063aaaf0824146104c5578063becd283f14610515578063c3a2a93a1461052e578063e094826b1461059b578063f8b2cb4f14610607578063f8d78e5d14610630578063fdbfe8361461065e575b610002565b346100025760408051602480356004818101356020818102808701820190975281865261071d96833596939560449501929182919085019084908082843750506040805160209735808a0135601f81018a90048a0283018a01909352828252969897606497919650602491909101945090925082915084018382808284375050604080519635808901356020818102808b018201909452818a529799986084989097506024929092019550935083925085019084908082843750506040805196358089013560208181028a81018201909452818a5297999860a4989097506024929092019550935083925085019084908082843750949650505050505050600060008351835114158061020d57508054680100000000000000009004600160a060020a039081163390911614155b156108bb57610002565b346100025761071f60408051602081810183526000808352835180830185529081526006805485518185028101850190965280865293949193909260079291849183018282801561029257602002820191906000526020600020905b8154600160a060020a0316815260019190910190602001808311610273575b505050505091508080548060200260200160405190810160405280929190818152602001828054801561030a57602002820191906000526020600020906000905b82829054906101000a900463ffffffff16815260200190600401906020826003010492830192600103820291508084116102d35790505b50505050509050915091509091565b346100025761071d600435610bee81335b60008080808060ff8716819011610ed757610002565b34610002576107a4604080516020818101835260008252825160018054600281831615610100026000190190911604601f8101849004840283018401909552848252929390929183018282801561058f5780601f10610bf25761010080835404028352916020019161058f565b3461000257610812600435600160a060020a0381166000908152600a602052604090205460ff165b919050565b34610002576108266008545b90565b346100025761071d33600160a060020a03166000908152600a602052604081205460ff1680610422575060096020526040812054600190105b15610c2157610002565b34610002576108266003546103e6565b346100025761071d600054680100000000000000009004600160a060020a039081163390911614610cd057610002565b3461000257610838600054680100000000000000009004600160a060020a03166103e6565b346100025761071d60043560243533600160a060020a031660009081526009602052604090205481901015610cea57610002565b346100025761071d60043533600160a060020a03166000908152600a602052604090205460ff16158061050b5750600160a060020a0381166000908152604090205460ff165b15610d5657610002565b3461000257610855600b546001608060020a03166103e6565b346100025761087160408051602081810183526000825282516004805480840283018401909552848252929390929183018282801561058f57602002820191906000526020600020905b816000505481526020019060010190808311610578575b505050505090506103e6565b346100025760408051602081810183526000825260028054845160018216156101000260001901909116829004601f81018490048402820184019095528481526107a494909283018282801561058f5780601f10610bf25761010080835404028352916020019161058f565b3461000257610826600435600160a060020a0381166000908152600960205260409020546103d5565b346100025761071d33600160a060020a03166000908152600a602052604081205460ff1615610db857610002565b346100025761071f60408051602081810183526000808352835180830185528181528451928301855281835260065494519394909380591061069d5750595b9080825280602002602001820160405280156106b4575b509150600090505b600654811015610e6a57600680546009916000918490811015610002576000918252602080832090910154600160a060020a0316835282019290925260400190205482518390839081101561000257602090810290910101526001016106bc565b005b6040518080602001806020018381038352858181518152602001915080519060200190602002808383829060006004602084601f0104600302600f01f1509050018381038252848181518152602001915080519060200190602002808383829060006004602084601f0104600302600f01f15090500194505050505060405180910390f35b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600302600f01f150905090810190601f1680156108045780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b604080519115158252519081900360200190f35b60408051918252519081900360200190f35b60408051600160a060020a03929092168252519081900360200190f35b604080516001608060020a039092168252519081900360200190f35b60405180806020018281038252838181518152602001915080519060200190602002808383829060006004602084601f0104600302600f01f1509050019250505060405180910390f35b6003879055855160048054828255829080158290116108ed578183600052602060002091820191016108ed91906109e1565b50505050600091505b60045482101561095857858281518110156100025790602001906020020151600460005083815481101561000257506000527f8a35acfbc15ff81a39ae7d344fd709f28e8600b4aa8c65c6b64bfe7fe36bd19b830155600191909101906108f6565b600b80546fffffffffffffffffffffffffffffffff19169055845160058054600082905290917f036b6384b5eca791c62761152d0c79bb0604c104a5fb6f4eb0703f3154bb3db0602060026001851615610100026000190190941693909304601f9081018490048201938a01908390106109f957805160ff19168380011785555b50610a299291505b808211156109f557600081556001016109e1565b5090565b828001600101855582156109d9579182015b828111156109d9578251826000505591602001919060010190610a0b565b50508351600660005081815481835581811511610a5957818360005260206000209182019101610a5991906109e1565b505050508251600760005081815481835581811511610a99576007016008900481600701600890048360005260206000209182019101610a9991906109e1565b50600094508493505050505b600654821015610be357838281518110156100025790602001906020020151600660005083815481101561000257506000527ff652222313e28459528d920b65115c16c04f3efc82aaedc97be59f3f377c0d3f8301805473ffffffffffffffffffffffffffffffffffffffff19169091179055825183908390811015610002579060200190602002015160076000508381548110156100025790600052602060002090600891828204019190066004026101000a81548163ffffffff0219169083021790555082828151811015610002579060200190602002015163ffffffff16600960005060008685815181101561000257505060208581028801810151600160a060020a0316825291909152604090205582518390839081101561000257505060208281028401015163ffffffff160160019190910190610aa5565b600855505050505050565b5050565b820191906000526020600020905b815481529060010190602001808311610c0057509394506103e69350505050565b5033600160a060020a031660008181526009602090815260408083208054600260018281019190910491829003909255600880548290039055600a845293829020805460ff191682179055600b80546001608060020a0381169092016fffffffffffffffffffffffffffffffff19909216919091179055805183815290519293927ff94802c523b09ee7e0f6b9feea5a54f6a3cba836d3f0eec2aa351d9d1d6475d0929181900390910190a250565b600054680100000000000000009004600160a060020a0316ff5b33600160a060020a03908116600081815260096020908152604080832080548790039055938616808352918490208054860190558351858152935191937f16cdf1707799c6655baac6e210f52b94b7cec08adcaf9ede7dfe8649da926146929081900390910190a35050565b33600160a060020a039081166000818152600a6020526040808220805460ff199081169091559385168083528183208054909516600117909455517f06fcafa3953f73a31f23dbc4f2c599113a6d9eccfdb6eeebac050fd6bc59adb59190a350565b610dc360013361032a565b33600160a060020a0316600081815260096020908152604080832080548690039055600880548690039055600a825291829020805460ff19166001908117909155600b80546fffffffffffffffffffffffffffffffff1981166001608060020a039091169092019190911790558151848152915193945091927ff94802c523b09ee7e0f6b9feea5a54f6a3cba836d3f0eec2aa351d9d1d6475d0929181900390910190a250565b60066000508281805480602002602001604051908101604052809291908181526020018280548015610ec657602002820191906000526020600020905b8154600160a060020a0316815260019190910190602001808311610ea7575b505050505091509350935050509091565b60009350600091505b600654821015610f5857600680548390811015610002576000918252602082200154600160a060020a03161480610f4e5750826000600660005084815481101561000257602060008181209381529290910154600160a060020a0316835282019290925260400190205460ff165b1561102257611125565b600091505b60065482101561113157600680548390811015610002576000918252602082200154600160a060020a031614801590610fcd5750826000600660005084815481101561000257602060008181209381529290910154600160a060020a0316835282019290925260400190205460ff165b156110165760008360006006600050858154811015610002576000918252602080832090910154600160a060020a031683528201929092526040019020805460ff191690911790555b60019190910190610f5d565b8660ff166103e70161ffff166009600050600060066000508581548110156100025750507ff652222313e28459528d920b65115c16c04f3efc82aaedc97be59f3f377c0d3f850154600160a060020a03168152602091909152604090205460ff89160281156100025704905080600960005060006006600050858154811015610002576040808420805496909603909555600160a060020a038b16835293822080548601905580546001945087935086908110156100025750507ff652222313e28459528d920b65115c16c04f3efc82aaedc97be59f3f377c0d3f850154600160a060020a0316815260209190915260409020805460ff19169091179055928301925b60019190910190610ee0565b85600160a060020a031630600160a060020a03167f16cdf1707799c6655baac6e210f52b94b7cec08adcaf9ede7dfe8649da926146866040518082815260200191505060405180910390a350919594505050505056000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000011426574746572205468616e20546f646179000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b4d6178204f726c616e646f0000000000000000000000000000000000000000002ba0698382b8bd21d3126f0aadba7d2ccf1f88058a05477978c3fc89c813cd65905ca040b82405178a8756e842fa48510e73d03f56de7987285be90cc7dae524558ddec0", 'hex')
const block = Block.fromRLPSerializedBlock(RLP, { common })

const stateDB = level('/Users/jochem/Library/Ethereum/ethereumjs/rinkeby/state')
const trie = new Trie(stateDB)
const stateManager = new DefaultStateManager({ trie, common })
// Ensure we run on the right root
stateManager.setStateRoot(Buffer.from('135fae6d4909a072d77707a797769afc72f6239a72173e0f411c7700af66ac26', 'hex'))


const chainDB = level('/Users/jochem/Library/Ethereum/ethereumjs/rinkeby/chain')
const blockchain = await Blockchain.create({
db: chainDB,
common,
validateBlocks: true,
validateConsensus: false,
})

const vm = new VM({ stateManager, blockchain, common })

compareTrace(vm, trace)

await vm.runBlock({ block })


})
})

Large diffs are not rendered by default.

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions packages/vm/tools/compareTrace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import VM from '../lib'
import { InterpreterStep } from '../lib/evm/interpreter'

// Usage: import the VM and the json trace of the transaction
// This is the raw JSON object which you get from calling debug_traceTransaction on Geth.
export function compareTrace(vm: VM, traceJSON: any) {
let opCounter = 0
let diverged = false
const entryPoint = traceJSON.structLogs
vm.on('step', (stepEvent: InterpreterStep) => {
const actualStep = entryPoint[opCounter]
if (!actualStep || diverged) {
return
}
if (actualStep.pc !== stepEvent.pc) {
console.log(`PC wrong, have ${stepEvent.pc}, need ${actualStep.pc}`)
diverged = true
}
if (stepEvent.gasLeft.toNumber() !== actualStep.gas) {
console.log(`Gas wrong, have ${stepEvent.gasLeft.toNumber()}, need ${actualStep.gas}`)
diverged = true
}
// Compare the stack

const newStack: string[] = []

stepEvent.stack.forEach((item) => {
newStack.push(item.toString('hex', 64))
})

newStack.forEach((item, index) => {
if (item !== actualStep.stack[index]) {
console.log('Stack is wrong. Dumping VM stack: ')
console.log(newStack)
console.log('Expected stack: ')
console.log(actualStep.stack)
console.log(
`PC wrong, have ${stepEvent.pc}, step ${opCounter}, current operation ${
actualStep.op
}, previous op ${entryPoint[opCounter - 1].op}`
)
console.log('Stack at previous operation: ')
console.log(entryPoint[opCounter - 1].stack)
diverged = true
}
})

opCounter++
})

vm.on('afterTx', console.log)
}

0 comments on commit 9fac0e2

Please sign in to comment.