core/asm: address more code review feedback.

This commit is contained in:
Valentin Wüstholz 2017-02-22 11:38:46 +01:00
parent 3cf2dffe28
commit 3f585d3b3c
2 changed files with 39 additions and 20 deletions

View file

@ -26,20 +26,18 @@ import (
// Iterator for disassembled EVM instructions // Iterator for disassembled EVM instructions
type instructionIterator struct { type instructionIterator struct {
code []byte code []byte
pc uint64 pc uint64
arg []byte arg []byte
op vm.OpCode op vm.OpCode
error error error error
started bool
} }
// Create a new instruction iterator. // Create a new instruction iterator.
func NewInstructionIterator(code []byte) *instructionIterator { func NewInstructionIterator(code []byte) *instructionIterator {
it := new(instructionIterator) it := new(instructionIterator)
it.code = code it.code = code
it.pc = 0
it.error = nil
it.arg = nil
return it return it
} }
@ -50,12 +48,15 @@ func (it *instructionIterator) Next() bool {
return false return false
} }
if it.arg == nil { if it.started {
// We are calling this procedure for the first time. // Since the iteration has been already started we move to the next instruction.
it.pc = 0 if it.arg != nil {
it.pc += uint64(len(it.arg))
}
it.pc++
} else { } else {
// We are moving to the next instruction. // We start the iteration from the first instruction.
it.pc += uint64(len(it.arg)) + 1 it.started = true
} }
if uint64(len(it.code)) <= it.pc { if uint64(len(it.code)) <= it.pc {
@ -73,7 +74,7 @@ func (it *instructionIterator) Next() bool {
} }
it.arg = it.code[it.pc+1 : u] it.arg = it.code[it.pc+1 : u]
} else { } else {
it.arg = make([]byte, 0) it.arg = nil
} }
return true return true
} }
@ -113,8 +114,8 @@ func PrintDisassembled(code string) error {
fmt.Printf("%06v: %v\n", it.PC(), it.Op()) fmt.Printf("%06v: %v\n", it.PC(), it.Op())
} }
} }
if it.Error() != nil { if err := it.Error(); err != nil {
return it.Error() return err
} }
return nil return nil
} }
@ -131,8 +132,8 @@ func Disassemble(script []byte) ([]string, error) {
instrs = append(instrs, fmt.Sprintf("%06v: %v\n", it.PC(), it.Op())) instrs = append(instrs, fmt.Sprintf("%06v: %v\n", it.PC(), it.Op()))
} }
} }
if it.Error() != nil { if err := it.Error(); err != nil {
return nil, it.Error() return nil, err
} }
return instrs, nil return instrs, nil
} }

View file

@ -32,8 +32,8 @@ func TestInstructionIteratorValid(t *testing.T) {
cnt++ cnt++
} }
if it.Error() != nil { if err := it.Error(); err != nil {
t.Errorf("Expected 2, but encountered error %v instead.", it.Error()) t.Errorf("Expected 2, but encountered error %v instead.", err)
} }
if cnt != 2 { if cnt != 2 {
t.Errorf("Expected 2, but got %v instead.", cnt) t.Errorf("Expected 2, but got %v instead.", cnt)
@ -54,3 +54,21 @@ func TestInstructionIteratorInvalid(t *testing.T) {
t.Errorf("Expected an error, but got %v instead.", cnt) t.Errorf("Expected an error, but got %v instead.", cnt)
} }
} }
// Tests disassembling the instructions for empty evm code
func TestInstructionIteratorEmpty(t *testing.T) {
cnt := 0
script, _ := hex.DecodeString("")
it := NewInstructionIterator(script)
for it.Next() {
cnt++
}
if err := it.Error(); err != nil {
t.Errorf("Expected 0, but encountered error %v instead.", err)
}
if cnt != 0 {
t.Errorf("Expected 0, but got %v instead.", cnt)
}
}