Skip to content

Commit

Permalink
added Address and Hash zero values (#145)
Browse files Browse the repository at this point in the history
* added `Address` and `Hash` zero values

* cleanup

---------

Co-authored-by: lmittmann <lmittmann@users.noreply.github.com>
  • Loading branch information
lmittmann and lmittmann authored May 13, 2024
1 parent d9411cc commit a587046
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
6 changes: 6 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down
4 changes: 1 addition & 3 deletions w3types/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 2 additions & 7 deletions w3vm/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,14 @@ 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"
"github.com/lmittmann/w3/internal/module"
"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[:])
Expand Down Expand Up @@ -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
}

////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
8 changes: 4 additions & 4 deletions w3vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit a587046

Please sign in to comment.