-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheth_client.go
305 lines (254 loc) · 6.41 KB
/
eth_client.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
package edb
import (
"context"
"fmt"
"math/big"
"github.com/aj3423/edb/util"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/holiman/uint256"
"github.com/pkg/errors"
)
func get_online_block_hash(
client *ethclient.Client,
blockNum uint64,
) (common.Hash, error) {
var hash common.Hash
if client == nil {
return hash, fmt.Errorf("no block hash for: %d", blockNum)
}
// color.Blue("get block hash: %d", blockNum)
block, e := client.BlockByNumber(
context.Background(),
new(big.Int).SetUint64(blockNum),
)
if e != nil {
return hash, e
}
hash = block.Hash()
// color.Green("value: %s", hash.Hex())
return hash, nil
}
func get_online_storage(
client *ethclient.Client,
address common.Address,
slot *uint256.Int,
blockNum uint64,
) (*uint256.Int, error) {
if client == nil {
return nil, fmt.Errorf("no storage for: %s", address.String())
}
// color.Blue("get storage slot: %s, contract: %s",
// slot.String(), address.String())
if address == util.ZeroAddress || blockNum == 0 {
return nil, errors.New("invalid AddressThis or Block.Number")
}
bs, e := client.StorageAt(
context.Background(),
address,
common.BigToHash(slot.ToBig()),
new(big.Int).SetUint64(blockNum),
)
if e != nil {
return nil, e
}
val := uint256.NewInt(0).SetBytes(bs)
// color.Green("value: %s", val.Hex())
return val, nil
}
func get_online_code(
client *ethclient.Client,
address common.Address,
blockNum uint64,
) ([]byte, error) {
if client == nil {
return nil, fmt.Errorf("no code for: %s", address.String())
}
// color.Blue("get contract code, address: %s, blockNum: %d",
// address.Hex(), blockNum)
if address == util.ZeroAddress || blockNum == 0 {
return nil, errors.New("invalid AddressThis or Block.Number")
}
code, e := client.CodeAt(
context.Background(), address, big.NewInt(int64(blockNum)))
if e != nil {
return nil, e
}
// color.Green("got %d bytes of code", len(code))
return code, nil
}
func get_online_balance(
client *ethclient.Client,
address common.Address,
blockNum uint64,
) (*big.Int, error) {
if client == nil {
return nil, fmt.Errorf("no balance for: %s", address.String())
}
// color.Blue("get balance, address: %s",
// address.Hex())
if address == util.ZeroAddress || blockNum == 0 {
return nil, errors.New("invalid AddressThis or Block.Number")
}
bal, e := client.BalanceAt(context.Background(), address, big.NewInt(int64(blockNum)))
if e != nil {
return nil, e
}
// color.Green("blance: %s", bal.String())
return bal, nil
}
func ContextFromTx(
node_url string,
tx_hash string,
) (*Context, error) {
client, e := ethclient.Dial(node_url)
if e != nil {
return nil, e
}
chain_id, e := client.ChainID(context.Background())
if e != nil {
return nil, e
}
tx, _, e := client.TransactionByHash(context.Background(), common.HexToHash(tx_hash))
if e != nil {
return nil, e
}
receipt, e := client.TransactionReceipt(context.Background(), common.HexToHash(tx_hash))
if e != nil {
return nil, e
}
msg, e := tx.AsMessage(types.NewLondonSigner(big.NewInt(int64(chain_id.Uint64()))), nil)
if e != nil {
return nil, e
}
blockNum := receipt.BlockNumber
block, e := client.BlockByNumber(context.Background(), blockNum)
if e != nil {
return nil, e
}
ctx := NewContext()
ctx.ethClient = client
ctx.Tx = Tx{
Hash: tx.Hash(),
Origin: msg.From(),
GasPrice: tx.GasPrice().Uint64(),
}
baseFee := big.NewInt(0)
if block.BaseFee() != nil {
baseFee = block.BaseFee()
}
ctx.Block = Block{
Number: block.NumberU64(),
Timestamp: block.Time(),
Difficulty: block.Difficulty().Uint64(),
Coinbase: block.Coinbase(),
GasLimit: block.GasLimit(),
BaseFee: baseFee.Uint64(),
}
ctx.BlockHashes[block.NumberU64()] = block.Hash()
ctx.Chain = Chain{
Id: chain_id.Uint64(),
NodeUrl: node_url,
}
to := *tx.To()
ctx.Call().This = to
ctx.Call().Msg = Msg{
Data: tx.Data(),
Gas: tx.Gas(),
Sender: msg.From(),
Value: msg.Value(),
}
_, e = ensure_code(ctx, to)
if e != nil {
return nil, e
}
return ctx, nil
}
// get *Contract at addr, create if not exists
func ensure_contract_at(ctx *Context, addr common.Address) *Contract {
contract, ok := ctx.Contracts[addr]
if !ok {
contract = NewContract()
ctx.Contracts[addr] = contract
}
return contract
}
// get from local map first
// fetch online if not exists
func ensure_balance(ctx *Context, address common.Address) (*big.Int, error) {
contract := ensure_contract_at(ctx, address)
if contract.Balance != nil { // if code exists in local cache
return contract.Balance, nil
}
bal, e := get_online_balance(ctx.ethClient, address, ctx.Block.Number-1) // block - 1
if e != nil {
return nil, e
}
// cache it
contract.Balance = bal
return bal, nil
}
// get from local map first
// fetch online if not exists
func ensure_code(ctx *Context, address common.Address) ([]byte, error) {
var binary []byte
contract := ensure_contract_at(ctx, address)
if len(contract.Code.Binary) > 0 { // if code exists in local cache
return contract.Code.Binary, nil
}
var e error
binary, e = get_online_code(ctx.ethClient, address, ctx.Block.Number)
if e != nil {
return nil, e
}
// cache it
if e := contract.Code.Set(binary); e != nil {
return nil, e
}
return binary, nil
}
// get from local map first
// fetch online if not exists
func ensure_storage(
ctx *Context,
address common.Address,
slot *uint256.Int,
) (*uint256.Int, error) {
contract := ensure_contract_at(ctx, address)
slotHash := common.BigToHash(slot.ToBig())
val, ok := contract.Storage[slotHash]
if ok { // if code exists in local cache
return val, nil
}
var e error
// from archive node we only get storage values after the block executed
// so we need to query block-1 to get the storage before executed
val, e = get_online_storage(
ctx.ethClient, address, slot, ctx.Block.Number-1) // block.number-1
if e != nil {
return nil, e
}
// cache it
contract.Storage[slotHash] = val
return val, nil
}
// get from local map first
// fetch online if not exists
func ensure_block_hash(
ctx *Context,
blockNum uint64,
) (common.Hash, error) {
hash, ok := ctx.BlockHashes[blockNum]
if ok { // if code exists in local cache
return hash, nil
}
var e error
hash, e = get_online_block_hash(ctx.ethClient, blockNum)
if e != nil {
return hash, e
}
// cache it
ctx.BlockHashes[blockNum] = hash
return hash, nil
}