-
Notifications
You must be signed in to change notification settings - Fork 8
/
estimator.go
640 lines (534 loc) · 17.8 KB
/
estimator.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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
package sequence
import (
"context"
"crypto/rand"
"fmt"
"math/big"
"sort"
"strings"
"sync"
"time"
"github.com/0xsequence/ethkit/ethcoder"
"github.com/0xsequence/ethkit/ethcontract"
"github.com/0xsequence/ethkit/ethrpc"
"github.com/0xsequence/ethkit/go-ethereum/accounts/abi"
"github.com/0xsequence/ethkit/go-ethereum/common"
"github.com/0xsequence/ethkit/go-ethereum/common/hexutil"
"github.com/0xsequence/go-sequence/contracts"
"github.com/0xsequence/go-sequence/contracts/gen/walletgasestimator"
"github.com/0xsequence/go-sequence/core"
v1 "github.com/0xsequence/go-sequence/core/v1"
v2 "github.com/0xsequence/go-sequence/core/v2"
"github.com/goware/cachestore"
"github.com/goware/cachestore/memlru"
)
const (
defaultEstimatorCacheSize = 500
areEOAsMaxConcurrentTasks = 10
)
var (
// byte values that represent booleans in the cache.
cachedTrue = byte('t')
cachedFalse = byte('f')
)
type CallOverride struct {
Code string `json:"code"`
Balance *big.Int `json:"balance"`
Nonce *big.Int `json:"nonce"`
StateDiff []*StateOverride `json:"stateDiff"`
State []*StateOverride `json:"state"`
}
type StateOverride struct {
Key string
Value string
}
type EstimateTransaction struct {
From common.Address
To common.Address
Data []byte
}
type Estimator struct {
BaseCost uint64
DataOneCost uint64
DataZeroCost uint64
cache cachestore.Store[[]byte]
}
type SimulateResult walletgasestimator.MainModuleGasEstimationSimulateResult
var defaultEstimator = &Estimator{
BaseCost: 21000,
DataOneCost: 16,
DataZeroCost: 4,
}
var gasEstimatorCode = hexutil.Encode(contracts.GasEstimator.DeployedBin)
var walletGasEstimatorCode = hexutil.Encode(contracts.WalletGasEstimator.DeployedBin)
var walletGasEstimatorCodeV2 = hexutil.Encode(contracts.V2.WalletGasEstimator.DeployedBin)
func NewEstimator() *Estimator {
defaultCache, _ := memlru.NewWithSize[[]byte](defaultEstimatorCacheSize)
return &Estimator{
BaseCost: defaultEstimator.BaseCost,
DataZeroCost: defaultEstimator.DataZeroCost,
DataOneCost: defaultEstimator.DataOneCost,
cache: defaultCache,
}
}
func (e *Estimator) SetCache(cache cachestore.Store[[]byte]) *Estimator {
e.cache = cache
return e
}
func (e *Estimator) CalldataCost(data []byte) uint64 {
cost := e.BaseCost
for _, b := range data {
if b == 0 {
cost += e.DataZeroCost
} else {
cost += e.DataOneCost
}
}
return cost
}
// BuildProxy for address based on https://eips.ethereum.org/EIPS/eip-1167
// the bytecode contains an aditional SLOAD to mimic the Sequence proxies
// bytecode:
//
// | 0x00000000 36 calldatasize cds
// | 0x00000001 3d returndatasize 0 cds
// | 0x00000002 3d returndatasize 0 0 cds
// | 0x00000003 37 calldatacopy
// | 0x00000004 30 address addr
// | 0x00000005 54 sload stub
// | 0x00000006 50 pop
// | 0x00000007 3d returndatasize 0
// | 0x00000008 3d returndatasize 0 0
// | 0x00000009 3d returndatasize 0 0 0
// | 0x0000000a 36 calldatasize cds 0 0 0
// | 0x0000000b 3d returndatasize 0 cds 0 0 0
// | 0x0000000c 73bebebebebe. push20 0xbebebebe 0xbebe 0 cds 0 0 0
// | 0x00000020 5a gas gas 0xbebe 0 cds 0 0 0
// | 0x00000021 f4 delegatecall suc 0
// | 0x00000022 3d returndatasize rds suc 0
// | 0x00000023 82 dup3 0 rds suc 0
// | 0x00000024 80 dup1 0 0 rds suc 0
// | 0x00000025 3e returndatacopy suc 0
// | 0x00000026 90 swap1 0 suc
// | 0x00000027 3d returndatasize rds 0 suc
// | 0x00000028 91 swap2 suc 0 rds
// | 0x00000029 602d push1 0x2e 0x2e suc 0 rds
// | ,=< 0x0000002b 57 jumpi 0 rds
// | | 0x0000002c fd revert
// | `-> 0x0000002d 5b jumpdest 0 rds
// \ 0x0000002e f3 return
func BuildProxy(addr common.Address) string {
return "0x363d3d3730543d3d3d363d73" + strings.Replace(addr.String(), "0x", "", 1) + "5af43d82803e903d91602d57fd5bf3"
}
func (e *Estimator) EstimateCall(ctx context.Context, provider *ethrpc.Provider, call *EstimateTransaction, overrides map[common.Address]*CallOverride, blockTag string) (*big.Int, error) {
if blockTag == "" {
blockTag = "latest"
}
from := call.From
if from == (common.Address{}) {
from = stubAddress()
}
finalOverrides := map[common.Address]*CallOverride{
from: {Code: gasEstimatorCode},
}
if overrides != nil {
for key, value := range overrides {
if key == from {
return nil, fmt.Errorf("can't override address from")
}
finalOverrides[key] = value
}
}
estimator := ethcontract.NewContractCaller(from, contracts.GasEstimator.ABI, provider)
callData, err := estimator.Encode("estimate", call.To, call.Data)
if err != nil {
return nil, err
}
type Call struct {
To common.Address `json:"to"`
Data string `json:"data"`
}
estimateCall := &Call{
To: from,
Data: "0x" + common.Bytes2Hex(callData),
}
var res string
rpcCall := ethrpc.NewCallBuilder[string]("eth_call", nil, estimateCall, blockTag, finalOverrides)
_, err = provider.Do(context.Background(), rpcCall.Into(&res))
if err != nil {
return nil, err
}
resBytes := common.Hex2Bytes(strings.Replace(res, "0x", "", 1))
var success bool
var result []byte
var gas *big.Int
if err := ethcoder.AbiDecoder([]string{"bool", "bytes", "uint256"}, resBytes, []interface{}{&success, &result, &gas}); err != nil {
return nil, err
}
gas.Add(gas, big.NewInt(int64(e.CalldataCost(call.Data))))
if !success {
reason, err := abi.UnpackRevert(result)
if err == nil {
return gas, fmt.Errorf("gas usage simulation failed: %v", reason)
} else {
return gas, fmt.Errorf("gas usage simulation failed: %v", hexutil.Encode(result))
}
}
return gas, nil
}
func (e *Estimator) AreEOAs(ctx context.Context, provider *ethrpc.Provider, walletConfig core.WalletConfig) (map[common.Address]bool, error) {
res := make(map[common.Address]bool, len(walletConfig.Signers()))
// Get non-eoa signers
// required for computing worse case scenario
var wg sync.WaitGroup
errCh := make(chan error)
workersCh := make(chan struct{}, areEOAsMaxConcurrentTasks)
var mutex sync.Mutex
for address := range walletConfig.Signers() {
wg.Add(1)
select {
case <-ctx.Done():
return nil, ctx.Err()
case err := <-errCh:
return nil, err
case workersCh <- struct{}{}: // wait until a worker slot becomes available to continue
}
go func(ctx context.Context, address common.Address) {
defer func() {
wg.Done()
<-workersCh // release the worker
}()
var err error
isEOA, err := e.isEOA(ctx, provider, address)
if err != nil {
errCh <- err
return
}
mutex.Lock()
res[address] = isEOA
mutex.Unlock()
}(ctx, address)
}
wg.Wait()
return res, nil
}
func (e *Estimator) isEOA(ctx context.Context, provider *ethrpc.Provider, address common.Address) (bool, error) {
ctx, cancel := context.WithTimeout(ctx, 25*time.Second)
defer cancel()
chainID, err := provider.ChainID(ctx)
if err != nil {
return false, err
}
key := fmt.Sprintf("isEOA::%d::%v", chainID, address)
if val, exists, _ := e.cache.Get(ctx, key); exists {
// we have recorded data for this key, let's use it
return val[0] == cachedTrue, nil
}
code, err := provider.CodeAt(ctx, address, nil)
if err != nil {
return false, err
}
if len(code) == 0 {
// if the address does not contain a smart contract, then it's an EOA
_ = e.cache.Set(ctx, key, []byte{cachedTrue})
return true, nil
}
_ = e.cache.Set(ctx, key, []byte{cachedFalse})
return false, nil
}
func (e *Estimator) PickSigners(ctx context.Context, walletConfig core.WalletConfig, isEoa map[common.Address]bool) (map[common.Address]bool, error) {
type SortedSigner struct {
s *v1.WalletConfigSigner
i int
}
// Create a copy of the signers array
// this will be sorted and used to pick the worst case scenario for the signers
signersMap := walletConfig.Signers()
sortedSigners := make([]*v1.WalletConfigSigner, 0, len(signersMap))
for signer, weight := range signersMap {
sortedSigners = append(sortedSigners, &v1.WalletConfigSigner{
Weight: uint8(weight),
Address: signer,
})
}
sort.SliceStable(sortedSigners, func(a, b int) bool {
if !isEoa[sortedSigners[a].Address] && isEoa[sortedSigners[b].Address] {
return true
} else if isEoa[sortedSigners[a].Address] && !isEoa[sortedSigners[b].Address] {
return false
}
return sortedSigners[a].Weight < sortedSigners[b].Weight
})
weightSum := 0
// Define what signers are goint go be signing
// it should construct a worse case scenario for the signature
willSign := make(map[common.Address]bool, len(walletConfig.Signers()))
threshold := int(walletConfig.Threshold())
// Pick signers until we reach the threshold we stop
// We use the sorted signers to get the ones with the non EOA and with lowest weight first
for _, s := range sortedSigners {
if weightSum >= threshold {
willSign[s.Address] = false
} else {
weightSum += int(s.Weight)
willSign[s.Address] = true
}
}
return willSign, nil
}
func stubAddress() common.Address {
raw := make([]byte, 20)
rand.Read(raw)
return common.BytesToAddress(raw)
}
func (e *Estimator) BuildStubSignature(walletConfig core.WalletConfig, willSign, isEoa map[common.Address]bool) []byte {
// pre-determined signature, tailored for worse-case scenario in gas costs
// TODO: Compute average siganture and present a more likely scenario for a more close estimation
sig := common.Hex2Bytes("1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a01b02")
stubSigner := func(ctx context.Context, signer common.Address, signatures []core.SignerSignature) (core.SignerSignatureType, []byte, error) {
if willSign[signer] {
if isEoa[signer] {
return core.SignerSignatureTypeEthSign, sig, nil
} else {
address1 := stubAddress()
address2 := stubAddress()
address3 := stubAddress()
var sig []byte
if _, ok := walletConfig.(*v1.WalletConfig); ok {
sig = e.BuildStubSignature(&v1.WalletConfig{
Threshold_: 2,
Signers_: v1.WalletConfigSigners{
{
Address: address1,
Weight: 1,
},
{
Address: address2,
Weight: 1,
},
{
Address: address3,
Weight: 1,
},
},
},
map[common.Address]bool{address1: false, address2: true, address3: true},
map[common.Address]bool{address1: true, address2: true, address3: true})
} else if _, ok := walletConfig.(*v2.WalletConfig); ok {
sig = e.BuildStubSignature(&v2.WalletConfig{
Threshold_: 2,
Tree: v2.WalletConfigTreeNodes(
&v2.WalletConfigTreeAddressLeaf{
Address: address1,
Weight: 1,
},
&v2.WalletConfigTreeAddressLeaf{
Address: address2,
Weight: 1,
},
&v2.WalletConfigTreeAddressLeaf{
Address: address3,
Weight: 1,
},
),
},
map[common.Address]bool{address1: false, address2: true, address3: true},
map[common.Address]bool{address1: true, address2: true, address3: true})
}
return core.SignerSignatureTypeEIP1271, sig, nil
}
} else {
return 0, nil, core.ErrSigningNoSigner
}
}
if confV1, ok := walletConfig.(*v1.WalletConfig); ok {
sigV1, err := confV1.BuildSignature(context.Background(), stubSigner, false)
if err != nil {
return nil
}
encoded, err := sigV1.Data()
if err != nil {
return nil
}
return encoded
} else if confV2, ok := walletConfig.(*v2.WalletConfig); ok {
sigV2, err := confV2.BuildRegularSignature(context.Background(), stubSigner, false)
if err != nil {
return nil
}
encoded, err := sigV2.Data()
if err != nil {
return nil
}
return encoded
} else {
return nil
}
}
func (e *Estimator) Estimate(ctx context.Context, provider *ethrpc.Provider, address common.Address, walletConfig core.WalletConfig, walletContext WalletContext, txs Transactions) (uint64, error) {
isEOA, err := e.AreEOAs(ctx, provider, walletConfig)
if err != nil {
return 0, err
}
willSign, err := e.PickSigners(ctx, walletConfig, isEOA)
if err != nil {
return 0, err
}
signature := e.BuildStubSignature(walletConfig, willSign, isEOA)
var overrides map[common.Address]*CallOverride
if _, ok := walletConfig.(*v1.WalletConfig); ok {
overrides = map[common.Address]*CallOverride{
walletContext.MainModuleAddress: {Code: walletGasEstimatorCode},
walletContext.MainModuleUpgradableAddress: {Code: walletGasEstimatorCode},
}
} else if _, ok := walletConfig.(*v2.WalletConfig); ok {
overrides = map[common.Address]*CallOverride{
walletContext.MainModuleAddress: {Code: walletGasEstimatorCodeV2},
walletContext.MainModuleUpgradableAddress: {Code: walletGasEstimatorCodeV2},
}
} else {
return 0, fmt.Errorf("unknown wallet config type")
}
isDeployed, err := IsWalletDeployed(provider, address)
if err != nil {
return 0, err
}
if !isDeployed {
overrides[address] = &CallOverride{
Code: BuildProxy(walletContext.MainModuleAddress),
}
}
estimates := make([]*big.Int, len(txs)+1)
// The nonce is ignored by the MainModuleGasEstimator
// so we use a stub nonce takes at least 4 bytes
nonce := big.NewInt(4294967295)
// Compute gas estimation for slices of all transactions
// including no transaction execution and all transactions
for i := range estimates {
subTxs := txs[0:i]
encTxs, err := subTxs.EncodedTransactions()
if err != nil {
return 0, err
}
var execData []byte
if _, ok := walletConfig.(*v1.WalletConfig); ok {
execData, err = contracts.WalletMainModule.Encode("execute", encTxs, nonce, signature)
if err != nil {
return 0, err
}
} else if _, ok := walletConfig.(*v2.WalletConfig); ok {
execData, err = contracts.V2.WalletMainModule.Encode("execute", encTxs, nonce, signature)
if err != nil {
return 0, err
}
}
estimated, err := e.EstimateCall(ctx, provider, &EstimateTransaction{
To: address,
Data: execData,
}, overrides, "")
if err != nil {
return 0, err
}
estimates[i] = estimated
}
// Apply gas limits to all transactions
for i := range txs {
txs[i].GasLimit = big.NewInt(0).Sub(estimates[i+1], estimates[i])
}
return estimates[len(estimates)-1].Uint64(), nil
}
func V1Simulate(provider *ethrpc.Provider, wallet common.Address, transactions Transactions, block string, overrides map[common.Address]*CallOverride) ([]SimulateResult, error) {
if block == "" {
block = "latest"
}
encoded, err := transactions.EncodedTransactions()
if err != nil {
return nil, err
}
callData, err := contracts.WalletGasEstimator.Encode("simulateExecute", encoded)
if err != nil {
return nil, err
}
type ethCallParams struct {
To common.Address `json:"to"`
Data string `json:"data"`
}
params := ethCallParams{
To: wallet,
Data: hexutil.Encode(callData),
}
allOverrides := map[common.Address]*CallOverride{
wallet: {Code: walletGasEstimatorCode},
}
for address, override := range overrides {
if address == wallet {
return nil, fmt.Errorf("cannot override wallet address %v", wallet.Hex())
}
allOverrides[address] = override
}
var response string
rpcCall := ethrpc.NewCallBuilder[string]("eth_call", nil, params, block, allOverrides)
_, err = provider.Do(context.Background(), rpcCall.Into(&response))
if err != nil {
return nil, err
}
resultsData, err := hexutil.Decode(response)
if err != nil {
return nil, err
}
var results []SimulateResult
err = contracts.WalletGasEstimator.Decode(&results, "simulateExecute", resultsData)
if err != nil {
return nil, err
}
return results, nil
}
func V2Simulate(provider *ethrpc.Provider, wallet common.Address, transactions Transactions, block string, overrides map[common.Address]*CallOverride) ([]SimulateResult, error) {
if block == "" {
block = "latest"
}
encoded, err := transactions.EncodedTransactions()
if err != nil {
return nil, err
}
callData, err := contracts.V2.WalletGasEstimator.Encode("simulateExecute", encoded)
if err != nil {
return nil, err
}
type ethCallParams struct {
To common.Address `json:"to"`
Data string `json:"data"`
}
params := ethCallParams{
To: wallet,
Data: hexutil.Encode(callData),
}
allOverrides := map[common.Address]*CallOverride{
wallet: {Code: walletGasEstimatorCodeV2},
}
for address, override := range overrides {
if address == wallet {
return nil, fmt.Errorf("cannot override wallet address %v", wallet.Hex())
}
allOverrides[address] = override
}
var response string
rpcCall := ethrpc.NewCallBuilder[string]("eth_call", nil, params, block, allOverrides)
_, err = provider.Do(context.Background(), rpcCall.Into(&response))
if err != nil {
return nil, err
}
resultsData, err := hexutil.Decode(response)
if err != nil {
return nil, err
}
var results []SimulateResult
err = contracts.V2.WalletGasEstimator.Decode(&results, "simulateExecute", resultsData)
if err != nil {
return nil, err
}
return results, nil
}
func Simulate(provider *ethrpc.Provider, wallet common.Address, transactions Transactions, block string, overrides map[common.Address]*CallOverride) ([]SimulateResult, error) {
return V2Simulate(provider, wallet, transactions, block, overrides)
}