递归EVM消息牢固地调用

发布于 2025-02-10 22:49:30 字数 1030 浏览 2 评论 0原文

我需要进行递归的EVM消息调用。根据我对坚固性的理解,同一合同中的呼叫不会触发EVM消息。但是对于我的用例,我希望每一个递归调用是EVM消息调用。这是一份斐波那契合同。这是上述正常版本

contract Fibonacci {
    function fibonacci(uint32 n) public pure returns(uint32) {
        if (n == 0 || n == 1) {
            return n;
        }
    
        return fibonacci(n - 1) + fibonacci(n - 2);
    }
}

fibonAcci在同一合同中发生的EVM链调用。要调用另一个智能合约的代码,您需要您要致电的合同的地址。因此,我使用相同的想法强制EVM递归链呼叫(或者我认为)。这是实现的

contract Fibonacci {
    function fibonacciIndirect(uint32 n) public view returns(uint32) {
        if (n == 0 || n == 1) {
            return n;
        }
        uint32 n1;
        uint32 n2;
        n1 = Fibonacci(this).fibonacciIndirect(n - 1);
        n2 = Fibonacci(this).fibonacciIndirect(n - 2);
        return n1 + n2;
    }
}

,因此我使用作为斐波那契合同的地址,并使用它来触发EVM链呼叫。我的测试表明,该功能正确地计算了斐波那契序列的第n个值,并且与普通版本相比,它也会消耗更多的气体,但是由于我在文档上没有看到它,所以我不确定它是否是正确的方法那。是的,我知道更多的EVM调用意味着更多的气体,因此,由于这个原因,没有人会在第一个实施中选择斐波那契的第二次实现。但是作为我的测试的一部分,我需要能够强制递归EVM消息调用。

I need to make recursive evm message calls. Based on my understanding of how solidity works, calls within the same contract don't trigger an evm message. But for my use case I want each of these recursive calls to be evm message calls. It's a fibonacci contract. Here's the normal version

contract Fibonacci {
    function fibonacci(uint32 n) public pure returns(uint32) {
        if (n == 0 || n == 1) {
            return n;
        }
    
        return fibonacci(n - 1) + fibonacci(n - 2);
    }
}

In the above the recursive calls of fibonacci don't trigger an evm chain call as they happen within the same contract. To call code of one smart contract from another, you need the address of the contract you want to call. So I used the same idea to force evm recursive chain calls(or so I think). Here's the implementation

contract Fibonacci {
    function fibonacciIndirect(uint32 n) public view returns(uint32) {
        if (n == 0 || n == 1) {
            return n;
        }
        uint32 n1;
        uint32 n2;
        n1 = Fibonacci(this).fibonacciIndirect(n - 1);
        n2 = Fibonacci(this).fibonacciIndirect(n - 2);
        return n1 + n2;
    }
}

So I'm using this as the address of the Fibonacci contract and use that to trigger an evm chain call. My tests show that the function correctly computes the nth value of the fibonacci sequence and also consumes more gas compared to the normal version but because I didn't see this on the documentation, I'm not really sure if its the right way to do that. Yes I'm aware that more evm calls mean more gas and no one will pick the second implementation of fibonacci over the first for this reason. But as part of my tests, I need to be able to force recursive evm message calls.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫﹏色ふ单纯 2025-02-17 22:49:30

您可以使用this.foo()将“外部呼叫”(即一个呼叫而不是跳跃)强制函数foo。文档在这里>。这是您在第二个示例中使用的。当然,您不应该在生产中使用它,或者只有有充分的理由这样做。

You can force an "external call" (i.e. a CALL instead of a JUMP) to the function foo in Solidity by calling your function using this.foo(). This is detailed in the documentation here. This is what you used in your second example. Of course, you should not use that in production or only if you have a very good reason to do so.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文