Skip to content
Open
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
14 changes: 12 additions & 2 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
"github.com/harmony-one/harmony/core/state"
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/core/vm"
"github.com/harmony-one/harmony/internal/utils"
Expand Down Expand Up @@ -89,7 +90,8 @@ type Message interface {
Value() *big.Int

Nonce() uint64
CheckNonce() bool
SkipNonceChecks() bool
SkipFromEOACheck() bool
Data() []byte
Type() types.TransactionType
BlockNum() *big.Int
Expand Down Expand Up @@ -195,7 +197,7 @@ func (st *StateTransition) buyGas() error {

func (st *StateTransition) preCheck() error {
// Make sure this transaction's nonce is correct.
if st.msg.CheckNonce() {
if !st.msg.SkipNonceChecks() {
nonce := st.state.GetNonce(st.msg.From())

if nonce < st.msg.Nonce() {
Expand All @@ -204,6 +206,14 @@ func (st *StateTransition) preCheck() error {
return ErrNonceTooLow
}
}

// Ensure that the sender is an EOA (EIP-3607)
if !st.msg.SkipFromEOACheck() {
if codeHash := st.state.GetCodeHash(st.msg.From()); codeHash != state.EmptyCodeHash && codeHash != (common.Hash{}) { // getCodeHash returns EmptyCodeHash for unused accounts
return ErrSenderNotEOA
}
}

return st.buyGas()
}

Expand Down
194 changes: 194 additions & 0 deletions core/state_transition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/harmony-one/harmony/core/state"
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/core/vm"
shardingconfig "github.com/harmony-one/harmony/internal/configs/sharding"
Expand Down Expand Up @@ -215,3 +216,196 @@ func TestCollectGasRounding(t *testing.T) {
}
}
}

func TestPreCheck(t *testing.T) {
key, _ := crypto.GenerateKey()

setCode := func(db *state.DB, addr common.Address) {
code := []byte("code")
db.SetCode(addr, code, false)
}

tests := []struct {
name string
msg types.Message
expectedError error
setup func(db *state.DB, addr common.Address)
}{
{
name: "NonceTooHigh", // nonce is 0, but expected is 2
msg: types.NewMessage(
crypto.PubkeyToAddress(key.PublicKey),
nil,
2,
big.NewInt(0),
21000,
big.NewInt(1),
[]byte{},
false,
false,
),
expectedError: ErrNonceTooHigh,
},
{
name: "NonceTooLow", // nonce is 1, but expected is 0
msg: types.NewMessage(
crypto.PubkeyToAddress(key.PublicKey),
nil,
0,
big.NewInt(0),
21000,
big.NewInt(1),
[]byte{},
false,
false,
),
expectedError: ErrNonceTooLow,
setup: func(db *state.DB, addr common.Address) {
db.SetNonce(addr, 1)
},
},
{
name: "SenderNotEOA", // sender has a code hash, thus not an EOA
msg: types.NewMessage(
crypto.PubkeyToAddress(key.PublicKey),
nil,
0,
big.NewInt(0),
21000,
big.NewInt(1),
[]byte{},
false,
false,
),
expectedError: ErrSenderNotEOA,
setup: setCode,
},
{
name: "SuccessfulPreCheck", // all checks pass
msg: types.NewMessage(
crypto.PubkeyToAddress(key.PublicKey),
nil,
0,
big.NewInt(0),
21000,
big.NewInt(1),
[]byte{},
false,
false,
),
expectedError: nil,
},
{
name: "SkipNonceChecks", // skip nonce checks
msg: types.NewMessage(
crypto.PubkeyToAddress(key.PublicKey),
nil,
2,
big.NewInt(0),
21000,
big.NewInt(1),
[]byte{},
true,
false,
),
expectedError: nil,
},
{
name: "SkipFromEOACheck", // skip EOA check
msg: types.NewMessage(
crypto.PubkeyToAddress(key.PublicKey),
nil,
0,
big.NewInt(0),
21000,
big.NewInt(1),
[]byte{},
false,
true,
),
expectedError: nil,
setup: setCode,
},
{
name: "SkipBothChecks", // skip both checks
msg: types.NewMessage(
crypto.PubkeyToAddress(key.PublicKey),
nil,
2,
big.NewInt(0),
21000,
big.NewInt(1),
[]byte{},
true,
true,
),
expectedError: nil,
setup: setCode,
},

// staking messages
{
name: "StakingWithCodeHash", // staking message with code hash
msg: types.NewStakingMessage(
crypto.PubkeyToAddress(key.PublicKey),
0,
21000,
big.NewInt(1),
[]byte{},
big.NewInt(1000),
),
expectedError: nil,
setup: setCode,
},
{
name: "StakingWithoutCodeHash", // staking message without code hash
msg: types.NewStakingMessage(
crypto.PubkeyToAddress(key.PublicKey),
0,
21000,
big.NewInt(1),
[]byte{},
big.NewInt(1000),
),
expectedError: nil,
},
{
name: "StakingNonceTooHigh", // staking message with nonce too high
msg: types.NewStakingMessage(
crypto.PubkeyToAddress(key.PublicKey),
2,
21000,
big.NewInt(1),
[]byte{},
big.NewInt(1000),
),
expectedError: ErrNonceTooHigh,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
chain, db, header, _ := getTestEnvironment(*key)
gp := new(GasPool).AddGas(math.MaxUint64)

initialBalance := big.NewInt(2e18)
addr := crypto.PubkeyToAddress(key.PublicKey)
db.AddBalance(addr, initialBalance)

if tt.setup != nil {
tt.setup(db, addr)
}

ctx := NewEVMContext(tt.msg, header, chain, nil) // coinbase is nil, no block reward
ctx.TxType = types.SameShardTx
vmenv := vm.NewEVM(ctx, db, params.TestChainConfig, vm.Config{})

st := NewStateTransition(vmenv, tt.msg, gp)
err := st.preCheck()

if !errors.Is(err, tt.expectedError) {
t.Errorf("expected error %v, got %v", tt.expectedError, err)
}
})
}
}
3 changes: 3 additions & 0 deletions core/tx_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ var (
ErrBlacklistTo = errors.New("`to` address of transaction in blacklist")

ErrAllowedTxs = errors.New("transaction allowed whitelist check failed.")

// ErrSenderNotEOA is returned if the transaction sender is a contract.
ErrSenderNotEOA = errors.New("sender not an eoa")
)

var (
Expand Down
15 changes: 8 additions & 7 deletions core/types/eth_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,14 @@ func (tx *EthTransaction) IsEthCompatible() bool {
// XXX Rename message to something less arbitrary?
func (tx *EthTransaction) AsMessage(s Signer) (Message, error) {
msg := Message{
nonce: tx.data.AccountNonce,
gasLimit: tx.data.GasLimit,
gasPrice: new(big.Int).Set(tx.data.Price),
to: tx.data.Recipient,
amount: tx.data.Amount,
data: tx.data.Payload,
checkNonce: true,
nonce: tx.data.AccountNonce,
gasLimit: tx.data.GasLimit,
gasPrice: new(big.Int).Set(tx.data.Price),
to: tx.data.Recipient,
value: tx.data.Amount,
data: tx.data.Payload,
skipNonceChecks: false,
skipFromEOACheck: false,
}

var err error
Expand Down
Loading