Skip to content

feat(ctb): MerkleTrie fuzzing#4008

Closed
clabby wants to merge 15 commits intosc/ctb-mini-trie-refactorfrom
@clabby/ctb/merkle-trie-fuzz
Closed

feat(ctb): MerkleTrie fuzzing#4008
clabby wants to merge 15 commits intosc/ctb-mini-trie-refactorfrom
@clabby/ctb/merkle-trie-fuzz

Conversation

@clabby
Copy link
Contributor

@clabby clabby commented Nov 15, 2022

Overview

WIP

Adds a fuzz test input generator for testing Bedrock's MerkleTrie.sol.

Go version of #3847

Tests

Adds fuzz tests for MerkleTrie

Additional context

n/a

Metadata
n/a

smartcontracts and others added 2 commits November 1, 2022 14:14
Small refactor that has MerkleTrie.get throw explicitly when an element
does not exist, rather than returning a non-existence boolean. Makes the
code much cleaner. We need to review this change very carefully.
@changeset-bot
Copy link

changeset-bot bot commented Nov 15, 2022

🦋 Changeset detected

Latest commit: c84e567

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
@eth-optimism/contracts-bedrock Patch
@eth-optimism/actor-tests Patch
@eth-optimism/sdk Patch
@eth-optimism/drippie-mon Patch
@eth-optimism/message-relayer Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@clabby clabby marked this pull request as draft November 15, 2022 20:59
@github-actions github-actions bot added 2-reviewers A-pkg-contracts-bedrock Area: packages/contracts-bedrock labels Nov 15, 2022
@clabby clabby force-pushed the @clabby/ctb/merkle-trie-fuzz branch from 11be109 to f6d0738 Compare November 15, 2022 21:51
@clabby clabby force-pushed the @clabby/ctb/merkle-trie-fuzz branch from f1001d0 to d033241 Compare November 18, 2022 02:47
@github-actions github-actions bot added C-protocol-critical Category: Modifies protocol-critical code M-contracts A-op-bindings Area: op-bindings A-pkg-core-utils Area: packages/core-utils A-integration Area: integration tests A-cannon Area: cannon A-ops Area: ops labels Nov 18, 2022
@mergify mergify bot requested a review from jvmi7 November 18, 2022 03:03
@clabby clabby force-pushed the @clabby/ctb/merkle-trie-fuzz branch from 82f42d1 to eb29cf9 Compare November 18, 2022 03:11
@github-actions github-actions bot removed M-contracts A-cannon Area: cannon A-integration Area: integration tests A-ops Area: ops A-pkg-sdk Area: packages/sdk C-protocol-critical Category: Modifies protocol-critical code A-op-bindings Area: op-bindings A-pkg-core-utils Area: packages/core-utils labels Nov 18, 2022
Copy link
Contributor

@trianglesphere trianglesphere left a comment

Choose a reason for hiding this comment

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

Really nice work. I've left a bunch of comments, but they're primarily focused on go tips & tricks and go style.

// Helper that generates a cryptographically secure random 64-bit integer
// between the range [min, max]
func randRange(min int64, max int64) int64 {
r, err := rand.Int(rand.Reader, new(big.Int).Sub(new(big.Int).SetInt64(max), new(big.Int).SetInt64(min)))
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd suggest using math/rand.Intn here. I don't think we need a cryptographically secure random number for fuzzing. math/rand will be faster in general & will avoid all of the big int math.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The idea behind switching to crypto/rand was to allow for randomness while executing these tests in parallel. Originally, I had passed the hash of a 32 byte fuzz input from the foundry test + the current time as the seed for math/rand, but felt this was a cleaner solution. See mark's above comment


var testCase trieTestCase
switch variant {
case "valid":
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer an enum (+ documentation) for the variants. In go they're typically implemented as follows (though we probably forgot the type inside the const statement).

type BatchValidity uint8
const (
// BatchDrop indicates that the batch is invalid, and will always be in the future, unless we reorg
BatchDrop = iota
// BatchAccept indicates that the batch is valid and should be processed
BatchAccept
// BatchUndecided indicates we are lacking L1 information until we can proceed batch filtering
BatchUndecided
// BatchFuture indicates that the batch may be valid, but cannot be processed yet and should be checked again later
BatchFuture
)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Swapped over to using enums- went with string variants so that they are readable within foundry tests w/o having to reference the variant docs.

i.e.

ffi.getMerkleTrieFuzzCase("valid") reads better than ffi.getMerkleTrieFuzzCase(0)

@@ -0,0 +1,13 @@
# `go-fuzz`
Copy link
Contributor

Choose a reason for hiding this comment

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

go-fuzz conflicts with some existing projects (https://github.com/google/gofuzz & https://github.com/dvyukov/go-fuzz). This isn't the end of the world, but I think that a more descriptive name (something along of the lines of TrieTestCaseGenerator would be a bit better).

@smartcontracts smartcontracts force-pushed the sc/ctb-mini-trie-refactor branch from b6058a3 to 991120f Compare November 29, 2022 19:01
@mergify
Copy link
Contributor

mergify bot commented Nov 29, 2022

Hey @clabby! This PR has merge conflicts. Please fix them before continuing review.

@mergify mergify bot added the conflict label Nov 29, 2022
@mergify mergify bot removed the conflict label Dec 1, 2022
@clabby clabby mentioned this pull request Dec 1, 2022
@clabby
Copy link
Contributor Author

clabby commented Dec 1, 2022

Reopened in #4142 - certain CI tasks didn't like the branch name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-indexer Area: indexer A-pkg-contracts-bedrock Area: packages/contracts-bedrock

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants