github.com/klaytn/klaytn@v1.12.1/blockchain/types/account/legacy_account.go (about)

     1  // Copyright 2019 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The klaytn library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package account
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"math/big"
    23  
    24  	"github.com/go-stack/stack"
    25  	"github.com/klaytn/klaytn/blockchain/types/accountkey"
    26  	"github.com/klaytn/klaytn/common"
    27  )
    28  
    29  // LegacyAccount is the Klaytn consensus representation of legacy accounts.
    30  // These objects are stored in the main account trie.
    31  type LegacyAccount struct {
    32  	Nonce    uint64
    33  	Balance  *big.Int
    34  	Root     common.Hash // merkle root of the storage trie
    35  	CodeHash []byte
    36  }
    37  
    38  // newEmptyLegacyAccount returns an empty legacy account.
    39  // This object will be used for RLP-decoding.
    40  func newEmptyLegacyAccount() *LegacyAccount {
    41  	return &LegacyAccount{}
    42  }
    43  
    44  // newLegacyAccount returns a LegacyAccount object whose all
    45  // attributes are initialized.
    46  // This object is used when an account is created.
    47  // Refer to StateDB.createObject().
    48  func newLegacyAccount() *LegacyAccount {
    49  	logger.CritWithStack("Legacy account is deprecated.")
    50  	return &LegacyAccount{
    51  		0, new(big.Int), common.Hash{}, emptyCodeHash,
    52  	}
    53  }
    54  
    55  func newLegacyAccountWithMap(values map[AccountValueKeyType]interface{}) *LegacyAccount {
    56  	acc := newLegacyAccount()
    57  
    58  	if v, ok := values[AccountValueKeyNonce].(uint64); ok {
    59  		acc.Nonce = v
    60  	}
    61  
    62  	if v, ok := values[AccountValueKeyBalance].(*big.Int); ok {
    63  		acc.Balance.Set(v)
    64  	}
    65  
    66  	if v, ok := values[AccountValueKeyStorageRoot].(common.Hash); ok {
    67  		acc.Root = v
    68  	}
    69  
    70  	if v, ok := values[AccountValueKeyCodeHash].([]byte); ok {
    71  		acc.CodeHash = v
    72  	}
    73  
    74  	return acc
    75  }
    76  
    77  func (a *LegacyAccount) Type() AccountType {
    78  	return LegacyAccountType
    79  }
    80  
    81  func (a *LegacyAccount) GetNonce() uint64 {
    82  	return a.Nonce
    83  }
    84  
    85  func (a *LegacyAccount) GetBalance() *big.Int {
    86  	return new(big.Int).Set(a.Balance)
    87  }
    88  
    89  func (a *LegacyAccount) GetHumanReadable() bool {
    90  	// LegacyAccount cannot be a human-readable account.
    91  	return false
    92  }
    93  
    94  func (a *LegacyAccount) GetStorageRoot() common.Hash {
    95  	return a.Root
    96  }
    97  
    98  func (a *LegacyAccount) GetCodeHash() []byte {
    99  	return a.CodeHash
   100  }
   101  
   102  func (a *LegacyAccount) SetNonce(n uint64) {
   103  	a.Nonce = n
   104  }
   105  
   106  func (a *LegacyAccount) SetBalance(b *big.Int) {
   107  	a.Balance.Set(b)
   108  }
   109  
   110  func (a *LegacyAccount) SetHumanReadable(b bool) {
   111  	// DO NOTHING.
   112  	logger.Warn("LegacyAccount.SetHumanReadable() should not be called. Please check the call stack.",
   113  		"callstack", stack.Caller(0).String())
   114  }
   115  
   116  func (a *LegacyAccount) SetStorageRoot(h common.Hash) {
   117  	a.Root = h
   118  }
   119  
   120  func (a *LegacyAccount) SetCodeHash(h []byte) {
   121  	a.CodeHash = h
   122  }
   123  
   124  func (a *LegacyAccount) Empty() bool {
   125  	return a.GetNonce() == 0 && a.GetBalance().Sign() == 0 && bytes.Equal(a.GetCodeHash(), emptyCodeHash)
   126  }
   127  
   128  func (a *LegacyAccount) UpdateKey(newKey accountkey.AccountKey, currentBlockNumber uint64) error {
   129  	return ErrAccountKeyNotModifiable
   130  }
   131  
   132  func (a *LegacyAccount) DeepCopy() Account {
   133  	return &LegacyAccount{
   134  		a.Nonce,
   135  		new(big.Int).Set(a.Balance),
   136  		a.Root,
   137  		a.CodeHash,
   138  	}
   139  }
   140  
   141  func (a *LegacyAccount) Dump() {
   142  	fmt.Println(a.String())
   143  }
   144  
   145  func (a *LegacyAccount) String() string {
   146  	return fmt.Sprintf(`
   147  	Nonce:        %d
   148  	Balance:      %v
   149  	StorageRoot:  %s
   150  	CodeHash:     %s`,
   151  		a.Nonce,
   152  		a.Balance,
   153  		a.Root.String(),
   154  		string(a.CodeHash))
   155  }
   156  
   157  func (a *LegacyAccount) Equal(b Account) bool {
   158  	tb, ok := b.(*LegacyAccount)
   159  	if !ok {
   160  		return false
   161  	}
   162  
   163  	return a.Nonce == tb.Nonce &&
   164  		a.Balance.Cmp(tb.Balance) == 0 &&
   165  		bytes.Equal(a.Root.Bytes(), tb.Root.Bytes()) &&
   166  		bytes.Equal(a.CodeHash, tb.CodeHash)
   167  }