From 82024ed860613681d760b5c0a87dce3b42038206 Mon Sep 17 00:00:00 2001 From: Mdaiki0730 Date: Wed, 18 Dec 2024 14:28:02 +0900 Subject: [PATCH] Fix incorrect condition in validate7702 --- blockchain/types/tx_internal_data.go | 29 ++++++++++--------- .../types/tx_internal_data_account_update.go | 5 ++-- ...ernal_data_fee_delegated_account_update.go | 6 ++-- ...fee_delegated_account_update_with_ratio.go | 4 +-- ..._fee_delegated_smart_contract_execution.go | 4 +-- ...ted_smart_contract_execution_with_ratio.go | 4 +-- ...ernal_data_fee_delegated_value_transfer.go | 4 +-- ..._data_fee_delegated_value_transfer_memo.go | 4 +-- ...elegated_value_transfer_memo_with_ratio.go | 4 +-- ...fee_delegated_value_transfer_with_ratio.go | 4 +-- ..._internal_data_smart_contract_execution.go | 4 +-- .../types/tx_internal_data_value_transfer.go | 4 +-- .../tx_internal_data_value_transfer_memo.go | 4 +-- kerrors/kerrors.go | 5 +++- tests/transaction_validation_test.go | 4 +-- tests/tx_validation_test.go | 8 ++--- 16 files changed, 50 insertions(+), 47 deletions(-) diff --git a/blockchain/types/tx_internal_data.go b/blockchain/types/tx_internal_data.go index e83f1845a..d81b5d5da 100644 --- a/blockchain/types/tx_internal_data.go +++ b/blockchain/types/tx_internal_data.go @@ -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" ) @@ -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, @@ -735,17 +736,17 @@ 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, @@ -753,17 +754,17 @@ func validate7702(stateDB StateDB, txType TxType, from, to common.Address) bool 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, @@ -771,19 +772,19 @@ func validate7702(stateDB StateDB, txType TxType, from, to common.Address) bool 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 } } diff --git a/blockchain/types/tx_internal_data_account_update.go b/blockchain/types/tx_internal_data_account_update.go index 1320a7336..86821828a 100644 --- a/blockchain/types/tx_internal_data_account_update.go +++ b/blockchain/types/tx_internal_data_account_update.go @@ -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 } diff --git a/blockchain/types/tx_internal_data_fee_delegated_account_update.go b/blockchain/types/tx_internal_data_fee_delegated_account_update.go index 94472957e..29fe97a30 100644 --- a/blockchain/types/tx_internal_data_fee_delegated_account_update.go +++ b/blockchain/types/tx_internal_data_fee_delegated_account_update.go @@ -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 } diff --git a/blockchain/types/tx_internal_data_fee_delegated_account_update_with_ratio.go b/blockchain/types/tx_internal_data_fee_delegated_account_update_with_ratio.go index 395299d97..ef905a6b8 100644 --- a/blockchain/types/tx_internal_data_fee_delegated_account_update_with_ratio.go +++ b/blockchain/types/tx_internal_data_fee_delegated_account_update_with_ratio.go @@ -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 } diff --git a/blockchain/types/tx_internal_data_fee_delegated_smart_contract_execution.go b/blockchain/types/tx_internal_data_fee_delegated_smart_contract_execution.go index fbd28a571..d1b544271 100644 --- a/blockchain/types/tx_internal_data_fee_delegated_smart_contract_execution.go +++ b/blockchain/types/tx_internal_data_fee_delegated_smart_contract_execution.go @@ -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) { diff --git a/blockchain/types/tx_internal_data_fee_delegated_smart_contract_execution_with_ratio.go b/blockchain/types/tx_internal_data_fee_delegated_smart_contract_execution_with_ratio.go index e168f9b50..db4c4f468 100644 --- a/blockchain/types/tx_internal_data_fee_delegated_smart_contract_execution_with_ratio.go +++ b/blockchain/types/tx_internal_data_fee_delegated_smart_contract_execution_with_ratio.go @@ -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) { diff --git a/blockchain/types/tx_internal_data_fee_delegated_value_transfer.go b/blockchain/types/tx_internal_data_fee_delegated_value_transfer.go index 8d4be7dca..4e24db28f 100644 --- a/blockchain/types/tx_internal_data_fee_delegated_value_transfer.go +++ b/blockchain/types/tx_internal_data_fee_delegated_value_transfer.go @@ -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 } diff --git a/blockchain/types/tx_internal_data_fee_delegated_value_transfer_memo.go b/blockchain/types/tx_internal_data_fee_delegated_value_transfer_memo.go index a89b781aa..7f714cb60 100644 --- a/blockchain/types/tx_internal_data_fee_delegated_value_transfer_memo.go +++ b/blockchain/types/tx_internal_data_fee_delegated_value_transfer_memo.go @@ -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 } diff --git a/blockchain/types/tx_internal_data_fee_delegated_value_transfer_memo_with_ratio.go b/blockchain/types/tx_internal_data_fee_delegated_value_transfer_memo_with_ratio.go index 93b1e2715..2e65e3517 100644 --- a/blockchain/types/tx_internal_data_fee_delegated_value_transfer_memo_with_ratio.go +++ b/blockchain/types/tx_internal_data_fee_delegated_value_transfer_memo_with_ratio.go @@ -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 } diff --git a/blockchain/types/tx_internal_data_fee_delegated_value_transfer_with_ratio.go b/blockchain/types/tx_internal_data_fee_delegated_value_transfer_with_ratio.go index 751621624..4b5f302b5 100644 --- a/blockchain/types/tx_internal_data_fee_delegated_value_transfer_with_ratio.go +++ b/blockchain/types/tx_internal_data_fee_delegated_value_transfer_with_ratio.go @@ -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 } diff --git a/blockchain/types/tx_internal_data_smart_contract_execution.go b/blockchain/types/tx_internal_data_smart_contract_execution.go index e73bf563c..791ce9972 100644 --- a/blockchain/types/tx_internal_data_smart_contract_execution.go +++ b/blockchain/types/tx_internal_data_smart_contract_execution.go @@ -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) { diff --git a/blockchain/types/tx_internal_data_value_transfer.go b/blockchain/types/tx_internal_data_value_transfer.go index eebc019da..5b81efe59 100644 --- a/blockchain/types/tx_internal_data_value_transfer.go +++ b/blockchain/types/tx_internal_data_value_transfer.go @@ -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 } diff --git a/blockchain/types/tx_internal_data_value_transfer_memo.go b/blockchain/types/tx_internal_data_value_transfer_memo.go index d5a57ea7a..56193618c 100644 --- a/blockchain/types/tx_internal_data_value_transfer_memo.go +++ b/blockchain/types/tx_internal_data_value_transfer_memo.go @@ -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 } diff --git a/kerrors/kerrors.go b/kerrors/kerrors.go index 5cf5e8068..e22a701d7 100644 --- a/kerrors/kerrors.go +++ b/kerrors/kerrors.go @@ -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") diff --git a/tests/transaction_validation_test.go b/tests/transaction_validation_test.go index d51a2dff4..7c2bf8b19 100644 --- a/tests/transaction_validation_test.go +++ b/tests/transaction_validation_test.go @@ -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() { diff --git a/tests/tx_validation_test.go b/tests/tx_validation_test.go index 35f5bb409..d700d8f91 100644 --- a/tests/tx_validation_test.go +++ b/tests/tx_validation_test.go @@ -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 @@ -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 @@ -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 @@ -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