Ethereum以太坊入门概念——账户、交易、共识与智能合约
https://github.com/ethereum/wiki/wiki/White-Paper#notes
Ethereum是为实现智能协议而实现的新链。
There are two types of accounts: externally owned accounts, controlled by private keys, and contract accounts, controlled by their contract code.
-
Ethereum Accounts
Each account having a 20-byte address and state transitions being direct transfers of value and information between accounts. An Ethereum account contains four fields:
-
The nonce, a counter used to make sure each transaction can only be processed once
-
The account's current ether balance
-
The account's contract code, if present
-
The account's storage (empty by default)
-
-
Transactions
Transactions由外部账户发送。
Transactions contain:
前三个是任意加密货币都具有的。
-
The recipient of the message
-
A signature identifying the sender
-
The amount of ether to transfer from the sender to the recipient
-
An optional data field
-
A STARTGAS value, representing the maximum number of computational steps the transaction execution is allowed to take
-
A GASPRICE value, representing the fee the sender pays per computational step
-
Messages
当运行使用CALL操作码时,Contract能够给contract发送消息(Messages), 一个Message可以触发另一个协议。 A message contains:
-
The sender of the message (implicit)
-
The recipient of the message
-
The amount of ether to transfer alongside the message
-
An optional data field
-
A STARTGAS value
如果接收者地址含code,则消息的负载(payload)作为合同代码的输入;如果接收者地址是0,则创建一个新合同,合同地址由发送者的地址和消息中的nonce得到,合同内容是交易中负载(payload of transaction)执行后的结果。
-
代码执行
Ethereum协议执行的代码是基于堆栈的字节码,称为 Ethereum virtual machine code, 每个Byte代表一个操作。执行过程可以看作是一个无限循环:读counter值,取操作码,counter++,直到遇到STOP或者RETURN.
以太坊code有3个存储的空间:
-
stack,后入先出的容器,最大可以包含1024个元素,每个元素256bits.
-
memory, 无限可扩展的byte array。 以byte宽度获取,读限制在256bit宽度,写限制在8bit或256bit宽度。可以以256bit位单位扩容,但需要额外付费,gas费用随使用量二次方增加
-
contract's long-term storage, 256bit到256bit的键值, 不同于前两者,计算初始化之后仍能保持数据。
EVM中完整计算状态可由元组定义(block_state, transaction, message, code, memory, stack, pc, gas),其中block_state存储了当前账户的余额和存储,PC是程序指针。
-
挖矿(10s / block)
与比特币链不同的是,以太坊区块链除了包含交易信息外,还包含最近状态的信息。除此之外还包括2个值:区块号,难度。
以太坊挖矿过程:
-
检验前一个区块的合法性
-
确认当前区块的时间戳在前一个之后,在未来15min以内。
-
检验区块号、难度值、交易根等底层信息
-
检查该区块的Prove of work是否有效
-
-
令s[0]为前一个区块的状态,Tx为区块的n项交易列表。
-
用每项Tx[i]计算APPLY( s[i],tx[i] ) , 直到计算完所有的交易,得到最终的状态s[n]
-
交易费付给矿工
-
其他矿工检验当前挖出的区块是否合法。
智能协议代码的执行实际上是计算状态转换的一个过程.
以太坊在发布时数量为1.198x,其后每年有0.26x以太币被挖出,但考虑到因为因为粗心或死亡造成的恒定遗失率(1%), 大概到26x时就会形成挖出-遗失平衡,进而总量保持不变。因此n年后最大的以太币数量为:60102216 * (1.198 + 0.26 * n),这也是以太坊的社会公约,即使以太坊组织灭亡后,接盘者也要遵循。
-
挖矿的中心化
以太坊区块链应该避免中心化的挖矿者(实际上这在比特币链上已经很严重)。所使用的挖矿方法是从过去N个区块中随机选取状态和交易,计算结果后hash.:( The current intent at Ethereum is to use a mining algorithm where miners are required to fetch random data from the state, compute some randomly selected transactions from the last N blocks in the blockchain, and return the hash of the result.)
这有2个好处,1是由于需要执行通用的计算,制造ASIC机器挖矿不可行,所以普通人也可以参与挖矿。2是强制矿工运行完整节点,因为需要用到过去的数据。
常用链接:
EthStats.net 是以太坊网络实时数据的仪表板,这个仪表板展示重要信息,诸如现在的区块,散表难度,gas价格和gas花费等。
https://remix.ethereum.org/ 智能合约在线编译器
https://solidity.readthedocs.io/en/v0.4.24/index.html solidity文档
https://learnblockchain.cn/2017/11/24/init-env/ 中文站点,深入浅出区块链,智能合约helloworld