-
Notifications
You must be signed in to change notification settings - Fork 9
/
structs.go
407 lines (349 loc) · 8.44 KB
/
structs.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package web3
import (
"encoding/hex"
"fmt"
"math/big"
"github.com/laizy/web3/utils"
"golang.org/x/crypto/sha3"
)
// Lengths of hashes and addresses in bytes.
const (
// HashLength is the expected length of the hash
HashLength = 32
// AddressLength is the expected length of the address
AddressLength = 20
)
// Address is an Ethereum address
type Address [20]byte
// HexToAddress converts an hex string value to an address object
func HexToAddress(str string) Address {
a := Address{}
err := a.UnmarshalText([]byte(str))
utils.Ensure(err)
return a
}
func (self Address) ToHash() Hash {
return BytesToHash(self[:])
}
// UnmarshalText implements the unmarshal interface
func (a *Address) UnmarshalText(b []byte) error {
return unmarshalTextByte(a[:], b, 20)
}
// SetBytes sets the address to the value of b.
// If b is larger than len(a), b will be cropped from the left.
func (a *Address) SetBytes(b []byte) {
if len(b) > len(a) {
b = b[len(b)-AddressLength:]
}
copy(a[AddressLength-len(b):], b)
}
func (a *Address) IsZero() bool {
var zero Address
return *a == zero
}
// MarshalText implements the marshal interface
func (a Address) MarshalText() ([]byte, error) {
return []byte(a.String()), nil
}
func (a Address) String() string {
return "0x" + hex.EncodeToString(a[:])
}
func (a Address) Bytes() []byte {
return a[:]
}
// Hash is an Ethereum hash
type Hash [32]byte
// HexToHash converts an hex string value to a hash object
func HexToHash(str string) Hash {
h := Hash{}
err := h.UnmarshalText([]byte(str))
utils.Ensure(err)
return h
}
// UnmarshalText implements the unmarshal interface
func (h *Hash) UnmarshalText(b []byte) error {
return unmarshalTextByte(h[:], b, 32)
}
// MarshalText implements the marshal interface
func (h Hash) MarshalText() ([]byte, error) {
return []byte(h.String()), nil
}
func (h Hash) String() string {
return "0x" + hex.EncodeToString(h[:])
}
func (h Hash) IsEmpty() bool {
empty := Hash{}
return h == empty
}
func (h Hash) Bytes() []byte {
return h[:]
}
type Header struct {
ParentHash Hash
Sha3Uncles Hash
Miner Address
StateRoot Hash
TransactionsRoot Hash
ReceiptsRoot Hash
LogsBloom [256]byte
Difficulty *big.Int
Number uint64
GasLimit uint64
GasUsed uint64
Timestamp uint64
ExtraData []byte
MixHash Hash
Nonce [8]byte
}
type Block struct {
Header
Hash Hash
Transactions []*Transaction
TransactionsHashes []Hash
Uncles []Hash
}
type Transaction struct {
hash Hash
From Address
To *Address
Input []byte
GasPrice uint64
Gas uint64
Value *big.Int
Nonce uint64
V []byte
R []byte
S []byte
BlockHash Hash
BlockNumber uint64
TxnIndex uint64
}
func (t *Transaction) Hash() Hash {
if t.hash.IsEmpty() {
hs := sha3.NewLegacyKeccak256()
hs.Write(t.MarshalRLP())
hs.Sum(t.hash[:0])
}
return t.hash
}
func (t *Transaction) ToCallMsg() *CallMsg {
return &CallMsg{
From: t.From,
To: t.To,
Data: t.Input,
Value: t.Value,
GasPrice: t.GasPrice,
}
}
type CallMsg struct {
From Address
To *Address
Data []byte
GasPrice uint64
Value *big.Int
}
type FilterOpts struct {
Start uint64 // Start of the queried range
End *uint64 // End of the range (nil = latest)
}
type LogFilter struct {
BlockHash *Hash // used by eth_getLogs, return logs only from block with this hash
From *BlockNumber // beginning of the queried range, nil means genesis block
To *BlockNumber // end of the range, nil means latest block
Address []Address // restricts matches to event created by specific contracts
// The Topic list restricts matches to particular event topics. Each event has a list
// of topics. Topics matches a prefix of that list. An empty element slice matches any
// topic. Non-empty elements represent an alternative that matches any of the
// contained topics.
//
// Examples:
// {} or nil matches any topic list
// {{A}} matches topic A in first position
// {{}, {B}} matches any topic in first position AND B in second position
// {{A}, {B}} matches topic A in first position AND B in second position
// {{A, B}, {C, D}} matches topic (A OR B) in first position AND (C OR D) in second position
Topics [][]Hash
}
func (l *LogFilter) SetFromUint64(num uint64) {
b := BlockNumber(num)
l.From = &b
}
func (l *LogFilter) SetToUint64(num uint64) {
b := BlockNumber(num)
l.To = &b
}
func (l *LogFilter) SetTo(b BlockNumber) {
l.To = &b
}
type Receipt struct {
Status uint64
TransactionHash Hash
TransactionIndex uint64
ContractAddress Address
BlockHash Hash
From Address
BlockNumber uint64
GasUsed uint64
CumulativeGasUsed uint64
LogsBloom []byte
Logs []*Log
}
const (
// ReceiptStatusSuccessful is the status code of a transaction if execution succeeded.
ReceiptStatusSuccessful = uint64(1)
)
func (self *Receipt) IsReverted() bool {
return self.Status != ReceiptStatusSuccessful
}
func (self *Receipt) EnsureNoRevert() *Receipt {
if self.IsReverted() {
b, _ := self.MarshalJSON()
panic(fmt.Errorf("receipt revert: %s", b))
}
return self
}
type ThinReceipt struct {
Status uint64
TransactionHash Hash
ContractAddress Address
From Address
GasUsed uint64
Logs []*ThinLog
}
type ThinLog struct {
Address Address
Topics []Hash `json:"topics,omitempty"`
Data []byte `json:"data,omitempty"`
Event *ParsedEvent
}
func (self *Receipt) Thin() *ThinReceipt {
var logs []*ThinLog
for _, log := range self.Logs {
topic := log.Topics
data := log.Data
if log.Event != nil {
topic = nil
data = nil
}
logs = append(logs, &ThinLog{
Address: log.Address,
Topics: topic,
Data: data,
Event: log.Event,
})
}
return &ThinReceipt{
Status: self.Status,
TransactionHash: self.TransactionHash,
ContractAddress: self.ContractAddress,
From: self.From,
GasUsed: self.GasUsed,
Logs: logs,
}
}
func (self *Receipt) AddStorageLogs(logs []*StorageLog) {
for ind, log := range logs {
l := &Log{
Removed: false,
LogIndex: uint64(ind),
TransactionIndex: self.TransactionIndex,
TransactionHash: self.TransactionHash,
BlockHash: self.BlockHash,
BlockNumber: self.BlockNumber,
Address: log.Address,
Topics: log.Topics,
Data: log.Data,
}
l.ParseEvent()
self.Logs = append(self.Logs, l)
}
}
type Log struct {
Removed bool
LogIndex uint64
TransactionIndex uint64
TransactionHash Hash
BlockHash Hash
BlockNumber uint64
Address Address
Topics []Hash
Data []byte
Event *ParsedEvent
}
func (self *Log) ParseEvent() {
parsed, err := GetParser().ParseLog(self)
if err == nil {
self.Event = parsed
}
}
type StorageLog struct {
Address Address
Topics []Hash
Data []byte
}
type BlockNumber int
const (
Latest BlockNumber = -1
Earliest = -2
Pending = -3
)
func (b BlockNumber) String() string {
switch b {
case Latest:
return "latest"
case Earliest:
return "earliest"
case Pending:
return "pending"
}
if b < 0 {
panic("internal. blocknumber is negative")
}
return fmt.Sprintf("0x%x", uint64(b))
}
func (b BlockNumber) MarshalText() ([]byte, error) {
return []byte(b.String()), nil
}
func EncodeBlock(block ...BlockNumber) BlockNumber {
if len(block) != 1 {
return Latest
}
return block[0]
}
type ParsedEvent struct {
Contract string
Sig string
Values map[string]interface{}
}
// BytesToHash sets b to hash.
// If b is larger than len(h), b will be cropped from the left.
func BytesToHash(b []byte) Hash {
var h Hash
h.SetBytes(b)
return h
}
// SetBytes sets the hash to the value of b.
// If b is larger than len(h), b will be cropped from the left.
func (h *Hash) SetBytes(b []byte) {
if len(b) > len(h) {
b = b[len(b)-HashLength:]
}
copy(h[HashLength-len(b):], b)
}
func BytesToAddress(b []byte) Address {
var a Address
a.SetBytes(b)
return a
}
func CopyBytes(b []byte) (copiedBytes []byte) {
if b == nil {
return nil
}
copiedBytes = make([]byte, len(b))
copy(copiedBytes, b)
return
}
func Hex2Bytes(str string) []byte {
h, _ := hex.DecodeString(str)
return h
}