我注意到在本地和使用 Remix 中使用相同的 solc 编译器生成的字节码存在差异。
这是源代码:
pragma solidity 0.8.20;
contract HelloWorld {}
我使用
solc 0.8.20+commit.a1b79de6.Darwin.appleclang
使用以下命令在本地编译它:solc --bin --evm-version=shanghai src/HelloWorld.sol
然后我使用 remix 编译它:
在本地使用 solc 我得到:
6080604052348015600e575f80fd5b50603e80601a5f395ff3fe60806040525f80fdfea26469706673582212207b2707958b214a2fbb3ee97873fd5450b38f093d306ca27f3ab234659b8a6f8064736f6c63430008140033
使用混音:
6080604052348015600e575f80fd5b50603e80601a5f395ff3fe60806040525f80fdfea2646970667358221220d6df573e47185fc28b4a57d786409da70c81ef5acd34160533da68ef8405873c64736f6c63430008140033
乍一看,输出看起来相同,但它们是不同的。我使用相同的编译器版本,相同的 evm 版本并且没有优化,所以我想知道还有什么可能导致这种差异?
这种差异可能与二进制文件中包含元数据哈希的部分有关,而不是与代码本身有关。请参阅https://docs.soliditylang.org/en/latest/metadata.html
您可以测试使用标志
--no-cbor-metadata
或使用以下说明修改编译器的设置:
// Metadata settings (optional)
"metadata": {
// The CBOR metadata is appended at the end of the bytecode by default.
// Setting this to false omits the metadata from the runtime and deploy time code.
"appendCBOR": true,
// Use only literal content and not URLs (false by default)
"useLiteralContent": true,
// Use the given hash method for the metadata hash that is appended to the bytecode.
// The metadata hash can be removed from the bytecode via option "none".
// The other options are "ipfs" and "bzzr1".
// If the option is omitted, "ipfs" is used by default.
"bytecodeHash": "ipfs"
},
完整详细信息可以在这里找到https://docs.soliditylang.org/en/latest/using-the-compiler.html#compiler-input-and-output-json-description%7D