|
| 1 | +package stakingindex |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "math/big" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + "go.uber.org/mock/gomock" |
| 10 | + |
| 11 | + "github.com/iotexproject/iotex-address/address" |
| 12 | + |
| 13 | + "github.com/iotexproject/iotex-core/v2/action/protocol" |
| 14 | + "github.com/iotexproject/iotex-core/v2/action/protocol/staking" |
| 15 | + "github.com/iotexproject/iotex-core/v2/action/protocol/staking/contractstaking" |
| 16 | + "github.com/iotexproject/iotex-core/v2/test/identityset" |
| 17 | +) |
| 18 | + |
| 19 | +func TestVoteView(t *testing.T) { |
| 20 | + require := require.New(t) |
| 21 | + ctrl := gomock.NewController(t) |
| 22 | + defer ctrl.Finish() |
| 23 | + |
| 24 | + mockIndexer := staking.NewMockContractStakingIndexer(ctrl) |
| 25 | + vv := NewVoteView( |
| 26 | + mockIndexer, |
| 27 | + &VoteViewConfig{ |
| 28 | + ContractAddr: identityset.Address(0), |
| 29 | + }, |
| 30 | + 100, |
| 31 | + nil, |
| 32 | + nil, |
| 33 | + func(b *contractstaking.Bucket, h uint64) *big.Int { |
| 34 | + return big.NewInt(1) |
| 35 | + }, |
| 36 | + ) |
| 37 | + mockHandler := staking.NewMockEventHandler(ctrl) |
| 38 | + t.Run("Migrate", func(t *testing.T) { |
| 39 | + buckets := make(map[uint64]*contractstaking.Bucket) |
| 40 | + buckets[1] = &contractstaking.Bucket{ |
| 41 | + Candidate: identityset.Address(1), |
| 42 | + Owner: identityset.Address(11), |
| 43 | + StakedAmount: big.NewInt(1000000000000000000), |
| 44 | + StakedDuration: 10, |
| 45 | + CreatedAt: 123456, |
| 46 | + UnlockedAt: 0, |
| 47 | + UnstakedAt: 0, |
| 48 | + IsTimestampBased: false, |
| 49 | + Muted: false, |
| 50 | + } |
| 51 | + buckets[2] = &contractstaking.Bucket{ |
| 52 | + Candidate: identityset.Address(2), |
| 53 | + Owner: identityset.Address(12), |
| 54 | + StakedAmount: big.NewInt(1000000000000000000), |
| 55 | + StakedDuration: 10, |
| 56 | + CreatedAt: 123456, |
| 57 | + UnlockedAt: 234567, |
| 58 | + UnstakedAt: 0, |
| 59 | + IsTimestampBased: true, |
| 60 | + Muted: false, |
| 61 | + } |
| 62 | + migratedBuckets := make(map[uint64]*contractstaking.Bucket) |
| 63 | + ctx := protocol.WithBlockCtx(context.Background(), protocol.BlockCtx{ |
| 64 | + BlockHeight: 100, |
| 65 | + }) |
| 66 | + mockIndexer.EXPECT().StartHeight().Return(uint64(0)).Times(1) |
| 67 | + mockIndexer.EXPECT().ContractStakingBuckets().Return(uint64(99), buckets, nil) |
| 68 | + mockHandler.EXPECT().PutBucket(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(addr address.Address, id uint64, bucket *contractstaking.Bucket) error { |
| 69 | + require.Equal(identityset.Address(0), addr) |
| 70 | + migratedBuckets[id] = bucket |
| 71 | + return nil |
| 72 | + }).Times(len(buckets)) |
| 73 | + require.NoError(vv.Migrate(ctx, mockHandler)) |
| 74 | + require.Equal(buckets, migratedBuckets) |
| 75 | + }) |
| 76 | +} |
0 commit comments