forked from 0xPolygonHermez/zkevm-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
207 lines (186 loc) · 6.05 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package state
import (
"fmt"
"math/big"
"time"
"github.com/0xPolygonHermez/zkevm-node/state/runtime/instrumentation"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
// ProcessRequest represents the request of a batch process.
type ProcessRequest struct {
BatchNumber uint64
GlobalExitRoot common.Hash
OldStateRoot common.Hash
OldAccInputHash common.Hash
Transactions []byte
Coinbase common.Address
Timestamp uint64
Caller CallerLabel
}
// ProcessBatchResponse represents the response of a batch process.
type ProcessBatchResponse struct {
NewStateRoot common.Hash
NewAccInputHash common.Hash
NewLocalExitRoot common.Hash
NewBatchNumber uint64
UsedZkCounters ZKCounters
Responses []*ProcessTransactionResponse
ExecutorError error
IsBatchProcessed bool
ReadWriteAddresses map[common.Address]*InfoReadWrite
}
// ProcessTransactionResponse represents the response of a tx process.
type ProcessTransactionResponse struct {
// TxHash is the hash of the transaction
TxHash common.Hash
// Type indicates legacy transaction
// It will be always 0 (legacy) in the executor
Type uint32
// ReturnValue is the returned data from the runtime (function result or data supplied with revert opcode)
ReturnValue []byte
// GasLeft is the total gas left as result of execution
GasLeft uint64
// GasUsed is the total gas used as result of execution or gas estimation
GasUsed uint64
// GasRefunded is the total gas refunded as result of execution
GasRefunded uint64
// RomError represents any error encountered during the execution
RomError error
// CreateAddress is the new SC Address in case of SC creation
CreateAddress common.Address
// StateRoot is the State Root
StateRoot common.Hash
// Logs emitted by LOG opcode
Logs []*types.Log
// IsProcessed indicates if this tx didn't fit into the batch
IsProcessed bool
// Tx is the whole transaction object
Tx types.Transaction
// ExecutionTrace contains the traces produced in the execution
ExecutionTrace []instrumentation.StructLog
// CallTrace contains the call trace.
CallTrace instrumentation.ExecutorTrace
}
// ZKCounters counters for the tx
type ZKCounters struct {
CumulativeGasUsed uint64
UsedKeccakHashes uint32
UsedPoseidonHashes uint32
UsedPoseidonPaddings uint32
UsedMemAligns uint32
UsedArithmetics uint32
UsedBinaries uint32
UsedSteps uint32
}
// SumUp sum ups zk counters with passed tx zk counters
func (z *ZKCounters) SumUp(other ZKCounters) {
z.CumulativeGasUsed += other.CumulativeGasUsed
z.UsedKeccakHashes += other.UsedKeccakHashes
z.UsedPoseidonHashes += other.UsedPoseidonHashes
z.UsedPoseidonPaddings += other.UsedPoseidonPaddings
z.UsedMemAligns += other.UsedMemAligns
z.UsedArithmetics += other.UsedArithmetics
z.UsedBinaries += other.UsedBinaries
z.UsedSteps += other.UsedSteps
}
// Sub subtract zk counters with passed zk counters (not safe)
func (z *ZKCounters) Sub(other ZKCounters) error {
// ZKCounters
if other.CumulativeGasUsed > z.CumulativeGasUsed {
return GetZKCounterError("CumulativeGasUsed")
}
if other.UsedKeccakHashes > z.UsedKeccakHashes {
return GetZKCounterError("UsedKeccakHashes")
}
if other.UsedPoseidonHashes > z.UsedPoseidonHashes {
return GetZKCounterError("UsedPoseidonHashes")
}
if other.UsedPoseidonPaddings > z.UsedPoseidonPaddings {
return fmt.Errorf("underflow ZKCounter: UsedPoseidonPaddings")
}
if other.UsedMemAligns > z.UsedMemAligns {
return GetZKCounterError("UsedMemAligns")
}
if other.UsedArithmetics > z.UsedArithmetics {
return GetZKCounterError("UsedArithmetics")
}
if other.UsedBinaries > z.UsedBinaries {
return GetZKCounterError("UsedBinaries")
}
if other.UsedSteps > z.UsedSteps {
return GetZKCounterError("UsedSteps")
}
z.CumulativeGasUsed -= other.CumulativeGasUsed
z.UsedKeccakHashes -= other.UsedKeccakHashes
z.UsedPoseidonHashes -= other.UsedPoseidonHashes
z.UsedPoseidonPaddings -= other.UsedPoseidonPaddings
z.UsedMemAligns -= other.UsedMemAligns
z.UsedArithmetics -= other.UsedArithmetics
z.UsedBinaries -= other.UsedBinaries
z.UsedSteps -= other.UsedSteps
return nil
}
// InfoReadWrite has information about modified addresses during the execution
type InfoReadWrite struct {
Address common.Address
Nonce *uint64
Balance *big.Int
}
const (
// DebugInfoErrorType_EXECUTOR_ERROR indicates a error happened in the executor
DebugInfoErrorType_EXECUTOR_ERROR = "EXECUTOR ERROR"
// DebugInfoErrorType_EXECUTOR_RLP_ERROR indicates a error happened decoding the RLP returned by the executor
DebugInfoErrorType_EXECUTOR_RLP_ERROR = "EXECUTOR RLP ERROR"
)
// DebugInfo allows handling runtime debug info
type DebugInfo struct {
ErrorType string
Timestamp time.Time
Payload string
}
// TraceConfig sets the debug configuration for the executor
type TraceConfig struct {
DisableStorage bool
DisableStack bool
EnableMemory bool
EnableReturnData bool
Tracer *string
}
// TrustedReorg represents a trusted reorg
type TrustedReorg struct {
BatchNumber uint64
Reason string
}
const (
// EventType_Prexecution_OOC indicates a preexecution out of couters error
EventType_Prexecution_OOC = "PREEXECUTION OOC"
// EventType_ZKCounters_Diff indicates big different in preexecution and execution regarding ZKCounters
EventType_ZKCounters_Diff = "ZK COUNTERS DIFF"
)
// Event represents a event that may be investigated
type Event struct {
EventType string
Timestamp time.Time
IP string
TxHash common.Hash
Payload string
}
// HexToAddressPtr create an address from a hex and returns its pointer
func HexToAddressPtr(hex string) *common.Address {
a := common.HexToAddress(hex)
return &a
}
// HexToHashPtr create a hash from a hex and returns its pointer
func HexToHashPtr(hex string) *common.Hash {
h := common.HexToHash(hex)
return &h
}
// AddressPtr returns a pointer to the provided address
func AddressPtr(i common.Address) *common.Address {
return &i
}
// HashPtr returns a pointer to the provided hash
func HashPtr(h common.Hash) *common.Hash {
return &h
}