forked from 0xPolygonHermez/zkevm-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
243 lines (217 loc) · 7.16 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package state
import (
"encoding/json"
"fmt"
"math/big"
"strings"
"time"
"github.com/0xPolygonHermez/zkevm-node/state/metrics"
"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 time.Time
Caller metrics.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
ReadWriteAddresses map[common.Address]*InfoReadWrite
IsRomLevelError bool
IsExecutorLevelError bool
IsRomOOCError bool
FlushID uint64
StoredFlushID uint64
ProverID string
}
// 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
// ChangesStateRoot indicates if this tx affects the state
ChangesStateRoot 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
// EffectiveGasPrice effective gas price used for the tx
EffectiveGasPrice string
//EffectivePercentage effective percentage used for the tx
EffectivePercentage uint32
}
// 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
}
// BatchResources is a struct that contains the ZKEVM resources used by a batch/tx
type BatchResources struct {
ZKCounters ZKCounters
Bytes uint64
}
// Sub subtracts the batch resources from other
func (r *BatchResources) Sub(other BatchResources) error {
// Bytes
if other.Bytes > r.Bytes {
return ErrBatchResourceBytesUnderflow
}
bytesBackup := r.Bytes
r.Bytes -= other.Bytes
err := r.ZKCounters.Sub(other.ZKCounters)
if err != nil {
r.Bytes = bytesBackup
return NewBatchRemainingResourcesUnderflowError(err, err.Error())
}
return err
}
// InfoReadWrite has information about modified addresses during the execution
type InfoReadWrite struct {
Address common.Address
Nonce *uint64
Balance *big.Int
}
// TraceConfig sets the debug configuration for the executor
type TraceConfig struct {
DisableStorage bool
DisableStack bool
EnableMemory bool
EnableReturnData bool
Tracer *string
TracerConfig json.RawMessage
}
// IsDefaultTracer returns true when no custom tracer is set
func (t *TraceConfig) IsDefaultTracer() bool {
return t.Tracer == nil || *t.Tracer == ""
}
// Is4ByteTracer returns true when should use 4byteTracer
func (t *TraceConfig) Is4ByteTracer() bool {
return t.Tracer != nil && *t.Tracer == "4byteTracer"
}
// IsCallTracer returns true when should use callTracer
func (t *TraceConfig) IsCallTracer() bool {
return t.Tracer != nil && *t.Tracer == "callTracer"
}
// IsNoopTracer returns true when should use noopTracer
func (t *TraceConfig) IsNoopTracer() bool {
return t.Tracer != nil && *t.Tracer == "noopTracer"
}
// IsPrestateTracer returns true when should use prestateTracer
func (t *TraceConfig) IsPrestateTracer() bool {
return t.Tracer != nil && *t.Tracer == "prestateTracer"
}
// IsJSCustomTracer returns true when should use js custom tracer
func (t *TraceConfig) IsJSCustomTracer() bool {
return t.Tracer != nil && strings.Contains(*t.Tracer, "result") && strings.Contains(*t.Tracer, "fault")
}
// TrustedReorg represents a trusted reorg
type TrustedReorg struct {
BatchNumber uint64
Reason 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
}