-
Notifications
You must be signed in to change notification settings - Fork 462
/
testing.go
949 lines (803 loc) · 28.8 KB
/
testing.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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
package chain
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"io/ioutil"
"sync"
"testing"
"github.com/filecoin-project/venus/pkg/util/blockstoreutil"
"github.com/ipld/go-car"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"golang.org/x/xerrors"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/filecoin-project/venus/fixtures/asset"
"github.com/filecoin-project/venus/pkg/chainsync/exchange"
"github.com/filecoin-project/venus/pkg/clock"
"github.com/filecoin-project/venus/pkg/config"
"github.com/filecoin-project/venus/pkg/crypto"
"github.com/filecoin-project/venus/pkg/repo"
"github.com/filecoin-project/venus/pkg/testhelpers"
"github.com/filecoin-project/venus/pkg/util"
"github.com/filecoin-project/venus/venus-shared/types"
)
// Builder builds fake chains and acts as a provider and fetcher for the chain thus generated.
// All blocks are unique (even if they share parents) and form valid chains of parents and heights,
// but do not carry valid tickets. Each block contributes a weight of 1.
// state root CIDs are computed by an abstract StateBuilder. The default FakeStateBuilder produces
// state CIDs that are distinct but not CIDs of any real state tree. A more sophisticated
// builder could actually apply the messages to a state tree (not yet implemented).
// The builder is deterministic: two builders receiving the same sequence of calls will produce
// exactly the same chain.
type Builder struct {
t *testing.T
genesis *types.TipSet
store *Store
minerAddress address.Address
stateBuilder StateBuilder
stamper TimeStamper
repo repo.Repo
bs blockstoreutil.Blockstore
cstore cbor.IpldStore
mstore *MessageStore
seq uint64 // For unique tickets
eval *FakeStateEvaluator
// Cache of the state root CID computed for each tipset key.
tipStateCids map[string]cid.Cid
stmgr IStmgr
evaLock sync.Mutex
}
func (f *Builder) IStmgr() IStmgr {
f.evaLock.Lock()
defer f.evaLock.Unlock()
return f.stmgr
}
func (f *Builder) FakeStateEvaluator() *FakeStateEvaluator {
f.evaLock.Lock()
defer f.evaLock.Unlock()
if f.eval != nil {
return f.eval
}
f.eval = &FakeStateEvaluator{
ChainStore: f.store,
MessageStore: f.mstore,
ChsWorkingOn: make(map[types.TipSetKey]chan struct{}, 1),
}
return f.eval
}
func (f *Builder) LoadTipSetMessage(ctx context.Context, ts *types.TipSet) ([]types.BlockMessagesInfo, error) {
// gather message
applied := make(map[address.Address]uint64)
selectMsg := func(m *types.Message) (bool, error) {
// The first match for a sender is guaranteed to have correct nonce -- the block isn't valid otherwise
if _, ok := applied[m.From]; !ok {
applied[m.From] = m.Nonce
}
if applied[m.From] != m.Nonce {
return false, nil
}
applied[m.From]++
return true, nil
}
blockMsg := []types.BlockMessagesInfo{}
for i := 0; i < ts.Len(); i++ {
blk := ts.At(i)
secpMsgs, blsMsgs, err := f.LoadMetaMessages(ctx, blk.Messages)
if err != nil {
return nil, errors.Wrapf(err, "syncing tip %s failed loading message list %s for block %s", ts.Key(), blk.Messages, blk.Cid())
}
var sBlsMsg []types.ChainMsg
var sSecpMsg []types.ChainMsg
for _, msg := range blsMsgs {
b, err := selectMsg(msg)
if err != nil {
return nil, xerrors.Errorf("failed to decide whether to select message for block: %w", err)
}
if b {
sBlsMsg = append(sBlsMsg, msg)
}
}
for _, msg := range secpMsgs {
b, err := selectMsg(&msg.Message)
if err != nil {
return nil, xerrors.Errorf("failed to decide whether to select message for block: %w", err)
}
if b {
sSecpMsg = append(sSecpMsg, msg)
}
}
blockMsg = append(blockMsg, types.BlockMessagesInfo{
BlsMessages: sBlsMsg,
SecpkMessages: sSecpMsg,
Block: blk,
})
}
return blockMsg, nil
}
func (f *Builder) Cstore() cbor.IpldStore {
return f.cstore
}
func (f *Builder) Genesis() *types.TipSet {
return f.genesis
}
func (f *Builder) Mstore() *MessageStore {
return f.mstore
}
func (f *Builder) BlockStore() blockstoreutil.Blockstore {
return f.bs
}
func (f *Builder) Repo() repo.Repo {
return f.repo
}
func (f *Builder) Store() *Store {
return f.store
}
func (f *Builder) RemovePeer(peer peer.ID) {}
var _ BlockProvider = (*Builder)(nil)
var _ TipSetProvider = (*Builder)(nil)
var _ MessageProvider = (*Builder)(nil)
type fakeStmgr struct {
cs *Store
eva *FakeStateEvaluator
}
func (f *fakeStmgr) GetActorAt(ctx context.Context, a address.Address, set *types.TipSet) (*types.Actor, error) {
return f.cs.GetActorAt(ctx, set, a)
}
func (f *fakeStmgr) RunStateTransition(ctx context.Context, set *types.TipSet) (root cid.Cid, receipts cid.Cid, err error) {
return f.eva.RunStateTransition(ctx, set)
}
var _ IStmgr = &fakeStmgr{}
// NewBuilder builds a new chain faker with default fake state building.
func NewBuilder(t *testing.T, miner address.Address) *Builder {
return NewBuilderWithDeps(t, miner, &FakeStateBuilder{}, &ZeroTimestamper{})
}
// NewBuilderWithDeps builds a new chain faker.
// Blocks will have `miner` set as the miner address, or a default if empty.
func NewBuilderWithDeps(t *testing.T, miner address.Address, sb StateBuilder, stamper TimeStamper) *Builder {
if miner.Empty() {
var err error
miner, err = address.NewSecp256k1Address([]byte("miner"))
require.NoError(t, err)
}
repo := repo.NewInMemoryRepo()
bs := repo.Datastore()
ds := repo.ChainDatastore()
cst := cbor.NewCborStore(bs)
b := &Builder{
t: t,
minerAddress: miner,
stateBuilder: sb,
stamper: stamper,
repo: repo,
bs: bs,
cstore: cst,
mstore: NewMessageStore(bs, config.DefaultForkUpgradeParam),
tipStateCids: make(map[string]cid.Cid),
}
ctx := context.TODO()
_, err := b.mstore.StoreMessages(ctx, []*types.SignedMessage{}, []*types.Message{})
require.NoError(t, err)
_, err = b.mstore.StoreReceipts(ctx, []types.MessageReceipt{})
require.NoError(t, err)
// append genesis
nullState := testhelpers.CidFromString(t, "null")
b.tipStateCids[types.NewTipSetKey().String()] = nullState
// create a fixed genesis
b.genesis = b.GeneratorGenesis()
b.store = NewStore(ds, bs, b.genesis.At(0).Cid(), NewMockCirculatingSupplyCalculator())
for _, block := range b.genesis.Blocks() {
// add block to cstore
_, err := b.cstore.Put(context.TODO(), block)
require.NoError(t, err)
}
stateRoot, receiptRoot := b.genesis.Blocks()[0].ParentStateRoot, b.genesis.Blocks()[0].ParentMessageReceipts
b.tipStateCids[b.genesis.Key().String()] = stateRoot
require.NoError(t, err)
tipsetMeta := &TipSetMetadata{
TipSetStateRoot: stateRoot,
TipSet: b.genesis,
TipSetReceipts: receiptRoot,
}
require.NoError(t, b.store.PutTipSetMetadata(context.TODO(), tipsetMeta))
err = b.store.SetHead(context.TODO(), b.genesis)
require.NoError(t, err)
b.stmgr = &fakeStmgr{cs: b.store, eva: b.FakeStateEvaluator()}
return b
}
// AppendBlockOnBlocks creates and returns a new block child of `parents`, with no messages.
func (f *Builder) AppendBlockOnBlocks(ctx context.Context, parents ...*types.BlockHeader) *types.BlockHeader {
var tip *types.TipSet
if len(parents) > 0 {
tip = testhelpers.RequireNewTipSet(f.t, parents...)
}
return f.AppendBlockOn(ctx, tip)
}
// AppendBlockOn creates and returns a new block child of `parent`, with no messages.
func (f *Builder) AppendBlockOn(ctx context.Context, parent *types.TipSet) *types.BlockHeader {
return f.Build(ctx, parent, 1, nil).At(0)
}
// AppendOn creates and returns a new `width`-block tipset child of `parents`, with no messages.
func (f *Builder) AppendOn(ctx context.Context, parent *types.TipSet, width int) *types.TipSet {
return f.Build(ctx, parent, width, nil)
}
func (f *Builder) FlushHead(ctx context.Context) error {
_, _, e := f.FakeStateEvaluator().RunStateTransition(ctx, f.store.GetHead())
return e
}
// AppendManyBlocksOnBlocks appends `height` blocks to the chain.
func (f *Builder) AppendManyBlocksOnBlocks(ctx context.Context, height int, parents ...*types.BlockHeader) *types.BlockHeader {
var tip *types.TipSet
if len(parents) > 0 {
tip = testhelpers.RequireNewTipSet(f.t, parents...)
}
return f.BuildManyOn(ctx, height, tip, nil).At(0)
}
// AppendManyBlocksOn appends `height` blocks to the chain.
func (f *Builder) AppendManyBlocksOn(ctx context.Context, height int, parent *types.TipSet) *types.BlockHeader {
return f.BuildManyOn(ctx, height, parent, nil).At(0)
}
// AppendManyOn appends `height` tipsets to the chain.
func (f *Builder) AppendManyOn(ctx context.Context, height int, parent *types.TipSet) *types.TipSet {
return f.BuildManyOn(ctx, height, parent, nil)
}
// BuildOnBlock creates and returns a new block child of singleton tipset `parent`. See Build.
func (f *Builder) BuildOnBlock(ctx context.Context, parent *types.BlockHeader, build func(b *BlockBuilder)) *types.BlockHeader {
var tip *types.TipSet
if parent != nil {
tip = testhelpers.RequireNewTipSet(f.t, parent)
}
return f.BuildOneOn(ctx, tip, build).At(0)
}
// BuildOneOn creates and returns a new single-block tipset child of `parent`.
func (f *Builder) BuildOneOn(ctx context.Context, parent *types.TipSet, build func(b *BlockBuilder)) *types.TipSet {
return f.Build(ctx, parent, 1, singleBuilder(build))
}
// BuildOn creates and returns a new `width` block tipset child of `parent`.
func (f *Builder) BuildOn(ctx context.Context, parent *types.TipSet, width int, build func(b *BlockBuilder, i int)) *types.TipSet {
return f.Build(ctx, parent, width, build)
}
// BuildManyOn builds a chain by invoking Build `height` times.
func (f *Builder) BuildManyOn(ctx context.Context, height int, parent *types.TipSet, build func(b *BlockBuilder)) *types.TipSet {
require.True(f.t, height > 0, "")
for i := 0; i < height; i++ {
parent = f.Build(ctx, parent, 1, singleBuilder(build))
}
return parent
}
// Build creates and returns a new tipset child of `parent`.
// The tipset carries `width` > 0 blocks with the same height and parents, but different tickets.
// Note: the blocks will all have the same miner, which is unrealistic and forbidden by consensus;
// generalise this to random miner addresses when that is rejected by the syncer.
// The `build` function is invoked to modify the block before it is stored.
func (f *Builder) Build(ctx context.Context, parent *types.TipSet, width int, build func(b *BlockBuilder, i int)) *types.TipSet {
tip := f.BuildOrphaTipset(parent, width, build)
for _, block := range tip.Blocks() {
// add block to cstore
_, err := f.cstore.Put(context.TODO(), block)
require.NoError(f.t, err)
}
// Compute and remember state for the tipset.
stateRoot, _ := f.ComputeState(ctx, tip)
f.tipStateCids[tip.Key().String()] = stateRoot
return tip
}
func (f *Builder) BuildOrphaTipset(parent *types.TipSet, width int, build func(b *BlockBuilder, i int)) *types.TipSet {
require.True(f.t, width > 0)
var blocks []*types.BlockHeader
height := abi.ChainEpoch(0)
if parent.Defined() {
var err error
height = parent.At(0).Height + 1
require.NoError(f.t, err)
} else {
parent = types.UndefTipSet
}
parentWeight, err := f.stateBuilder.Weigh(context.TODO(), parent)
require.NoError(f.t, err)
emptyBLSSig := crypto.Signature{
Type: crypto.SigTypeBLS,
Data: []byte(""),
// Data: (*bls.Aggregate([]bls.Signature{}))[:],
}
for i := 0; i < width; i++ {
ticket := types.Ticket{}
ticket.VRFProof = make([]byte, binary.Size(f.seq))
binary.BigEndian.PutUint64(ticket.VRFProof, f.seq)
f.seq++
b := &types.BlockHeader{
Ticket: &ticket,
Miner: f.minerAddress,
BeaconEntries: nil,
ParentWeight: parentWeight,
Parents: parent.Key().Cids(),
Height: height,
Messages: testhelpers.EmptyTxMetaCID,
ParentMessageReceipts: testhelpers.EmptyReceiptsCID,
BLSAggregate: &emptyBLSSig,
// Omitted fields below
// ParentStateRoot: stateRoot,
// EPoStInfo: ePoStInfo,
// ForkSignaling: forkSig,
Timestamp: f.stamper.Stamp(height),
BlockSig: &crypto.Signature{Type: crypto.SigTypeSecp256k1, Data: []byte{}},
ElectionProof: &types.ElectionProof{VRFProof: []byte{0x0c, 0x0d}, WinCount: int64(10)},
}
if build != nil {
build(&BlockBuilder{b, f.t, f.mstore}, i)
}
// Compute state root for this block.
ctx := context.Background()
prevState := f.StateForKey(ctx, parent.Key())
smsgs, umsgs, err := f.mstore.LoadMetaMessages(ctx, b.Messages)
require.NoError(f.t, err)
var sBlsMsg []types.ChainMsg
var sSecpMsg []types.ChainMsg
for _, m := range umsgs {
sBlsMsg = append(sBlsMsg, m)
}
for _, m := range smsgs {
sSecpMsg = append(sSecpMsg, m)
}
blkMsgInfo := types.BlockMessagesInfo{
BlsMessages: sBlsMsg,
SecpkMessages: sSecpMsg,
Block: b,
}
stateRootRaw, _, err := f.stateBuilder.ComputeState(prevState, []types.BlockMessagesInfo{blkMsgInfo})
require.NoError(f.t, err)
b.ParentStateRoot = stateRootRaw
blocks = append(blocks, b)
}
return testhelpers.RequireNewTipSet(f.t, blocks...)
}
// StateForKey loads (or computes) the state root for a tipset key.
func (f *Builder) StateForKey(ctx context.Context, key types.TipSetKey) cid.Cid {
state, found := f.tipStateCids[key.String()]
if found {
return state
}
// No state yet computed for this tip (perhaps because the blocks in it have not previously
// been considered together as a tipset).
tip, err := f.GetTipSet(ctx, key)
require.NoError(f.t, err)
state, _ = f.ComputeState(ctx, tip)
return state
}
// GetBlockstoreValue gets data straight out of the underlying blockstore by cid
func (f *Builder) GetBlockstoreValue(ctx context.Context, c cid.Cid) (blocks.Block, error) {
return f.bs.Get(ctx, c)
}
// ComputeState computes the state for a tipset from its parent state.
func (f *Builder) ComputeState(ctx context.Context, tip *types.TipSet) (cid.Cid, []types.MessageReceipt) {
parentKey := tip.Parents()
// Load the state of the parent tipset and compute the required state (recursively).
prev := f.StateForKey(ctx, parentKey)
blockMsgInfo := f.tipMessages(tip)
state, receipt, err := f.stateBuilder.ComputeState(prev, blockMsgInfo)
require.NoError(f.t, err)
return state, receipt
}
// tipMessages returns the messages of a tipset. Each block's messages are
// grouped into a slice and a slice of these slices is returned.
func (f *Builder) tipMessages(tip *types.TipSet) []types.BlockMessagesInfo {
ctx := context.Background()
var blockMessageInfos []types.BlockMessagesInfo
for i := 0; i < tip.Len(); i++ {
smsgs, blsMsg, err := f.mstore.LoadMetaMessages(ctx, tip.At(i).Messages)
require.NoError(f.t, err)
var sBlsMsg []types.ChainMsg
var sSecpMsg []types.ChainMsg
for _, m := range blsMsg {
sBlsMsg = append(sBlsMsg, m)
}
for _, m := range smsgs {
sSecpMsg = append(sSecpMsg, m)
}
blockMessageInfos = append(blockMessageInfos, types.BlockMessagesInfo{
BlsMessages: sBlsMsg,
SecpkMessages: sSecpMsg,
Block: tip.At(i),
})
}
return blockMessageInfos
}
// Wraps a simple build function in one that also accepts an index, propagating a nil function.
func singleBuilder(build func(b *BlockBuilder)) func(b *BlockBuilder, i int) {
if build == nil {
return nil
}
return func(b *BlockBuilder, i int) { build(b) }
}
// /// BlockHeader builder /////
// BlockBuilder mutates blocks as they are generated.
type BlockBuilder struct {
block *types.BlockHeader
t *testing.T
messages *MessageStore
}
// SetTicket sets the block's ticket.
func (bb *BlockBuilder) SetTicket(raw []byte) {
bb.block.Ticket = &types.Ticket{VRFProof: raw}
}
// SetTimestamp sets the block's timestamp.
func (bb *BlockBuilder) SetTimestamp(timestamp uint64) {
bb.block.Timestamp = timestamp
}
// IncHeight increments the block's height, implying a number of null blocks before this one
// is mined.
func (bb *BlockBuilder) IncHeight(nullBlocks abi.ChainEpoch) {
bb.block.Height += nullBlocks
}
// SetBlockSig set a new signature
func (bb *BlockBuilder) SetBlockSig(signature crypto.Signature) {
bb.block.BlockSig = &signature
}
// AddMessages adds a message & receipt collection to the block.
func (bb *BlockBuilder) AddMessages(secpmsgs []*types.SignedMessage, blsMsgs []*types.Message) {
ctx := context.Background()
meta, err := bb.messages.StoreMessages(ctx, secpmsgs, blsMsgs)
require.NoError(bb.t, err)
bb.block.Messages = meta
}
// SetStateRoot sets the block's state root.
func (bb *BlockBuilder) SetStateRoot(root cid.Cid) {
bb.block.ParentStateRoot = root
}
// /// state builder /////
// StateBuilder abstracts the computation of state root CIDs from the chain builder.
type StateBuilder interface {
ComputeState(prev cid.Cid, blockmsg []types.BlockMessagesInfo) (cid.Cid, []types.MessageReceipt, error)
Weigh(ctx context.Context, tip *types.TipSet) (big.Int, error)
}
// FakeStateBuilder computes a fake state CID by hashing the CIDs of a block's parents and messages.
type FakeStateBuilder struct {
}
// ComputeState computes a fake state from a previous state root CID and the messages contained
// in list-of-lists of messages in blocks. Note that if there are no messages, the resulting state
// is the same as the input state.
// This differs from the true state transition function in that messages that are duplicated
// between blocks in the tipset are not ignored.
func (FakeStateBuilder) ComputeState(prev cid.Cid, blockmsg []types.BlockMessagesInfo) (cid.Cid, []types.MessageReceipt, error) {
receipts := []types.MessageReceipt{}
// Accumulate the cids of the previous state and of all messages in the tipset.
inputs := []cid.Cid{prev}
for _, blockMessages := range blockmsg {
for _, msg := range append(blockMessages.BlsMessages, blockMessages.SecpkMessages...) {
mCId := msg.Cid()
inputs = append(inputs, mCId)
receipts = append(receipts, types.MessageReceipt{
ExitCode: 0,
Return: mCId.Bytes(),
GasUsed: 3,
})
}
}
if len(inputs) == 1 {
// If there are no messages, the state doesn't change!
return prev, receipts, nil
}
root, err := util.MakeCid(inputs)
if err != nil {
return cid.Undef, []types.MessageReceipt{}, err
}
return root, receipts, nil
}
// Weigh computes a tipset's weight as its parent weight plus one for each block in the tipset.
func (FakeStateBuilder) Weigh(context context.Context, tip *types.TipSet) (big.Int, error) {
parentWeight := big.Zero()
if tip.Defined() {
parentWeight = tip.ParentWeight()
}
return big.Add(parentWeight, big.NewInt(int64(tip.Len()))), nil
}
// /// Timestamper /////
// TimeStamper is an object that timestamps blocks
type TimeStamper interface {
Stamp(abi.ChainEpoch) uint64
}
// ZeroTimestamper writes a default of 0 to the timestamp
type ZeroTimestamper struct{}
// Stamp returns a stamp for the current block
func (zt *ZeroTimestamper) Stamp(height abi.ChainEpoch) uint64 {
return uint64(0)
}
// ClockTimestamper writes timestamps based on a blocktime and genesis time
type ClockTimestamper struct {
c clock.ChainEpochClock
}
// NewClockTimestamper makes a new stamper for creating production valid timestamps
func NewClockTimestamper(chainClock clock.ChainEpochClock) *ClockTimestamper {
return &ClockTimestamper{
c: chainClock,
}
}
// Stamp assigns a valid timestamp given genesis time and block time to
// a block of the provided height.
func (ct *ClockTimestamper) Stamp(height abi.ChainEpoch) uint64 {
startTime := ct.c.StartTimeOfEpoch(height)
return uint64(startTime.Unix())
}
// /// state evaluator /////
// FakeStateEvaluator is a syncStateEvaluator that delegates to the FakeStateBuilder.
type FakeStateEvaluator struct {
ChainStore *Store
MessageStore *MessageStore
FakeStateBuilder
ChsWorkingOn map[types.TipSetKey]chan struct{}
stLk sync.Mutex
}
// RunStateTransition delegates to StateBuilder.ComputeState
func (e *FakeStateEvaluator) RunStateTransition(ctx context.Context, ts *types.TipSet) (rootCid cid.Cid, receiptCid cid.Cid, err error) {
key := ts.Key()
e.stLk.Lock()
workingCh, exist := e.ChsWorkingOn[key]
if exist {
e.stLk.Unlock()
select {
case <-workingCh:
e.stLk.Lock()
case <-ctx.Done():
return cid.Undef, cid.Undef, ctx.Err()
}
}
if m, _ := e.ChainStore.LoadTipsetMetadata(ctx, ts); m != nil {
e.stLk.Unlock()
return m.TipSetStateRoot, m.TipSetReceipts, nil
}
workingCh = make(chan struct{})
e.ChsWorkingOn[key] = workingCh
e.stLk.Unlock()
defer func() {
e.stLk.Lock()
delete(e.ChsWorkingOn, key)
if err == nil {
_ = e.ChainStore.PutTipSetMetadata(ctx, &TipSetMetadata{
TipSetStateRoot: rootCid,
TipSet: ts,
TipSetReceipts: receiptCid,
})
}
e.stLk.Unlock()
close(workingCh)
}()
// gather message
blockMessageInfo, err := e.MessageStore.LoadTipSetMessage(ctx, ts)
if err != nil {
return cid.Undef, cid.Undef, xerrors.Errorf("failed to gather message in tipset %v", err)
}
var receipts []types.MessageReceipt
rootCid, receipts, err = e.ComputeState(ts.At(0).ParentStateRoot, blockMessageInfo)
if err != nil {
return cid.Undef, cid.Undef, errors.Wrap(err, "error compute state")
}
receiptCid, err = e.MessageStore.StoreReceipts(ctx, receipts)
if err != nil {
return cid.Undef, cid.Undef, xerrors.Errorf("failed to save receipt: %v", err)
}
return rootCid, receiptCid, nil
}
func (e *FakeStateEvaluator) ValidateFullBlock(ctx context.Context, blk *types.BlockHeader) error {
parent, err := e.ChainStore.GetTipSet(ctx, types.NewTipSetKey(blk.Parents...))
if err != nil {
return err
}
root, receipts, err := e.RunStateTransition(ctx, parent)
if err != nil {
return err
}
return e.ChainStore.PutTipSetMetadata(ctx, &TipSetMetadata{
TipSetStateRoot: root,
TipSet: parent,
TipSetReceipts: receipts,
})
}
// /// Chain selector /////
// FakeChainSelector is a syncChainSelector that delegates to the FakeStateBuilder
type FakeChainSelector struct {
FakeStateBuilder
}
// IsHeavier compares chains weighed with StateBuilder.Weigh.
func (e *FakeChainSelector) IsHeavier(ctx context.Context, a, b *types.TipSet) (bool, error) {
aw, err := e.Weigh(ctx, a)
if err != nil {
return false, err
}
bw, err := e.Weigh(ctx, b)
if err != nil {
return false, err
}
return aw.GreaterThan(bw), nil
}
// Weight delegates to the statebuilder
func (e *FakeChainSelector) Weight(ctx context.Context, ts *types.TipSet) (big.Int, error) {
return e.Weigh(ctx, ts)
}
// /// Interface and accessor implementations /////
// GetBlock returns the block identified by `c`.
func (f *Builder) GetBlock(ctx context.Context, c cid.Cid) (*types.BlockHeader, error) {
var block types.BlockHeader
if err := f.cstore.Get(ctx, c, &block); err != nil {
return nil, err
}
return &block, nil
}
// GetBlocks returns the blocks identified by `cids`.
func (f *Builder) GetBlocksByIds(ctx context.Context, cids []cid.Cid) ([]*types.BlockHeader, error) {
ret := make([]*types.BlockHeader, len(cids))
for i, c := range cids {
var block types.BlockHeader
if err := f.cstore.Get(ctx, c, &block); err != nil {
return nil, err
}
ret[i] = &block
}
return ret, nil
}
// GetTipSet returns the tipset identified by `key`.
func (f *Builder) GetTipSet(ctx context.Context, key types.TipSetKey) (*types.TipSet, error) {
var blocks []*types.BlockHeader
for _, bid := range key.Cids() {
var blk types.BlockHeader
if err := f.cstore.Get(ctx, bid, &blk); err != nil {
return nil, fmt.Errorf("no block %s", bid)
}
blocks = append(blocks, &blk)
}
return types.NewTipSet(blocks)
}
// FetchTipSets fetchs the tipset at `tsKey` from the fetchers blockStore backed by the Builder.
func (f *Builder) FetchTipSets(ctx context.Context, key types.TipSetKey, from peer.ID, done func(t *types.TipSet) (bool, error)) ([]*types.TipSet, error) {
var tips []*types.TipSet
for {
tip, err := f.GetTipSet(ctx, key)
if err != nil {
return nil, err
}
tips = append(tips, tip)
ok, err := done(tip)
if err != nil {
return nil, err
}
if ok {
break
}
key = tip.Parents()
}
return tips, nil
}
// FetchTipSetHeaders fetchs the tipset at `tsKey` from the fetchers blockStore backed by the Builder.
func (f *Builder) FetchTipSetHeaders(ctx context.Context, key types.TipSetKey, from peer.ID, done func(t *types.TipSet) (bool, error)) ([]*types.TipSet, error) {
return f.FetchTipSets(ctx, key, from, done)
}
// GetTipSetStateRoot returns the state root that was computed for a tipset.
func (f *Builder) GetTipSetStateRoot(key types.TipSetKey) (cid.Cid, error) {
found, ok := f.tipStateCids[key.String()]
if !ok {
return cid.Undef, errors.Errorf("no state for %s", key)
}
return found, nil
}
func (f *Builder) GetTipSetByHeight(ctx context.Context, ts *types.TipSet, h abi.ChainEpoch, prev bool) (*types.TipSet, error) {
if !ts.Defined() {
return ts, nil
}
if epoch := ts.Height(); epoch == h {
return ts, nil
}
for {
ts = f.RequireTipSet(ctx, ts.Parents())
height := ts.Height()
if height >= 0 && height == h {
return ts, nil
} else if height < h {
return ts, nil
}
}
}
// RequireTipSet returns a tipset by key, which must exist.
func (f *Builder) RequireTipSet(ctx context.Context, key types.TipSetKey) *types.TipSet {
tip, err := f.GetTipSet(ctx, key)
require.NoError(f.t, err)
return tip
}
// RequireTipSets returns a chain of tipsets from key, which must exist and be long enough.
func (f *Builder) RequireTipSets(ctx context.Context, head types.TipSetKey, count int) []*types.TipSet {
var tips []*types.TipSet
for i := 0; i < count; i++ {
tip := f.RequireTipSet(ctx, head)
tips = append(tips, tip)
head = tip.Parents()
}
return tips
}
// LoadReceipts returns the message collections tracked by the builder.
func (f *Builder) LoadReceipts(ctx context.Context, c cid.Cid) ([]types.MessageReceipt, error) {
return f.mstore.LoadReceipts(ctx, c)
}
// LoadTxMeta returns the tx meta wrapper tracked by the builder.
func (f *Builder) LoadTxMeta(ctx context.Context, metaCid cid.Cid) (types.MessageRoot, error) {
return f.mstore.LoadTxMeta(ctx, metaCid)
}
// StoreReceipts stores message receipts and returns a commitment.
func (f *Builder) StoreReceipts(ctx context.Context, receipts []types.MessageReceipt) (cid.Cid, error) {
return f.mstore.StoreReceipts(ctx, receipts)
}
// StoreTxMeta stores a tx meta
func (f *Builder) StoreTxMeta(ctx context.Context, meta types.MessageRoot) (cid.Cid, error) {
return f.mstore.StoreTxMeta(ctx, meta)
}
func (f *Builder) LoadUnsignedMessagesFromCids(ctx context.Context, blsCids []cid.Cid) ([]*types.Message, error) {
return f.mstore.LoadUnsignedMessagesFromCids(ctx, blsCids)
}
func (f *Builder) LoadSignedMessagesFromCids(ctx context.Context, secpCids []cid.Cid) ([]*types.SignedMessage, error) {
return f.mstore.LoadSignedMessagesFromCids(ctx, secpCids)
}
// LoadMessages returns the message collections tracked by the builder.
func (f *Builder) LoadMetaMessages(ctx context.Context, metaCid cid.Cid) ([]*types.SignedMessage, []*types.Message, error) {
return f.mstore.LoadMetaMessages(ctx, metaCid)
}
func (f *Builder) ReadMsgMetaCids(ctx context.Context, metaCid cid.Cid) ([]cid.Cid, []cid.Cid, error) {
return f.mstore.ReadMsgMetaCids(ctx, metaCid)
}
// /// exchange /////
func (f *Builder) GetBlocks(ctx context.Context, tsk types.TipSetKey, count int) ([]*types.TipSet, error) {
ts, err := f.GetTipSet(ctx, tsk)
if err != nil {
return nil, err
}
result := []*types.TipSet{ts}
for i := 1; i < count; i++ {
if ts.Height() == 0 {
break
}
ts, err = f.GetTipSet(ctx, ts.Parents())
if err != nil {
return nil, err
}
result = append(result, ts)
}
return result, nil
}
func (f *Builder) GetChainMessages(ctx context.Context, tipsets []*types.TipSet) ([]*exchange.CompactedMessages, error) {
result := []*exchange.CompactedMessages{}
for _, ts := range tipsets {
bmsgs, bmincl, smsgs, smincl, err := exchange.GatherMessages(ctx, f, f.mstore, ts)
if err != nil {
return nil, err
}
compactMsg := &exchange.CompactedMessages{}
compactMsg.Bls = bmsgs
compactMsg.BlsIncludes = bmincl
compactMsg.Secpk = smsgs
compactMsg.SecpkIncludes = smincl
result = append(result, compactMsg)
}
return result, nil
}
func (f *Builder) GetFullTipSet(ctx context.Context, peer []peer.ID, tsk types.TipSetKey) (*types.FullTipSet, error) {
panic("implement me")
}
func (f *Builder) AddPeer(peer peer.ID) {}
func (f *Builder) GeneratorGenesis() *types.TipSet {
b, err := asset.Asset("fixtures/_assets/car/calibnet.car")
require.NoError(f.t, err)
source := ioutil.NopCloser(bytes.NewReader(b))
ch, err := car.LoadCar(context.Background(), f.bs, source)
require.NoError(f.t, err)
// need to check if we are being handed a car file with a single genesis block or an entire chain.
bsBlk, err := f.bs.Get(context.Background(), ch.Roots[0])
require.NoError(f.t, err)
cur, err := types.DecodeBlock(bsBlk.RawData())
require.NoError(f.t, err)
ts, err := types.NewTipSet([]*types.BlockHeader{cur})
require.NoError(f.t, err)
return ts
}