core/vm: customisable jump table

This commit is contained in:
Jeffrey Wilcke 2017-01-04 13:57:10 +01:00
parent f02b4012c9
commit 007cae717f
2 changed files with 819 additions and 804 deletions

View file

@ -48,7 +48,10 @@ type operation struct {
valid bool
}
var jumpTable = [256]operation{
var defaultJumpTable = NewJumpTable()
func NewJumpTable() [256]operation {
return [256]operation{
ADD: operation{
execute: opAdd,
gasCost: constGasFunc(GasFastestStep),
@ -853,3 +856,4 @@ var jumpTable = [256]operation{
valid: true,
},
}
}

View file

@ -44,6 +44,10 @@ type Config struct {
NoRecursion bool
// Disable gas metering
DisableGasMetering bool
// JumpTable contains the EVM instruction table. This
// may me left uninitialised and will be set the default
// table.
JumpTable [256]operation
}
// Interpreter is used to run Ethereum based contracts and will utilise the
@ -58,6 +62,13 @@ type Interpreter struct {
// NewInterpreter returns a new instance of the Interpreter.
func NewInterpreter(env *EVM, cfg Config) *Interpreter {
// We use the STOP instruction whether to see
// the jump table was initialised. If it was not
// we'll set the default jump table.
if !cfg.JumpTable[STOP].valid {
cfg.JumpTable = defaultJumpTable
}
return &Interpreter{
env: env,
cfg: cfg,
@ -121,7 +132,7 @@ func (evm *Interpreter) Run(contract *Contract, input []byte) (ret []byte, err e
op = contract.GetOp(pc)
// get the operation from the jump table matching the opcode
operation := jumpTable[op]
operation := evm.cfg.JumpTable[op]
// if the op is invalid abort the process and return an error
if !operation.valid {