
这是UniswapV2Factory契约createPair函数。我知道create2操作码包含4个参数(值、偏移量、大小、盐分)。
添加(字节码,32)如何给出偏移量?
mload(字节码)如何给出实际字节码的大小?
“字节码”是实际的字节码,不是吗?
发布于 2022-06-02 13:28:47
mload(字节码)如何给出实际字节码的大小?
内存数组具有特定的布局。长度写在变量地址的32个字节上,然后是数据。
举个例子:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Test {
function test() public pure returns (uint256 length) {
// bytes array containing 2 bytes
bytes memory array = '\xFF\xFF';
// mload from `array` which is an address containing the length
// of the array as a 32 bytes value
assembly {
length := mload(array)
}
// returns 2
}
}添加(字节码,32)如何给出偏移量?
在这种情况下,偏移量可以转换为“开始数据的地址”。由于我们在上面已经看到,长度在开始时编码为32个字节,接下来的是数据。add(bytecode, 32)跳过包含长度值的32个字节,以获得数组数据所在的地址。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Test {
// the bytes32 type is only for convenience / the example.
function test() public pure returns (bytes32 data) {
// bytes array containing 2 bytes
bytes memory array = '\xFF\xFF';
// mload from `array` + 32 which is the address of the data.
// Skipping the length field encoded on the first 32 bytes
assembly {
data := mload(add(array, 32))
}
// returns 0xffff000000000000000000000000000000000000000000000000000000000000
}
}“字节码”是实际的字节码,不是吗?
现在应该清楚的是,答案是否定的。bytecode参数实际上是一个内存位置的地址,在这个地址上,实际字节码的长度写在32个字节上,然后是字节码。
我希望这能回答你的问题。
https://ethereum.stackexchange.com/questions/129419
复制相似问题