再读Solidity官网文档 1

这篇具有很好参考价值的文章主要介绍了再读Solidity官网文档 1。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

由于solidity英文描述,所以每次读理解就有点不一样,领会的更多.比如: 

1. 区块链和交易

A blockchain is a globally shared, transactional database. This means that everyone can read entries in the database just by participating in the network. If you want to change something in the database, you have to create a so-called transaction which has to be accepted by all others. The word transaction implies that the change you want to make (assume you want to change two values at the same time) is either not done at all or completely applied. Furthermore, while your transaction is being applied to the database, no other transaction can alter it.

区块链是一个全局共享的,存储交易的数据库。(精辟)   意味着每个人可以通过加入区块链节点网络读取数据库里面的条目.  如果你想改变数据库里面的某些东西,你不得不创建一个必须被所有其他节点接受的"交易"。 交易(Transaction)这个词意味着你想对数据库做的改变要么完全无效,要么被完全接受。 进一步说,一旦你的交易被应用到数据库,将不会被其他交易所改变。

2.区块

One major obstacle to overcome is what (in Bitcoin terms) is called a “double-spend attack.

对区块链来说,需要攻克的最重要的障碍是双花.

Transactions are not guaranteed to be included in the next block or any specific future block, since it is not up to the submitter of a transaction, but up to the miners to determine in which block the transaction is included.

交易不能被保证一定会包含在下一个区块里面,或者特定的未来某个区块里. 因为这并不是取决于交易的提交者而是取决于矿工,矿工决定交易会被包含在那一个区块里.

3.The Ethereum Virtual Machine

在以太坊链中, EVM是合约的运行环境.

It is not only sandboxed but actually completely isolated, which means that code running inside the EVM has no access to network, filesystem or other processes. Smart contracts even have limited access to other smart contracts.

Evm 不仅是沙盒而且还完全隔离,意味着代码运行的环境没有网络权限,没有文件系统访问权限,没有其他进程访问权限。合约甚至限制访问其他合约。

4. Accounts

账户分两类: 外部账户. 合约账户.

外部账户: 公私钥控制的账户.    地址由公钥衍变而来.

合约账户: 被合约代码控制.  合约代码和account一起存储.  合约地址是在合约创建的时候确定(由合约创建者地址address和nonce决定)

Regardless of whether or not the account stores code, the two types are treated equally by the EVM.

无论account是否存储代码(无论外部账户和合约账户),两种类型被Evm同等对待.

Every account has a persistent key-value store mapping 256-bit words to 256-bit words called storage.

每一个账户有一个持久性key-value 对象storage,  映射256位字符到256位字符.

Furthermore, every account has a balance in Ether (in “Wei” to be exact, 1 ether is 10**18 wei) which can be modified by sending transactions that include Ether.

进一步说, 每一个账户有一个eth余额,可以被包括eth的交易来修改.

5.Transactions

A transaction is a message that is sent from one account to another account (which might be the same or empty, see below). It can include binary data (which is called “payload”) and Ether.

一个交易是一个message,  可以从一个账户发送到另一个账户。它可以包含二进制数据(payload)和eth.

If the target account contains code, that code is executed and the payload is provided as input data.   如果目标账户包括代码, 这些代码会被执行并且payload会作为输入参数。

If the target account is not set (the transaction does not have a recipient or the recipient is set to null), the transaction creates a new contract

如果目标账户没有设置,(交易里面没有接收人或者接收人被设置成null), 这个交易将创建一个新的合约。 合约地址由交易发送者和nonce共同衍生而来。

The payload of such a contract creation transaction is taken to be EVM bytecode and executed. The output data of this execution is permanently stored as the code of the contract. This means that in order to create a contract, you do not send the actual code of the contract, but in fact code that returns that code when executed.

创建合约的交易的payload 被作为Evm的字节码执行,执行输出数据被永久的作为合约代码存储。这意味着为了创建一个合约, 你没有发送实际的合约代码,而是当执行完payload后的的代码。

While a contract is being created, its code is still empty. Because of that, you should not call back into the contract under construction until its constructor has finished executing.

当合约刚被创建, 合约的代码依然是空的。由于这个,你不应该在合约构造函数执行完成前调用合约的任何方法。 

6. GAS 费用.

Upon creation, each transaction is charged with a certain amount of gas that has to be paid for by the originator of the transaction (tx.origin)

创建后,每笔交易都会收取一定数量的天然气,这些天然气必须由交易发起人支付(tx.origin)

While the EVM executes the transaction, the gas is gradually depleted according to specific rules. If the gas is used up at any point (i.e. it would be negative), an out-of-gas exception is triggered, which ends execution and reverts all modifications made to the state in the current call frame.

当虚拟机执行交易的时候, gas会根据特定的规则被逐渐消耗枯尽。 如果gas在执行过程中的任意一个点耗尽,一个out-of-gas异常会被触发,接着结束往下执行,回滚当前调用帧中的所有的修改状态。

This mechanism incentivizes economical use of EVM execution time and also compensates EVM executors (i.e. miners / stakers) for their work. Since each block has a maximum amount of gas, it also limits the amount of work needed to validate a block.

这个机制激励着经济的使用EVM执行时间 和 为EVM的执行者(矿工或质押者)的工作进行补偿。

由于每一个区块拥有最大数量的gas的设置, 意味着验证区块的工作量也是受到限制的。

Since EVM executors can choose to include a transaction or not, transaction senders cannot abuse the system by setting a low gas price.

由于EVM 执行者打包时,可以去选择去包含一个交易或者不包含, 交易发送者不会设置一个非常低的gasprice去滥用系统.

7. Storage(存储), Memory(内存) and the Stack(栈)

Each account has a data area called storage.

每一个账户有一个数据区域,叫 storage 存储.是函数间互相调用的存储. storage是一个256位和256位之间的key-value间的映射.

It is not possible to enumerate storage from within a contract, it is comparatively costly to read, and even more to initialise and modify storage. 

在合约内部枚举storage是不可能的, 因为读取将会使用相对高的开销花费, 初始化和修改会开销更多。

 Because of this cost, you should minimize what you store in persistent storage to what the contract needs to run.  正因为开销,你应该最小化的持久化数据到storage, 只存储合约需要运行的数据。

Store data like derived calculations, caching, and aggregates outside of the contract. A contract can neither read nor write to any storage apart from its own.

所以要在合约外,进行存储数据的计算,缓存和累计计算。 一个合约除了它自己的storage, 不能任意的读取其他的账户的storage.

 

The second data area is called memory,  第二个数据区域叫做内存。每次一个消息调用都会获取一个全新的memory.

Memory is linear and can be addressed at byte level, but reads are limited to a width of 256 bits, while writes can be either 8 bits or 256 bits wide.

内存是一个线性的数据结构,可以在字节层面进行地址标记,但是读取宽度被限制在256位. 写的宽度可以是8位或256位.

Memory is expanded by a word (256-bit), when accessing (either reading or writing) a previously untouched memory word (i.e. any offset within a word). At the time of expansion, the cost in gas must be paid. Memory is more costly the larger it grows (it scales quadratically).

当访问(读取或写入)先前未被触及的存储器字(即,字内的任何偏移)时,存储器扩展一个字(256位)。在扩建时,必须支付天然气成本。内存增长得越大,成本就越高(按二次方扩展)。

The EVM is not a register machine but a stack machine, so all computations are performed on a data area called the stack

虚拟机EVM 不是一个注册机而是一个堆栈机, 所以所有的计算将在stack(栈)里面执行.

Access to the stack is limited to the top end in the following way: It is possible to copy one of the topmost 16 elements to the top of the stack or swap the topmost element with one of the 16 elements below it. 

通过以下方式访问堆栈仅限于顶端:可以将最顶层的16个元素中的一个复制到堆栈顶部,也可以将最高层的元素与其下方的16个元件中的一个中交换。

All other operations take the topmost two (or one, or more, depending on the operation) elements from the stack and push the result onto the stack. 所有其他操作都从堆栈中获取最上面的两个(或一个,或多个,具体取决于操作)元素,并将结果推送到堆栈上。

Of course it is possible to move stack elements to storage or memory in order to get deeper access to the stack, but it is not possible to just access arbitrary elements deeper in the stack without first removing the top of the stack.

为了获取更深层的栈数据,移动栈数据到storage或memory中是可行的。 如果不先移除栈顶数据,就访问栈栈底部数据,是不可能的。

Instruction Set 指令集

The instruction set of the EVM is kept minimal in order to avoid incorrect or inconsistent implementations which could cause consensus problems.  为避免不正确的或不一致的引起共识问题的实践,EVN虚拟机指令集保持一个最新的数量.

Message Calls 

Contracts can call other contracts or send Ether to non-contract accounts by the means of message calls.  合约可以调用其他合约,或发送eth给其他非合约地址通过消息调用,Message calls. 

Message calls are similar to transactions, in that they have a source, a target, data payload, Ether, gas and return data.  Message Call 跟交易很相似, 它里面也有源头source, target目标,data payload 请求数据, eth, gas, 返回数据.

In fact, every transaction consists of a top-level message call which in turn can create further message calls.事实上,每个事务都由一个顶级消息调用组成,该消息调用又可以创建更多的消息调用。

As already said, the called contract (which can be the same as the caller) will receive a freshly cleared instance of memory and has access to the call payload - which will be provided in a separate area called the calldata. After it has finished execution, it can return data which will be stored at a location in the caller’s memory preallocated by the caller. All such calls are fully synchronous.

正如已经说过的, 被调用合约(或者调用合约)会接收到一个全新的内存空间,并且有权限访问payload数据,  payload存储在单独的叫做calldata的区域.  在它完成执行之后, 返回数据将会存储在调用者之前分配好的内存空间中.   这些调用都是异步的.

Calls are limited to a depth of 1024, which means that for more complex operations, loops should be preferred over recursive calls. Furthermore, only 63/64th of the gas can be forwarded in a message call, which causes a depth limit of a little less than 1000 in practice.

调用的深度限制为1024,这意味着对于更复杂的操作,循环应该优先于递归调用。此外,在消息调用中只能转发63/64的气体,这在实践中导致深度限制略小于1000。

Delegatecall and Libraries

There exists a special variant of a message call, named delegatecall which is identical to a message call apart from the fact that the code at the target address is executed in the context (i.e. at the address) of the calling contract and msg.sender and msg.value do not change their values.  这里存在一种特殊的变种的消息调用,叫代理调用delegatecall,   代理调用是被认为是一种消息调用---目标地址合约代码在调用合约的上下文中执行, msg.sender和msg.value 没有改变.

This means that a contract can dynamically load code from a different address at runtime. Storage, current address and balance still refer to the calling contract, only the code is taken from the called address. 这意味着一个合约可以从不同的合约地址里面加载它们的代码到自己的运行环境里。存储,当前地址, 余额仍然指向调用合约,仅仅是把被调用合约的代码加载过来。

This makes it possible to implement the “library” feature in Solidity: Reusable library code that can be applied to a contract’s storage, e.g. in order to implement a complex data structure.

这个机制为Solidity实现了library功能提供了可能: 可重复利用的库代码可以被应用到合约的数据上, 实现一个复杂的数据结构。

 Logs

It is possible to store data in a specially indexed data structure that maps all the way up to the block level.   把数据存储到特定的索引数据结构中直到映射到块级别是一个可能。

Create

Contracts can even create other contracts using a special opcode (i.e. they do not simply call the zero address as a transaction would).  合约设置可以通过特定的opcode去创建一个新合约.

The only difference between these create calls and normal message calls is that the payload data is executed and the result stored as code and the caller / creator receives the address of the new contract on the stack.     创建调用call和普通的消息call的区别是payload执行完成结果作为代码存储,创建者或调用者接收到一个新合约的地址。

Deactivate and Self-destruct

 The only way to remove code from the blockchain is when a contract at that address performs the selfdestruct operation.  唯一的从链上移除代码的方法是合约执行selfdestruct 操作.

The remaining Ether stored at that address is sent to a designated target and then the storage and code is removed from the state. Removing the contract in theory sounds like a good idea, but it is potentially dangerous, as if someone sends Ether to removed contracts, the Ether is forever lost. 剩余的eth会被发送到提前设置好的目标地址里面,  storage,代码将从state里面移除。移除合约理论上听起来很好,但是有潜在风险, 如果有人发送eth到移除的合约中,eth将永远消失.

Even if a contract is removed by selfdestruct, it is still part of the history of the blockchain and probably retained by most Ethereum nodes. So using selfdestruct is not the same as deleting data from a hard disk.

虽然合约通过selfdestruct移除了, 但是依然是区块链的历史一部分,很可能被大多数eth nodes持有. 所以selftdestruct跟从磁盘上删除的概念是完全不一样的。

If you want to deactivate your contracts, you should instead disable them by changing some internal state which causes all functions to revert. This makes it impossible to use the contract, as it returns Ether immediately. 

如果你想deactivate你的合约,你通过应该改变他们的内部状态从而使所有的函数revert达到disable它, 这使的无法使用这个合约,因为它会立即返回eth.

Precompiled Contracts

There is a small set of contract addresses that are special: The address range between 1 and (including) 8 contains “precompiled contracts” that can be called as any other contract but their behaviour (and their gas consumption) is not defined by EVM code stored at that address (they do not contain code) but instead is implemented in the EVM execution environment itself.

EVM 系统合约, 预编译合约,在EVM 执行环境本身去执行,不受gas的影响.文章来源地址https://www.toymoban.com/news/detail-471780.html

到了这里,关于再读Solidity官网文档 1的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包