• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

unpackdev / solgo / 8960975808

05 May 2024 08:34PM UTC coverage: 64.769% (-0.3%) from 65.065%
8960975808

push

github

web-flow
Opcode functions and instruction tree + JSON normalization (#210)

245 of 449 new or added lines in 61 files covered. (54.57%)

2 existing lines in 1 file now uncovered.

27471 of 42414 relevant lines covered (64.77%)

0.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.36
/opcode/opcodes.go
1
package opcode
2

3
import (
4
        "fmt"
5

6
        opcode_pb "github.com/unpackdev/protos/dist/go/opcode"
7
)
8

9
// OpCode represents an Ethereum operation code (opcode).
10
// Opcodes are single byte values that represent a specific operation in the EVM.
11
// The OpCode type provides methods to retrieve the string representation of the opcode,
12
// and to determine if an opcode corresponds to specific operations like PUSH or JUMP.
13
type OpCode byte
14

15
// String returns the string representation of the OpCode.
16
// This method tries to fetch the human-readable name of the opcode from the opCodeToString map.
17
// If the opcode is not found in the map, it returns a formatted string indicating the undefined opcode.
18
func (op OpCode) String() string {
1✔
19
        str := opCodeToString[op]
1✔
20
        if len(str) == 0 {
2✔
21
                return fmt.Sprintf("opcode %#x not defined", int(op))
1✔
22
        }
1✔
23

24
        return str
1✔
25
}
26

27
// ToProto converts the opcode to a protobuf message.
28
func (op OpCode) ToProto() opcode_pb.OpCode {
1✔
29
        return opcode_pb.OpCode(op)
1✔
30
}
1✔
31

32
// IsPush checks if the given opcode is a PUSH opcode.
33
// In the Ethereum instruction set, there are several PUSH opcodes ranging from PUSH1 to PUSH32.
34
// These opcodes are used to place a series of bytes onto the stack.
35
func (op OpCode) IsPush() bool {
1✔
36
        return PUSH1 <= op && op <= PUSH32
1✔
37
}
1✔
38

39
// IsJump determines if an opcode corresponds to a jump operation.
40
// Jump operations in the EVM allow for altering the sequence of execution.
41
// This method checks for three specific jump-related opcodes: JUMP, JUMPI, and JUMPDEST.
42
func (op OpCode) IsJump() bool {
1✔
43
        switch op {
1✔
44
        case JUMP, JUMPI, JUMPDEST:
1✔
45
                return true
1✔
46
        default:
1✔
47
                return false
1✔
48
        }
49
}
50

51
// IsArithmetic checks if the given opcode corresponds to an arithmetic operation.
52
// Arithmetic operations in the EVM include addition, multiplication, subtraction, etc.
53
func (op OpCode) IsArithmetic() bool {
1✔
54
        switch op {
1✔
55
        case ADD, MUL, SUB, DIV, SDIV, MOD, SMOD, ADDMOD, MULMOD, EXP, SIGNEXTEND:
1✔
56
                return true
1✔
57
        default:
1✔
58
                return false
1✔
59
        }
60
}
61

62
// IsComparison checks if the given opcode corresponds to a comparison operation.
63
// Comparison operations in the EVM include less than, greater than, equal to, etc.
64
func (op OpCode) IsComparison() bool {
1✔
65
        switch op {
1✔
66
        case LT, GT, SLT, SGT, EQ, ISZERO:
1✔
67
                return true
1✔
68
        default:
1✔
69
                return false
1✔
70
        }
71
}
72

73
// IsBitwise checks if the given opcode corresponds to a bitwise operation.
74
// Bitwise operations in the EVM include AND, OR, XOR, NOT, etc.
75
func (op OpCode) IsBitwise() bool {
1✔
76
        switch op {
1✔
77
        case AND, OR, XOR, NOT, BYTE, SHL, SHR, SAR:
1✔
78
                return true
1✔
79
        default:
1✔
80
                return false
1✔
81
        }
82
}
83

84
// IsBlockInformation checks if the given opcode provides information about the current block.
85
// These opcodes provide details like the current block's hash, coinbase, timestamp, etc.
86
func (op OpCode) IsBlockInformation() bool {
1✔
87
        switch op {
1✔
88
        case BLOCKHASH, COINBASE, TIMESTAMP, NUMBER, DIFFICULTY, GASLIMIT:
1✔
89
                return true
1✔
90
        default:
1✔
91
                return false
1✔
92
        }
93
}
94

95
// IsStack checks if the given opcode is related to stack operations.
96
// Stack operations in the EVM include operations that interact with the main stack.
97
func (op OpCode) IsStack() bool {
1✔
98
        switch op {
1✔
99
        case POP, MLOAD, MSTORE, MSTORE8, SLOAD, SSTORE, JUMP, JUMPI, PC, MSIZE, GAS:
1✔
100
                return true
1✔
101
        default:
1✔
102
                return false
1✔
103
        }
104
}
105

106
// IsMemory checks if the given opcode is related to memory operations.
107
// Memory operations in the EVM include operations that interact with the memory segment.
108
func (op OpCode) IsMemory() bool {
1✔
109
        switch op {
1✔
110
        case MLOAD, MSTORE, MSTORE8, MSIZE:
1✔
111
                return true
1✔
112
        default:
1✔
113
                return false
1✔
114
        }
115
}
116

117
// IsStorage checks if the given opcode is related to storage operations.
118
// Storage operations in the EVM include operations that interact with the contract's storage.
119
func (op OpCode) IsStorage() bool {
1✔
120
        switch op {
1✔
121
        case SLOAD, SSTORE:
1✔
122
                return true
1✔
123
        default:
1✔
124
                return false
1✔
125
        }
126
}
127

128
// IsFlowControl checks if the given opcode is related to flow control operations.
129
// Flow control operations in the EVM include operations that alter the sequence of execution.
130
func (op OpCode) IsFlowControl() bool {
1✔
131
        switch op {
1✔
132
        case JUMP, JUMPI, PC, MSIZE, GAS, STOP, RETURN, REVERT, INVALID, SELFDESTRUCT:
1✔
133
                return true
1✔
134
        default:
1✔
135
                return false
1✔
136
        }
137
}
138

139
// IsSystem checks if the given opcode is a system operation.
140
// System operations in the EVM include operations like contract creation, external calls, etc.
141
func (op OpCode) IsSystem() bool {
1✔
142
        switch op {
1✔
143
        case CREATE, CALL, CALLCODE, RETURN, DELEGATECALL, CREATE2, STATICCALL, REVERT, INVALID, SELFDESTRUCT:
1✔
144
                return true
1✔
145
        default:
1✔
146
                return false
1✔
147
        }
148
}
149

150
// IsFunctionStart checks if the opcode represents the start of a function.
NEW
151
func (op OpCode) IsFunctionStart() bool {
×
NEW
152
        // JUMPDEST indicates a valid jump destination, which commonly marks the beginning of a function.
×
NEW
153
        return op == JUMPDEST
×
NEW
154
}
×
155

156
// IsFunctionEnd checks if the opcode represents the end of a function.
NEW
157
func (op OpCode) IsFunctionEnd() bool {
×
NEW
158
        // RETURN or STOP typically indicate the end of execution for a function.
×
NEW
159
        return op == RETURN || op == STOP
×
NEW
160
}
×
161

162
// IsSelfDestruct checks if the given opcode corresponds to the SELFDESTRUCT operation.
163
// The SELFDESTRUCT opcode is used in the EVM to destroy the current contract, sending its funds to the provided address.
164
func (op OpCode) IsSelfDestruct() bool {
1✔
165
        return op == SELFDESTRUCT
1✔
166
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc