Skip to content

Commit

Permalink
core/vm: clear linter warnings (ethereum#17057)
Browse files Browse the repository at this point in the history
* core/vm: clear linter warnings

* core/vm: review input

* core/vm.go: revert lint in noop as per request
  • Loading branch information
gballet authored and karalabe committed Jun 26, 2018
1 parent 4612918 commit 598f786
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 30 deletions.
1 change: 1 addition & 0 deletions core/vm/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package vm

import "errors"

// List execution errors
var (
ErrOutOfGas = errors.New("out of gas")
ErrCodeStoreOutOfGas = errors.New("contract creation code storage out of gas")
Expand Down
4 changes: 3 additions & 1 deletion core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ import (
var emptyCodeHash = crypto.Keccak256Hash(nil)

type (
// CanTransferFunc is the signature of a transfer guard function
CanTransferFunc func(StateDB, common.Address, *big.Int) bool
TransferFunc func(StateDB, common.Address, common.Address, *big.Int)
// TransferFunc is the signature of a transfer function
TransferFunc func(StateDB, common.Address, common.Address, *big.Int)
// GetHashFunc returns the nth block hash in the blockchain
// and is used by the BLOCKHASH EVM op code.
GetHashFunc func(uint64) common.Hash
Expand Down
1 change: 1 addition & 0 deletions core/vm/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/ethereum/go-ethereum/params"
)

// Gas costs
const (
GasQuickStep uint64 = 2
GasFastestStep uint64 = 3
Expand Down
2 changes: 1 addition & 1 deletion core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ func makeDup(size int64) executionFunc {
// make swap instruction function
func makeSwap(size int64) executionFunc {
// switch n + 1 otherwise n would be swapped with n
size += 1
size++
return func(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
stack.swap(int(size))
return nil, nil
Expand Down
22 changes: 11 additions & 11 deletions core/vm/jump_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ type operation struct {
}

var (
frontierInstructionSet = NewFrontierInstructionSet()
homesteadInstructionSet = NewHomesteadInstructionSet()
byzantiumInstructionSet = NewByzantiumInstructionSet()
constantinopleInstructionSet = NewConstantinopleInstructionSet()
frontierInstructionSet = newFrontierInstructionSet()
homesteadInstructionSet = newHomesteadInstructionSet()
byzantiumInstructionSet = newByzantiumInstructionSet()
constantinopleInstructionSet = newConstantinopleInstructionSet()
)

// NewConstantinopleInstructionSet returns the frontier, homestead
// byzantium and contantinople instructions.
func NewConstantinopleInstructionSet() [256]operation {
func newConstantinopleInstructionSet() [256]operation {
// instructions that can be executed during the byzantium phase.
instructionSet := NewByzantiumInstructionSet()
instructionSet := newByzantiumInstructionSet()
instructionSet[SHL] = operation{
execute: opSHL,
gasCost: constGasFunc(GasFastestStep),
Expand All @@ -85,9 +85,9 @@ func NewConstantinopleInstructionSet() [256]operation {

// NewByzantiumInstructionSet returns the frontier, homestead and
// byzantium instructions.
func NewByzantiumInstructionSet() [256]operation {
func newByzantiumInstructionSet() [256]operation {
// instructions that can be executed during the homestead phase.
instructionSet := NewHomesteadInstructionSet()
instructionSet := newHomesteadInstructionSet()
instructionSet[STATICCALL] = operation{
execute: opStaticCall,
gasCost: gasStaticCall,
Expand Down Expand Up @@ -123,8 +123,8 @@ func NewByzantiumInstructionSet() [256]operation {

// NewHomesteadInstructionSet returns the frontier and homestead
// instructions that can be executed during the homestead phase.
func NewHomesteadInstructionSet() [256]operation {
instructionSet := NewFrontierInstructionSet()
func newHomesteadInstructionSet() [256]operation {
instructionSet := newFrontierInstructionSet()
instructionSet[DELEGATECALL] = operation{
execute: opDelegateCall,
gasCost: gasDelegateCall,
Expand All @@ -138,7 +138,7 @@ func NewHomesteadInstructionSet() [256]operation {

// NewFrontierInstructionSet returns the frontier instructions
// that can be executed during the frontier phase.
func NewFrontierInstructionSet() [256]operation {
func newFrontierInstructionSet() [256]operation {
return [256]operation{
STOP: {
execute: opStop,
Expand Down
8 changes: 8 additions & 0 deletions core/vm/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ import (
"github.com/ethereum/go-ethereum/core/types"
)

// Storage represents a contract's storage.
type Storage map[common.Hash]common.Hash

// Copy duplicates the current storage.
func (s Storage) Copy() Storage {
cpy := make(Storage)
for key, value := range s {
Expand Down Expand Up @@ -76,10 +78,12 @@ type structLogMarshaling struct {
ErrorString string `json:"error"` // adds call to ErrorString() in MarshalJSON
}

// OpName formats the operand name in a human-readable format.
func (s *StructLog) OpName() string {
return s.Op.String()
}

// ErrorString formats the log's error as a string.
func (s *StructLog) ErrorString() string {
if s.Err != nil {
return s.Err.Error()
Expand Down Expand Up @@ -124,6 +128,7 @@ func NewStructLogger(cfg *LogConfig) *StructLogger {
return logger
}

// CaptureStart implements the Tracer interface to initialize the tracing operation.
func (l *StructLogger) CaptureStart(from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) error {
return nil
}
Expand Down Expand Up @@ -178,10 +183,13 @@ func (l *StructLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost ui
return nil
}

// CaptureFault implements the Tracer interface to trace an execution fault
// while running an opcode.
func (l *StructLogger) CaptureFault(env *EVM, pc uint64, op OpCode, gas, cost uint64, memory *Memory, stack *Stack, contract *Contract, depth int, err error) error {
return nil
}

// CaptureEnd is called after the call finishes to finalize the tracing.
func (l *StructLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) error {
l.output = output
l.err = err
Expand Down
2 changes: 2 additions & 0 deletions core/vm/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Memory struct {
lastGasCost uint64
}

// NewMemory returns a new memory memory model.
func NewMemory() *Memory {
return &Memory{}
}
Expand Down Expand Up @@ -107,6 +108,7 @@ func (m *Memory) Data() []byte {
return m.store
}

// Print dumps the content of the memory.
func (m *Memory) Print() {
fmt.Printf("### mem %d bytes ###\n", len(m.store))
if len(m.store) > 0 {
Expand Down
37 changes: 21 additions & 16 deletions core/vm/opcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
// OpCode is an EVM opcode
type OpCode byte

// IsPush specifies if an opcode is a PUSH opcode.
func (op OpCode) IsPush() bool {
switch op {
case PUSH1, PUSH2, PUSH3, PUSH4, PUSH5, PUSH6, PUSH7, PUSH8, PUSH9, PUSH10, PUSH11, PUSH12, PUSH13, PUSH14, PUSH15, PUSH16, PUSH17, PUSH18, PUSH19, PUSH20, PUSH21, PUSH22, PUSH23, PUSH24, PUSH25, PUSH26, PUSH27, PUSH28, PUSH29, PUSH30, PUSH31, PUSH32:
Expand All @@ -31,12 +32,13 @@ func (op OpCode) IsPush() bool {
return false
}

// IsStaticJump specifies if an opcode is JUMP.
func (op OpCode) IsStaticJump() bool {
return op == JUMP
}

// 0x0 range - arithmetic ops.
const (
// 0x0 range - arithmetic ops
STOP OpCode = iota
ADD
MUL
Expand All @@ -51,6 +53,7 @@ const (
SIGNEXTEND
)

// 0x10 range - comparison ops.
const (
LT OpCode = iota + 0x10
GT
Expand All @@ -70,8 +73,8 @@ const (
SHA3 = 0x20
)

// 0x30 range - closure state.
const (
// 0x30 range - closure state
ADDRESS OpCode = 0x30 + iota
BALANCE
ORIGIN
Expand All @@ -89,8 +92,8 @@ const (
RETURNDATACOPY
)

// 0x40 range - block operations.
const (
// 0x40 range - block operations
BLOCKHASH OpCode = 0x40 + iota
COINBASE
TIMESTAMP
Expand All @@ -99,8 +102,8 @@ const (
GASLIMIT
)

// 0x50 range - 'storage' and execution.
const (
// 0x50 range - 'storage' and execution
POP OpCode = 0x50 + iota
MLOAD
MSTORE
Expand All @@ -115,8 +118,8 @@ const (
JUMPDEST
)

// 0x60 range.
const (
// 0x60 range
PUSH1 OpCode = 0x60 + iota
PUSH2
PUSH3
Expand Down Expand Up @@ -183,6 +186,7 @@ const (
SWAP16
)

// 0xa0 range - logging ops.
const (
LOG0 OpCode = 0xa0 + iota
LOG1
Expand All @@ -191,15 +195,15 @@ const (
LOG4
)

// unofficial opcodes used for parsing
// unofficial opcodes used for parsing.
const (
PUSH OpCode = 0xb0 + iota
DUP
SWAP
)

// 0xf0 range - closures.
const (
// 0xf0 range - closures
CREATE OpCode = 0xf0 + iota
CALL
CALLCODE
Expand All @@ -211,9 +215,9 @@ const (
SELFDESTRUCT = 0xff
)

// Since the opcodes aren't all in order we can't use a regular slice
// Since the opcodes aren't all in order we can't use a regular slice.
var opCodeToString = map[OpCode]string{
// 0x0 range - arithmetic ops
// 0x0 range - arithmetic ops.
STOP: "STOP",
ADD: "ADD",
MUL: "MUL",
Expand All @@ -232,7 +236,7 @@ var opCodeToString = map[OpCode]string{
ISZERO: "ISZERO",
SIGNEXTEND: "SIGNEXTEND",

// 0x10 range - bit ops
// 0x10 range - bit ops.
AND: "AND",
OR: "OR",
XOR: "XOR",
Expand All @@ -243,10 +247,10 @@ var opCodeToString = map[OpCode]string{
ADDMOD: "ADDMOD",
MULMOD: "MULMOD",

// 0x20 range - crypto
// 0x20 range - crypto.
SHA3: "SHA3",

// 0x30 range - closure state
// 0x30 range - closure state.
ADDRESS: "ADDRESS",
BALANCE: "BALANCE",
ORIGIN: "ORIGIN",
Expand All @@ -263,15 +267,15 @@ var opCodeToString = map[OpCode]string{
RETURNDATASIZE: "RETURNDATASIZE",
RETURNDATACOPY: "RETURNDATACOPY",

// 0x40 range - block operations
// 0x40 range - block operations.
BLOCKHASH: "BLOCKHASH",
COINBASE: "COINBASE",
TIMESTAMP: "TIMESTAMP",
NUMBER: "NUMBER",
DIFFICULTY: "DIFFICULTY",
GASLIMIT: "GASLIMIT",

// 0x50 range - 'storage' and execution
// 0x50 range - 'storage' and execution.
POP: "POP",
//DUP: "DUP",
//SWAP: "SWAP",
Expand All @@ -287,7 +291,7 @@ var opCodeToString = map[OpCode]string{
GAS: "GAS",
JUMPDEST: "JUMPDEST",

// 0x60 range - push
// 0x60 range - push.
PUSH1: "PUSH1",
PUSH2: "PUSH2",
PUSH3: "PUSH3",
Expand Down Expand Up @@ -360,7 +364,7 @@ var opCodeToString = map[OpCode]string{
LOG3: "LOG3",
LOG4: "LOG4",

// 0xf0 range
// 0xf0 range.
CREATE: "CREATE",
CALL: "CALL",
RETURN: "RETURN",
Expand Down Expand Up @@ -524,6 +528,7 @@ var stringToOp = map[string]OpCode{
"SELFDESTRUCT": SELFDESTRUCT,
}

// StringToOp finds the opcode whose name is stored in `str`.
func StringToOp(str string) OpCode {
return stringToOp[str]
}
4 changes: 3 additions & 1 deletion core/vm/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"math/big"
)

// stack is an object for basic stack operations. Items popped to the stack are
// Stack is an object for basic stack operations. Items popped to the stack are
// expected to be changed and modified. stack does not take care of adding newly
// initialised objects.
type Stack struct {
Expand All @@ -32,6 +32,7 @@ func newstack() *Stack {
return &Stack{data: make([]*big.Int, 0, 1024)}
}

// Data returns the underlying big.Int array.
func (st *Stack) Data() []*big.Int {
return st.data
}
Expand Down Expand Up @@ -80,6 +81,7 @@ func (st *Stack) require(n int) error {
return nil
}

// Print dumps the content of the stack
func (st *Stack) Print() {
fmt.Println("### stack ###")
if len(st.data) > 0 {
Expand Down

0 comments on commit 598f786

Please sign in to comment.