账户
与BTC不同,ETH使用的是基于账户的账本(account-based ledger),账本中记录了各个用户的的余额,交易时不必追溯币的来源。
Unlike BTC xff0c; ETH uses account-based accounts xff08; account-based ledger) xff0c; balances of individual users are recorded in the books xff0c; transactions need not be traced back to the origin of the currency.
基于账户的账本对双花攻击(duoble spending attack)有着天然的防御作用,账户余额是系统中全节点维护的一个状态,称为balance,交易发生时余额随机改变,但与之而来的问题便是重放攻击(reply attack):若将已发布的交易再一次进行广播发布,造成支付者多次支付,接收者将因此获利。
The account book based on the account ran an attack on
ETH中使用nonce解决这一问题,nonce作为账户状态中的一个字段,有着计数器的功能,记录了账户有史以来交易的次数,转账时,nonce成为交易内容的一部分,受支付者的签名保护。
ETH uses nonce to solve the problem xff0c; nonce as a field in the account status xff0c; functions with counters xff0c; records the number of transactions in the account's history xff0c; transfers xff0c; nonce becomes part of the transaction xff0c; is protected by the signature of the payer.
ETH有两类账户:
ETH has two types of accounts xff1a;
- 外部账户(externally owned account)
也称普通账户,由公私钥对进行控制,包含之前所述的balance和nonce;另外一类账户为合约账户; - 合约账户(smart contract account)
合约账户不是通过公私钥对进行控制。在创建合约时,合约将返回一个地址,由此地址可调用合约,但合约账户不能主动发起一次交易,所有的交易只能由外部账户发起,之后可能会引起合约间的相互调用。另外,除了balance和nonce,合约账户还有代码code和存储storage。
由于ETH支持智能合约,要求参与者有比较稳定的身份,这与BTC的设计有所不同。
Because ETH supports smart contracts & #xff0c; requires participants to have a more stable identity & #xff0c; this is different from the BTC design.
状态树
ETH的账户地址是160bits,共20个字节,账户地址到账户状态为一对映射,账户状态为前述的包括balance,nonce,code,storage等。所有的账户组织成一个改进的Merkle Patricia Tree。
The account address of ETH is 160bits, 20 bytes & #xff0c; the account address to account status is a pair & #xff0c; the account status is as described above, including balance, nonce, code, store. All accounts are organized into an improved Merkele Patricia Tree.
存储账户状态的结构为状态树,其基本数据结构为Merkle Patricia Tree。MPT是一种融合了merkle tree和patricia tree两种树结构优点的数据结构,用路径压缩提高效率,并可计算出一个账户状态的根哈希值, 保存于block header中。
The structure of the storage account state is & #xff0c; its basic data structure is
merkle tree可参考:【区块链基础】2——BTC区块结构
patricia tree可参考:patricia-tree
xff1a;
MPT丰富了节点类型,共有如下4个类型的节点: MPT enriches the node type xff0c; there are four nodes xff1a of the following types; ETH对MPT做了略微的改进: ETH has made minor improvements to MPT #xff1a; 具体以官方的下图为例: Examples are xff1a; 用于记录交易的数据信息,其结构也为MPT,其键值为交易在发布的区块中交易的序号。 Data information for recording transactions & #xff0c; it is also structured as MPT, the key value is the serial number of the transaction that is traded in the published block. 每笔交易完成后形成一个收据,用于记录交易的相关信息与执行结果,其数据结构也为MPT,其键值为交易在发布的区块中交易的序号,与交易树中的节点一一对应,。由于ETH智能合约执行过程比较复杂,增加收据树有利于快速查询执行结果。 Upon completion of each transaction, a receipt & #xff0c is formed; the relevant information and execution results of the transaction are recorded & #xff0c; its data structure is also MPT, its key value is the serial number of the transaction in the published block & #xff0c; it corresponds to the node in the transaction tree xff0c; due to the complexity of the ETH smart contract implementation xff0c; the addition of a receipt tree facilitates quick query implementation results. bloom filter可以比较高效的查找某个元素是否包含在比较大的集合之内。直观的说,bloom算法类似一个hash set,用来判断某个元素(key)是否在某个集合中。和一般的hash set不同的是,这个算法无需存储key的值,对于每个key,只需要k个比特位,每个存储一个标志,用来判断key是否在集合中。 Bloom filter can be more efficient in finding whether an element is contained in a larger collection. Intuitive & #xff0c; bloom algorithms are similar to a hash set, used to determine whether an element & #xff08; key) is in a particular collection. Unlike the normal hash set & #xff0c; this algorithm does not need to store the value of key xff0c; for each key #xff0c; only a kbit xff0c; for each store a sign xff0c; used to determine whether key is in a pool. 在查找某一交易时可快速过滤掉大量无关的区块 A large number of unrelated blocks can be quickly filtered off when searching for a transaction header block extblock extblock为真正发布的区块信息,即为block结构的前三项。 Extblock is the actual release of block information & #xff0c; that is, the first three block structures. 新区块发布 New blocks release status-sharing tree sharing nodes xff0c; each release of new nodes xff0c; part of MPT tree status changes xff0c; but changes do not modify xff0c; instead, new branches xff0c; reserve status. xff0c need to modify xff0c; other unmodified nodes directly point forward the corresponding nodes in a block xff0c; trading trees and receipts only organize transactions issued within the current block xff0c; xff1a; ETH为一个交易驱动的状态机,其状态为状态树中账户的状态,通过执行交易树的交易,可驱动系统由当前状态转移到下一个状态,其状态转移时确定性的,即通过给定的当前状态和交易,能确定性的转移到下一个状态。 ETH is a transaction-driven state machine xff0c; its status is the state of the account in the status tree xff0c; transactions by executing the transaction tree xff0c; driveable systems from current status to next status xff0c; determinative xff0c at the time of the transfer; i.e., by giving the current state and transaction xff0c; and capable of making a determinative transition to the next state. 欢迎打赏Σ(っ°Д°;)っ Welcome to
MPT作用如下:
MPT_xff1a;
只要根哈希值不变,整个树的任何部分都无法被篡改,每个账户的状态不会被篡改。
账户所在的分支自下而上作为merkle proof发给轻节点,轻节点进行验证。
交易树
收据树
布隆过滤器 bloom filter
算法:
> algorithm #xff1a;
区块
多个区块的状态树共享节点,每次发布新区块,MPT树中部分节点状态会改变,但改变并非在原地修改,而是新建一些分支,保留原本状态。当仅仅有新发生改变的节点才需要修改,其他未修改节点直接指向前一个区块中的对应节点。而交易树与收据树只将当前区块内发布的交易组织起来,如图所示:
由于以太坊中出块时间15s左右,会产生很多的分叉,保持历史记录的一个好处是,当某些分叉需要回滚时可以更好的查看历史记录。
交易驱动的状态机 transaction-drived state machine
参考资料
区块链基础系列
注册有任何问题请添加 微信:MVIP619 拉你进入群
打开微信扫一扫
添加客服
进入交流群
发表评论