Skip to content

Commit 24bcc77

Browse files
committed
fix test
1 parent 8f83e24 commit 24bcc77

File tree

12 files changed

+871
-121
lines changed

12 files changed

+871
-121
lines changed

action/protocol/staking/contractstakeview_mock.go

Lines changed: 223 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

action/protocol/staking/protocol.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ func (p *Protocol) CreatePreStates(ctx context.Context, sm protocol.StateManager
503503
if err != nil {
504504
return err
505505
}
506-
if h != blkCtx.BlockHeight-1 {
506+
if indexer.StartHeight() <= blkCtx.BlockHeight && h != blkCtx.BlockHeight-1 {
507507
return errors.Errorf("bucket cache height %d does not match current height %d", h, blkCtx.BlockHeight-1)
508508
}
509509
buckets[i] = bs
@@ -527,7 +527,7 @@ func (p *Protocol) CreatePreStates(ctx context.Context, sm protocol.StateManager
527527
if err != nil {
528528
return err
529529
}
530-
if h != blkCtx.BlockHeight-1 {
530+
if indexer.StartHeight() <= blkCtx.BlockHeight && h != blkCtx.BlockHeight-1 {
531531
return errors.Errorf("bucket cache height %d does not match current height %d", h, blkCtx.BlockHeight-1)
532532
}
533533
buckets[i] = bs

action/protocol/staking/protocol_test.go

Lines changed: 0 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -428,113 +428,6 @@ func Test_CreateGenesisStates(t *testing.T) {
428428
}
429429
}
430430

431-
func TestProtocol_ActiveCandidates(t *testing.T) {
432-
require := require.New(t)
433-
ctrl := gomock.NewController(t)
434-
sm := testdb.NewMockStateManagerWithoutHeightFunc(ctrl)
435-
csIndexer := NewMockContractStakingIndexerWithBucketType(ctrl)
436-
437-
selfStake, _ := new(big.Int).SetString("1200000000000000000000000", 10)
438-
g := genesis.TestDefault()
439-
cfg := g.Staking
440-
cfg.BootstrapCandidates = []genesis.BootstrapCandidate{
441-
{
442-
OwnerAddress: identityset.Address(22).String(),
443-
OperatorAddress: identityset.Address(23).String(),
444-
RewardAddress: identityset.Address(23).String(),
445-
Name: "test1",
446-
SelfStakingTokens: selfStake.String(),
447-
},
448-
}
449-
p, err := NewProtocol(HelperCtx{
450-
DepositGas: nil,
451-
BlockInterval: getBlockInterval,
452-
}, &BuilderConfig{
453-
Staking: cfg,
454-
PersistStakingPatchBlock: math.MaxUint64,
455-
SkipContractStakingViewHeight: math.MaxUint64,
456-
Revise: ReviseConfig{
457-
VoteWeight: g.Staking.VoteWeightCalConsts,
458-
},
459-
}, nil, nil, csIndexer, nil)
460-
require.NoError(err)
461-
462-
blkHeight := g.QuebecBlockHeight + 1
463-
ctx := protocol.WithBlockCtx(
464-
genesis.WithGenesisContext(context.Background(), g),
465-
protocol.BlockCtx{
466-
BlockHeight: blkHeight,
467-
},
468-
)
469-
ctx = protocol.WithFeatureCtx(protocol.WithFeatureWithHeightCtx(ctx))
470-
sm.EXPECT().Height().DoAndReturn(func() (uint64, error) {
471-
return blkHeight, nil
472-
}).AnyTimes()
473-
// csIndexer.EXPECT().StartView(gomock.Any()).Return(nil, nil)
474-
csIndexer.EXPECT().Start(gomock.Any()).Return(nil).AnyTimes()
475-
csIndexer.EXPECT().StartHeight().Return(uint64(blkHeight - 3)).AnyTimes()
476-
csIndexer.EXPECT().Height().Return(uint64(blkHeight), nil).AnyTimes()
477-
csIndexer.EXPECT().LoadStakeView(gomock.Any(), gomock.Any()).Return(nil, nil)
478-
479-
v, err := p.Start(ctx, sm)
480-
require.NoError(err)
481-
require.NoError(sm.WriteView(_protocolID, v))
482-
483-
err = p.CreateGenesisStates(ctx, sm)
484-
require.NoError(err)
485-
486-
var csIndexerHeight, csVotes uint64
487-
csIndexer.EXPECT().Height().Return(uint64(0), nil).AnyTimes()
488-
csIndexer.EXPECT().BucketsByCandidate(gomock.Any(), gomock.Any()).DoAndReturn(func(ownerAddr address.Address, height uint64) ([]*VoteBucket, error) {
489-
if height != csIndexerHeight {
490-
return nil, errors.Errorf("invalid height %d", height)
491-
}
492-
return []*VoteBucket{
493-
NewVoteBucket(identityset.Address(22), identityset.Address(22), big.NewInt(int64(csVotes)), 1, time.Now(), true),
494-
}, nil
495-
}).AnyTimes()
496-
497-
t.Run("contract staking indexer falls behind", func(t *testing.T) {
498-
_, err := p.ActiveCandidates(ctx, sm, 0)
499-
require.ErrorContains(err, "invalid height")
500-
})
501-
502-
t.Run("contract staking votes before Redsea", func(t *testing.T) {
503-
csIndexerHeight = blkHeight - 1
504-
csVotes = 0
505-
cands, err := p.ActiveCandidates(ctx, sm, 0)
506-
require.NoError(err)
507-
require.Len(cands, 1)
508-
originCandVotes := cands[0].Votes
509-
csVotes = 100
510-
cands, err = p.ActiveCandidates(ctx, sm, 0)
511-
require.NoError(err)
512-
require.Len(cands, 1)
513-
require.EqualValues(100, cands[0].Votes.Sub(cands[0].Votes, originCandVotes).Uint64())
514-
})
515-
t.Run("contract staking votes after Redsea", func(t *testing.T) {
516-
blkHeight = g.RedseaBlockHeight
517-
ctx := protocol.WithBlockCtx(
518-
genesis.WithGenesisContext(context.Background(), g),
519-
protocol.BlockCtx{
520-
BlockHeight: blkHeight,
521-
},
522-
)
523-
ctx = protocol.WithFeatureCtx(protocol.WithFeatureWithHeightCtx(ctx))
524-
csIndexerHeight = blkHeight - 1
525-
csVotes = 0
526-
cands, err := p.ActiveCandidates(ctx, sm, 0)
527-
require.NoError(err)
528-
require.Len(cands, 1)
529-
originCandVotes := cands[0].Votes
530-
csVotes = 100
531-
cands, err = p.ActiveCandidates(ctx, sm, 0)
532-
require.NoError(err)
533-
require.Len(cands, 1)
534-
require.EqualValues(103, cands[0].Votes.Sub(cands[0].Votes, originCandVotes).Uint64())
535-
})
536-
}
537-
538431
func TestIsSelfStakeBucket(t *testing.T) {
539432
r := require.New(t)
540433
ctrl := gomock.NewController(t)

action/protocol/staking/staking_statereader_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func TestStakingStateReader(t *testing.T) {
165165
}
166166
t.Run("readStateBuckets", func(t *testing.T) {
167167
sf, _, stakeSR, ctx, r := prepare(t)
168-
sf.EXPECT().States(gomock.Any(), gomock.Any()).DoAndReturn(func(arg0 ...protocol.StateOption) (uint64, state.Iterator, error) {
168+
sf.EXPECT().States(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(arg0 ...protocol.StateOption) (uint64, state.Iterator, error) {
169169
iter, err := state.NewIterator(keys, states)
170170
r.NoError(err)
171171
return uint64(1), iter, nil
@@ -195,7 +195,7 @@ func TestStakingStateReader(t *testing.T) {
195195
})
196196
t.Run("readStateBucketsWithEndorsement", func(t *testing.T) {
197197
sf, _, stakeSR, ctx, r := prepare(t)
198-
sf.EXPECT().States(gomock.Any(), gomock.Any()).DoAndReturn(func(arg0 ...protocol.StateOption) (uint64, state.Iterator, error) {
198+
sf.EXPECT().States(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(arg0 ...protocol.StateOption) (uint64, state.Iterator, error) {
199199
iter, err := state.NewIterator(keys, states)
200200
r.NoError(err)
201201
return uint64(1), iter, nil

blockindex/contractstaking/indexer.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,13 @@ func (s *Indexer) LoadStakeView(ctx context.Context, sr protocol.StateReader) (s
111111
if ok && !featureCtx.StoreVoteOfNFTBucketIntoView {
112112
return nil, nil
113113
}
114-
114+
srHeight, err := sr.Height()
115+
if err != nil {
116+
return nil, errors.Wrap(err, "failed to get state reader height")
117+
}
118+
if s.config.ContractDeployHeight <= srHeight && srHeight != s.height {
119+
return nil, errors.New("state reader height does not match indexer height")
120+
}
115121
ids, typs, infos := s.cache.Buckets()
116122
buckets := make(map[uint64]*contractstaking.Bucket)
117123
for i, id := range ids {

0 commit comments

Comments
 (0)