Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit dd6b02b

Browse files
committedAug 26, 2021
Example fuzz testing
1 parent 92d0e12 commit dd6b02b

File tree

5 files changed

+129
-7
lines changed

5 files changed

+129
-7
lines changed
 

‎module/Makefile

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PACKAGES=$(shell go list ./... | grep -v '/simulation')
1+
PACKAGES=$(shell gotip list ./... | grep -v '/simulation')
22
VERSION := $(shell git describe --abbrev=6 --dirty --always --tags)
33
COMMIT := $(shell git log -1 --format='%H')
44
DOCKER := $(shell which docker)
@@ -50,20 +50,20 @@ BUILD_FLAGS := -ldflags '$(ldflags)' -gcflags="all=-N -l"
5050
all: install
5151

5252
install: go.sum
53-
go install $(BUILD_FLAGS) ./cmd/gravity
53+
gotip install $(BUILD_FLAGS) ./cmd/gravity
5454

5555
go.sum: go.mod
5656
@echo "--> Ensure dependencies have not been modified"
57-
GO111MODULE=on go mod verify
57+
GO111MODULE=on gotip mod verify
5858

5959
test:
60-
@go test -mod=readonly $(PACKAGES)
60+
@gotip test -mod=readonly $(PACKAGES)
6161

6262
# look into .golangci.yml for enabling / disabling linters
6363
lint:
6464
@echo "--> Running linter"
6565
@golangci-lint run
66-
@go mod verify
66+
@gotip mod verify
6767

6868
###############################################################################
6969
### Protobuf ###
@@ -161,7 +161,7 @@ proto-tools-stamp:
161161
rm -f ${PROTOC_ZIP})
162162

163163
echo "Installing protoc-gen-gocosmos..."
164-
go install github.com/regen-network/cosmos-proto/protoc-gen-gocosmos
164+
gotip install github.com/regen-network/cosmos-proto/protoc-gen-gocosmos
165165

166166
# Create dummy file to satisfy dependency and avoid
167167
# rebuilding when this Makefile target is hit twice

‎module/go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ go 1.15
44

55
require (
66
github.com/cosmos/cosmos-sdk v0.42.1
7+
github.com/cosmos/iavl v0.15.3
78
github.com/ethereum/go-ethereum v1.10.3
89
github.com/gogo/protobuf v1.3.3
910
github.com/golang/protobuf v1.4.3
11+
github.com/google/gofuzz v1.2.0 // indirect
1012
github.com/gorilla/mux v1.8.0
1113
github.com/grpc-ecosystem/grpc-gateway v1.16.0
14+
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d
1215
github.com/pkg/errors v0.9.1
1316
github.com/rakyll/statik v0.1.7
1417
github.com/spf13/cast v1.3.1

‎module/go.sum

+2-1
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,9 @@ github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
307307
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
308308
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI=
309309
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
310-
github.com/google/gofuzz v1.1.1-0.20200604201612-c04b05f3adfa h1:Q75Upo5UN4JbPFURXZ8nLKYUvF85dyFRop/vQ0Rv+64=
311310
github.com/google/gofuzz v1.1.1-0.20200604201612-c04b05f3adfa/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
311+
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
312+
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
312313
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
313314
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
314315
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=

‎module/x/gravity/keeper/pool_test.go

+114
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
// +build gofuzzbeta
2+
13
package keeper
24

35
import (
6+
"encoding/binary"
7+
"fmt"
8+
"math"
49
"math/big"
510
"testing"
611

@@ -79,6 +84,115 @@ func TestAddToOutgoingPool(t *testing.T) {
7984
assert.Equal(t, exp, got)
8085
}
8186

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+
82196
func TestTotalBatchFeeInPool(t *testing.T) {
83197
input := CreateTestEnv(t)
84198
ctx := input.Context
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
go test fuzz v1
2+
[]byte("\x01")
3+
[]byte("d")
4+
string("0")

0 commit comments

Comments
 (0)
Please sign in to comment.