1

I am looking go-ethereum code and it is a bit hard to understand, how actually an account holds its information.

Based on Ethereum White Paper,

An Ethereum account contains four fields:

  • The nonce
  • The account's current ether balance
  • The account's contract code, if present
  • The account's storage (empty by default)

When I look at account type(/accounts/accounts.go) in go-ethereum, it only has Address and Url. I looked at Address type but there is not much information what I am looking for but I found StateDB has some more functions which are related the White Paper's explanation.

Could you explain Account and StateDB relationship or the mechanism how an account holds that information?

2

The account data is stored in the world state trie, with each leaf in the trie representing an account.

See: What are the storage and state tries?

The account data structure is RLP-encoded, and contains:

  • the nonce;
  • the balance;
  • the storage root, which is a hash of the root of the account's storage trie;
  • the code hash, which is a hash of the code associated with the account (i.e. contract code).

(See the tag.)

This is represented in state_object.go as:

// Account is the Ethereum consensus representation of accounts.
// These objects are stored in the main account trie.
type Account struct {
    Nonce    uint64
    Balance  *big.Int
    Root     common.Hash // merkle root of the storage trie
    CodeHash []byte
}

Associated handling and functionality are mostly in the files in go-ethereum/core/state/ rather than go-ethereum/accounts/.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.