-
Notifications
You must be signed in to change notification settings - Fork 1
/
state.go
750 lines (651 loc) · 19.2 KB
/
state.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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/filecoin-project/go-state-types/big"
lotusbs "github.com/filecoin-project/lotus/blockstore"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/types/ethtypes"
"github.com/hashicorp/go-multierror"
leveldb "github.com/ipfs/go-ds-leveldb"
exchange "github.com/ipfs/go-ipfs-exchange-interface"
ipld "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-verifcid"
carbs "github.com/ipld/go-car/v2/blockstore"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/network"
"sync"
"sync/atomic"
"time"
filaddr "github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
filbig "github.com/filecoin-project/go-state-types/big"
lchadt "github.com/filecoin-project/lotus/chain/actors/adt"
lbi "github.com/filecoin-project/lotus/chain/actors/builtin"
lbimsig "github.com/filecoin-project/lotus/chain/actors/builtin/multisig"
lchstmgr "github.com/filecoin-project/lotus/chain/stmgr"
lchtypes "github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/go-hamt-ipld/v3"
"github.com/filecoin-project/go-state-types/builtin"
"github.com/filecoin-project/go-state-types/builtin/v10/util/adt"
cbg "github.com/whyrusleeping/cbor-gen"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
blockstore "github.com/ipfs/go-ipfs-blockstore"
ipldcbor "github.com/ipfs/go-ipld-cbor"
"golang.org/x/sync/errgroup"
"golang.org/x/xerrors"
)
type countingBlockstore struct {
blockstore.Blockstore
mx sync.Mutex
m map[cid.Cid]int
orderedCids []cid.Cid
}
func (c *countingBlockstore) Get(ctx context.Context, cid cid.Cid) (blocks.Block, error) {
blk, err := c.Blockstore.Get(ctx, cid)
if err != nil {
return nil, err
}
c.mx.Lock()
_, found := c.m[cid]
c.m[cid] = len(blk.RawData())
if !found {
c.orderedCids = append(c.orderedCids, cid)
}
c.mx.Unlock()
return blk, nil
}
var _ blockstore.Blockstore = (*countingBlockstore)(nil)
func getStateFromCar(ctx context.Context, srcSnapshot string) (blockstore.Blockstore, lchtypes.TipSetKey, error) {
start := time.Now()
defer func() {
fmt.Printf("duration: %v\n", time.Since(start))
}()
carbs, err := blockstoreFromSnapshot(ctx, srcSnapshot)
if err != nil {
return nil, lchtypes.EmptyTSK, err
}
fmt.Printf("duration to load snapshot: %v\n", time.Since(start))
var tsk lchtypes.TipSetKey
carRoots, err := carbs.Roots()
if err != nil {
return nil, lchtypes.EmptyTSK, err
}
tsk = lchtypes.NewTipSetKey(carRoots...)
return carbs, tsk, nil
}
func getStateDynamicallyLoadedFromBitswap(ctx context.Context) (blockstore.Blockstore, error) {
start := time.Now()
defer func() {
fmt.Printf("duration: %v\n", time.Since(start))
}()
h, err := libp2p.New(libp2p.ResourceManager(&network.NullResourceManager{}))
if err != nil {
return nil, err
}
bscFil, err := setupFilContentFetching(h, ctx)
if err != nil {
return nil, err
}
bscIpfs, err := setupIPFSContentFetching(h, ctx)
if err != nil {
return nil, err
}
lds, err := leveldb.NewDatastore("", nil)
if err != nil {
return nil, err
}
baseBstore := blockstore.NewBlockstore(lds)
cf := &combineFetcher{
0, 0,
bscFil.NewSession(ctx),
bscIpfs.NewSession(ctx),
}
bs := blockstore.NewIdStore(&bservBstoreWrapper{Blockstore: baseBstore, bserv: cf})
go func() {
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
peers := h.Network().Peers()
nPeersWithFilBitswap := 0
nPeersWithIpfsBitswap := 0
for _, p := range peers {
const ipfsBs = "/ipfs/bitswap/1.2.0"
const filBs = "/chain" + ipfsBs
retProto, err := h.Peerstore().FirstSupportedProtocol(p, filBs, ipfsBs)
if err != nil || retProto == "" {
continue
}
//TODO: This isn't technically correct since peers can support both, but in practice none do
switch retProto {
case filBs:
nPeersWithFilBitswap++
case ipfsBs:
nPeersWithIpfsBitswap++
}
}
nwantsFil := len(bscFil.GetWantlist())
nwantsIpfs := len(bscIpfs.GetWantlist())
nFilBlks := atomic.LoadUint64(&cf.numFirstBlocks)
nIpfsBlks := atomic.LoadUint64(&cf.numSecondBlocks)
fmt.Printf("numPeers: %d, "+
"nPeersWithFilBitswap: %d, numWantsFil: %d, nblksFil: %d, "+
"nPeersWithIpfsBitswap: %d, numWantsIpfs: %d, nblksIpfs: %d \n",
len(peers), nPeersWithFilBitswap, nwantsFil, nFilBlks, nPeersWithIpfsBitswap, nwantsIpfs, nIpfsBlks)
}
}
}()
fmt.Printf("duration to setup bitswap fetching: %v\n", time.Since(start))
return bs, nil
}
type combineFetcher struct {
numFirstBlocks, numSecondBlocks uint64
first, second exchange.Fetcher
}
func (f *combineFetcher) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error) {
subCtx, cancel := context.WithCancel(ctx)
defer cancel()
firstCh, firstErr := f.first.GetBlocks(subCtx, []cid.Cid{c})
secondCh, secondErr := f.second.GetBlocks(subCtx, []cid.Cid{c})
if firstErr != nil && secondErr != nil {
return nil, multierror.Append(firstErr, secondErr)
}
if firstErr != nil {
blk := <-secondCh
atomic.AddUint64(&f.numSecondBlocks, 1)
return blk, nil
}
if secondErr != nil {
blk := <-firstCh
atomic.AddUint64(&f.numFirstBlocks, 1)
return blk, nil
}
var blk blocks.Block
select {
case blk = <-firstCh:
atomic.AddUint64(&f.numFirstBlocks, 1)
case blk = <-secondCh:
atomic.AddUint64(&f.numSecondBlocks, 1)
case <-ctx.Done():
return nil, ctx.Err()
}
if blk == nil {
if ctx.Err() != nil {
return nil, ctx.Err()
}
return nil, fmt.Errorf("block missing... this shouldn't happen. Report an error")
}
return blk, nil
}
func (f *combineFetcher) GetBlocks(ctx context.Context, cids []cid.Cid) (<-chan blocks.Block, error) {
//TODO implement me
panic("implement me")
}
var _ exchange.Fetcher = (*combineFetcher)(nil)
// this is the wrong way around, but this is the easiest way to hack it
type bservBstoreWrapper struct {
blockstore.Blockstore
bserv exchange.Fetcher
}
func (b *bservBstoreWrapper) Get(ctx context.Context, c cid.Cid) (blocks.Block, error) {
err := verifcid.ValidateCid(c) // hash security
if err != nil {
return nil, err
}
block, err := b.Blockstore.Get(ctx, c)
if err == nil {
return block, nil
}
if ipld.IsNotFound(err) {
// TODO be careful checking ErrNotFound. If the underlying
// implementation changes, this will break.
blk, err := b.bserv.GetBlock(ctx, c)
if err != nil {
return nil, err
}
// also write in the blockstore for caching
err = b.Blockstore.Put(ctx, blk)
if err != nil {
return nil, err
}
return blk, nil
}
return nil, err
}
func (b *bservBstoreWrapper) GetSize(ctx context.Context, c cid.Cid) (int, error) {
has, err := b.Blockstore.Has(ctx, c)
if has {
return b.Blockstore.GetSize(ctx, c)
}
if !ipld.IsNotFound(err) {
// TODO be careful checking ErrNotFound. If the underlying
// implementation changes, this will break.
return 0, err
}
blk, err := b.bserv.GetBlock(ctx, c)
if err != nil {
return 0, err
}
// also write in the blockstore for caching
err = b.Blockstore.Put(ctx, blk)
if err != nil {
return 0, err
}
return len(blk.RawData()), nil
}
var _ blockstore.Blockstore = (*bservBstoreWrapper)(nil)
func getCoins(ctx context.Context, bstore blockstore.Blockstore, tsk lchtypes.TipSetKey, addr filaddr.Address) (defErr error) {
cbs := &countingBlockstore{Blockstore: bstore, m: make(map[cid.Cid]int)}
ebs := NewEphemeralBlockstore(cbs)
sm, err := newFilStateReader(ebs)
if err != nil {
return xerrors.Errorf("unable to initialize a StateManager: %w", err)
}
ts, err := sm.ChainStore().GetTipSetFromKey(ctx, tsk)
if err != nil {
return xerrors.Errorf("unable to load target tipset: %w", err)
}
eg, shCtx := errgroup.WithContext(ctx)
foundAttoFil := abi.NewTokenAmount(0)
go func() {
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
for {
select {
case <-shCtx.Done():
return
case <-ticker.C:
cbs.mx.Lock()
nb := len(cbs.m)
cbs.mx.Unlock()
fmt.Printf("numberOfBlocksLoaded: %d \n", nb)
}
}
}()
eg.Go(func() error { return parseActors(shCtx, sm, ts, addr, foundAttoFil) })
err = eg.Wait()
if err != nil {
return err
}
fmt.Printf("total attofil: %s\n", foundAttoFil)
fmt.Printf("total blocks read: %d\n", len(cbs.m))
totalSizeBytes := 0
for _, v := range cbs.m {
totalSizeBytes += v
}
fmt.Printf("total block sizes: %d\n", totalSizeBytes)
return
}
type getManyCborStore struct {
*ipldcbor.BasicIpldStore
}
func (g *getManyCborStore) GetMany(ctx context.Context, cids []cid.Cid, outs []interface{}) <-chan *hamt.OptionalInteger {
outCh := make(chan *hamt.OptionalInteger)
wg := sync.WaitGroup{}
processingCh := make(chan int)
go func() {
for i := 0; i < len(cids); i++ {
processingCh <- i
}
close(processingCh)
}()
const concurrency = 8
wg.Add(concurrency)
for i := 0; i < concurrency; i++ {
go func() {
defer wg.Done()
for index := range processingCh {
c := cids[index]
o := outs[index]
err := g.Get(ctx, c, o)
if err != nil {
outCh <- &hamt.OptionalInteger{Error: err}
} else {
outCh <- &hamt.OptionalInteger{Value: index}
}
}
}()
}
go func() {
wg.Wait()
close(outCh)
}()
return outCh
}
type getManyIPLDStore interface {
GetMany(ctx context.Context, cids []cid.Cid, outs []interface{}) <-chan *hamt.OptionalInteger
}
var _ getManyIPLDStore = (*getManyCborStore)(nil)
func parseActors(ctx context.Context, sm *lchstmgr.StateManager, ts *lchtypes.TipSet, rootAddr filaddr.Address, foundAttoFil abi.TokenAmount) error {
ast := lchadt.WrapStore(ctx, ipldcbor.NewCborStore(sm.ChainStore().UnionStore()))
getManyAst := &getManyCborStore{
BasicIpldStore: ipldcbor.NewCborStore(sm.ChainStore().StateBlockstore())}
stateTree, err := sm.StateTree(ts.ParentState())
if err != nil {
return err
}
rootAddrID, err := stateTree.LookupID(rootAddr)
if err != nil {
return err
}
var root lchtypes.StateRoot
// Try loading as a new-style state-tree (version/actors tuple).
if err := ast.Get(context.TODO(), ts.ParentState(), &root); err != nil {
return err
}
hamtOptions := append(adt.DefaultHamtOptions, hamt.UseTreeBitWidth(builtin.DefaultHamtBitwidth))
node, err := hamt.LoadNode(ctx, getManyAst, root.Actors, hamtOptions...)
if err != nil {
return err
}
var mx sync.Mutex
return node.ForEachParallel(ctx, func(k string, val *cbg.Deferred) error {
act := &lchtypes.Actor{}
addr, err := filaddr.NewFromBytes([]byte(k))
if err != nil {
return xerrors.Errorf("invalid address (%x) found in state tree key: %w", []byte(k), err)
}
err = act.UnmarshalCBOR(bytes.NewReader(val.Raw))
if err != nil {
return err
}
if ctx.Err() != nil {
return ctx.Err()
}
switch {
case lbi.IsMultisigActor(act.Code):
ms, err := lbimsig.Load(ast, act)
if err != nil {
return err
}
msID := mustAddrID(addr)
_ = msID
tr, _ := ms.Threshold()
actors, err := ms.Signers()
if err != nil {
return err
}
foundActor := false
for _, a := range actors {
if bytes.Equal(a.Bytes(), rootAddrID.Bytes()) {
foundActor = true
}
}
if foundActor && tr == 1 {
// msig balance needs calculating for epoch in question
lb, err := ms.LockedBalance(ts.Height())
if err != nil {
return err
}
bal := filbig.Sub(act.Balance, lb)
mx.Lock()
foundAttoFil.Add(foundAttoFil.Int, bal.Int)
mx.Unlock()
fmt.Printf("found msig %v\n", addr)
} else if foundActor {
fmt.Printf("more than just you is needed to claim the coins\n")
}
return err
default:
return nil
}
})
}
func getActors(ctx context.Context, bstore blockstore.Blockstore, tsk lchtypes.TipSetKey, countOnly bool) error {
cbs := &countingBlockstore{Blockstore: bstore, m: make(map[cid.Cid]int)}
ebs := NewEphemeralBlockstore(cbs)
sm, err := newFilStateReader(ebs)
if err != nil {
return xerrors.Errorf("unable to initialize a StateManager: %w", err)
}
ts, err := sm.ChainStore().GetTipSetFromKey(ctx, tsk)
if err != nil {
return xerrors.Errorf("unable to load target tipset: %w", err)
}
eg, shCtx := errgroup.WithContext(ctx)
go func() {
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
for {
select {
case <-shCtx.Done():
return
case <-ticker.C:
cbs.mx.Lock()
nb := len(cbs.m)
cbs.mx.Unlock()
fmt.Printf("numberOfBlocksLoaded: %d \n", nb)
}
}
}()
var numActors uint64
eg.Go(func() error {
ast := lchadt.WrapStore(ctx, ipldcbor.NewCborStore(sm.ChainStore().UnionStore()))
getManyAst := &getManyCborStore{
BasicIpldStore: ipldcbor.NewCborStore(sm.ChainStore().StateBlockstore())}
var root lchtypes.StateRoot
// Try loading as a new-style state-tree (version/actors tuple).
if err := ast.Get(context.TODO(), ts.ParentState(), &root); err != nil {
return err
}
hamtOptions := append(adt.DefaultHamtOptions, hamt.UseTreeBitWidth(builtin.DefaultHamtBitwidth))
node, err := hamt.LoadNode(ctx, getManyAst, root.Actors, hamtOptions...)
if err != nil {
return err
}
return node.ForEachParallel(ctx, func(k string, val *cbg.Deferred) error {
act := &lchtypes.Actor{}
addr, err := filaddr.NewFromBytes([]byte(k))
if err != nil {
return xerrors.Errorf("invalid address (%x) found in state tree key: %w", []byte(k), err)
}
err = act.UnmarshalCBOR(bytes.NewReader(val.Raw))
if err != nil {
return err
}
if ctx.Err() != nil {
return ctx.Err()
}
atomic.AddUint64(&numActors, 1)
if !countOnly {
fmt.Printf("%v\n", addr)
}
return nil
})
})
err = eg.Wait()
if err != nil {
return err
}
fmt.Printf("total actors found: %d\n", numActors)
fmt.Printf("total blocks read: %d\n", len(cbs.m))
totalSizeBytes := 0
for _, v := range cbs.m {
totalSizeBytes += v
}
fmt.Printf("total block sizes: %d\n", totalSizeBytes)
return nil
}
func getBalance(ctx context.Context, bstore blockstore.Blockstore, tsk lchtypes.TipSetKey, addr filaddr.Address) error {
cbs := &countingBlockstore{Blockstore: bstore, m: make(map[cid.Cid]int)}
ebs := NewEphemeralBlockstore(cbs)
sm, err := newFilStateReader(ebs)
if err != nil {
return xerrors.Errorf("unable to initialize a StateManager: %w", err)
}
ts, err := sm.ChainStore().GetTipSetFromKey(ctx, tsk)
if err != nil {
return xerrors.Errorf("unable to load target tipset: %w", err)
}
stateTree, err := sm.StateTree(ts.ParentState())
if err != nil {
return err
}
act, err := stateTree.GetActor(addr)
if err != nil {
return err
}
fmt.Printf("total actor balance: %s\n", act.Balance)
fmt.Printf("total blocks read: %d\n", len(cbs.m))
totalSizeBytes := 0
for _, v := range cbs.m {
totalSizeBytes += v
}
fmt.Printf("total block sizes: %d\n", totalSizeBytes)
return nil
}
func fevmExec(ctx context.Context, bstore blockstore.Blockstore, tsk lchtypes.TipSetKey, eaddr *ethtypes.EthAddress, edata ethtypes.EthBytes, outputCAR string) error {
cbs := &countingBlockstore{Blockstore: bstore, m: make(map[cid.Cid]int)}
ebs := NewEphemeralBlockstore(cbs)
ts, sm, filMsg, err := getFevmRequest(ctx, ebs, tsk, eaddr, edata)
if err != nil {
return err
}
fmt.Printf("epoch %s\n", ts.Height())
act, err := sm.LoadActor(ctx, filMsg.To, ts)
if err != nil {
return xerrors.Errorf("could not load actor the message is from: %w", err)
}
actorStateRoot := act.Head
fmt.Printf("actor state root: %s\n", actorStateRoot)
_, err = ebs.Get(ctx, actorStateRoot)
if err != nil {
return fmt.Errorf("error loading state root %w", err)
}
fmt.Printf("total blocks read: %d\n", len(cbs.m))
totalSizeBytes := 0
for _, v := range cbs.m {
totalSizeBytes += v
}
fmt.Printf("total block sizes: %d\n", totalSizeBytes)
for _, c := range cbs.orderedCids {
fmt.Printf("pre-call cid: %s\n", c)
}
res, err := sm.Call(ctx, filMsg, ts)
if err != nil {
return xerrors.Errorf("unable to make a call: %w", err)
}
str, err := json.Marshal(res)
if err != nil {
fmt.Println(res)
return xerrors.Errorf("could not marshal response as json: %w", err)
}
fmt.Println(string(str))
fmt.Printf("total blocks read: %d\n", len(cbs.m))
totalSizeBytes = 0
for _, v := range cbs.m {
totalSizeBytes += v
}
fmt.Printf("total block sizes: %d\n", totalSizeBytes)
cidsInOrder := cbs.orderedCids
if err != nil {
return err
}
for _, c := range cidsInOrder {
fmt.Printf("all cid: %s\n", c)
}
if outputCAR != "" {
carw, err := carbs.OpenReadWrite(outputCAR, []cid.Cid{actorStateRoot}, carbs.WriteAsCarV1(true))
if err != nil {
return err
}
for _, c := range cidsInOrder {
blk, err := bstore.Get(ctx, c)
if err != nil {
return err
}
if err := carw.Put(ctx, blk); err != nil {
return err
}
}
if err := carw.Finalize(); err != nil {
return err
}
}
return nil
}
func getFevmRequest(ctx context.Context, ebs lotusbs.Blockstore, tsk lchtypes.TipSetKey, eaddr *ethtypes.EthAddress, edata ethtypes.EthBytes) (*lchtypes.TipSet, *lchstmgr.StateManager, *lchtypes.Message, error) {
sm, err := newFilStateReader(ebs)
if err != nil {
return nil, nil, nil, xerrors.Errorf("unable to initialize a StateManager: %w", err)
}
ts, err := sm.ChainStore().GetTipSetFromKey(ctx, tsk)
if err != nil {
return nil, nil, nil, xerrors.Errorf("unable to load target tipset: %w", err)
}
tx := ethtypes.EthCall{
From: nil,
To: eaddr,
Gas: 0,
GasPrice: ethtypes.EthBigInt{},
Value: ethtypes.EthBigInt{},
Data: edata,
}
filMsg, err := ethCallToFilecoinMessage(ctx, tx)
if err != nil {
return nil, nil, nil, xerrors.Errorf("unable to convert ethcall to filecoin message: %w", err)
}
return ts, sm, filMsg, nil
}
func ethCallToFilecoinMessage(ctx context.Context, tx ethtypes.EthCall) (*lchtypes.Message, error) {
var from filaddr.Address
if tx.From == nil || *tx.From == (ethtypes.EthAddress{}) {
// Send from the filecoin "system" address.
var err error
from, err = (ethtypes.EthAddress{}).ToFilecoinAddress()
if err != nil {
return nil, fmt.Errorf("failed to construct the ethereum system address: %w", err)
}
} else {
// The from address must be translatable to an f4 address.
var err error
from, err = tx.From.ToFilecoinAddress()
if err != nil {
return nil, fmt.Errorf("failed to translate sender address (%s): %w", tx.From.String(), err)
}
if p := from.Protocol(); p != filaddr.Delegated {
return nil, fmt.Errorf("expected a class 4 address, got: %d: %w", p, err)
}
}
var params []byte
if len(tx.Data) > 0 {
initcode := abi.CborBytes(tx.Data)
params2, err := actors.SerializeParams(&initcode)
if err != nil {
return nil, fmt.Errorf("failed to serialize params: %w", err)
}
params = params2
}
var to filaddr.Address
var method abi.MethodNum
if tx.To == nil {
// this is a contract creation
to = builtin.EthereumAddressManagerActorAddr
method = builtin.MethodsEAM.CreateExternal
} else {
addr, err := tx.To.ToFilecoinAddress()
if err != nil {
return nil, xerrors.Errorf("cannot get Filecoin address: %w", err)
}
to = addr
method = builtin.MethodsEVM.InvokeContract
}
return &lchtypes.Message{
From: from,
To: to,
Value: big.Int(tx.Value),
Method: method,
Params: params,
GasLimit: build.BlockGasLimit,
GasFeeCap: big.Zero(),
GasPremium: big.Zero(),
}, nil
}