-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
models.go
368 lines (326 loc) · 9.74 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
package types
import (
"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/smartcontractkit/chainlink/core/null"
"github.com/smartcontractkit/chainlink/core/utils"
)
// Head represents a BlockNumber, BlockHash.
type Head struct {
ID uint64
Hash common.Hash
Number int64
L1BlockNumber null.Int64
ParentHash common.Hash
Parent *Head
EVMChainID *utils.Big
Timestamp time.Time
CreatedAt time.Time
BaseFeePerGas *utils.Big
ReceiptsRoot common.Hash
TransactionsRoot common.Hash
StateRoot common.Hash
}
// NewHead returns a Head instance.
func NewHead(number *big.Int, blockHash common.Hash, parentHash common.Hash, timestamp uint64, chainID *utils.Big) Head {
return Head{
Number: number.Int64(),
Hash: blockHash,
ParentHash: parentHash,
Timestamp: time.Unix(int64(timestamp), 0),
EVMChainID: chainID,
}
}
// EarliestInChain recurses through parents until it finds the earliest one
func (h *Head) EarliestInChain() *Head {
for h.Parent != nil {
h = h.Parent
}
return h
}
// 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) 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))
}
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"`
}
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 = (*utils.Big)(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
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"`
}
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(int64(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
}
return json.Marshal(jsonHead)
}
// 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 utils.HasHexPrefix(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
}