hyperledger / besu

An enterprise-grade Java-based, Apache 2.0 licensed Ethereum client https://wiki.hyperledger.org/display/besu
https://www.hyperledger.org/projects/besu
Apache License 2.0
1.49k stars 813 forks source link

Smartcontracts complied for Berlin EVM fail in private network on Berlin milestone. #5215

Closed jorgesjv closed 1 year ago

jorgesjv commented 1 year ago

Description

In a private Besu network that transitioned to Berlin in the past we get fails with contracts compiled for Berlin EVM with Remix. The same contracts work OK when compiled for Constantinople.

Steps to Reproduce (Bug)

  1. Private network running
  2. Update to Berlin Protocol from ConstantinopleFixBlock.
  3. Test with SC compiled for EVM Berlin with transfer function and failed.
  4. Test with SC compiled for EVM Constantinople with transfer function and success.

genesis.json

{
  "config" : {
    "chainId" : 1972,
    "constantinopleFixBlock": 0,
    "berlinBlock":10782300,
    "ibft2" : {
      "blockperiodseconds" : 5,
      "epochlength" : 30000,
      "requesttimeoutseconds" : 10
    },
    "transitions": {
      "ibft2": [
        {
          "block": 8943109,
          "validators":[
            "0x555709de3d8f26f35143ef414c68b9459972575c",
            "0xb4ce159828a61066c072f8f4b37285b453cca86a",
            "0x5fcc72d50ca999a5472bb196c22d42cef8b13725",
            "0x65889c20b9451979894f01868162655aec35dbcf"
          ]
        }
      ]
    }
  },
  "nonce" : "0x0",
  "timestamp" : "0x58ee40ba",
  "gasLimit" : "0x1fffffffffffff",
  "difficulty" : "0x1",
  "mixHash" : "0x63746963616c2062797a616e74696e65206661756c7420746f6c6572616e6365",
  "coinbase" : "0x2491522b83c8fFE33bB6156fb9385799FBf39a23",
  "alloc" : {
    "0x2491522b83c8fFE33bB6156fb9385799FBf39a23": {
      "balance" : "90000000000000000000000000000000000000000000000000000000000"
    },
    "7f2843A8F0Cffb51f5dc2A2026Ef5948eE155856" : {
      "balance" : "90000000000000000000000"
    },
    "Cc3196FD83A4ea41E5581AA57a7898e54B95CE44" : {
      "balance" : "90000000000000000000000"
    },
    "fC288399ee6225BfA6fC3252a9f0DBbFD66b6A42" : {
      "balance" : "90000000000000000000000"
    },
    "0x0000000000000000000000000000000000008888": {
      "comment": "Account Ingress smart contract",
      "balance": "0",
      "code": "60806040...",
      "storage": {
        "0x0000000000000000000000000000000000000000000000000000000000000000": "0x72756c6573000000000000000000000000000000000000000000000000000000",
        "0x0000000000000000000000000000000000000000000000000000000000000001": "0x61646d696e697374726174696f6e000000000000000000000000000000000000",
        "0x0000000000000000000000000000000000000000000000000000000000000004": "0x0f4240"
      }
    },
    "0x0000000000000000000000000000000000009999": {
      "comment": "Node Ingress smart contract",
      "balance": "0",
      "code": "6080...",
      "storage": {
        "0x0000000000000000000000000000000000000000000000000000000000000000": "0x72756c6573000000000000000000000000000000000000000000000000000000",
        "0x0000000000000000000000000000000000000000000000000000000000000001": "0x61646d696e697374726174696f6e000000000000000000000000000000000000",
        "0x0000000000000000000000000000000000000000000000000000000000000004": "0x0f4240"
      }
    }
  },
  "extraData" : "0xf87ea00000000000000000000000000000000000000000000000000000000000000000f85494b4ce159828a61066c072f8f4b37285b453cca86a945fcc72d50ca999a5472bb196c22d42cef8b1372594555709de3d8f26f35143ef414c68b9459972575c9465889c20b9451979894f01868162655aec35dbcf808400000000c0"
}

test.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

contract ReceiveEther {

    // Function to receive Ether. msg.data must be empty
    receive() external payable {}

    // Fallback function is called when msg.data is not empty
    fallback() external payable {}

    function getBalance() public view returns (uint) {
        return address(this).balance;
    }
}

contract SendEther {
    function sendViaTransfer(address payable _to) public payable {
        // This function is no longer recommended for sending Ether.
        _to.transfer(msg.value);
    }

    function sendViaSend(address payable _to) public payable {
        // Send returns a boolean value indicating success or failure.
        // This function is not recommended for sending Ether.
        bool sent = _to.send(msg.value);
        require(sent, "Failed to send Ether");
    }

    function sendViaCall(address payable _to) public payable {
        // Call returns a boolean value indicating success or failure.
        // This is the current recommended method to use.
        (bool sent, bytes memory data) = _to.call{value: msg.value}("");
        require(sent, "Failed to send Ether");
    }

        // Function to receive Ether. msg.data must be empty
    receive() external payable {}

    // Fallback function is called when msg.data is not empty
    fallback() external payable {}
}

Logs from node

2023-03-13 12:27:09.536+00:00 | main | INFO  | Besu | Using the Java implementation of alt bn128
2023-03-13 12:27:09.546+00:00 | main | INFO  | Besu | Using the native implementation of the signature algorithm
2023-03-13 12:27:09.552+00:00 | main | INFO  | Besu | Using the native implementation of the blake2bf algorithm
2023-03-13 12:27:09.558+00:00 | main | INFO  | Besu | Starting Besu version: besu/v22.7.6/linux-x86_64/openjdk-java-11
2023-03-13 12:27:10.304+00:00 | main | INFO  | Besu | Static Nodes file = /mnt/okapi/besu/static-nodes.json
2023-03-13 12:27:10.309+00:00 | main | INFO  | StaticNodesParser | StaticNodes file /mnt/okapi/besu/static-nodes.json does not exist, no static connections will be created.
2023-03-13 12:27:10.309+00:00 | main | INFO  | Besu | Connecting to 0 static nodes.
2023-03-13 12:27:10.314+00:00 | main | INFO  | Besu | Security Module: localfile
2023-03-13 12:27:10.344+00:00 | main | INFO  | DatabaseMetadata | Lookup database metadata file in data directory: /mnt/okapi/besu
2023-03-13 12:27:10.451+00:00 | main | INFO  | RocksDBKeyValueStorageFactory | Existing database detected at /mnt/okapi/besu. Version 1
2023-03-13 12:27:15.497+00:00 | main | WARN  | Besu | --tx-pool-future-max-by-account has been deprecated, use --tx-pool-limit-by-account-percentage instead.
2023-03-13 12:27:15.558+00:00 | main | INFO  | KeyPairUtil | Loaded public key 0x2f2b921f4e3b52783ee34fbbbee817110d8b1662e6e1eac15009c77bf6932ec74635523350cdc6a7047d6834e64e841484f949c44aaf1153af674bc49b408aa5 from /mnt/okapi/besu/keys/besu/key
2023-03-13 12:27:17.613+00:00 | main | INFO  | AbstractAltBnPrecompiledContract | Native alt bn128 not available
2023-03-13 12:27:17.616+00:00 | main | INFO  | AbstractAltBnPrecompiledContract | Native alt bn128 not available
2023-03-13 12:27:17.618+00:00 | main | INFO  | AbstractAltBnPrecompiledContract | Native alt bn128 not available
2023-03-13 12:27:17.670+00:00 | main | INFO  | AbstractAltBnPrecompiledContract | Native alt bn128 not available
2023-03-13 12:27:17.671+00:00 | main | INFO  | AbstractAltBnPrecompiledContract | Native alt bn128 not available
2023-03-13 12:27:17.671+00:00 | main | INFO  | AbstractAltBnPrecompiledContract | Native alt bn128 not available
2023-03-13 12:27:17.674+00:00 | main | INFO  | AbstractAltBnPrecompiledContract | Native alt bn128 not available
2023-03-13 12:27:17.675+00:00 | main | INFO  | AbstractAltBnPrecompiledContract | Native alt bn128 not available
2023-03-13 12:27:17.677+00:00 | main | INFO  | AbstractAltBnPrecompiledContract | Native alt bn128 not available
2023-03-13 12:27:17.677+00:00 | main | INFO  | AbstractAltBnPrecompiledContract | Native alt bn128 not available
2023-03-13 12:27:17.677+00:00 | main | INFO  | AbstractAltBnPrecompiledContract | Native alt bn128 not available
2023-03-13 12:27:17.678+00:00 | main | INFO  | AbstractAltBnPrecompiledContract | Native alt bn128 not available
2023-03-13 12:27:17.711+00:00 | main | INFO  | ProtocolScheduleBuilder | Protocol schedule created with milestones: [Petersburg: 0, Petersburg: 8943109, Berlin: 10782300]
2023-03-13 12:27:18.097+00:00 | main | INFO  | TransactionPoolFactory | Enabling transaction pool
2023-03-13 12:27:18.159+00:00 | main | INFO  | BesuControllerBuilder | TTD difficulty is not present, creating initial sync phase for PoW
2023-03-13 12:27:18.721+00:00 | main | INFO  | RunnerBuilder | Detecting NAT service.
2023-03-13 12:27:20.387+00:00 | main | INFO  | Runner | Starting external services ...
2023-03-13 12:27:20.388+00:00 | main | INFO  | MetricsHttpService | Starting metrics http service on 0.0.0.0:9545
2023-03-13 12:27:20.665+00:00 | main | INFO  | JsonRpcHttpService | Starting JSON-RPC service on 0.0.0.0:8545
2023-03-13 12:27:20.664+00:00 | vert.x-eventloop-thread-1 | INFO  | MetricsHttpService | Metrics service started and listening on 0.0.0.0:9545
2023-03-13 12:27:20.844+00:00 | vert.x-eventloop-thread-1 | INFO  | JsonRpcHttpService | JSON-RPC service started and listening on 0.0.0.0:8545
2023-03-13 12:27:20.849+00:00 | main | INFO  | WebSocketService | Starting Websocket service on 0.0.0.0:8546
2023-03-13 12:27:20.855+00:00 | vert.x-eventloop-thread-1 | INFO  | WebSocketService | Websocket service started and listening on 0.0.0.0:8546
2023-03-13 12:27:20.859+00:00 | main | INFO  | AutoTransactionLogBloomCachingService | Starting auto transaction log bloom caching service.
2023-03-13 12:27:20.860+00:00 | main | INFO  | LogBloomCacheMetadata | Lookup cache metadata file in data directory: /mnt/okapi/besu/caches
2023-03-13 12:27:20.883+00:00 | main | INFO  | Runner | Starting Ethereum main loop ...
2023-03-13 12:27:20.883+00:00 | main | INFO  | NatService | No NAT environment detected so no service could be started
2023-03-13 12:27:20.884+00:00 | main | INFO  | NetworkRunner | Starting Network.
2023-03-13 12:27:20.950+00:00 | nioEventLoopGroup-2-1 | INFO  | RlpxAgent | P2P RLPx agent started and listening on /0:0:0:0:0:0:0:0:30303.
2023-03-13 12:27:20.952+00:00 | main | INFO  | PeerDiscoveryAgent | Starting peer discovery agent on host=0.0.0.0, port=30303
2023-03-13 12:27:21.778+00:00 | vert.x-eventloop-thread-1 | INFO  | VertxPeerDiscoveryAgent | Started peer discovery agent successfully, on effective host=0:0:0:0:0:0:0:0%0 and port=30303
2023-03-13 12:27:21.781+00:00 | vert.x-eventloop-thread-1 | INFO  | PeerDiscoveryAgent | P2P peer discovery agent started and listening on /0:0:0:0:0:0:0:0%0:30303
2023-03-13 12:27:23.126+00:00 | vert.x-eventloop-thread-1 | INFO  | PeerDiscoveryAgent | Writing node record to disk. NodeRecord{seq=135, publicKey=0x032f2b921f4e3b52783ee34fbbbee817110d8b1662e6e1eac15009c77bf6932ec7, udpAddress=Optional[/180.49.47.19:30303], tcpAddress=Optional[/180.49.47.19:30303], asBase64=-Ji4QIqnf8Ks0FFR89grumWgoiuFDfRAJsBSv8JY-e-5Qxe_at1Nvfr-heK0nv-Fwi-72VoiASE2akD8S3zlVjDyVnmBh4NldGjHxoRmEhQpgIJpZIJ2NIJpcIS0MS8TiXNlY3AyNTZrMaEDLyuSH047Ung-40-7vugXEQ2LFmLm4erBUAnHe_aTLseDdGNwgnZfg3VkcIJ2Xw, nodeId=0x53bd63b09e3a8ca8e824ec52e8033804e9ab3901cb3ef9c762df8c1b188b8aba, customFields={tcp=30303, udp=30303, ip=0xb4312f13, eth=[[0x66121429, 0x]], id=V4, secp256k1=0x032f2b921f4e3b52783ee34fbbbee817110d8b1662e6e1eac15009c77bf6932ec7}}
2023-03-13 12:27:23.245+00:00 | main | INFO  | DefaultP2PNetwork | Enode URL enode://2f2b921f4e3b52783ee34fbbbee817110d8b1662e6e1eac15009c77bf6932ec74635523350cdc6a7047d6834e64e841484f949c44aaf1153af674bc49b408aa5@180.49.47.19:30303
2023-03-13 12:27:23.246+00:00 | main | INFO  | DefaultP2PNetwork | Node address 0xe8033804e9ab3901cb3ef9c762df8c1b188b8aba
2023-03-13 12:27:23.260+00:00 | main | INFO  | DefaultSynchronizer | Starting synchronizer.
2023-03-13 12:27:23.286+00:00 | main | INFO  | FullSyncDownloader | Starting full sync.
2023-03-13 12:27:23.287+00:00 | main | INFO  | FullSyncTargetManager | No sync target, waiting for peers. Current peers: 0
2023-03-13 12:27:24.224+00:00 | nioEventLoopGroup-3-2 | INFO  | FullSyncTargetManager | No sync target, waiting for peers. Current peers: 4
2023-03-13 12:27:25.009+00:00 | main | INFO  | Runner | Ethereum main loop is up.
2023-03-13 12:27:30.039+00:00 | EthScheduler-Workers-0 | INFO  | PersistBlockTask | Imported #13,718,739 / 0 tx / 0 om / 0 (0.0%) gas / (0x932845caa51b95682c58a225f251398be23bea1452ebbccf6c05c9589e6fca26) in 0.103s. Peers: 4
2023-03-13 12:27:35.268+00:00 | EthScheduler-Workers-1 | INFO  | PersistBlockTask | Imported #13,718,740 / 0 tx / 0 om / 0 (0.0%) gas / (0x2e3c665766699ba5493325ed5c1ecd8d8d99456a7475294415f8647304a34b6d) in 0.049s. Peers: 6
2023-03-13 12:27:39.115+00:00 | EthScheduler-Workers-2 | INFO  | PersistBlockTask | Imported #13,718,741 / 0 tx / 0 om / 0 (0.0%) gas / (0x61d524bfd83963c65a0b20393fefe27bd2ef6a3ad04a55fbf365bd442b5adf56) in 0.053s. Peers: 6
2023-03-13 12:27:44.084+00:00 | EthScheduler-Workers-3 | INFO  | PersistBlockTask | Imported #13,718,742 / 0 tx / 0 om / 0 (0.0%) gas / (0x2d6bf285c49de5a17bc6298dc139e405a39667e7397001ee455545a5466f9ccc) in 0.034s. Peers: 6
2023-03-13 12:27:49.092+00:00 | EthScheduler-Workers-4 | INFO  | PersistBlockTask | Imported #13,718,743 / 0 tx / 0 om / 0 (0.0%) gas / (0x2c2252fe722551212b4ca5d9278aca1ae016dddce4376a0a06c6c19bc16ce361) in 0.042s. Peers: 6
2023-03-13 12:27:17.711+00:00 | main | INFO  | ProtocolScheduleBuilder | Protocol schedule created with milestones: [Petersburg: 0, Petersburg: 8943109, Berlin: 10782300]

This log proves the Berlin milestone passed long ago.

And the moment it was updated to Berlín.

Captura_transicion_berlin

In order to test it in Remix IDE, i compiled the SC for Berlin EVM version and call sendViaTransfer, sendViaCall and sendViaSend functions, and all failed.

TX_failed

And when i compiled the same SC for Constantinople EVM version, and call the same send functions, it worked.

TX_success

Versions

gfukushima commented 1 year ago

Hi @jorgesjv I've set up an environment to replicate your issue and it still happens on the latest version of besu. What I've noticed is that when you enable the trace logs from besu you can see the contract call gets this logs raised in besu: "MessageFrame evaluation halted because of INSUFFICIENT_GAS" So if you're working with a wallet like metamask you can probably edit the the gas limit you're giving to the transaction. It's likely that you've seen issues after going through Berlin because some opcodes had gas prices increased in Berlin. You can find some of the informations of why this transaction might require more gas after berlin here: https://eips.ethereum.org/EIPS/eip-2929 And the difficulty of calculating a estimated gas limit (which is hard to get accurate) here: https://github.com/trufflesuite/ganache-cli-archive/releases/tag/v6.4.2

That said, there might be improvements that we could do on our side to provide a better gas limit estimation but it will require further investigation.

gfukushima commented 1 year ago

Also I just noticed that you've called the Constantinople version of the contract passing 0 as value, if you do the same for Berlin it the call will be successful. The same applies for the contrary if you call the contract compiled to Constantinople passing 5 ethers (5000000000000000000 wei) as value it will fail.

gfukushima commented 1 year ago

Closing due to inactivity. Please feel free to re-open if necessary.

jorgesanjose commented 1 year ago

Hi @gfukushima, sorry for the delay. Thanks for the info, i will check it out and test it again these days.

jorgesanjose commented 1 year ago

Hi @gfukushima, i've already test it, and you were right. It was a gas problem, i`ve increaded the gas limit up to 300000 and both versions were successful. My Metamask, by default, set gas limit under the necesary for the transactions and have to edited for the purpose.

Thanks a lot!

jorgesanjose commented 1 year ago

Hi again @gfukushima. I've check again the results and i've notice something that i don't really understand. Like i've said before, when i send the transaction, i have to increase gas limit in Metamask, if not, the transaction fails; but after increase it and the transaction is succesfull, the transaction cost is below the previously estimated. For example: Estimated Gas by Metamask: image Later i have to increase the gas limit to 320520 (for example,just a zero at the end, just to be sure). And the transaction response: image

The transaction cost is below the estimated, but if i don't increase it, transaction fails.

Any sense of that?

Thanks in advance.

gfukushima commented 1 year ago

The link I shared in one of my comments above explains why that happens. Re-sharing: https://github.com/trufflesuite/ganache-cli-archive/releases/tag/v6.4.2