Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Floria: Add nonce and eoa check #974

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 35 additions & 14 deletions go/processor/floria/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (

createGasCostPerByte = 200
maxCodeSize = 24576
maxInitCodeSize = 2 * maxCodeSize

MaxRecursiveDepth = 1024 // Maximum depth of call/create stack.
)
Expand Down Expand Up @@ -55,6 +56,14 @@ func (p *processor) Run(
}
gas := transaction.GasLimit

if nonceCheck(transaction.Nonce, context.GetNonce(transaction.Sender)) != nil {
return tosca.Receipt{}, nil
}

if eoaCheck(transaction.Sender, context) != nil {
return tosca.Receipt{}, nil
}

if err := buyGas(transaction, context); err != nil {
return tosca.Receipt{}, nil
}
Expand All @@ -65,8 +74,9 @@ func (p *processor) Run(
}
gas -= intrinsicGas

if err := handleNonce(transaction, context); err != nil {
return errorReceipt, nil
if blockParameters.Revision >= tosca.R12_Shanghai && transaction.Recipient == nil &&
len(transaction.Input) > maxInitCodeSize {
return tosca.Receipt{}, nil
facuMH marked this conversation as resolved.
Show resolved Hide resolved
}

transactionParameters := tosca.TransactionParameters{
Expand All @@ -91,6 +101,10 @@ func (p *processor) Run(
callParameters := callParameters(transaction, gas)
kind := callKind(transaction)

if kind == tosca.Call {
LuisPH3 marked this conversation as resolved.
Show resolved Hide resolved
facuMH marked this conversation as resolved.
Show resolved Hide resolved
context.SetNonce(transaction.Sender, context.GetNonce(transaction.Sender)+1)
}

result, err := runContext.Call(kind, callParameters)
if err != nil {
return errorReceipt, err
Expand Down Expand Up @@ -123,6 +137,25 @@ func (p *processor) Run(
}, nil
}

func nonceCheck(transactionNonce uint64, stateNonce uint64) error {
if transactionNonce != stateNonce {
LuisPH3 marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("nonce mismatch: %v != %v", transactionNonce, stateNonce)
}
if stateNonce+1 < stateNonce {
LuisPH3 marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("nonce overflow")
}
return nil
}

// Only accept transactions from externally owned accounts (EOAs) and not from contracts
func eoaCheck(sender tosca.Address, context tosca.TransactionContext) error {
simonlechner marked this conversation as resolved.
Show resolved Hide resolved
codehash := context.GetCodeHash(sender)
if codehash != (tosca.Hash{}) && codehash != emptyCodeHash {
return fmt.Errorf("sender is not an EOA")
}
return nil
}

func setUpAccessList(transaction tosca.Transaction, context tosca.TransactionContext, revision tosca.Revision) {
if transaction.AccessList == nil {
return
Expand Down Expand Up @@ -237,18 +270,6 @@ func setupGasBilling(transaction tosca.Transaction) tosca.Gas {
return tosca.Gas(gas)
}

func handleNonce(transaction tosca.Transaction, context tosca.TransactionContext) error {
stateNonce := context.GetNonce(transaction.Sender)
messageNonce := transaction.Nonce
if messageNonce != stateNonce {
return fmt.Errorf("nonce mismatch: %v != %v", messageNonce, stateNonce)
}
if transaction.Recipient != nil {
context.SetNonce(transaction.Sender, stateNonce+1)
}
return nil
}

func buyGas(transaction tosca.Transaction, context tosca.TransactionContext) error {
gas := transaction.GasPrice.Scale(uint64(transaction.GasLimit))

Expand Down
68 changes: 47 additions & 21 deletions go/processor/floria/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package floria

import (
"math"
"reflect"
"testing"

Expand All @@ -35,38 +36,63 @@ func TestProcessor_HandleNonce(t *testing.T) {
context := tosca.NewMockTransactionContext(ctrl)

context.EXPECT().GetNonce(tosca.Address{1}).Return(uint64(9))
context.EXPECT().SetNonce(tosca.Address{1}, uint64(10))
context.EXPECT().GetNonce(tosca.Address{1}).Return(uint64(10))

transaction := tosca.Transaction{
Sender: tosca.Address{1},
Recipient: &tosca.Address{2},
Nonce: 9,
Sender: tosca.Address{1},
Nonce: 9,
}

err := handleNonce(transaction, context)
err := nonceCheck(transaction.Nonce, context.GetNonce(transaction.Sender))
if err != nil {
t.Errorf("handleNonce returned an error: %v", err)
}
if context.GetNonce(transaction.Sender) != 10 {
t.Errorf("Nonce was not incremented")
t.Errorf("nonceCheck returned an error: %v", err)
}
}

func TestProcessor_NonceMissmatch(t *testing.T) {
ctrl := gomock.NewController(t)
context := tosca.NewMockTransactionContext(ctrl)
func TestProcessor_NonceOverflowIsDetected(t *testing.T) {
err := nonceCheck(math.MaxUint64, math.MaxUint64)
facuMH marked this conversation as resolved.
Show resolved Hide resolved
if err == nil {
t.Errorf("nonceCheck did not spot nonce overflow")
}
}

context.EXPECT().GetNonce(tosca.Address{1}).Return(uint64(5))
func TestProcessor_NonceMissMatch(t *testing.T) {
err := nonceCheck(uint64(10), uint64(42))
if err == nil {
simonlechner marked this conversation as resolved.
Show resolved Hide resolved
facuMH marked this conversation as resolved.
Show resolved Hide resolved
t.Errorf("nonceCheck did not spot nonce miss match")
}
}

transaction := tosca.Transaction{
Sender: tosca.Address{1},
Recipient: &tosca.Address{2},
Nonce: 10,
func TestProcessor_EoaCheck(t *testing.T) {
tests := map[string]struct {
codeHash tosca.Hash
isEOA bool
}{
"empty": {
tosca.Hash{},
false,
},
"emptyHash": {
emptyCodeHash,
false,
},
"nonEmpty": {
tosca.Hash{1, 2, 3},
true,
},
}
err := handleNonce(transaction, context)
if err == nil {
t.Errorf("handleNonce did not spot nonce miss match")

for testName, test := range tests {
t.Run(testName, func(t *testing.T) {
ctrl := gomock.NewController(t)
context := tosca.NewMockTransactionContext(ctrl)

context.EXPECT().GetCodeHash(tosca.Address{1}).Return(test.codeHash)

err := eoaCheck(tosca.Address{1}, context)
if test.isEOA && err == nil {
t.Errorf("eoaCheck returned wrong result: %v", err)
}
})
}
}

Expand Down
Loading