메서드가 없을 때 호출되는 폴백 함수에 대한 가스 제한은 얼마입니까?

Nov 15 2020

문서에 따르면 일치하지 않는 함수 식별자가 대체 함수를 트리거합니다. 누군가가 내 계약을 호출하는 경우 그래서 일이 "thisMethodDoesntExist는 ()"는이 대체가 함께 트리거 될 때와 같은 2300의 제한이있을 것입니다 send/ transfer, 또는 같은 호출 방법으로에서 가스를해야합니다 .call.value?

답변

2 goodvibration Nov 15 2020 at 20:38

놀랍게도 아니요, 공식 문서 는 실제로 이것에 대해 명확하지 않습니다.

그래서 다음은 fallback 함수의 시작 부분에 남아있는 가스를 인쇄하는 간단한 테스트입니다.

견고성 계약 :

pragma solidity 0.6.12;

interface Interface0 {
    function thisMethodDoesntExist() external;
}

contract Contract1 {
    // note that prior to solidity 0.6.0, you need to replace `fallback` with `function`
    fallback() external payable {
        string memory message;
        uint256 x = gasleft();
        while (x > 0) {
            message = string(abi.encodePacked(uint8(x % 10 + 48), message));
            x /= 10;
        }
        revert(message);
    }
}

contract Contract2 {
    function test(Interface0 contract1) external {
        contract1.thisMethodDoesntExist();
    }
}

Truffle 5.x 테스트 :

const Contract1 = artifacts.require("Contract1");
const Contract2 = artifacts.require("Contract2");

contract("test", (accounts) => {
    it("test", async () => {
        contract1 = await Contract1.new();
        contract2 = await Contract2.new();
        try {
            await contract2.test(contract1.address);
        }
        catch (error) {
            console.log(error.message);
        }
    });
});

이 테스트의 출력은 다음과 같습니다.

VM Exception while processing transaction: revert 9328773 -- Reason given: 9328773