以太坊原理分析2 账户

账户分为两种:

  • 外部拥有账户(EOA),一般指自然人拥有的账户。
EOA特征
    codeHash为空
    storageRoot为空
    通过私钥控制
    发起交易(转移以太币或触发合约代码)
  • 合约账户(CA),为智能合约分配的账户。
CA特征
    不能发起交易,可以被触发执行合约代码(通过EOA发起的交易或者从其他CA接收的消息调用激活)

账户数据

  • nonce : 外部账户为交易次数,合约账户为创建的合约序号
  • balance : 此地址的以太币余额
  • storageRoot : 账户存储内容组成的默克尔树根的哈希值。
  • codeHash : 账户EVM代码的hash值。合约账户即为合约代码的哈希值,外部账户为空字符串的哈希值

账户数据结构

core/state/state_object.go:
type Account struct {
    Nonce    uint64
    Balance  *big.Int
    Root     common.Hash // merkle root of the storage trie
    CodeHash []byte
}

合约账户

  • 智能合约账户,就是一段代码,也可以理解为一个状态机。
  • 由交易创建
  • 由交易驱动,每次想改变智能合约状态,需要发送一笔交易
  • 合约账户不能主动发起交易,所有交易都由普通账户发出
  • 智能合约由evm直接运行,运行后的结果,可能会改变智能合约的状态,改变普通账户的状态,由每个节点本地运行evm, 只要有相同的输入,就会由相同的输出

storage root

所有智能合约的数据,都存在leveldb本地。数据组成一颗mpt树,树根会被存放在账户中,存放在链上

对于一个fullnode节点,在同步数据的时候,会构建所有的普通账户,所有智能合约账户,以及所有智能合约账户的storage存储

因此,同receipt一样,你可以理解为,所有的storage都存储在链下,但是根存储在链上

Ethereum uses Patricia Trie data structure implemented using Google’s leveldb database. It handles storage of different kinds of data on the Patricia tries – transaction trie, state trie, storage trie and receipts trie. Only the root node hashes of the tries are stored directly in the blockchain as part of each blocks.

A storage trie is where all of the contract data lives. Each Ethereum account has its own storage trie.The trie is updated whenever there is a state change. (i.e. When a change is made to an existing state variable.) All nodes will make the changes to their own copy of the trie when they receive a new block. Each node in the state trie (i.e. the account) has its own storage trie. A transaction trie is per block, and contains a trie of transactions in the block. The receipt trie contains log entries for the transactions in the transactions trie.

https://stackoverflow.com/questions/51034366/ethereum-smart-contract-storage

发表评论

电子邮件地址不会被公开。 必填项已用*标注