|
2 | 2 | 合约的元数据
|
3 | 3 | #################
|
4 | 4 |
|
| 5 | +.. include:: glossaries.rst |
5 | 6 | .. index:: metadata, contract verification
|
6 | 7 |
|
7 |
| -The Solidity compiler automatically generates a JSON file, the |
8 |
| -contract metadata, that contains information about the current contract. |
9 |
| -It can be used to query the compiler version, the sources used, the ABI |
10 |
| -and NatSpec documentation in order to more safely interact with the contract |
11 |
| -and to verify its source code. |
12 |
| - |
13 |
| -The compiler appends a Swarm hash of the metadata file to the end of the |
14 |
| -bytecode (for details, see below) of each contract, so that you can retrieve |
15 |
| -the file in an authenticated way without having to resort to a centralized |
16 |
| -data provider. |
17 |
| - |
18 |
| -Of course, you have to publish the metadata file to Swarm (or some other service) |
19 |
| -so that others can access it. The file can be output by using ``solc --metadata`` |
20 |
| -and the file will be called ``ContractName_meta.json``. |
21 |
| -It will contain Swarm references to the source code, so you have to upload |
22 |
| -all source files and the metadata file. |
23 |
| - |
24 |
| -The metadata file has the following format. The example below is presented in a |
25 |
| -human-readable way. Properly formatted metadata should use quotes correctly, |
26 |
| -reduce whitespace to a minimum and sort the keys of all objects to arrive at a |
27 |
| -unique formatting. |
28 |
| -Comments are of course also not permitted and used here only for explanatory purposes. |
| 8 | +Solidity编译器自动生成JSON文件,即合约的元数据,其中包含了当前合约的相关信息。 |
| 9 | +它可以用于查询编译器版本,所使用的源代码,|ABI| 和 |natspec| 文档,以便更安全地与合约进行交互并验证其源代码。 |
| 10 | + |
| 11 | +编译器会将元数据文件的 Swarm 哈希值附加到每个合约的字节码末尾(详情请参阅下文), |
| 12 | +以便你可以以认证的方式获取该文件,而不必求助于中心化的数据提供者。 |
| 13 | + |
| 14 | +当然,你必须将元数据文件发布到 Swarm (或其他服务),以便其他人可以访问它。 |
| 15 | +该文件可以通过使用 ``solc --metadata`` 来生成,并被命名为 ``ContractName_meta.json`` 。 |
| 16 | +它将包含源代码的在 Swarm 上的引用,因此你必须上传所有源文件和元数据文件。 |
| 17 | + |
| 18 | +元数据文件具有以下格式。 下面的例子将以人类可读的方式呈现。 |
| 19 | +正确格式化的元数据应正确使用引号,将空白减少到最小,并对所有对象的键值进行排序以得到唯一的格式。 |
| 20 | +代码注释当然也是不允许的,这里仅用于解释目的。 |
29 | 21 |
|
30 | 22 | .. code-block:: none
|
31 | 23 |
|
32 | 24 | {
|
33 |
| - // Required: The version of the metadata format |
| 25 | + // 必选:元数据格式的版本 |
34 | 26 | version: "1",
|
35 |
| - // Required: Source code language, basically selects a "sub-version" |
36 |
| - // of the specification |
| 27 | + // 必选:源代码的编程语言,一般会选择规范的“子版本” |
37 | 28 | language: "Solidity",
|
38 |
| - // Required: Details about the compiler, contents are specific |
39 |
| - // to the language. |
| 29 | + // 必选:编译器的细节,内容视语言而定。 |
40 | 30 | compiler: {
|
41 |
| - // Required for Solidity: Version of the compiler |
| 31 | + // 对 Solidity 来说是必须的:编译器的版本 |
42 | 32 | version: "0.4.6+commit.2dabbdf0.Emscripten.clang",
|
43 |
| - // Optional: Hash of the compiler binary which produced this output |
| 33 | + // 可选: 生成此输出的编译器二进制文件的哈希值 |
44 | 34 | keccak256: "0x123..."
|
45 | 35 | },
|
46 |
| - // Required: Compilation source files/source units, keys are file names |
| 36 | + // 必选:编译的源文件/源单位,键值为文件名 |
47 | 37 | sources:
|
48 | 38 | {
|
49 | 39 | "myFile.sol": {
|
50 |
| - // Required: keccak256 hash of the source file |
| 40 | + // 必选:源文件的 keccak256 哈希值 |
51 | 41 | "keccak256": "0x123...",
|
52 |
| - // Required (unless "content" is used, see below): Sorted URL(s) |
53 |
| - // to the source file, protocol is more or less arbitrary, but a |
54 |
| - // Swarm URL is recommended |
| 42 | + // 必选(除非定义了“content”,详见下文): |
| 43 | + // 已排序的源文件的URL,URL的协议可以是任意的,但建议使用 Swarm 的URL |
55 | 44 | "urls": [ "bzzr://56ab..." ]
|
56 | 45 | },
|
57 | 46 | "mortal": {
|
58 |
| - // Required: keccak256 hash of the source file |
| 47 | + // 必选:源文件的 keccak256 哈希值 |
59 | 48 | "keccak256": "0x234...",
|
60 |
| - // Required (unless "url" is used): literal contents of the source file |
| 49 | + // 必选(除非定义了“urls”): 源文件的字面内容 |
61 | 50 | "content": "contract mortal is owned { function kill() { if (msg.sender == owner) selfdestruct(owner); } }"
|
62 | 51 | }
|
63 | 52 | },
|
64 |
| - // Required: Compiler settings |
| 53 | + // 必选:编译器的设置 |
65 | 54 | settings:
|
66 | 55 | {
|
67 |
| - // Required for Solidity: Sorted list of remappings |
| 56 | + // 对 Solidity 来说是必须的: 已排序的重定向列表 |
68 | 57 | remappings: [ ":g/dir" ],
|
69 |
| - // Optional: Optimizer settings (enabled defaults to false) |
| 58 | + // 可选: 优化器的设置( enabled 默认设为 false ) |
70 | 59 | optimizer: {
|
71 | 60 | enabled: true,
|
72 | 61 | runs: 500
|
73 | 62 | },
|
74 |
| - // Required for Solidity: File and name of the contract or library this |
75 |
| - // metadata is created for. |
| 63 | + // 对 Solidity 来说是必须的:用以生成该元数据的文件名和合约名或库名 |
76 | 64 | compilationTarget: {
|
77 | 65 | "myFile.sol": "MyContract"
|
78 | 66 | },
|
79 |
| - // Required for Solidity: Addresses for libraries used |
| 67 | + // 对 Solidity 来说是必须的:所使用的库的地址 |
80 | 68 | libraries: {
|
81 | 69 | "MyLib": "0x123123..."
|
82 | 70 | }
|
83 | 71 | },
|
84 |
| - // Required: Generated information about the contract. |
| 72 | + // 必选:合约的生成信息 |
85 | 73 | output:
|
86 | 74 | {
|
87 |
| - // Required: ABI definition of the contract |
| 75 | + // 必选:合约的 ABI 定义 |
88 | 76 | abi: [ ... ],
|
89 |
| - // Required: NatSpec user documentation of the contract |
| 77 | + // 必选:合约的 NatSpec 用户文档 |
90 | 78 | userdoc: [ ... ],
|
91 |
| - // Required: NatSpec developer documentation of the contract |
| 79 | + // 必选:合约的 NatSpec 开发者文档 |
92 | 80 | devdoc: [ ... ],
|
93 | 81 | }
|
94 | 82 | }
|
95 | 83 |
|
96 | 84 | .. note::
|
97 |
| - Note the ABI definition above has no fixed order. It can change with compiler versions. |
| 85 | + 需注意,上面的 ABI 没有固定的顺序,随编译器的版本而不同。 |
98 | 86 |
|
99 | 87 | .. note::
|
100 |
| - Since the bytecode of the resulting contract contains the metadata hash, any change to |
101 |
| - the metadata will result in a change of the bytecode. Furthermore, since the metadata |
102 |
| - includes a hash of all the sources used, a single whitespace change in any of the source |
103 |
| - codes will result in a different metadata, and subsequently a different bytecode. |
| 88 | + 由于生成的合约的字节码包含元数据的哈希值,因此对元数据的任何更改都会导致字节码的更改。 |
| 89 | + 此外,由于元数据包含所有使用的源代码的哈希值,所以任何源代码中的, |
| 90 | + 哪怕是一个空格的变化都将导致不同的元数据,并随后产生不同的字节代码。 |
104 | 91 |
|
105 | 92 | 元数据哈希字节码的编码
|
106 | 93 | =============================================
|
107 | 94 |
|
108 |
| -Because we might support other ways to retrieve the metadata file in the future, |
109 |
| -the mapping ``{"bzzr0": <Swarm hash>}`` is stored |
110 |
| -`CBOR <https://tools.ietf.org/html/rfc7049>`_-encoded. Since the beginning of that |
111 |
| -encoding is not easy to find, its length is added in a two-byte big-endian |
112 |
| -encoding. The current version of the Solidity compiler thus adds the following |
113 |
| -to the end of the deployed bytecode:: |
| 95 | +由于在将来我们可能会支持其他方式来获取元数据文件, |
| 96 | +类似 ``{"bzzr0":<Swarm hash>}`` 的键值对,将会以 `CBOR <https://tools.ietf.org/html/rfc7049>`_ 编码来存储。 |
| 97 | +由于这种编码的起始位不容易找到,因此添加两个字节来表述其长度,以大端方式编码。 |
| 98 | +所以,当前版本的Solidity编译器,将以下内容添加到部署的字节码的末尾:: |
114 | 99 |
|
115 | 100 | 0xa1 0x65 'b' 'z' 'z' 'r' '0' 0x58 0x20 <32 bytes swarm hash> 0x00 0x29
|
116 | 101 |
|
117 |
| -So in order to retrieve the data, the end of the deployed bytecode can be checked |
118 |
| -to match that pattern and use the Swarm hash to retrieve the file. |
| 102 | +因此,为了获取数据,可以检查部署的字节码的末尾以匹配该模式,并使用 Swarm 哈希来获取元数据文件。 |
119 | 103 |
|
120 |
| -自动化接口生成和NatSpec的使用方法 |
| 104 | +自动化接口生成和 |natspec| 的使用方法 |
121 | 105 | ====================================================
|
122 | 106 |
|
123 |
| -The metadata is used in the following way: A component that wants to interact |
124 |
| -with a contract (e.g. Mist) retrieves the code of the contract, from that |
125 |
| -the Swarm hash of a file which is then retrieved. |
126 |
| -That file is JSON-decoded into a structure like above. |
| 107 | +元数据以下列方式被使用:想要与合约交互的组件(例如,Mist)读取合约的字节码, |
| 108 | +从中获取元数据文件的 Swarm 哈希,然后从 Swarm 获取该文件。该文件被解码为上面的 JSON 结构。 |
127 | 109 |
|
128 |
| -The component can then use the ABI to automatically generate a rudimentary |
129 |
| -user interface for the contract. |
| 110 | +然后该组件可以使用ABI自动生成合约的基本用户接口。 |
130 | 111 |
|
131 |
| -Furthermore, Mist can use the userdoc to display a confirmation message to the user |
132 |
| -whenever they interact with the contract. |
| 112 | +此外,Mist可以使用 userdoc 在用户与合约进行交互时向用户显示确认消息。 |
133 | 113 |
|
134 |
| -Additional information about Ethereum Natural Specification (NatSpec) can be found `here <https://github.com/ethereum/wiki/wiki/Ethereum-Natural-Specification-Format>`_. |
| 114 | +有关 |natspec| 的其他信息可以在 `这里 <https://github.com/ethereum/wiki/wiki/Ethereum-Natural-Specification-Format>`_ 找到。 |
135 | 115 |
|
136 | 116 | 源代码验证的使用方法
|
137 | 117 | ==================================
|
138 | 118 |
|
139 |
| -In order to verify the compilation, sources can be retrieved from Swarm |
140 |
| -via the link in the metadata file. |
141 |
| -The compiler of the correct version (which is checked to be part of the "official" compilers) |
142 |
| -is invoked on that input with the specified settings. The resulting |
143 |
| -bytecode is compared to the data of the creation transaction or ``CREATE`` opcode data. |
144 |
| -This automatically verifies the metadata since its hash is part of the bytecode. |
145 |
| -Excess data corresponds to the constructor input data, which should be decoded |
146 |
| -according to the interface and presented to the user. |
| 119 | +为了验证编译,可以通过元数据文件中的链接从 Swarm 中获取源代码。 |
| 120 | +获取到的源码,会根据元数据中指定的设置,被正确版本的编译器(应该为“官方”编译器之一)所处理。 |
| 121 | +处理得到的字节码会与创建交易的数据或者 ``CREATE`` 操作码使用的数据进行比较。 |
| 122 | +这会自动验证元数据,因为它的哈希值是字节码的一部分。 |
| 123 | +而额外的数据,则是与基于接口进行编码并展示给用户的构造输入数据相符的。 |
0 commit comments