Skip to content

Commit

Permalink
Tests fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal committed Feb 9, 2024
1 parent 6cfb0d0 commit 4c84579
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 26 deletions.
46 changes: 37 additions & 9 deletions state/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,10 +466,17 @@ func (t *Transition) subGasLimitPrice(msg *types.Transaction) error {
}

func (t *Transition) nonceCheck(msg *types.Transaction) error {
nonce := t.state.GetNonce(msg.From())
currentNonce := t.state.GetNonce(msg.From())

if nonce != msg.Nonce() {
return ErrNonceIncorrect
if msgNonce := msg.Nonce(); currentNonce < msgNonce {
return fmt.Errorf("%w: address %v, tx: %d state: %d", ErrNonceTooHigh,
msg.From(), msgNonce, currentNonce)
} else if currentNonce > msgNonce {
return fmt.Errorf("%w: address %v, tx: %d state: %d", ErrNonceTooLow,
msg.From(), msgNonce, currentNonce)
} else if currentNonce+1 < currentNonce {
return fmt.Errorf("%w: address %v, nonce: %d", ErrNonceMax,
msg.From(), currentNonce)
}

return nil
Expand Down Expand Up @@ -517,7 +524,9 @@ func (t *Transition) checkDynamicFees(msg *types.Transaction) error {
// surfacing of these errors reject the transaction thus not including it in the block

var (
ErrNonceIncorrect = errors.New("incorrect nonce")
ErrNonceTooLow = errors.New("nonce too low")
ErrNonceTooHigh = errors.New("nonce too high")
ErrNonceMax = errors.New("nonce has max value")
ErrNotEnoughFundsForGas = errors.New("not enough funds to cover gas costs")
ErrBlockLimitReached = errors.New("gas limit reached in the pool")
ErrIntrinsicGasOverflow = errors.New("overflow in intrinsic gas calculation")
Expand Down Expand Up @@ -631,8 +640,13 @@ func (t *Transition) apply(msg *types.Transaction) (*runtime.ExecutionResult, er
result = t.Call2(msg.From(), *(msg.To()), msg.Input(), value, gasLeft, initialAccessList)
}

refundQuotient := LegacyRefundQuotient
if t.config.London {
refundQuotient = LondonRefundQuotient
}

refund := t.state.GetRefund()
result.UpdateGasUsed(msg.Gas(), refund)
result.UpdateGasUsed(msg.Gas(), refund, refundQuotient)

if t.ctx.Tracer != nil {
t.ctx.Tracer.TxEnd(result.GasLeft)
Expand Down Expand Up @@ -660,10 +674,10 @@ func (t *Transition) apply(msg *types.Transaction) (*runtime.ExecutionResult, er

// Burn some amount if the london hardfork is applied.
// Basically, burn amount is just transferred to the current burn contract.
if t.config.London && msg.Type() != types.StateTx {
burnAmount := new(big.Int).Mul(new(big.Int).SetUint64(result.GasUsed), t.ctx.BaseFee)
t.state.AddBalance(t.ctx.BurnContract, burnAmount)
}
// if t.config.London && msg.Type() != types.StateTx {
// burnAmount := new(big.Int).Mul(new(big.Int).SetUint64(result.GasUsed), t.ctx.BaseFee)
// t.state.AddBalance(t.ctx.BurnContract, burnAmount)
// }

// return gas to the pool
t.addGasPool(result.GasLeft)
Expand Down Expand Up @@ -948,6 +962,20 @@ func (t *Transition) applyCreate(c *runtime.Contract, host runtime.Host) *runtim
}
}

// Reject code starting with 0xEF if EIP-3541 is enabled.
if result.Err == nil && len(result.ReturnValue) >= 1 && result.ReturnValue[0] == 0xEF && t.config.London {
if err := t.state.RevertToSnapshot(snapshot); err != nil {
return &runtime.ExecutionResult{
Err: err,
}
}

return &runtime.ExecutionResult{
GasLeft: 0,
Err: runtime.ErrInvalidCode,
}
}

gasCost := uint64(len(result.ReturnValue)) * 200

if result.GasLeft < gasCost {
Expand Down
6 changes: 5 additions & 1 deletion state/runtime/precompiled/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ func (e *ecrecover) run(input []byte, caller types.Address, _ runtime.Host) ([]b
return nil, nil
}

pubKey, err := crypto.Ecrecover(input[:32], append(input[64:128], v))
sig := make([]byte, 65)
copy(sig, input[64:128])
sig[64] = v

pubKey, err := crypto.Ecrecover(input[:32], sig)
if err != nil {
return nil, nil
}
Expand Down
5 changes: 3 additions & 2 deletions state/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ func (r *ExecutionResult) Succeeded() bool { return r.Err == nil }
func (r *ExecutionResult) Failed() bool { return r.Err != nil }
func (r *ExecutionResult) Reverted() bool { return errors.Is(r.Err, ErrExecutionReverted) }

func (r *ExecutionResult) UpdateGasUsed(gasLimit uint64, refund uint64) {
func (r *ExecutionResult) UpdateGasUsed(gasLimit uint64, refund, refundQuotient uint64) {
r.GasUsed = gasLimit - r.GasLeft

// Refund can go up to half the gas used
if maxRefund := r.GasUsed / 2; refund > maxRefund {
if maxRefund := r.GasUsed / refundQuotient; refund > maxRefund {
refund = maxRefund
}

Expand All @@ -143,6 +143,7 @@ var (
ErrUnauthorizedCaller = errors.New("unauthorized caller")
ErrInvalidInputData = errors.New("invalid input data")
ErrNotAuth = errors.New("not in allow list")
ErrInvalidCode = errors.New("invalid code: must not begin with 0xef")
)

// StackUnderflowError wraps an evm error when the items on the stack less
Expand Down
19 changes: 16 additions & 3 deletions state/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ import (

var emptyStateHash = types.StringToHash("0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")

const (
BerlinClearingRefund = uint64(15000)
LondonClearingRefund = uint64(4800)

LegacyRefundQuotient = uint64(2)
LondonRefundQuotient = uint64(5)
)

type readSnapshot interface {
GetStorage(addr types.Address, root types.Hash, key types.Hash) types.Hash
GetAccount(addr types.Address) (*Account, error)
Expand Down Expand Up @@ -247,13 +255,18 @@ func (txn *Txn) SetStorage(
return runtime.StorageModified
}

clearingRefund := BerlinClearingRefund
if config.London {
clearingRefund = LondonClearingRefund
}

if original == current {
if original == types.ZeroHash { // create slot (2.1.1)
return runtime.StorageAdded
}

if value == types.ZeroHash { // delete slot (2.1.2b)
txn.AddRefund(15000)
txn.AddRefund(clearingRefund)

return runtime.StorageDeleted
}
Expand All @@ -263,9 +276,9 @@ func (txn *Txn) SetStorage(

if original != types.ZeroHash { // Storage slot was populated before this transaction started
if current == types.ZeroHash { // recreate slot (2.2.1.1)
txn.SubRefund(15000)
txn.SubRefund(clearingRefund)
} else if value == types.ZeroHash { // delete slot (2.2.1.2)
txn.AddRefund(15000)
txn.AddRefund(clearingRefund)
}
}

Expand Down
29 changes: 18 additions & 11 deletions tests/state_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,13 @@ func (t *stTransaction) At(i indexes, baseFee *big.Int) (*types.Transaction, err
}), nil
}

txType := types.LegacyTx
if isDynamicTransaction {
txType = types.DynamicFeeTx
}

return types.NewTx(&types.MixedTxn{
Type: txType,
From: t.From,
To: t.To,
Nonce: t.Nonce,
Expand Down Expand Up @@ -561,17 +567,18 @@ var Forks = map[string]*chain.Forks{
chain.Berlin: chain.NewFork(0),
chain.London: chain.NewFork(5),
},
// "London": {
// chain.Homestead: chain.NewFork(0),
// chain.EIP150: chain.NewFork(0),
// chain.EIP155: chain.NewFork(0),
// chain.EIP158: chain.NewFork(0),
// chain.Byzantium: chain.NewFork(0),
// chain.Constantinople: chain.NewFork(0),
// chain.Petersburg: chain.NewFork(0),
// chain.Istanbul: chain.NewFork(0),
// chain.London: chain.NewFork(0),
// },
"London": {
chain.Homestead: chain.NewFork(0),
chain.EIP150: chain.NewFork(0),
chain.EIP155: chain.NewFork(0),
chain.EIP158: chain.NewFork(0),
chain.Byzantium: chain.NewFork(0),
chain.Constantinople: chain.NewFork(0),
chain.Petersburg: chain.NewFork(0),
chain.Istanbul: chain.NewFork(0),
chain.Berlin: chain.NewFork(0),
chain.London: chain.NewFork(0),
},
}

func contains(l []string, name string) bool {
Expand Down

0 comments on commit 4c84579

Please sign in to comment.