-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
models.go
618 lines (538 loc) · 16.9 KB
/
models.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
package types
import (
"bytes"
"database/sql/driver"
"encoding/json"
"fmt"
"math/big"
"regexp"
"strings"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/pkg/errors"
"github.com/ugorji/go/codec"
"github.com/smartcontractkit/chainlink-common/pkg/utils/hex"
htrktypes "github.com/smartcontractkit/chainlink/v2/common/headtracker/types"
commontypes "github.com/smartcontractkit/chainlink/v2/common/types"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/types/internal/blocks"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils"
ubig "github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils/big"
"github.com/smartcontractkit/chainlink/v2/core/null"
)
// Head represents a BlockNumber, BlockHash.
type Head struct {
ID uint64
Hash common.Hash
Number int64
L1BlockNumber null.Int64
ParentHash common.Hash
Parent *Head
EVMChainID *ubig.Big
Timestamp time.Time
CreatedAt time.Time
BaseFeePerGas *assets.Wei
ReceiptsRoot common.Hash
TransactionsRoot common.Hash
StateRoot common.Hash
Difficulty *big.Int
TotalDifficulty *big.Int
}
var _ commontypes.Head[common.Hash] = &Head{}
var _ htrktypes.Head[common.Hash, *big.Int] = &Head{}
// NewHead returns a Head instance.
func NewHead(number *big.Int, blockHash common.Hash, parentHash common.Hash, timestamp uint64, chainID *ubig.Big) Head {
return Head{
Number: number.Int64(),
Hash: blockHash,
ParentHash: parentHash,
Timestamp: time.Unix(int64(timestamp), 0),
EVMChainID: chainID,
}
}
func (h *Head) BlockNumber() int64 {
return h.Number
}
func (h *Head) BlockHash() common.Hash {
return h.Hash
}
func (h *Head) GetParentHash() common.Hash {
return h.ParentHash
}
func (h *Head) GetParent() commontypes.Head[common.Hash] {
if h.Parent == nil {
return nil
}
return h.Parent
}
func (h *Head) GetTimestamp() time.Time {
return h.Timestamp
}
func (h *Head) BlockDifficulty() *big.Int {
return h.Difficulty
}
// EarliestInChain recurses through parents until it finds the earliest one
func (h *Head) EarliestInChain() *Head {
for h.Parent != nil {
h = h.Parent
}
return h
}
// EarliestHeadInChain recurses through parents until it finds the earliest one
func (h *Head) EarliestHeadInChain() commontypes.Head[common.Hash] {
return h.EarliestInChain()
}
// IsInChain returns true if the given hash matches the hash of a head in the chain
func (h *Head) IsInChain(blockHash common.Hash) bool {
for {
if h.Hash == blockHash {
return true
}
if h.Parent != nil {
h = h.Parent
} else {
break
}
}
return false
}
// HashAtHeight returns the hash of the block at the given height, if it is in the chain.
// If not in chain, returns the zero hash
func (h *Head) HashAtHeight(blockNum int64) common.Hash {
for {
if h.Number == blockNum {
return h.Hash
}
if h.Parent != nil {
h = h.Parent
} else {
break
}
}
return common.Hash{}
}
// ChainLength returns the length of the chain followed by recursively looking up parents
func (h *Head) ChainLength() uint32 {
if h == nil {
return 0
}
l := uint32(1)
for {
if h.Parent != nil {
l++
if h == h.Parent {
panic("circular reference detected")
}
h = h.Parent
} else {
break
}
}
return l
}
// ChainHashes returns an array of block hashes by recursively looking up parents
func (h *Head) ChainHashes() []common.Hash {
var hashes []common.Hash
for {
hashes = append(hashes, h.Hash)
if h.Parent != nil {
if h == h.Parent {
panic("circular reference detected")
}
h = h.Parent
} else {
break
}
}
return hashes
}
func (h *Head) ChainID() *big.Int {
return h.EVMChainID.ToInt()
}
func (h *Head) HasChainID() bool {
return h.EVMChainID != nil
}
func (h *Head) IsValid() bool {
return h != nil
}
func (h *Head) ChainString() string {
var sb strings.Builder
for {
sb.WriteString(h.String())
if h.Parent != nil {
if h == h.Parent {
panic("circular reference detected")
}
sb.WriteString("->")
h = h.Parent
} else {
break
}
}
sb.WriteString("->nil")
return sb.String()
}
// String returns a string representation of this head
func (h *Head) String() string {
return fmt.Sprintf("Head{Number: %d, Hash: %s, ParentHash: %s}", h.ToInt(), h.Hash.Hex(), h.ParentHash.Hex())
}
// ToInt return the height as a *big.Int. Also handles nil by returning nil.
func (h *Head) ToInt() *big.Int {
if h == nil {
return nil
}
return big.NewInt(h.Number)
}
// GreaterThan compares BlockNumbers and returns true if the receiver BlockNumber is greater than
// the supplied BlockNumber
func (h *Head) GreaterThan(r *Head) bool {
if h == nil {
return false
}
if h != nil && r == nil {
return true
}
return h.Number > r.Number
}
// NextInt returns the next BlockNumber as big.int, or nil if nil to represent latest.
func (h *Head) NextInt() *big.Int {
if h == nil {
return nil
}
return new(big.Int).Add(h.ToInt(), big.NewInt(1))
}
// AsSlice returns a slice of heads up to length k
// len(heads) may be less than k if the available chain is not long enough
func (h *Head) AsSlice(k int) (heads []*Head) {
if k < 1 || h == nil {
return
}
heads = make([]*Head, 1)
heads[0] = h
for len(heads) < k && h.Parent != nil {
h = h.Parent
heads = append(heads, h)
}
return
}
func (h *Head) UnmarshalJSON(bs []byte) error {
type head struct {
Hash common.Hash `json:"hash"`
Number *hexutil.Big `json:"number"`
ParentHash common.Hash `json:"parentHash"`
Timestamp hexutil.Uint64 `json:"timestamp"`
L1BlockNumber *hexutil.Big `json:"l1BlockNumber"`
BaseFeePerGas *hexutil.Big `json:"baseFeePerGas"`
ReceiptsRoot common.Hash `json:"receiptsRoot"`
TransactionsRoot common.Hash `json:"transactionsRoot"`
StateRoot common.Hash `json:"stateRoot"`
Difficulty *hexutil.Big `json:"difficulty"`
TotalDifficulty *hexutil.Big `json:"totalDifficulty"`
}
var jsonHead head
err := json.Unmarshal(bs, &jsonHead)
if err != nil {
return err
}
if jsonHead.Number == nil {
*h = Head{}
return nil
}
h.Hash = jsonHead.Hash
h.Number = (*big.Int)(jsonHead.Number).Int64()
h.ParentHash = jsonHead.ParentHash
h.Timestamp = time.Unix(int64(jsonHead.Timestamp), 0).UTC()
h.BaseFeePerGas = assets.NewWei((*big.Int)(jsonHead.BaseFeePerGas))
if jsonHead.L1BlockNumber != nil {
h.L1BlockNumber = null.Int64From((*big.Int)(jsonHead.L1BlockNumber).Int64())
}
h.ReceiptsRoot = jsonHead.ReceiptsRoot
h.TransactionsRoot = jsonHead.TransactionsRoot
h.StateRoot = jsonHead.StateRoot
h.Difficulty = jsonHead.Difficulty.ToInt()
h.TotalDifficulty = jsonHead.TotalDifficulty.ToInt()
return nil
}
func (h *Head) MarshalJSON() ([]byte, error) {
type head struct {
Hash *common.Hash `json:"hash,omitempty"`
Number *hexutil.Big `json:"number,omitempty"`
ParentHash *common.Hash `json:"parentHash,omitempty"`
Timestamp *hexutil.Uint64 `json:"timestamp,omitempty"`
ReceiptsRoot *common.Hash `json:"receiptsRoot,omitempty"`
TransactionsRoot *common.Hash `json:"transactionsRoot,omitempty"`
StateRoot *common.Hash `json:"stateRoot,omitempty"`
Difficulty *hexutil.Big `json:"difficulty,omitempty"`
TotalDifficulty *hexutil.Big `json:"totalDifficulty,omitempty"`
}
var jsonHead head
if h.Hash != (common.Hash{}) {
jsonHead.Hash = &h.Hash
}
if h.ReceiptsRoot != (common.Hash{}) {
jsonHead.ReceiptsRoot = &h.ReceiptsRoot
}
if h.TransactionsRoot != (common.Hash{}) {
jsonHead.TransactionsRoot = &h.TransactionsRoot
}
if h.StateRoot != (common.Hash{}) {
jsonHead.StateRoot = &h.StateRoot
}
jsonHead.Number = (*hexutil.Big)(big.NewInt(h.Number))
if h.ParentHash != (common.Hash{}) {
jsonHead.ParentHash = &h.ParentHash
}
if h.Timestamp != (time.Time{}) {
t := hexutil.Uint64(h.Timestamp.UTC().Unix())
jsonHead.Timestamp = &t
}
jsonHead.Difficulty = (*hexutil.Big)(h.Difficulty)
jsonHead.TotalDifficulty = (*hexutil.Big)(h.TotalDifficulty)
return json.Marshal(jsonHead)
}
// Block represents an ethereum block
// This type is only used for the block history estimator, and can be expensive to unmarshal. Don't add unnecessary fields here.
type Block struct {
Number int64
Hash common.Hash
ParentHash common.Hash
BaseFeePerGas *assets.Wei
Timestamp time.Time
Transactions []Transaction
}
// MarshalJSON implements json marshalling for Block
func (b Block) MarshalJSON() ([]byte, error) {
bi := &blocks.BlockInternal{
Number: hexutil.EncodeBig(big.NewInt(b.Number)),
Hash: b.Hash,
ParentHash: b.ParentHash,
BaseFeePerGas: (*hexutil.Big)(b.BaseFeePerGas),
Timestamp: (hexutil.Uint64)(uint64(b.Timestamp.Unix())),
Transactions: toInternalTxnSlice(b.Transactions),
}
buf := bytes.NewBuffer(make([]byte, 0, 1024))
enc := codec.NewEncoder(buf, &codec.JsonHandle{})
err := enc.Encode(bi)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
var ErrMissingBlock = errors.New("missing block")
// UnmarshalJSON unmarshals to a Block
func (b *Block) UnmarshalJSON(data []byte) error {
var h codec.Handle = new(codec.JsonHandle)
bi := blocks.BlockInternal{}
dec := codec.NewDecoderBytes(data, h)
err := dec.Decode(&bi)
if err != nil {
return err
}
if bi.Empty() {
return errors.WithStack(ErrMissingBlock)
}
n, err := hexutil.DecodeBig(bi.Number)
if err != nil {
return errors.Wrapf(err, "failed to decode block number while unmarshalling block, got: '%s' in '%s'", bi.Number, data)
}
*b = Block{
Number: n.Int64(),
Hash: bi.Hash,
ParentHash: bi.ParentHash,
BaseFeePerGas: (*assets.Wei)(bi.BaseFeePerGas),
Timestamp: time.Unix((int64((uint64)(bi.Timestamp))), 0),
Transactions: fromInternalTxnSlice(bi.Transactions),
}
return nil
}
// thin public wrapper for internal type of the same name
// and which has to be internal for JSON un/marshal'ing code gen consistency
type TxType uint8
// Transaction represents an ethereum transaction
// Use our own type because geth's type has validation failures on e.g. zero
// gas used, which can occur on other chains.
// This type is only used for the block history estimator, and can be expensive to unmarshal. Don't add unnecessary fields here.
type Transaction struct {
GasPrice *assets.Wei `json:"gasPrice"`
GasLimit uint32 `json:"gasLimit"`
MaxFeePerGas *assets.Wei `json:"maxFeePerGas"`
MaxPriorityFeePerGas *assets.Wei `json:"maxPriorityFeePerGas"`
Type TxType `json:"type"`
Hash common.Hash `json:"hash"`
}
const LegacyTxType = blocks.TxType(0x0)
// UnmarshalJSON unmarshals a Transaction
func (t *Transaction) UnmarshalJSON(data []byte) error {
var h codec.Handle = new(codec.JsonHandle)
ti := blocks.TransactionInternal{}
dec := codec.NewDecoderBytes(data, h)
err := dec.Decode(&ti)
if err != nil {
return err
}
if ti.Gas == nil {
return errors.Errorf("expected 'gas' to not be null, got: '%s'", data)
}
if ti.Type == nil {
tpe := LegacyTxType
ti.Type = &tpe
}
*t = fromInternalTxn(ti)
return nil
}
func (t *Transaction) MarshalJSON() ([]byte, error) {
ti := toInternalTxn(*t)
buf := bytes.NewBuffer(make([]byte, 0, 256))
enc := codec.NewEncoder(buf, &codec.JsonHandle{})
err := enc.Encode(ti)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// WeiPerEth is amount of Wei currency units in one Eth.
var WeiPerEth = new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)
// ChainlinkFulfilledTopic is the signature for the event emitted after calling
// ChainlinkClient.validateChainlinkCallback(requestId). See
// ../../contracts/src/v0.6/ChainlinkClient.sol
var ChainlinkFulfilledTopic = utils.MustHash("ChainlinkFulfilled(bytes32)")
// ReceiptIndicatesRunLogFulfillment returns true if this tx receipt is the result of a
// fulfilled run log.
func ReceiptIndicatesRunLogFulfillment(txr types.Receipt) bool {
for _, log := range txr.Logs {
if log.Topics[0] == ChainlinkFulfilledTopic {
return true
}
}
return false
}
// FunctionSelector is the first four bytes of the call data for a
// function call and specifies the function to be called.
type FunctionSelector [FunctionSelectorLength]byte
// FunctionSelectorLength should always be a length of 4 as a byte.
const FunctionSelectorLength = 4
// BytesToFunctionSelector converts the given bytes to a FunctionSelector.
func BytesToFunctionSelector(b []byte) FunctionSelector {
var f FunctionSelector
f.SetBytes(b)
return f
}
// HexToFunctionSelector converts the given string to a FunctionSelector.
func HexToFunctionSelector(s string) FunctionSelector {
return BytesToFunctionSelector(common.FromHex(s))
}
// String returns the FunctionSelector as a string type.
func (f FunctionSelector) String() string { return hexutil.Encode(f[:]) }
// Bytes returns the FunctionSelector as a byte slice
func (f FunctionSelector) Bytes() []byte { return f[:] }
// SetBytes sets the FunctionSelector to that of the given bytes (will trim).
func (f *FunctionSelector) SetBytes(b []byte) { copy(f[:], b[:FunctionSelectorLength]) }
var hexRegexp = regexp.MustCompile("^[0-9a-fA-F]*$")
func unmarshalFromString(s string, f *FunctionSelector) error {
if hex.HasPrefix(s) {
if !hexRegexp.Match([]byte(s)[2:]) {
return fmt.Errorf("function selector %s must be 0x-hex encoded", s)
}
bytes := common.FromHex(s)
if len(bytes) != FunctionSelectorLength {
return errors.New("function ID must be 4 bytes in length")
}
f.SetBytes(bytes)
} else {
bytes, err := utils.Keccak256([]byte(s))
if err != nil {
return err
}
f.SetBytes(bytes[0:4])
}
return nil
}
// UnmarshalJSON parses the raw FunctionSelector and sets the FunctionSelector
// type to the given input.
func (f *FunctionSelector) UnmarshalJSON(input []byte) error {
var s string
err := json.Unmarshal(input, &s)
if err != nil {
return err
}
return unmarshalFromString(s, f)
}
// MarshalJSON returns the JSON encoding of f
func (f FunctionSelector) MarshalJSON() ([]byte, error) {
return json.Marshal(f.String())
}
// Value returns this instance serialized for database storage
func (f FunctionSelector) Value() (driver.Value, error) {
return f.Bytes(), nil
}
// Scan returns the selector from its serialization in the database
func (f *FunctionSelector) Scan(value interface{}) error {
temp, ok := value.([]byte)
if !ok {
return fmt.Errorf("unable to convent %v of type %T to FunctionSelector", value, value)
}
if len(temp) != FunctionSelectorLength {
return fmt.Errorf("function selector %v should have length %d, but has length %d",
temp, FunctionSelectorLength, len(temp))
}
copy(f[:], temp)
return nil
}
// This data can contain anything and is submitted by user on-chain, so we must
// be extra careful how we interact with it
type UntrustedBytes []byte
// SafeByteSlice returns an error on out of bounds access to a byte array, where a
// normal slice would panic instead
func (ary UntrustedBytes) SafeByteSlice(start int, end int) ([]byte, error) {
if end > len(ary) || start > end || start < 0 || end < 0 {
var empty []byte
return empty, errors.New("out of bounds slice access")
}
return ary[start:end], nil
}
// toInternalTxn converts a Transaction into the internal intermediate representation
func toInternalTxn(txn Transaction) blocks.TransactionInternal {
gas := (hexutil.Uint64)(uint64(txn.GasLimit))
itype := blocks.TxType(txn.Type)
return blocks.TransactionInternal{
GasPrice: (*hexutil.Big)(txn.GasPrice),
Gas: &gas,
MaxFeePerGas: (*hexutil.Big)(txn.MaxFeePerGas),
MaxPriorityFeePerGas: (*hexutil.Big)(txn.MaxPriorityFeePerGas),
Type: &itype,
Hash: txn.Hash,
}
}
// toInternalTxn converts a []Transaction into the internal intermediate representation
func toInternalTxnSlice(txns []Transaction) []blocks.TransactionInternal {
out := make([]blocks.TransactionInternal, len(txns))
for i, txn := range txns {
out[i] = toInternalTxn(txn)
}
return out
}
// fromInternalTxn converts an internal intermediate representation into a Transaction
func fromInternalTxn(ti blocks.TransactionInternal) Transaction {
if ti.Type == nil {
tpe := LegacyTxType
ti.Type = &tpe
}
return Transaction{
GasPrice: (*assets.Wei)(ti.GasPrice),
GasLimit: uint32(*ti.Gas),
MaxFeePerGas: (*assets.Wei)(ti.MaxFeePerGas),
MaxPriorityFeePerGas: (*assets.Wei)(ti.MaxPriorityFeePerGas),
Type: TxType(*ti.Type),
Hash: ti.Hash,
}
}
// fromInternalTxnSlice converts a slice of internal intermediate representation into a []Transaction
func fromInternalTxnSlice(tis []blocks.TransactionInternal) []Transaction {
out := make([]Transaction, len(tis))
for i, ti := range tis {
out[i] = fromInternalTxn(ti)
}
return out
}