Skip to content

Commit

Permalink
Merge pull request #182 from Mdaiki0730/fix/invalid-condition-validat…
Browse files Browse the repository at this point in the history
…e-7702

Fix incorrect condition in validate7702
  • Loading branch information
Mdaiki0730 authored Dec 19, 2024
2 parents 4443832 + 44c0074 commit 4340fd5
Show file tree
Hide file tree
Showing 20 changed files with 500 additions and 68 deletions.
190 changes: 190 additions & 0 deletions blockchain/types/mocks/statedb_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
Loading

0 comments on commit 4340fd5

Please sign in to comment.