|
| 1 | +// +build gofuzzbeta |
| 2 | + |
1 | 3 | package keeper
|
2 | 4 |
|
3 | 5 | import (
|
| 6 | + "encoding/binary" |
| 7 | + "fmt" |
| 8 | + "math" |
4 | 9 | "math/big"
|
5 | 10 | "testing"
|
6 | 11 |
|
@@ -79,6 +84,115 @@ func TestAddToOutgoingPool(t *testing.T) {
|
79 | 84 | assert.Equal(t, exp, got)
|
80 | 85 | }
|
81 | 86 |
|
| 87 | +func createSeedUint64ByteArrayWithValue(numElts int, value uint64) []byte { |
| 88 | + numBytes := numElts * 64 |
| 89 | + arr := make([]byte, numBytes) |
| 90 | + for i := 0; i < numElts; i++ { |
| 91 | + binary.PutUvarint(arr[64*i:64*(i+1)], value) |
| 92 | + } |
| 93 | + return arr |
| 94 | +} |
| 95 | + |
| 96 | +func deserializeByteArrayToUint64Array(bytes []byte, numElts int) []uint64 { |
| 97 | + uints := make([]uint64, numElts) |
| 98 | + |
| 99 | + for j := 0; j < numElts; j++ { |
| 100 | + uints[j] = binary.BigEndian.Uint64(bytes[64*j : 64*(j+1)]) |
| 101 | + } |
| 102 | + return uints |
| 103 | +} |
| 104 | + |
| 105 | +func FuzzAddToOutgoingPool(f *testing.F) { |
| 106 | + numInputs := 6 |
| 107 | + ones := createSeedUint64ByteArrayWithValue(numInputs, uint64(1)) |
| 108 | + oneHundreds := createSeedUint64ByteArrayWithValue(numInputs, uint64(100)) |
| 109 | + |
| 110 | + f.Add(ones, oneHundreds, "0000000000000000000000000000000000000000") |
| 111 | + f.Fuzz(func(t *testing.T, feez []byte, amountz []byte, contractAddr string) { |
| 112 | + shouldFail := false |
| 113 | + if types.ValidateEthAddress(contractAddr) != nil { |
| 114 | + shouldFail = true |
| 115 | + } |
| 116 | + fees := deserializeByteArrayToUint64Array(feez, numInputs) |
| 117 | + amounts := deserializeByteArrayToUint64Array(amountz, numInputs) |
| 118 | + for j := 0; j < numInputs; j++ { |
| 119 | + fees[j] = binary.BigEndian.Uint64(feez[64*j : 64*(j+1)]) |
| 120 | + amounts[j] = binary.BigEndian.Uint64(amountz[64*j : 64*(j+1)]) |
| 121 | + } |
| 122 | + |
| 123 | + input := CreateTestEnv(t) |
| 124 | + ctx := input.Context |
| 125 | + var ( |
| 126 | + mySender, _ = sdk.AccAddressFromBech32("cosmos1ahx7f8wyertuus9r20284ej0asrs085case3kn") |
| 127 | + myReceiver = "0xd041c41EA1bf0F006ADBb6d2c9ef9D425dE5eaD7" |
| 128 | + myTokenContractAddr = "0x" + contractAddr |
| 129 | + ) |
| 130 | + |
| 131 | + // mint some voucher first |
| 132 | + var balance uint64 = math.MaxUint64 |
| 133 | + allVouchers := sdk.Coins{types.NewERC20Token(balance, myTokenContractAddr).GravityCoin()} |
| 134 | + err := input.BankKeeper.MintCoins(ctx, types.ModuleName, allVouchers) |
| 135 | + if err != nil { |
| 136 | + if shouldFail { |
| 137 | + t.Skip() |
| 138 | + } else { |
| 139 | + t.Fatal(err) |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + // set senders balance |
| 144 | + input.AccountKeeper.NewAccountWithAddress(ctx, mySender) |
| 145 | + err = input.BankKeeper.SetBalances(ctx, mySender, allVouchers) |
| 146 | + if err != nil { |
| 147 | + if shouldFail { |
| 148 | + t.Skip() |
| 149 | + } else { |
| 150 | + t.Fatal(err) |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + transacts := make([]*types.OutgoingTransferTx, len(fees)) |
| 155 | + // create transactions |
| 156 | + for i, _ := range fees { |
| 157 | + txAmt := amounts[i] |
| 158 | + txFee := fees[i] |
| 159 | + if uint64(txAmt+txFee) >= balance { |
| 160 | + txAmt = uint64(balance / 2) |
| 161 | + txFee = uint64(txAmt / 2) |
| 162 | + } |
| 163 | + amount := types.NewERC20Token(txAmt, myTokenContractAddr).GravityCoin() |
| 164 | + fee := types.NewERC20Token(txFee, myTokenContractAddr).GravityCoin() |
| 165 | + transacts[i] = &types.OutgoingTransferTx{ |
| 166 | + Id: uint64(i + 1), |
| 167 | + Sender: mySender.String(), |
| 168 | + DestAddress: myReceiver, |
| 169 | + Erc20Token: types.NewSDKIntERC20Token(amount.Amount, amount.Denom), |
| 170 | + Erc20Fee: types.NewSDKIntERC20Token(fee.Amount, fee.Denom), |
| 171 | + } |
| 172 | + r, err := input.GravityKeeper.AddToOutgoingPool(ctx, mySender, myReceiver, amount, fee) |
| 173 | + balance = balance - uint64(txAmt+txFee) |
| 174 | + require.NoError(t, err) |
| 175 | + t.Logf("___ response: %#v", r) |
| 176 | + // Should create: |
| 177 | + // 1: amount 100, fee 2 |
| 178 | + // 2: amount 101, fee 3 |
| 179 | + // 3: amount 102, fee 2 |
| 180 | + // 4: amount 103, fee 1 |
| 181 | + |
| 182 | + } |
| 183 | + |
| 184 | + got := input.GravityKeeper.GetUnbatchedTransactionsByContract(ctx, myTokenContractAddr) |
| 185 | + if len(got) != len(fees) { |
| 186 | + if shouldFail { |
| 187 | + t.Skip() |
| 188 | + } else { |
| 189 | + t.Fatal(fmt.Errorf("generated transactions do not match ones received\nexpected: %v\nreceived: %v", transacts, got)) |
| 190 | + } |
| 191 | + } |
| 192 | + }) |
| 193 | + |
| 194 | +} |
| 195 | + |
82 | 196 | func TestTotalBatchFeeInPool(t *testing.T) {
|
83 | 197 | input := CreateTestEnv(t)
|
84 | 198 | ctx := input.Context
|
|
0 commit comments