github.com/iotexproject/iotex-core@v1.14.1-rc1/action/protocol/staking/ethabi/stakebase_test.go (about) 1 package ethabi 2 3 import ( 4 "encoding/hex" 5 "testing" 6 7 "github.com/iotexproject/iotex-proto/golang/iotextypes" 8 "github.com/stretchr/testify/require" 9 ) 10 11 var errInvalidMsg = "address length = 40, expecting 41: invalid address" 12 13 func TestBuildReadStateRequestError(t *testing.T) { 14 r := require.New(t) 15 16 data, _ := hex.DecodeString("8ae8a8a4") 17 req, err := BuildReadStateRequest(data) 18 19 r.Nil(req) 20 r.EqualValues("invalid call sig", err.Error()) 21 } 22 23 func TestBuildReadStateRequestInvalid(t *testing.T) { 24 r := require.New(t) 25 26 data, _ := hex.DecodeString("8ae8a8") 27 req, err := BuildReadStateRequest(data) 28 29 r.Nil(req) 30 r.EqualValues("invalid call binary data", err.Error()) 31 } 32 33 func TestEncodeCandidateToEth(t *testing.T) { 34 r := require.New(t) 35 36 candidate := &iotextypes.CandidateV2{ 37 OwnerAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqps833xv", 38 OperatorAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqz75y8gn", 39 RewardAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqrrzsj4p", 40 Name: "hello", 41 TotalWeightedVotes: "10000000000000000000", 42 SelfStakeBucketIdx: 100, 43 SelfStakingTokens: "5000000000000000000", 44 } 45 46 cand, err := encodeCandidateToEth(candidate) 47 48 r.Nil(err) 49 r.EqualValues("0x0000000000000000000000000000000000000001", cand.OwnerAddress.Hex()) 50 r.EqualValues("0x0000000000000000000000000000000000000002", cand.OperatorAddress.Hex()) 51 r.EqualValues("0x0000000000000000000000000000000000000003", cand.RewardAddress.Hex()) 52 r.EqualValues("hello", cand.Name) 53 r.EqualValues("10000000000000000000", cand.TotalWeightedVotes.String()) 54 r.EqualValues(100, cand.SelfStakeBucketIdx) 55 r.EqualValues("5000000000000000000", cand.SelfStakingTokens.String()) 56 } 57 58 func TestEncodeCandidateToEthErrorOwnerAddress(t *testing.T) { 59 r := require.New(t) 60 61 candidate := &iotextypes.CandidateV2{ 62 OwnerAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqps833x", 63 OperatorAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqz75y8gn", 64 RewardAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqrrzsj4p", 65 Name: "hello", 66 TotalWeightedVotes: "10000000000000000000", 67 SelfStakeBucketIdx: 100, 68 SelfStakingTokens: "5000000000000000000", 69 } 70 71 cand, err := encodeCandidateToEth(candidate) 72 73 r.Nil(cand) 74 r.EqualError(err, errInvalidMsg) 75 } 76 77 func TestEncodeCandidateToEthErrorOperatorAddress(t *testing.T) { 78 r := require.New(t) 79 80 candidate := &iotextypes.CandidateV2{ 81 OwnerAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqps833xv", 82 OperatorAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqz75y8g", 83 RewardAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqrrzsj4p", 84 Name: "hello", 85 TotalWeightedVotes: "10000000000000000000", 86 SelfStakeBucketIdx: 100, 87 SelfStakingTokens: "5000000000000000000", 88 } 89 90 cand, err := encodeCandidateToEth(candidate) 91 92 r.Nil(cand) 93 r.EqualError(err, errInvalidMsg) 94 } 95 96 func TestEncodeCandidateToEthErrorRewardAddress(t *testing.T) { 97 r := require.New(t) 98 99 candidate := &iotextypes.CandidateV2{ 100 OwnerAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqps833xv", 101 OperatorAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqz75y8gn", 102 RewardAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqrrzddd", 103 Name: "hello", 104 TotalWeightedVotes: "10000000000000000000", 105 SelfStakeBucketIdx: 100, 106 SelfStakingTokens: "5000000000000000000", 107 } 108 109 cand, err := encodeCandidateToEth(candidate) 110 111 r.Nil(cand) 112 r.EqualError(err, errInvalidMsg) 113 } 114 115 func TestEncodeCandidateToEthErrorTotalWeightedVotes(t *testing.T) { 116 r := require.New(t) 117 118 candidate := &iotextypes.CandidateV2{ 119 OwnerAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqps833xv", 120 OperatorAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqz75y8gn", 121 RewardAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqrrzsj4p", 122 Name: "hello", 123 TotalWeightedVotes: "XXX", 124 SelfStakeBucketIdx: 100, 125 SelfStakingTokens: "5000000000000000000", 126 } 127 128 cand, err := encodeCandidateToEth(candidate) 129 130 r.Nil(cand) 131 r.EqualValues("convert big number error", err.Error()) 132 } 133 134 func TestEncodeCandidateToEthErrorSelfStakingTokens(t *testing.T) { 135 r := require.New(t) 136 137 candidate := &iotextypes.CandidateV2{ 138 OwnerAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqps833xv", 139 OperatorAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqz75y8gn", 140 RewardAddress: "io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqrrzsj4p", 141 Name: "hello", 142 TotalWeightedVotes: "5000000000000000000", 143 SelfStakeBucketIdx: 100, 144 SelfStakingTokens: "XXXX", 145 } 146 147 cand, err := encodeCandidateToEth(candidate) 148 149 r.Nil(cand) 150 r.EqualValues("convert big number error", err.Error()) 151 }