From a58704679ff80cb7a48001fd7d1634b620ee7f8a Mon Sep 17 00:00:00 2001 From: lmittmann <3458786+lmittmann@users.noreply.github.com> Date: Mon, 13 May 2024 10:05:02 +0200 Subject: [PATCH] added `Address` and `Hash` zero values (#145) * added `Address` and `Hash` zero values * cleanup --------- Co-authored-by: lmittmann --- util.go | 6 ++++++ w3types/message.go | 4 +--- w3vm/util.go | 9 ++------- w3vm/vm.go | 8 ++++---- 4 files changed, 13 insertions(+), 14 deletions(-) diff --git a/util.go b/util.go index a31aa248..4f3c8fb5 100644 --- a/util.go +++ b/util.go @@ -19,6 +19,12 @@ var ( BigMaxUint256 = new(big.Int).Sub(new(big.Int).Lsh(Big1, 256), Big1) ) +// Zero Values. +var ( + Addr0 common.Address + Hash0 common.Hash +) + // A returns an address from a hexstring or panics if the hexstring does not // represent a valid address. // diff --git a/w3types/message.go b/w3types/message.go index 03ac5fc5..a046874b 100644 --- a/w3types/message.go +++ b/w3types/message.go @@ -10,8 +10,6 @@ import ( "github.com/ethereum/go-ethereum/core/types" ) -var addr0 = common.Address{} - // Message represents a transaction without the signature. // // If no input data is given, but Func is not null, the input data is @@ -119,7 +117,7 @@ type message struct { // MarshalJSON implements the [json.Marshaler]. func (msg *Message) MarshalJSON() ([]byte, error) { var enc message - if msg.From != addr0 { + if msg.From != (common.Address{}) { enc.From = &msg.From } enc.To = msg.To diff --git a/w3vm/util.go b/w3vm/util.go index 11a58c17..1b78439f 100644 --- a/w3vm/util.go +++ b/w3vm/util.go @@ -13,6 +13,7 @@ import ( "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/holiman/uint256" + "github.com/lmittmann/w3" "github.com/lmittmann/w3/internal/crypto" w3hexutil "github.com/lmittmann/w3/internal/hexutil" "github.com/lmittmann/w3/internal/mod" @@ -20,12 +21,6 @@ import ( "github.com/lmittmann/w3/w3types" ) -// zero values -var ( - addr0 common.Address - hash0 common.Hash -) - // RandA returns a random address. func RandA() (addr common.Address) { rand.Read(addr[:]) @@ -73,7 +68,7 @@ func nilToZero[T any](ptr *T) *T { // zeroHashFunc implements a [vm.GetHashFunc] that always returns the zero hash. func zeroHashFunc(uint64) common.Hash { - return hash0 + return w3.Hash0 } //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/w3vm/vm.go b/w3vm/vm.go index d2017bbf..dbbb5ef9 100644 --- a/w3vm/vm.go +++ b/w3vm/vm.go @@ -58,7 +58,7 @@ func New(opts ...Option) (*VM, error) { // set DB db := newDB(vm.opts.fetcher) - vm.db, _ = gethState.New(hash0, db, nil) + vm.db, _ = gethState.New(w3.Hash0, db, nil) for addr, acc := range vm.opts.preState { vm.db.SetNonce(addr, acc.Nonce) if acc.Balance != nil { @@ -126,7 +126,7 @@ func (v *VM) apply(msg *w3types.Message, isCall bool, hooks *tracing.Hooks) (*Re GasRefund: result.RefundedGas, GasLimit: result.UsedGas + v.db.GetRefund(), Output: result.ReturnData, - Logs: v.db.GetLogs(txHash, 0, hash0), + Logs: v.db.GetLogs(txHash, 0, w3.Hash0), } if err := result.Err; err != nil { @@ -224,14 +224,14 @@ func (vm *VM) Code(addr common.Address) ([]byte, error) { func (vm *VM) StorageAt(addr common.Address, slot common.Hash) (common.Hash, error) { val := vm.db.GetState(addr, slot) if vm.db.Error() != nil { - return hash0, fmt.Errorf("%w: failed to fetch storage of %s at %s", ErrFetch, addr, slot) + return w3.Hash0, fmt.Errorf("%w: failed to fetch storage of %s at %s", ErrFetch, addr, slot) } return val, nil } func (v *VM) buildMessage(msg *w3types.Message, skipAccChecks bool) (*core.Message, *vm.TxContext, error) { nonce := msg.Nonce - if !skipAccChecks && nonce == 0 && msg.From != addr0 { + if !skipAccChecks && nonce == 0 && msg.From != w3.Addr0 { var err error nonce, err = v.Nonce(msg.From) if err != nil {