github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/evm/emulator/state/account.go (about)

     1  package state
     2  
     3  import (
     4  	"math/big"
     5  
     6  	gethCommon "github.com/onflow/go-ethereum/common"
     7  	gethTypes "github.com/onflow/go-ethereum/core/types"
     8  	"github.com/onflow/go-ethereum/rlp"
     9  )
    10  
    11  // Account holds the metadata of an address and provides (de)serialization functionality
    12  //
    13  // Note that code and storage slots of an address is not part of this data structure
    14  type Account struct {
    15  	// address
    16  	Address gethCommon.Address
    17  	// balance of the address
    18  	Balance *big.Int
    19  	// nonce of the address
    20  	Nonce uint64
    21  	// hash of the code
    22  	// if no code the gethTypes.EmptyCodeHash is stored
    23  	CodeHash gethCommon.Hash
    24  	// the id of the collection holds storage slots for this account
    25  	// this value is nil for EOA accounts
    26  	CollectionID []byte
    27  }
    28  
    29  // NewAccount constructs a new account
    30  func NewAccount(
    31  	address gethCommon.Address,
    32  	balance *big.Int,
    33  	nonce uint64,
    34  	codeHash gethCommon.Hash,
    35  	collectionID []byte,
    36  ) *Account {
    37  	return &Account{
    38  		Address:      address,
    39  		Balance:      balance,
    40  		Nonce:        nonce,
    41  		CodeHash:     codeHash,
    42  		CollectionID: collectionID,
    43  	}
    44  }
    45  
    46  func (a *Account) HasCode() bool {
    47  	return a.CodeHash != gethTypes.EmptyCodeHash
    48  }
    49  
    50  // Encode encodes the account
    51  func (a *Account) Encode() ([]byte, error) {
    52  	return rlp.EncodeToBytes(a)
    53  }
    54  
    55  // DecodeAccount constructs a new account from the encoded data
    56  func DecodeAccount(inp []byte) (*Account, error) {
    57  	if len(inp) == 0 {
    58  		return nil, nil
    59  	}
    60  	a := &Account{}
    61  	return a, rlp.DecodeBytes(inp, a)
    62  }