Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: tx fuzz testing #311

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

ChristianBorst
Copy link

Example of a Fuzz test using the recently released (June 3 2021) beta fuzz testing mechanism

@@ -1,4 +1,4 @@
PACKAGES=$(shell go list ./... | grep -v '/simulation')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation mentions that in order to get access to the beta fuzz feature you need to install gotip and use that in place of the go command:

$ go get golang.org/dl/gotip
$ gotip download dev.fuzz

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

func createSeedUint64ByteArrayWithValue(numElts int, value uint64) []byte {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fuzzing currently doesn't support typed arrays, only byte arrays. These methods make it easy to "seed" the fuzzer with example data to work off of


func FuzzAddToOutgoingPool(f *testing.F) {
numInputs := 6
ones := createSeedUint64ByteArrayWithValue(numInputs, uint64(1))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create some example uint64 arrays. I wanted to start out with fees all equal to 1, and amounts all equal to 100

ones := createSeedUint64ByteArrayWithValue(numInputs, uint64(1))
oneHundreds := createSeedUint64ByteArrayWithValue(numInputs, uint64(100))

f.Add(ones, oneHundreds, "0000000000000000000000000000000000000000")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Add function creates a single point of seed input into the fuzzer. I don't fully understand how it will use the seed data, but I'm guessing it just speeds up the time it takes to randomly generate acceptable inputs


f.Add(ones, oneHundreds, "0000000000000000000000000000000000000000")
f.Fuzz(func(t *testing.T, feez []byte, amountz []byte, contractAddr string) {
if types.ValidateEthAddress(contractAddr) != nil {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plenty of the input strings were too short, if the fuzzer generates a bad string then we just skip with t.Skip()

amounts[j] = binary.BigEndian.Uint64(amountz[64*j : 64*(j+1)])
}

input := CreateTestEnv(t)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From this point onward this test is basically a copy of the TestAddToOutgoingPool test above it

for i, _ := range fees {
txAmt := amounts[i]
txFee := fees[i]
if uint64(txAmt+txFee) >= balance {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the account doesn't have enough balance (starts with uint64 max) to cover this transaction, we modify the amount and fee to make the transaction work out. Later on we check that there are a correct number of transactions created so I didn't want to just skip.

@@ -0,0 +1,4 @@
go test fuzz v1
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file was generated automatically when I ran gotip test -fuzz=FuzzAddToOutgoingPool in the terminal. This is somehow related to the fuzzer's seed method f.Add I mentioned above, but I'm not sure how it gets generated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should probably be git-ignored

IntegralTeam pushed a commit to IntegralTeam/cosmos-gravity-bridge that referenced this pull request Dec 13, 2021
)

Fix the Document comments to correct refer to the --cosmos-phrase instead of --cosmos-key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants