首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >CREATE2码偏移量和尺寸

CREATE2码偏移量和尺寸
EN

Ethereum用户
提问于 2022-06-02 12:50:41
回答 1查看 224关注 0票数 1

这是UniswapV2Factory契约createPair函数。我知道create2操作码包含4个参数(值、偏移量、大小、盐分)。

添加(字节码,32)如何给出偏移量?

mload(字节码)如何给出实际字节码的大小?

“字节码”是实际的字节码,不是吗?

EN

回答 1

Ethereum用户

发布于 2022-06-02 13:28:47

mload(字节码)如何给出实际字节码的大小?

内存数组具有特定的布局。长度写在变量地址的32个字节上,然后是数据。

举个例子:

代码语言:javascript
运行
复制
// 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个字节,以获得数组数据所在的地址。

代码语言:javascript
运行
复制
// 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个字节上,然后是字节码。

我希望这能回答你的问题。

票数 3
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/129419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档