From cd0cc1c8622e6aaa5e559dc1b1439402b453f82a Mon Sep 17 00:00:00 2001 From: Shawn <44221603+shaspitz@users.noreply.github.com> Date: Mon, 20 Nov 2023 23:36:59 -0800 Subject: [PATCH] fix fuzz test --- tests/fuzzers/bls12381/precompile_fuzzer.go | 30 ++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/tests/fuzzers/bls12381/precompile_fuzzer.go b/tests/fuzzers/bls12381/precompile_fuzzer.go index 763ed56e9f..7d635e4c6e 100644 --- a/tests/fuzzers/bls12381/precompile_fuzzer.go +++ b/tests/fuzzers/bls12381/precompile_fuzzer.go @@ -19,6 +19,8 @@ package bls import ( "bytes" "fmt" + "math/big" + "time" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/vm" @@ -60,6 +62,32 @@ func checkInput(id byte, inputLen int) bool { panic("programmer error") } +var vmBlockCtx = vm.BlockContext{ + CanTransfer: func(db vm.StateDB, addr common.Address, amount *big.Int) bool { + return db.GetBalance(addr).Cmp(amount) >= 0 + }, + Transfer: func(vm.StateDB, common.Address, common.Address, *big.Int) { + panic("transfer: not implemented") + }, + GetHash: func(u uint64) common.Hash { + panic("getHash: not implemented") + }, + Coinbase: common.Address{}, + BlockNumber: new(big.Int).SetUint64(10), + Time: uint64(time.Now().Unix()), +} + +var vmTxCtx = vm.TxContext{ + GasPrice: common.Big1, + Origin: common.HexToAddress("a11ce"), +} + +// Create a global mock EVM for use in the following tests. +var mockEVM = &vm.EVM{ + Context: vmBlockCtx, + TxContext: vmTxCtx, +} + // The function must return // // - 1 if the fuzzer should increase priority of the @@ -82,7 +110,7 @@ func fuzz(id byte, data []byte) int { } cpy := make([]byte, len(data)) copy(cpy, data) - _, err := precompile.Run(cpy) + _, err := precompile.Run(cpy, vm.NewContext(common.HexToAddress("1337"), mockEVM)) if !bytes.Equal(cpy, data) { panic(fmt.Sprintf("input data modified, precompile %d: %x %x", id, data, cpy)) }