diff --git a/blockchain/types/account/account.go b/blockchain/types/account/account.go index 3d02e7125..68492876e 100644 --- a/blockchain/types/account/account.go +++ b/blockchain/types/account/account.go @@ -98,14 +98,8 @@ type Account interface { // - codeHash is the same as emptyCodeHash Empty() bool - // Equal returns true if all the attributes are exactly same. Otherwise, returns false. - Equal(Account) bool - // DeepCopy copies all the attributes. DeepCopy() Account - - // String returns all attributes of this object as a string. - String() string } // ProgramAccount is an interface of an account having a program (code + storage). diff --git a/blockchain/types/account/account_common.go b/blockchain/types/account/account_common.go index 9771606a2..82f1bc869 100644 --- a/blockchain/types/account/account_common.go +++ b/blockchain/types/account/account_common.go @@ -20,7 +20,6 @@ package account import ( "encoding/json" - "fmt" "io" "math/big" @@ -210,15 +209,3 @@ func (e *AccountCommon) UpdateKey(newKey accountkey.AccountKey, currentBlockNumb } return e.ReplaceKey(newKey, currentBlockNumber) } - -func (e *AccountCommon) Equal(ta *AccountCommon) bool { - return e.nonce == ta.nonce && - e.balance.Cmp(ta.balance) == 0 && - e.humanReadable == ta.humanReadable && - e.key.Equal(ta.key) -} - -func (e *AccountCommon) String() string { - return fmt.Sprintf("{Nonce:%d, Balance:%s, HumanReadable:%t key:%s}\n", e.nonce, e.balance.String(), e.humanReadable, - e.key.String()) -} diff --git a/blockchain/types/account/account_test.go b/blockchain/types/account/account_test.go index 147721d2d..237c96f9d 100644 --- a/blockchain/types/account/account_test.go +++ b/blockchain/types/account/account_test.go @@ -19,6 +19,7 @@ package account import ( + "bytes" "encoding/json" "math/big" "testing" @@ -44,6 +45,35 @@ var ( _ AccountWithKey = (*SmartContractAccount)(nil) ) +func checkCommonFields(t *testing.T, a, b AccountWithKey) { + assert.Equal(t, a.GetNonce(), b.GetNonce()) + assert.Zero(t, a.GetBalance().Cmp(b.GetBalance())) + assert.Equal(t, a.GetHumanReadable(), b.GetHumanReadable()) + assert.True(t, a.GetKey().Equal(b.GetKey())) +} + +func checkProgramFields(t *testing.T, a, b ProgramAccount) { + assert.Equal(t, a.GetStorageRoot(), b.GetStorageRoot()) + assert.True(t, bytes.Equal(a.GetCodeHash(), b.GetCodeHash())) + assert.Equal(t, a.GetCodeFormat(), b.GetCodeFormat()) + assert.Equal(t, a.GetVmVersion(), b.GetVmVersion()) +} + +func checkEqual(t *testing.T, a, b Account) { + assert.Equal(t, a.Type(), b.Type()) + + // Using AccountWithKey to check `Key` field together. + ak := GetAccountWithKey(a) + bk := GetAccountWithKey(b) + checkCommonFields(t, ak, bk) + + // Since the implementation of EIP-7702, the EOA also has program fields. + // So, we use ProgramAccount to check these fields for both EOA and SCA. + pa := GetProgramAccount(a) + pb := GetProgramAccount(b) + checkProgramFields(t, pa, pb) +} + func checkEncode(t *testing.T, account Account, expected string) { enc := NewAccountSerializerWithAccount(account) b, err := rlp.EncodeToBytes(enc) @@ -63,7 +93,7 @@ func checkDecode(t *testing.T, encoded string, expected Account) { dec := NewAccountSerializer() err := rlp.DecodeBytes(b, &dec) assert.Nil(t, err) - assert.True(t, dec.GetAccount().Equal(expected)) + checkEqual(t, expected, dec.GetAccount()) } func checkDecodeExt(t *testing.T, encoded string, expected Account) { @@ -71,7 +101,7 @@ func checkDecodeExt(t *testing.T, encoded string, expected Account) { dec := NewAccountSerializerExt() err := rlp.DecodeBytes(b, &dec) assert.Nil(t, err) - assert.True(t, dec.GetAccount().Equal(expected)) + checkEqual(t, expected, dec.GetAccount()) } func checkEncodeJSON(t *testing.T, account Account, expectedMap map[string]interface{}) { @@ -91,7 +121,7 @@ func checkDecodeJSON(t *testing.T, j map[string]interface{}, expected Account) { dec := NewAccountSerializer() err = dec.UnmarshalJSON(b) assert.Nil(t, err) - assert.True(t, dec.GetAccount().Equal(expected)) + checkEqual(t, expected, dec.GetAccount()) } func TestAccountSerializer(t *testing.T) { diff --git a/blockchain/types/account/externally_owned_account.go b/blockchain/types/account/externally_owned_account.go index 71b10f478..6ddd30d37 100644 --- a/blockchain/types/account/externally_owned_account.go +++ b/blockchain/types/account/externally_owned_account.go @@ -21,7 +21,6 @@ package account import ( "bytes" "encoding/json" - "fmt" "io" "math/big" @@ -94,21 +93,6 @@ func (e *ExternallyOwnedAccount) Type() AccountType { return ExternallyOwnedAccountType } -func (e *ExternallyOwnedAccount) Dump() { - fmt.Println(e.String()) -} - -func (e *ExternallyOwnedAccount) String() string { - return fmt.Sprintf(`EOA:%s - StorageRoot: %s - CodeHash: %s - CodeInfo: %s`, - e.AccountCommon.String(), - e.storageRoot.String(), - common.Bytes2Hex(e.codeHash), - e.codeInfo.String()) -} - func (e *ExternallyOwnedAccount) DeepCopy() Account { return &ExternallyOwnedAccount{ AccountCommon: e.AccountCommon.DeepCopy(), @@ -118,18 +102,6 @@ func (e *ExternallyOwnedAccount) DeepCopy() Account { } } -func (e *ExternallyOwnedAccount) Equal(a Account) bool { - e2, ok := a.(*ExternallyOwnedAccount) - if !ok { - return false - } - - return e.AccountCommon.Equal(e2.AccountCommon) && - e.storageRoot == e2.storageRoot && - bytes.Equal(e.codeHash, e2.codeHash) && - e.codeInfo == e2.codeInfo -} - func (e *ExternallyOwnedAccount) GetStorageRoot() common.ExtHash { return e.storageRoot } diff --git a/blockchain/types/account/legacy_account.go b/blockchain/types/account/legacy_account.go index 23e8476e5..8f619ce4f 100644 --- a/blockchain/types/account/legacy_account.go +++ b/blockchain/types/account/legacy_account.go @@ -20,7 +20,6 @@ package account import ( "bytes" - "fmt" "math/big" "github.com/go-stack/stack" @@ -139,31 +138,3 @@ func (a *LegacyAccount) DeepCopy() Account { a.CodeHash, } } - -func (a *LegacyAccount) Dump() { - fmt.Println(a.String()) -} - -func (a *LegacyAccount) String() string { - return fmt.Sprintf(` - Nonce: %d - Balance: %v - StorageRoot: %s - CodeHash: %s`, - a.Nonce, - a.Balance, - a.Root.String(), - string(a.CodeHash)) -} - -func (a *LegacyAccount) Equal(b Account) bool { - tb, ok := b.(*LegacyAccount) - if !ok { - return false - } - - return a.Nonce == tb.Nonce && - a.Balance.Cmp(tb.Balance) == 0 && - bytes.Equal(a.Root.Bytes(), tb.Root.Bytes()) && - bytes.Equal(a.CodeHash, tb.CodeHash) -} diff --git a/blockchain/types/account/smart_contract_account.go b/blockchain/types/account/smart_contract_account.go index ecf6dab21..60ad11234 100644 --- a/blockchain/types/account/smart_contract_account.go +++ b/blockchain/types/account/smart_contract_account.go @@ -21,7 +21,6 @@ package account import ( "bytes" "encoding/json" - "fmt" "io" "math/big" @@ -257,18 +256,6 @@ func (sca *SmartContractAccount) UpdateKey(newKey accountkey.AccountKey, current return ErrAccountKeyNotModifiable } -func (sca *SmartContractAccount) Equal(a Account) bool { - sca2, ok := a.(*SmartContractAccount) - if !ok { - return false - } - - return sca.AccountCommon.Equal(sca2.AccountCommon) && - sca.storageRoot == sca2.storageRoot && - bytes.Equal(sca.codeHash, sca2.codeHash) && - sca.codeInfo == sca2.codeInfo -} - func (sca *SmartContractAccount) DeepCopy() Account { return &SmartContractAccount{ AccountCommon: sca.AccountCommon.DeepCopy(), @@ -277,14 +264,3 @@ func (sca *SmartContractAccount) DeepCopy() Account { codeInfo: sca.codeInfo, } } - -func (sca *SmartContractAccount) String() string { - return fmt.Sprintf(`Common:%s - StorageRoot: %s - CodeHash: %s - CodeInfo: %s`, - sca.AccountCommon.String(), - sca.storageRoot.String(), - common.Bytes2Hex(sca.codeHash), - sca.codeInfo.String()) -}