Skip to content

Commit

Permalink
Merge pull request #181 from hyeonLewis/remove-test-methods
Browse files Browse the repository at this point in the history
Remove test purpose method in account type
  • Loading branch information
hyeonLewis authored Dec 17, 2024
2 parents be96359 + 25624c3 commit 4443832
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 103 deletions.
6 changes: 0 additions & 6 deletions blockchain/types/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
13 changes: 0 additions & 13 deletions blockchain/types/account/account_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package account

import (
"encoding/json"
"fmt"
"io"
"math/big"

Expand Down Expand Up @@ -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())
}
36 changes: 33 additions & 3 deletions blockchain/types/account/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package account

import (
"bytes"
"encoding/json"
"math/big"
"testing"
Expand All @@ -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)
Expand All @@ -63,15 +93,15 @@ 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) {
b := common.FromHex(encoded)
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{}) {
Expand All @@ -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) {
Expand Down
28 changes: 0 additions & 28 deletions blockchain/types/account/externally_owned_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package account
import (
"bytes"
"encoding/json"
"fmt"
"io"
"math/big"

Expand Down Expand Up @@ -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(),
Expand All @@ -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
}
Expand Down
29 changes: 0 additions & 29 deletions blockchain/types/account/legacy_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package account

import (
"bytes"
"fmt"
"math/big"

"github.com/go-stack/stack"
Expand Down Expand Up @@ -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)
}
24 changes: 0 additions & 24 deletions blockchain/types/account/smart_contract_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package account
import (
"bytes"
"encoding/json"
"fmt"
"io"
"math/big"

Expand Down Expand Up @@ -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(),
Expand All @@ -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())
}

0 comments on commit 4443832

Please sign in to comment.