github.com/iotexproject/iotex-core@v1.14.1-rc1/action/protocol/staking/ethabi/stakebuckets_test.go (about) 1 package ethabi 2 3 import ( 4 "encoding/hex" 5 "reflect" 6 "testing" 7 8 "github.com/iotexproject/iotex-proto/golang/iotexapi" 9 "github.com/iotexproject/iotex-proto/golang/iotextypes" 10 "github.com/stretchr/testify/require" 11 "google.golang.org/protobuf/proto" 12 "google.golang.org/protobuf/types/known/timestamppb" 13 ) 14 15 func TestBuildReadStateRequestBuckets(t *testing.T) { 16 r := require.New(t) 17 18 data, _ := hex.DecodeString("b1ff5c2400000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000005") 19 req, err := BuildReadStateRequest(data) 20 21 r.Nil(err) 22 r.EqualValues("*ethabi.BucketsStateContext", reflect.TypeOf(req).String()) 23 24 method := &iotexapi.ReadStakingDataMethod{ 25 Method: iotexapi.ReadStakingDataMethod_BUCKETS, 26 } 27 methodBytes, _ := proto.Marshal(method) 28 r.EqualValues(methodBytes, req.Parameters().MethodName) 29 30 arguments := &iotexapi.ReadStakingDataRequest{ 31 Request: &iotexapi.ReadStakingDataRequest_Buckets{ 32 Buckets: &iotexapi.ReadStakingDataRequest_VoteBuckets{ 33 Pagination: &iotexapi.PaginationParam{ 34 Offset: 1, 35 Limit: 5, 36 }, 37 }, 38 }, 39 } 40 argumentsBytes, _ := proto.Marshal(arguments) 41 r.EqualValues([][]byte{argumentsBytes}, req.Parameters().Arguments) 42 } 43 44 func TestEncodeVoteBucketListToEth(t *testing.T) { 45 r := require.New(t) 46 47 buckets := make([]*iotextypes.VoteBucket, 2) 48 49 buckets[0] = &iotextypes.VoteBucket{ 50 Index: 1, 51 CandidateAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqryn4k9fw", 52 StakedAmount: "1000000000000000000", 53 StakedDuration: 1_000_000, 54 CreateTime: ×tamppb.Timestamp{Seconds: 1_000_000_000}, 55 StakeStartTime: ×tamppb.Timestamp{Seconds: 1_000_000_001}, 56 UnstakeStartTime: ×tamppb.Timestamp{Seconds: 1_000_000_002}, 57 AutoStake: true, 58 Owner: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqxgce2xkh", 59 } 60 buckets[1] = &iotextypes.VoteBucket{ 61 Index: 2, 62 CandidateAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqr9wrzs5u", 63 StakedAmount: "2000000000000000000", 64 StakedDuration: 2_000_000, 65 CreateTime: ×tamppb.Timestamp{Seconds: 1_000_000_100}, 66 StakeStartTime: ×tamppb.Timestamp{Seconds: 1_000_000_101}, 67 UnstakeStartTime: ×tamppb.Timestamp{Seconds: 1_000_000_102}, 68 AutoStake: false, 69 Owner: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqxf907nt9", 70 } 71 72 data, err := encodeVoteBucketListToEth(_bucketsMethod.Outputs, &iotextypes.VoteBucketList{ 73 Buckets: buckets, 74 }) 75 76 r.Nil(err) 77 r.EqualValues("00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000000000000000000000de0b6b3a764000000000000000000000000000000000000000000000000000000000000000f4240000000000000000000000000000000000000000000000000000000003b9aca00000000000000000000000000000000000000000000000000000000003b9aca01000000000000000000000000000000000000000000000000000000003b9aca02000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000c8000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000650000000000000000000000000000000000000000000000001bc16d674ec8000000000000000000000000000000000000000000000000000000000000001e8480000000000000000000000000000000000000000000000000000000003b9aca64000000000000000000000000000000000000000000000000000000003b9aca65000000000000000000000000000000000000000000000000000000003b9aca66000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c9", data) 78 } 79 80 func TestEncodeVoteBucketListToEthEmptyBuckets(t *testing.T) { 81 r := require.New(t) 82 83 buckets := make([]*iotextypes.VoteBucket, 0) 84 85 data, err := encodeVoteBucketListToEth(_bucketsMethod.Outputs, &iotextypes.VoteBucketList{ 86 Buckets: buckets, 87 }) 88 89 r.Nil(err) 90 r.EqualValues("00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000", data) 91 } 92 93 func TestEncodeVoteBucketListToEthErrorCandidateAddress(t *testing.T) { 94 r := require.New(t) 95 96 buckets := make([]*iotextypes.VoteBucket, 1) 97 98 buckets[0] = &iotextypes.VoteBucket{ 99 Index: 1, 100 CandidateAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqryn4k9f", 101 StakedAmount: "1000000000000000000", 102 StakedDuration: 1_000_000, 103 CreateTime: ×tamppb.Timestamp{Seconds: 1_000_000_000}, 104 StakeStartTime: ×tamppb.Timestamp{Seconds: 1_000_000_001}, 105 UnstakeStartTime: ×tamppb.Timestamp{Seconds: 1_000_000_002}, 106 AutoStake: true, 107 Owner: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqxgce2xkh", 108 } 109 110 _, err := encodeVoteBucketListToEth(_bucketsMethod.Outputs, &iotextypes.VoteBucketList{ 111 Buckets: buckets, 112 }) 113 114 r.EqualError(err, errInvalidMsg) 115 } 116 117 func TestEncodeVoteBucketListToEthErrorStakedAmount(t *testing.T) { 118 r := require.New(t) 119 120 buckets := make([]*iotextypes.VoteBucket, 1) 121 122 buckets[0] = &iotextypes.VoteBucket{ 123 Index: 1, 124 CandidateAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqryn4k9fw", 125 StakedAmount: "xxx", 126 StakedDuration: 1_000_000, 127 CreateTime: ×tamppb.Timestamp{Seconds: 1_000_000_000}, 128 StakeStartTime: ×tamppb.Timestamp{Seconds: 1_000_000_001}, 129 UnstakeStartTime: ×tamppb.Timestamp{Seconds: 1_000_000_002}, 130 AutoStake: true, 131 Owner: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqxgce2xkh", 132 } 133 134 _, err := encodeVoteBucketListToEth(_bucketsMethod.Outputs, &iotextypes.VoteBucketList{ 135 Buckets: buckets, 136 }) 137 138 r.EqualValues("convert big number error", err.Error()) 139 } 140 141 func TestEncodeVoteBucketListToEthErrorOwner(t *testing.T) { 142 r := require.New(t) 143 144 buckets := make([]*iotextypes.VoteBucket, 1) 145 146 buckets[0] = &iotextypes.VoteBucket{ 147 Index: 1, 148 CandidateAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqryn4k9fw", 149 StakedAmount: "1000000000000000000", 150 StakedDuration: 1_000_000, 151 CreateTime: ×tamppb.Timestamp{Seconds: 1_000_000_000}, 152 StakeStartTime: ×tamppb.Timestamp{Seconds: 1_000_000_001}, 153 UnstakeStartTime: ×tamppb.Timestamp{Seconds: 1_000_000_002}, 154 AutoStake: true, 155 Owner: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqxgce2xk", 156 } 157 158 _, err := encodeVoteBucketListToEth(_bucketsMethod.Outputs, &iotextypes.VoteBucketList{ 159 Buckets: buckets, 160 }) 161 162 r.EqualError(err, errInvalidMsg) 163 }