Commit d75a38a3 authored by Mark Tyneway's avatar Mark Tyneway Committed by GitHub

Merge pull request #7807 from ethereum-optimism/cl/ctb/consolidate-go-ffi

feat(ctb): Cleanup ffi utils
parents c7d9a7cc 1672ca53
......@@ -307,81 +307,81 @@ LegacyERC20ETH_Test:test_transferFrom_doesNotExist_reverts() (gas: 12957)
LegacyERC20ETH_Test:test_transfer_doesNotExist_reverts() (gas: 10755)
LegacyMessagePasser_Test:test_passMessageToL1_succeeds() (gas: 34524)
LibPosition_Test:test_pos_correctness_succeeds() (gas: 38689)
MIPS_Test:test_add_succeeds() (gas: 122466)
MIPS_Test:test_addiSign_succeeds() (gas: 122457)
MIPS_Test:test_addi_succeeds() (gas: 122654)
MIPS_Test:test_addu_succeeds() (gas: 122508)
MIPS_Test:test_addui_succeeds() (gas: 122716)
MIPS_Test:test_and_succeeds() (gas: 122527)
MIPS_Test:test_andi_succeeds() (gas: 122460)
MIPS_Test:test_beq_succeeds() (gas: 202893)
MIPS_Test:test_bgez_succeeds() (gas: 121753)
MIPS_Test:test_bgtz_succeeds() (gas: 121674)
MIPS_Test:test_blez_succeeds() (gas: 121630)
MIPS_Test:test_bltz_succeeds() (gas: 121773)
MIPS_Test:test_bne_succeeds() (gas: 121839)
MIPS_Test:test_branch_inDelaySlot_fails() (gas: 86048)
MIPS_Test:test_brk_succeeds() (gas: 122143)
MIPS_Test:test_clo_succeeds() (gas: 122195)
MIPS_Test:test_clone_succeeds() (gas: 122096)
MIPS_Test:test_clz_succeeds() (gas: 122666)
MIPS_Test:test_div_succeeds() (gas: 122645)
MIPS_Test:test_divu_succeeds() (gas: 122630)
MIPS_Test:test_exit_succeeds() (gas: 122145)
MIPS_Test:test_fcntl_succeeds() (gas: 204375)
MIPS_Test:test_illegal_instruction_fails() (gas: 91511)
MIPS_Test:test_invalid_root_fails() (gas: 435685)
MIPS_Test:test_jal_nonzeroRegion_succeeds() (gas: 120783)
MIPS_Test:test_jal_succeeds() (gas: 120772)
MIPS_Test:test_jalr_succeeds() (gas: 121891)
MIPS_Test:test_jr_succeeds() (gas: 121585)
MIPS_Test:test_jump_inDelaySlot_fails() (gas: 85416)
MIPS_Test:test_jump_nonzeroRegion_succeeds() (gas: 120527)
MIPS_Test:test_jump_succeeds() (gas: 120457)
MIPS_Test:test_lb_succeeds() (gas: 127698)
MIPS_Test:test_lbu_succeeds() (gas: 127596)
MIPS_Test:test_lh_succeeds() (gas: 127719)
MIPS_Test:test_lhu_succeeds() (gas: 127636)
MIPS_Test:test_ll_succeeds() (gas: 127858)
MIPS_Test:test_lui_succeeds() (gas: 121739)
MIPS_Test:test_lw_succeeds() (gas: 127487)
MIPS_Test:test_lwl_succeeds() (gas: 242138)
MIPS_Test:test_lwr_succeeds() (gas: 242426)
MIPS_Test:test_mfhi_succeeds() (gas: 122100)
MIPS_Test:test_mflo_succeeds() (gas: 122229)
MIPS_Test:test_mmap_succeeds() (gas: 119063)
MIPS_Test:test_movn_succeeds() (gas: 203565)
MIPS_Test:test_movz_succeeds() (gas: 203433)
MIPS_Test:test_mthi_succeeds() (gas: 122144)
MIPS_Test:test_mtlo_succeeds() (gas: 122252)
MIPS_Test:test_mul_succeeds() (gas: 121744)
MIPS_Test:test_mult_succeeds() (gas: 122448)
MIPS_Test:test_multu_succeeds() (gas: 122485)
MIPS_Test:test_nor_succeeds() (gas: 122577)
MIPS_Test:test_or_succeeds() (gas: 122534)
MIPS_Test:test_ori_succeeds() (gas: 122537)
MIPS_Test:test_preimage_read_succeeds() (gas: 234549)
MIPS_Test:test_preimage_write_succeeds() (gas: 127085)
MIPS_Test:test_prestate_exited_succeeds() (gas: 113326)
MIPS_Test:test_sb_succeeds() (gas: 160569)
MIPS_Test:test_sc_succeeds() (gas: 160752)
MIPS_Test:test_sh_succeeds() (gas: 160606)
MIPS_Test:test_sll_succeeds() (gas: 121705)
MIPS_Test:test_sllv_succeeds() (gas: 121934)
MIPS_Test:test_slt_succeeds() (gas: 204760)
MIPS_Test:test_sltu_succeeds() (gas: 122751)
MIPS_Test:test_sra_succeeds() (gas: 121956)
MIPS_Test:test_srav_succeeds() (gas: 122224)
MIPS_Test:test_srl_succeeds() (gas: 121787)
MIPS_Test:test_srlv_succeeds() (gas: 121952)
MIPS_Test:test_add_succeeds() (gas: 122932)
MIPS_Test:test_addiSign_succeeds() (gas: 122923)
MIPS_Test:test_addi_succeeds() (gas: 123120)
MIPS_Test:test_addu_succeeds() (gas: 122974)
MIPS_Test:test_addui_succeeds() (gas: 123182)
MIPS_Test:test_and_succeeds() (gas: 122993)
MIPS_Test:test_andi_succeeds() (gas: 122926)
MIPS_Test:test_beq_succeeds() (gas: 203359)
MIPS_Test:test_bgez_succeeds() (gas: 122219)
MIPS_Test:test_bgtz_succeeds() (gas: 122140)
MIPS_Test:test_blez_succeeds() (gas: 122096)
MIPS_Test:test_bltz_succeeds() (gas: 122239)
MIPS_Test:test_bne_succeeds() (gas: 122305)
MIPS_Test:test_branch_inDelaySlot_fails() (gas: 86514)
MIPS_Test:test_brk_succeeds() (gas: 122609)
MIPS_Test:test_clo_succeeds() (gas: 122661)
MIPS_Test:test_clone_succeeds() (gas: 122562)
MIPS_Test:test_clz_succeeds() (gas: 123132)
MIPS_Test:test_div_succeeds() (gas: 123111)
MIPS_Test:test_divu_succeeds() (gas: 123096)
MIPS_Test:test_exit_succeeds() (gas: 122611)
MIPS_Test:test_fcntl_succeeds() (gas: 204841)
MIPS_Test:test_illegal_instruction_fails() (gas: 91977)
MIPS_Test:test_invalid_root_fails() (gas: 436151)
MIPS_Test:test_jal_nonzeroRegion_succeeds() (gas: 121249)
MIPS_Test:test_jal_succeeds() (gas: 121238)
MIPS_Test:test_jalr_succeeds() (gas: 122357)
MIPS_Test:test_jr_succeeds() (gas: 122051)
MIPS_Test:test_jump_inDelaySlot_fails() (gas: 85882)
MIPS_Test:test_jump_nonzeroRegion_succeeds() (gas: 120993)
MIPS_Test:test_jump_succeeds() (gas: 120923)
MIPS_Test:test_lb_succeeds() (gas: 128164)
MIPS_Test:test_lbu_succeeds() (gas: 128062)
MIPS_Test:test_lh_succeeds() (gas: 128185)
MIPS_Test:test_lhu_succeeds() (gas: 128102)
MIPS_Test:test_ll_succeeds() (gas: 128324)
MIPS_Test:test_lui_succeeds() (gas: 122205)
MIPS_Test:test_lw_succeeds() (gas: 127953)
MIPS_Test:test_lwl_succeeds() (gas: 243070)
MIPS_Test:test_lwr_succeeds() (gas: 243358)
MIPS_Test:test_mfhi_succeeds() (gas: 122566)
MIPS_Test:test_mflo_succeeds() (gas: 122695)
MIPS_Test:test_mmap_succeeds() (gas: 119529)
MIPS_Test:test_movn_succeeds() (gas: 204031)
MIPS_Test:test_movz_succeeds() (gas: 203899)
MIPS_Test:test_mthi_succeeds() (gas: 122610)
MIPS_Test:test_mtlo_succeeds() (gas: 122718)
MIPS_Test:test_mul_succeeds() (gas: 122210)
MIPS_Test:test_mult_succeeds() (gas: 122914)
MIPS_Test:test_multu_succeeds() (gas: 122951)
MIPS_Test:test_nor_succeeds() (gas: 123043)
MIPS_Test:test_or_succeeds() (gas: 123000)
MIPS_Test:test_ori_succeeds() (gas: 123003)
MIPS_Test:test_preimage_read_succeeds() (gas: 235481)
MIPS_Test:test_preimage_write_succeeds() (gas: 127551)
MIPS_Test:test_prestate_exited_succeeds() (gas: 113792)
MIPS_Test:test_sb_succeeds() (gas: 161501)
MIPS_Test:test_sc_succeeds() (gas: 161684)
MIPS_Test:test_sh_succeeds() (gas: 161538)
MIPS_Test:test_sll_succeeds() (gas: 122171)
MIPS_Test:test_sllv_succeeds() (gas: 122400)
MIPS_Test:test_slt_succeeds() (gas: 205226)
MIPS_Test:test_sltu_succeeds() (gas: 123217)
MIPS_Test:test_sra_succeeds() (gas: 122422)
MIPS_Test:test_srav_succeeds() (gas: 122690)
MIPS_Test:test_srl_succeeds() (gas: 122253)
MIPS_Test:test_srlv_succeeds() (gas: 122418)
MIPS_Test:test_step_abi_succeeds() (gas: 58467)
MIPS_Test:test_sub_succeeds() (gas: 122561)
MIPS_Test:test_subu_succeeds() (gas: 122558)
MIPS_Test:test_sw_succeeds() (gas: 160581)
MIPS_Test:test_swl_succeeds() (gas: 160642)
MIPS_Test:test_swr_succeeds() (gas: 160717)
MIPS_Test:test_xor_succeeds() (gas: 122562)
MIPS_Test:test_xori_succeeds() (gas: 122614)
MIPS_Test:test_sub_succeeds() (gas: 123027)
MIPS_Test:test_subu_succeeds() (gas: 123024)
MIPS_Test:test_sw_succeeds() (gas: 161513)
MIPS_Test:test_swl_succeeds() (gas: 161574)
MIPS_Test:test_swr_succeeds() (gas: 161649)
MIPS_Test:test_xor_succeeds() (gas: 123028)
MIPS_Test:test_xori_succeeds() (gas: 123080)
MerkleTrie_get_Test:test_get_corruptedProof_reverts() (gas: 5733)
MerkleTrie_get_Test:test_get_extraProofElements_reverts() (gas: 58889)
MerkleTrie_get_Test:test_get_invalidDataRemainder_reverts() (gas: 35845)
......@@ -447,9 +447,9 @@ OptimismPortal_FinalizeWithdrawal_Test:test_finalizeWithdrawalTransaction_ifOutp
OptimismPortal_FinalizeWithdrawal_Test:test_finalizeWithdrawalTransaction_ifOutputTimestampIsNotFinalized_reverts() (gas: 182306)
OptimismPortal_FinalizeWithdrawal_Test:test_finalizeWithdrawalTransaction_ifWithdrawalNotProven_reverts() (gas: 41780)
OptimismPortal_FinalizeWithdrawal_Test:test_finalizeWithdrawalTransaction_ifWithdrawalProofNotOldEnough_reverts() (gas: 173953)
OptimismPortal_FinalizeWithdrawal_Test:test_finalizeWithdrawalTransaction_onInsufficientGas_reverts() (gas: 180724)
OptimismPortal_FinalizeWithdrawal_Test:test_finalizeWithdrawalTransaction_onInsufficientGas_reverts() (gas: 181188)
OptimismPortal_FinalizeWithdrawal_Test:test_finalizeWithdrawalTransaction_onRecentWithdrawal_reverts() (gas: 154740)
OptimismPortal_FinalizeWithdrawal_Test:test_finalizeWithdrawalTransaction_onReentrancy_reverts() (gas: 218770)
OptimismPortal_FinalizeWithdrawal_Test:test_finalizeWithdrawalTransaction_onReentrancy_reverts() (gas: 219235)
OptimismPortal_FinalizeWithdrawal_Test:test_finalizeWithdrawalTransaction_onReplay_reverts() (gas: 220983)
OptimismPortal_FinalizeWithdrawal_Test:test_finalizeWithdrawalTransaction_paused_reverts() (gas: 38706)
OptimismPortal_FinalizeWithdrawal_Test:test_finalizeWithdrawalTransaction_provenWithdrawalHash_succeeds() (gas: 209679)
......@@ -660,9 +660,9 @@ SequencerFeeVault_Test:test_withdraw_toL1_succeeds() (gas: 171675)
SetPrevBaseFee_Test:test_setPrevBaseFee_succeeds() (gas: 11549)
StandardBridge_Stateless_Test:test_isCorrectTokenPair_succeeds() (gas: 49936)
StandardBridge_Stateless_Test:test_isOptimismMintableERC20_succeeds() (gas: 33072)
Storage_Roundtrip_Test:test_setGetAddress_succeeds(bytes32,address) (runs: 64, μ: 31821, ~: 31821)
Storage_Roundtrip_Test:test_setGetAddress_succeeds(bytes32,address) (runs: 64, μ: 31510, ~: 31821)
Storage_Roundtrip_Test:test_setGetBytes32_succeeds(bytes32,bytes32) (runs: 64, μ: 31598, ~: 31598)
Storage_Roundtrip_Test:test_setGetUint_succeeds(bytes32,uint256) (runs: 64, μ: 30731, ~: 31664)
Storage_Roundtrip_Test:test_setGetUint_succeeds(bytes32,uint256) (runs: 64, μ: 30420, ~: 31664)
SystemConfig_Initialize_Test:test_initialize_events_succeeds() (gas: 71972)
SystemConfig_Initialize_Test:test_initialize_startBlockNoop_reverts() (gas: 81247)
SystemConfig_Initialize_Test:test_initialize_startBlockOverride_succeeds() (gas: 65143)
......
......@@ -9,11 +9,8 @@ typechain
coverage.out
.resource-metering.csv
# Tests
test-case-generator/fuzz
# Scripts
scripts/differential-testing/differential-testing
scripts/go-ffi/go-ffi
# Environment Variables
.envrc
......
......@@ -15,14 +15,13 @@
"bindings:go": "pnpm clean && pnpm build && pnpm bindings:go:no-build",
"prebuild": "./scripts/verify-foundry-install.sh",
"build": "forge build",
"build:differential": "go build -o ./scripts/differential-testing/differential-testing ./scripts/differential-testing",
"build:fuzz": "(cd test-case-generator && go build ./cmd/fuzz.go)",
"build:go-ffi": "(cd scripts/go-ffi && go build)",
"autogen:invariant-docs": "npx tsx scripts/invariant-doc-gen.ts",
"test": "pnpm build:differential && pnpm build:fuzz && forge test",
"coverage": "pnpm build:differential && pnpm build:fuzz && forge coverage",
"coverage:lcov": "pnpm build:differential && pnpm build:fuzz && forge coverage --report lcov",
"test": "pnpm build:go-ffi && forge test",
"coverage": "pnpm build:go-ffi && forge coverage",
"coverage:lcov": "pnpm build:go-ffi && forge coverage --report lcov",
"gas-snapshot:no-build": "forge snapshot --no-match-test 'testDiff|testFuzz|invariant|generateArtifact'",
"gas-snapshot": "pnpm build:differential && pnpm build:fuzz && pnpm gas-snapshot:no-build",
"gas-snapshot": "pnpm build:go-ffi && pnpm gas-snapshot:no-build",
"storage-snapshot": "./scripts/storage-snapshot.sh",
"semver-lock": "forge script scripts/SemverLock.s.sol",
"validate-deploy-configs": "./scripts/check-deploy-configs.sh",
......@@ -30,10 +29,10 @@
"validate-spacers": "pnpm build && pnpm validate-spacers:no-build",
"slither": "./scripts/slither.sh",
"slither:triage": "TRIAGE_MODE=1 ./scripts/slither.sh",
"clean": "rm -rf ./artifacts ./forge-artifacts ./cache ./tsconfig.tsbuildinfo ./tsconfig.build.tsbuildinfo ./test-case-generator/fuzz ./scripts/differential-testing/differential-testing",
"clean": "rm -rf ./artifacts ./forge-artifacts ./cache ./tsconfig.tsbuildinfo ./tsconfig.build.tsbuildinfo ./scripts/go-ffi/go-ffi",
"preinstall": "npx only-allow pnpm",
"pre-pr:no-build": "pnpm gas-snapshot:no-build && pnpm storage-snapshot && pnpm semver-lock && pnpm autogen:invariant-docs && pnpm lint && pnpm bindings:go",
"pre-pr": "pnpm clean && pnpm build:differential && pnpm build:fuzz && pnpm build && pnpm pre-pr:no-build",
"pre-pr": "pnpm clean && pnpm build:go-ffi && pnpm build && pnpm pre-pr:no-build",
"pre-pr:full": "pnpm test && pnpm slither && pnpm validate-deploy-configs && pnpm validate-spacers && pnpm pre-pr",
"lint:ts:check": "eslint . --max-warnings=0",
"lint:forge-tests:check": "npx tsx scripts/forge-test-names.ts",
......
# `ctb-test-case-generator`
# `ctb-go-ffi`
A lightweight input fuzzing utility used for testing various Bedrock contracts.
A lightweight binary for utilities accessed via `forge`'s `ffi` cheatcode in the `contracts-bedrock` test suite.
<pre>
├── test-case-generator
│ ├── <a href="./cmd">cmd</a>: `ctb-test-case-genertor`'s binary
│ └── <a href="./trie">trie</a>: Utility for generating random merkle trie roots / inclusion proofs
├── go-ffi
│ ├── <a href="./bin.go">bin</a>: `go-ffi`'s binary
│ ├── <a href="./trie.go">trie</a>: Utility for generating random merkle trie roots / inclusion proofs
│ └── <a href="./differential-testing.go">diff-testing</a>: Utility for differential testing Solidity implementations against their respective Go implementations.
</pre>
## Usage
To build, run `pnpm build:fuzz` from this directory or the `contract-bedrock` package.
To build, run `pnpm build:go-ffi` from this directory or the `contract-bedrock` package.
To generate an abi-encoded fuzz case, pass in a mode via the `-m` flag as well as an optional variant via the `-v` flag.
### In a Forge Test
To use `go-ffi` in a forge test, simply invoke the binary via the `vm.ffi` cheatcode.
```solidity
function myFFITest() public {
string[] memory commands = new string[](3);
commands[0] = "./scripts/go-ffi/go-ffi";
commands[1] = "trie";
commands[2] = "valid";
bytes memory result = vm.ffi(commands);
// Do something with the result of the command
}
```
### Available Modes
There are two modes available in `go-ffi`: `diff` and `trie`. Each are present as a subcommand to the `go-ffi` binary, with their own set of variants.
#### `diff`
> **Note**
> Variant required for diff mode.
| Variant | Description |
| ------------------------------------- | --------------------------------------------------------------------------------------------------------------------
| `decodeVersionedNonce` | Decodes a versioned nonce and prints the decoded arguments |
| `encodeCrossDomainMessage` | Encodes a cross domain message and prints the encoded message |
| `hashCrossDomainMessage` | Encodes and hashes a cross domain message and prints the digest |
| `hashDepositTransaction` | Encodes and hashes a deposit transaction and prints the digest |
| `encodeDepositTransaction` | RLP encodes a deposit transaction |
| `hashWithdrawal` | Hashes a withdrawal message and prints the digest |
| `hashOutputRootProof` | Hashes an output root proof and prints the digest |
| `getProveWithdrawalTransactionInputs` | Generates the inputs for a `getProveWithdrawalTransaction` call to the `OptimismPortal` given a withdrawal message |
| `cannonMemoryProof` | Computes a merkle proof of Cannon's memory |
#### `trie`
> **Note**
......
package main
import (
"log"
"os"
)
func main() {
switch os.Args[1] {
case "diff":
DiffTestUtils()
case "trie":
FuzzTrie()
default:
log.Fatal("Must pass a subcommand")
}
}
......@@ -74,15 +74,16 @@ var (
}
)
func main() {
args := os.Args[1:]
func DiffTestUtils() {
args := os.Args[2:]
variant := args[0]
// This command requires arguments
if len(args) == 0 {
panic("Error: No arguments provided")
}
switch args[0] {
switch variant {
case "decodeVersionedNonce":
// Parse input arguments
input, ok := new(big.Int).SetString(args[1], 10)
......
package trie
package main
import (
"crypto/rand"
"fmt"
"log"
"math/big"
"os"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/ethdb/memorydb"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie"
)
......@@ -39,7 +40,8 @@ const (
)
// Generate an abi-encoded `trieTestCase` of a specified variant
func FuzzTrie(variant string) {
func FuzzTrie() {
variant := os.Args[2]
if len(variant) < 1 {
log.Fatal("Must pass a variant to the trie fuzzer!")
}
......@@ -48,12 +50,10 @@ func FuzzTrie(variant string) {
switch variant {
case valid:
testCase = genTrieTestCase(false)
break
case extraProofElems:
testCase = genTrieTestCase(false)
// Duplicate the last element of the proof
testCase.Proof = append(testCase.Proof, [][]byte{testCase.Proof[len(testCase.Proof)-1]}...)
break
case corruptedProof:
testCase = genTrieTestCase(false)
......@@ -61,17 +61,17 @@ func FuzzTrie(variant string) {
idx := randRange(0, int64(len(testCase.Proof)))
encoded, _ := rlp.EncodeToBytes(testCase.Proof[idx])
testCase.Proof[idx] = encoded
break
case invalidDataRemainder:
testCase = genTrieTestCase(false)
// Alter true length of random proof element by appending random bytes
// Do not update the encoded length
idx := randRange(0, int64(len(testCase.Proof)))
bytes := make([]byte, randRange(1, 512))
rand.Read(bytes)
testCase.Proof[idx] = append(testCase.Proof[idx], bytes...)
break
b := make([]byte, randRange(1, 512))
if _, err := rand.Read(b); err != nil {
log.Fatal("Error generating random bytes")
}
testCase.Proof[idx] = append(testCase.Proof[idx], b...)
case invalidLargeInternalHash:
testCase = genTrieTestCase(false)
......@@ -80,7 +80,9 @@ func FuzzTrie(variant string) {
// bytes to overwrite.
idx := randRange(1, int64(len(testCase.Proof)))
b := make([]byte, 4)
rand.Read(b)
if _, err := rand.Read(b); err != nil {
log.Fatal("Error generating random bytes")
}
testCase.Proof[idx] = append(
testCase.Proof[idx][:20],
append(
......@@ -88,27 +90,26 @@ func FuzzTrie(variant string) {
testCase.Proof[idx][24:]...,
)...,
)
break
case invalidInternalNodeHash:
testCase = genTrieTestCase(false)
// Assign the last proof element to an encoded list containing a
// random 29 byte value
b := make([]byte, 29)
rand.Read(b)
if _, err := rand.Read(b); err != nil {
log.Fatal("Error generating random bytes")
}
e, _ := rlp.EncodeToBytes(b)
testCase.Proof[len(testCase.Proof)-1] = append([]byte{0xc0 + 30}, e...)
break
case prefixedValidKey:
testCase = genTrieTestCase(false)
bytes := make([]byte, randRange(1, 16))
rand.Read(bytes)
testCase.Key = append(bytes, testCase.Key...)
break
b := make([]byte, randRange(1, 16))
if _, err := rand.Read(b); err != nil {
log.Fatal("Error generating random bytes")
}
testCase.Key = append(b, testCase.Key...)
case emptyKey:
testCase = genTrieTestCase(true)
break
case partialProof:
testCase = genTrieTestCase(false)
......@@ -120,7 +121,6 @@ func FuzzTrie(variant string) {
}
testCase.Proof = newProof
break
default:
log.Fatal("Invalid variant passed to trie fuzzer!")
}
......@@ -132,8 +132,8 @@ func FuzzTrie(variant string) {
// Generate a random test case for Bedrock's MerkleTrie verifier.
func genTrieTestCase(selectEmptyKey bool) trieTestCase {
// Create an empty merkle trie
memdb := memorydb.New()
randTrie := trie.NewEmpty(trie.NewDatabase(memdb))
memdb := rawdb.NewMemoryDatabase()
randTrie := trie.NewEmpty(trie.NewDatabase(memdb, nil))
// Get a random number of elements to put into the trie
randN := randRange(2, 1024)
......@@ -152,8 +152,12 @@ func genTrieTestCase(selectEmptyKey bool) trieTestCase {
// Add `randN` elements to the trie
for i := int64(0); i < randN; i++ {
// Randomize the contents of `randKey` and `randValue`
rand.Read(randKey)
rand.Read(randValue)
if _, err := rand.Read(randKey); err != nil {
log.Fatal("Error generating random bytes")
}
if _, err := rand.Read(randValue); err != nil {
log.Fatal("Error generating random bytes")
}
// Clear the selected key if `selectEmptyKey` is true
if i == randSelect && selectEmptyKey {
......@@ -161,7 +165,7 @@ func genTrieTestCase(selectEmptyKey bool) trieTestCase {
}
// Insert the random k/v pair into the trie
if err := randTrie.TryUpdate(randKey, randValue); err != nil {
if err := randTrie.Update(randKey, randValue); err != nil {
log.Fatal("Error adding key-value pair to trie")
}
......@@ -174,7 +178,7 @@ func genTrieTestCase(selectEmptyKey bool) trieTestCase {
// Generate proof for `key`'s inclusion in our trie
var proof proofList
if err := randTrie.Prove(key, 0, &proof); err != nil {
if err := randTrie.Prove(key, &proof); err != nil {
log.Fatal("Error creating proof for randomly selected key's inclusion in generated trie")
}
......@@ -233,15 +237,3 @@ func randRange(min int64, max int64) int64 {
return (new(big.Int).Add(r, new(big.Int).SetInt64(min))).Int64()
}
// Custom type to write the generated proof to
type proofList [][]byte
func (n *proofList) Put(key []byte, value []byte) error {
*n = append(*n, value)
return nil
}
func (n *proofList) Delete(key []byte) error {
panic("not supported")
}
package main
import (
"flag"
"log"
t "github.com/ethereum-optimism/optimism/packages/contracts-bedrock/ctb-test-case-generator/trie"
)
// Mode enum
const (
// Enables the `trie` fuzzer
trie string = "trie"
)
func main() {
mode := flag.String("m", "", "Fuzzer mode")
variant := flag.String("v", "", "Mode variant")
flag.Parse()
if len(*mode) < 1 {
log.Fatal("Must pass a mode for the fuzzer!")
}
switch *mode {
case trie:
t.FuzzTrie(*variant)
default:
log.Fatal("Invalid mode!")
}
}
module github.com/ethereum-optimism/optimism/packages/contracts-bedrock/ctb-test-case-generator
go 1.20
require github.com/ethereum/go-ethereum v1.10.26
require (
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
github.com/VictoriaMetrics/fastcache v1.6.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/go-ole/go-ole v1.2.1 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/tsdb v0.7.1 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
github.com/tklauser/go-sysconf v0.3.5 // indirect
github.com/tklauser/numcpus v0.2.2 // indirect
golang.org/x/crypto v0.1.0 // indirect
golang.org/x/sys v0.1.0 // indirect
)
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment