Skip to content

Commit

Permalink
Fix incorrect condition in validate7702
Browse files Browse the repository at this point in the history
  • Loading branch information
Mdaiki0730 committed Dec 18, 2024
1 parent 4443832 commit 82024ed
Show file tree
Hide file tree
Showing 16 changed files with 50 additions and 47 deletions.
29 changes: 15 additions & 14 deletions blockchain/types/tx_internal_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/kaiachain/kaia/blockchain/types/accountkey"
"github.com/kaiachain/kaia/common"
"github.com/kaiachain/kaia/crypto"
"github.com/kaiachain/kaia/kerrors"
"github.com/kaiachain/kaia/params"
"github.com/kaiachain/kaia/rlp"
)
Expand Down Expand Up @@ -724,7 +725,7 @@ func calculateTxSize(data TxInternalData) common.StorageSize {
return common.StorageSize(c)
}

func validate7702(stateDB StateDB, txType TxType, from, to common.Address) bool {
func validate7702(stateDB StateDB, txType TxType, from, to common.Address) error {
switch txType {
// Group 1: Recipient must be EOA without code
case TxTypeValueTransfer,
Expand All @@ -735,55 +736,55 @@ func validate7702(stateDB StateDB, txType TxType, from, to common.Address) bool
TxTypeFeeDelegatedValueTransferMemoWithRatio:
acc := stateDB.GetAccount(to)
if acc == nil {
return true
return nil
}
if acc.Type() == account.SmartContractAccountType {
return false
return kerrors.ErrToMustBeEOAWithoutCode
}
eoa, ok := acc.(*account.ExternallyOwnedAccount)
if !ok || !bytes.Equal(eoa.GetCodeHash(), emptyCodeHash) {
return false
return kerrors.ErrToMustBeEOAWithoutCode
}

return true
return nil

// Group 2: From must be EOA without code
case TxTypeAccountUpdate,
TxTypeFeeDelegatedAccountUpdate,
TxTypeFeeDelegatedAccountUpdateWithRatio:
acc := stateDB.GetAccount(from)
if acc == nil {
return false
return nil
}
if acc.Type() == account.SmartContractAccountType {
return false
return kerrors.ErrFromMustBeEOAWithoutCode
}
eoa, ok := acc.(*account.ExternallyOwnedAccount)
if !ok || !bytes.Equal(eoa.GetCodeHash(), emptyCodeHash) {
return false
return kerrors.ErrFromMustBeEOAWithoutCode
}

return true
return nil

// Group 3: Recipient must be EOA with code or SCA
case TxTypeSmartContractExecution,
TxTypeFeeDelegatedSmartContractExecution,
TxTypeFeeDelegatedSmartContractExecutionWithRatio:
acc := stateDB.GetAccount(to)
if acc == nil {
return false
return kerrors.ErrToMustBeEOAWithCodeOrSCA
}
if acc.Type() == account.SmartContractAccountType {
return true
return nil
}
eoa, ok := acc.(*account.ExternallyOwnedAccount)
if !ok || !bytes.Equal(eoa.GetCodeHash(), emptyCodeHash) {
return true
return nil
}

return true
return kerrors.ErrToMustBeEOAWithCodeOrSCA

default:
return false
return nil
}
}
5 changes: 2 additions & 3 deletions blockchain/types/tx_internal_data_account_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,9 @@ func (t *TxInternalDataAccountUpdate) ValidateMutableValue(stateDB StateDB, curr
if err := accountkey.CheckReplacable(oldKey, t.Key, currentBlockNumber); err != nil {
return err
}
if !validate7702(stateDB, t.Type(), t.From, common.Address{}) {
return kerrors.ErrNotEOAWithoutCode
if err := validate7702(stateDB, t.Type(), t.From, common.Address{}); err != nil {
return err
}

return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,13 +424,13 @@ func (t *TxInternalDataFeeDelegatedAccountUpdate) Validate(stateDB StateDB, curr
}

func (t *TxInternalDataFeeDelegatedAccountUpdate) ValidateMutableValue(stateDB StateDB, currentBlockNumber uint64) error {
if !validate7702(stateDB, t.Type(), t.From, common.Address{}) {
return kerrors.ErrNotEOAWithoutCode
}
oldKey := stateDB.GetKey(t.From)
if err := accountkey.CheckReplacable(oldKey, t.Key, currentBlockNumber); err != nil {
return err
}
if err := validate7702(stateDB, t.Type(), t.From, common.Address{}); err != nil {
return err
}
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,8 @@ func (t *TxInternalDataFeeDelegatedAccountUpdateWithRatio) ValidateMutableValue(
if err := accountkey.CheckReplacable(oldKey, t.Key, currentBlockNumber); err != nil {
return err
}
if !validate7702(stateDB, t.Type(), t.From, common.Address{}) {
return kerrors.ErrNotEOAWithoutCode
if err := validate7702(stateDB, t.Type(), t.From, common.Address{}); err != nil {
return err
}
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,8 @@ func (t *TxInternalDataFeeDelegatedSmartContractExecution) Validate(stateDB Stat
}

func (t *TxInternalDataFeeDelegatedSmartContractExecution) ValidateMutableValue(stateDB StateDB, currentBlockNumber uint64) error {
if !validate7702(stateDB, t.Type(), t.From, t.Recipient) {
return kerrors.ErrNotProgramAccount
if err := validate7702(stateDB, t.Type(), t.From, t.Recipient); err != nil {
return err
}
// Fail if the target address is not a program account.
if !stateDB.IsContractAvailable(t.Recipient) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ func (t *TxInternalDataFeeDelegatedSmartContractExecutionWithRatio) Validate(sta
}

func (t *TxInternalDataFeeDelegatedSmartContractExecutionWithRatio) ValidateMutableValue(stateDB StateDB, currentBlockNumber uint64) error {
if !validate7702(stateDB, t.Type(), t.From, t.Recipient) {
return kerrors.ErrNotProgramAccount
if err := validate7702(stateDB, t.Type(), t.From, t.Recipient); err != nil {
return err
}
// Fail if the target address is not a program account.
if !stateDB.IsContractAvailable(t.Recipient) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ func (t *TxInternalDataFeeDelegatedValueTransfer) Validate(stateDB StateDB, curr
}

func (t *TxInternalDataFeeDelegatedValueTransfer) ValidateMutableValue(stateDB StateDB, currentBlockNumber uint64) error {
if !validate7702(stateDB, t.Type(), t.From, t.Recipient) {
return kerrors.ErrNotEOAWithoutCode
if err := validate7702(stateDB, t.Type(), t.From, t.Recipient); err != nil {
return err
}
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,8 @@ func (t *TxInternalDataFeeDelegatedValueTransferMemo) Validate(stateDB StateDB,
}

func (t *TxInternalDataFeeDelegatedValueTransferMemo) ValidateMutableValue(stateDB StateDB, currentBlockNumber uint64) error {
if !validate7702(stateDB, t.Type(), t.From, t.Recipient) {
return kerrors.ErrNotEOAWithoutCode
if err := validate7702(stateDB, t.Type(), t.From, t.Recipient); err != nil {
return err
}
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,8 @@ func (t *TxInternalDataFeeDelegatedValueTransferMemoWithRatio) Validate(stateDB
}

func (t *TxInternalDataFeeDelegatedValueTransferMemoWithRatio) ValidateMutableValue(stateDB StateDB, currentBlockNumber uint64) error {
if !validate7702(stateDB, t.Type(), t.From, t.Recipient) {
return kerrors.ErrNotEOAWithoutCode
if err := validate7702(stateDB, t.Type(), t.From, t.Recipient); err != nil {
return err
}
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ func (t *TxInternalDataFeeDelegatedValueTransferWithRatio) Validate(stateDB Stat
}

func (t *TxInternalDataFeeDelegatedValueTransferWithRatio) ValidateMutableValue(stateDB StateDB, currentBlockNumber uint64) error {
if !validate7702(stateDB, t.Type(), t.From, t.Recipient) {
return kerrors.ErrNotEOAWithoutCode
if err := validate7702(stateDB, t.Type(), t.From, t.Recipient); err != nil {
return err
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions blockchain/types/tx_internal_data_smart_contract_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ func (t *TxInternalDataSmartContractExecution) Validate(stateDB StateDB, current
}

func (t *TxInternalDataSmartContractExecution) ValidateMutableValue(stateDB StateDB, currentBlockNumber uint64) error {
if !validate7702(stateDB, t.Type(), t.From, t.Recipient) {
return kerrors.ErrNotProgramAccount
if err := validate7702(stateDB, t.Type(), t.From, t.Recipient); err != nil {
return err
}
// Fail if the target address is not a program account.
if !stateDB.IsContractAvailable(t.Recipient) {
Expand Down
4 changes: 2 additions & 2 deletions blockchain/types/tx_internal_data_value_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ func (t *TxInternalDataValueTransfer) Validate(stateDB StateDB, currentBlockNumb
}

func (t *TxInternalDataValueTransfer) ValidateMutableValue(stateDB StateDB, currentBlockNumber uint64) error {
if !validate7702(stateDB, t.Type(), t.From, t.Recipient) {
return kerrors.ErrNotEOAWithoutCode
if err := validate7702(stateDB, t.Type(), t.From, t.Recipient); err != nil {
return err
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions blockchain/types/tx_internal_data_value_transfer_memo.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,8 @@ func (t *TxInternalDataValueTransferMemo) Validate(stateDB StateDB, currentBlock
}

func (t *TxInternalDataValueTransferMemo) ValidateMutableValue(stateDB StateDB, currentBlockNumber uint64) error {
if !validate7702(stateDB, t.Type(), t.From, t.Recipient) {
return kerrors.ErrNotEOAWithoutCode
if err := validate7702(stateDB, t.Type(), t.From, t.Recipient); err != nil {
return err
}
return nil
}
Expand Down
5 changes: 4 additions & 1 deletion kerrors/kerrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ var (
ErrMaxKeysExceedInValidation = errors.New("the number of keys exceeds the limit in the validation check")
ErrMaxFeeRatioExceeded = errors.New("fee ratio exceeded the maximum")
ErrEmptySlice = errors.New("slice is empty")
ErrNotEOAWithoutCode = errors.New("this type transaction must have a to or from EOA without a code.")
ErrNotEOAWithoutCode = errors.New("this type transaction must have a to or from EOA without a code")
ErrFromMustBeEOAWithoutCode = errors.New("this type transaction must have an EOA without a code as the from")
ErrToMustBeEOAWithoutCode = errors.New("this type transaction must have an EOA without a code as the to")
ErrToMustBeEOAWithCodeOrSCA = errors.New("this type transaction must have an EOA with a code or SCA as the to")
ErrNotProgramAccount = errors.New("not a program account (e.g., an account having code and storage)")
ErrPrecompiledContractAddress = errors.New("the address is reserved for pre-compiled contracts")
ErrInvalidCodeFormat = errors.New("smart contract code format is invalid")
Expand Down
4 changes: 2 additions & 2 deletions tests/transaction_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,12 @@ func TestValidatingUnavailableContractExecution(t *testing.T) {
assert.Equal(t, kerrors.ErrNotProgramAccount, err)
}

// 3. contract execution transaction to the EOA account.
// 3. contract execution transaction to the EOA without a code account.
{
tx, _ := genSmartContractExecution(t, signer, reservoir, EOA, nil, gasPrice)

err = txpool.AddRemote(tx)
assert.Equal(t, kerrors.ErrNotProgramAccount, err)
assert.Equal(t, kerrors.ErrToMustBeEOAWithCodeOrSCA, err)
}

if testing.Verbose() {
Expand Down
8 changes: 4 additions & 4 deletions tests/tx_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ func valueTransferToEOAWithCode(txType types.TxType, values txValueMap, contract
txType = toBasicType(txType)
if txType == types.TxTypeValueTransfer || txType == types.TxTypeValueTransferMemo {
values[types.TxValueKeyTo] = contract
return values, kerrors.ErrNotEOAWithoutCode
return values, kerrors.ErrToMustBeEOAWithoutCode
}

return values, nil
Expand All @@ -703,7 +703,7 @@ func keyUpdateFromEOAWithCode(txType types.TxType, values txValueMap, contract c
txType = toBasicType(txType)
if txType == types.TxTypeAccountUpdate {
values[types.TxValueKeyFrom] = contract
return values, kerrors.ErrNotEOAWithoutCode
return values, kerrors.ErrFromMustBeEOAWithoutCode
}

return values, nil
Expand Down Expand Up @@ -731,7 +731,7 @@ func valueTransferToContract(txType types.TxType, values txValueMap, contract co
txType = toBasicType(txType)
if txType == types.TxTypeValueTransfer || txType == types.TxTypeValueTransferMemo {
values[types.TxValueKeyTo] = contract
return values, kerrors.ErrNotEOAWithoutCode
return values, kerrors.ErrToMustBeEOAWithoutCode
}

return values, nil
Expand All @@ -741,7 +741,7 @@ func valueTransferToContract(txType types.TxType, values txValueMap, contract co
func executeToEOA(txType types.TxType, values txValueMap, contract common.Address) (txValueMap, error) {
if toBasicType(txType) == types.TxTypeSmartContractExecution {
values[types.TxValueKeyTo] = values[types.TxValueKeyFrom].(common.Address)
return values, kerrors.ErrNotProgramAccount
return values, kerrors.ErrToMustBeEOAWithCodeOrSCA
}

return values, nil
Expand Down

0 comments on commit 82024ed

Please sign in to comment.