/
helper.go
241 lines (195 loc) · 5.73 KB
/
helper.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
package jsonrpc
import (
"errors"
"fmt"
"math/big"
"github.com/0xPolygon/polygon-edge/types"
)
var (
ErrHeaderNotFound = errors.New("header not found")
ErrLatestNotFound = errors.New("latest header not found")
ErrNegativeBlockNumber = errors.New("invalid argument 0: block number must not be negative")
ErrFailedFetchGenesis = errors.New("error fetching genesis block header")
ErrNoDataInContractCreation = errors.New("contract creation without data provided")
)
type latestHeaderGetter interface {
Header() *types.Header
}
// GetNumericBlockNumber returns block number based on current state or specified number
func GetNumericBlockNumber(number BlockNumber, store latestHeaderGetter) (uint64, error) {
switch number {
case LatestBlockNumber, PendingBlockNumber:
latest := store.Header()
if latest == nil {
return 0, ErrLatestNotFound
}
return latest.Number, nil
case EarliestBlockNumber:
return 0, nil
default:
if number < 0 {
return 0, ErrNegativeBlockNumber
}
return uint64(number), nil
}
}
type headerGetter interface {
Header() *types.Header
GetHeaderByNumber(uint64) (*types.Header, bool)
}
// GetBlockHeader returns a header using the provided number
func GetBlockHeader(number BlockNumber, store headerGetter) (*types.Header, error) {
switch number {
case PendingBlockNumber, LatestBlockNumber:
return store.Header(), nil
case EarliestBlockNumber:
header, ok := store.GetHeaderByNumber(uint64(0))
if !ok {
return nil, ErrFailedFetchGenesis
}
return header, nil
default:
// Convert the block number from hex to uint64
header, ok := store.GetHeaderByNumber(uint64(number))
if !ok {
return nil, fmt.Errorf("error fetching block number %d header", uint64(number))
}
return header, nil
}
}
type txLookupAndBlockGetter interface {
ReadTxLookup(types.Hash) (types.Hash, bool)
GetBlockByHash(types.Hash, bool) (*types.Block, bool)
}
// GetTxAndBlockByTxHash returns the tx and the block including the tx by given tx hash
func GetTxAndBlockByTxHash(txHash types.Hash, store txLookupAndBlockGetter) (*types.Transaction, *types.Block) {
blockHash, ok := store.ReadTxLookup(txHash)
if !ok {
return nil, nil
}
block, ok := store.GetBlockByHash(blockHash, true)
if !ok {
return nil, nil
}
if txn, _ := types.FindTxByHash(block.Transactions, txHash); txn != nil {
return txn, block
}
return nil, nil
}
type blockGetter interface {
Header() *types.Header
GetHeaderByNumber(uint64) (*types.Header, bool)
GetBlockByHash(types.Hash, bool) (*types.Block, bool)
}
func GetHeaderFromBlockNumberOrHash(bnh BlockNumberOrHash, store blockGetter) (*types.Header, error) {
// The filter is empty, use the latest block by default
if bnh.BlockNumber == nil && bnh.BlockHash == nil {
bnh.BlockNumber, _ = createBlockNumberPointer(latest)
}
if bnh.BlockNumber != nil {
// block number
header, err := GetBlockHeader(*bnh.BlockNumber, store)
if err != nil {
return nil, fmt.Errorf("failed to get the header of block %d: %w", *bnh.BlockNumber, err)
}
return header, nil
}
// block hash
block, ok := store.GetBlockByHash(*bnh.BlockHash, false)
if !ok {
return nil, fmt.Errorf("could not find block referenced by the hash %s", bnh.BlockHash.String())
}
return block.Header, nil
}
type nonceGetter interface {
Header() *types.Header
GetHeaderByNumber(uint64) (*types.Header, bool)
GetNonce(types.Address) uint64
GetAccount(root types.Hash, addr types.Address) (*Account, error)
}
func GetNextNonce(address types.Address, number BlockNumber, store nonceGetter) (uint64, error) {
if number == PendingBlockNumber {
// Grab the latest pending nonce from the TxPool
// If the account is not initialized in the local TxPool,
// return the latest nonce from the world state
return store.GetNonce(address), nil
}
header, err := GetBlockHeader(number, store)
if err != nil {
return 0, err
}
acc, err := store.GetAccount(header.StateRoot, address)
//nolint:govet
if errors.Is(err, ErrStateNotFound) {
// If the account doesn't exist / isn't initialized,
// return a nonce value of 0
return 0, nil
} else if err != nil {
return 0, err
}
return acc.Nonce, nil
}
func DecodeTxn(arg *txnArgs, blockNumber uint64, store nonceGetter, forceSetNonce bool) (*types.Transaction, error) {
if arg == nil {
return nil, errors.New("missing value for required argument 0")
}
// set default values
if arg.From == nil {
arg.From = &types.ZeroAddress
arg.Nonce = argUintPtr(0)
} else if arg.Nonce == nil || forceSetNonce {
// get nonce from the pool
nonce, err := GetNextNonce(*arg.From, LatestBlockNumber, store)
if err != nil {
return nil, err
}
arg.Nonce = argUintPtr(nonce)
}
if arg.Value == nil {
arg.Value = argBytesPtr([]byte{})
}
if arg.GasPrice == nil {
arg.GasPrice = argBytesPtr([]byte{})
}
if arg.GasTipCap == nil {
arg.GasTipCap = argBytesPtr([]byte{})
}
if arg.GasFeeCap == nil {
arg.GasFeeCap = argBytesPtr([]byte{})
}
var input []byte
if arg.Data != nil {
input = *arg.Data
} else if arg.Input != nil {
input = *arg.Input
}
if arg.To == nil && input == nil {
return nil, ErrNoDataInContractCreation
}
if input == nil {
input = []byte{}
}
if arg.Gas == nil {
arg.Gas = argUintPtr(0)
}
txType := types.LegacyTx
if arg.Type != nil {
txType = types.TxType(*arg.Type)
}
txn := &types.Transaction{
From: *arg.From,
Gas: uint64(*arg.Gas),
GasPrice: new(big.Int).SetBytes(*arg.GasPrice),
GasTipCap: new(big.Int).SetBytes(*arg.GasTipCap),
GasFeeCap: new(big.Int).SetBytes(*arg.GasFeeCap),
Value: new(big.Int).SetBytes(*arg.Value),
Input: input,
Nonce: uint64(*arg.Nonce),
Type: txType,
}
if arg.To != nil {
txn.To = arg.To
}
txn.ComputeHash(blockNumber)
return txn, nil
}