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.52k stars 847 forks source link

Transaction has been reverted by the EVM (when trying to deploy a smart contract) #6304

Closed jeremyGrelaud closed 10 months ago

jeremyGrelaud commented 11 months ago

Description

Hi, I am trying to deploy a smart contract in my private besu network (IBFT 2.0) but the transaction to do so is being reverted by the EVM. I was succesfully able to deploy the same smart contract with remix and then interract with it as described in the besu documentation with a js script.

Acceptance Criteria

Steps to Reproduce (Bug)

  1. Deploy a Besu network following the documentation
  2. Create a SimpleStorage.sol smart contract using the following
pragma solidity ^0.7.0;

contract SimpleStorage {
  uint public storedData;
  event stored(address _to, uint _amount);

  constructor(uint initVal) public {
    emit stored(msg.sender, initVal);
    storedData = initVal;
  }

  function set(uint x) public {
    emit stored(msg.sender, x);
    storedData = x;
  }

  function get() view public returns (uint retVal) {
    return storedData;
  }
}
  1. Compile the smart contract with a compile.js file just like the documentation and generating the bin and abi with solc

  2. Creating a public_tx.js file

const host = 'http://127.0.0.1:8545'; // URL of the node
const fs = require('fs');
const path = require('path');
const Tx = require('ethereumjs-tx').Transaction;

const { Web3 } = require('web3');

async function deployContract() {
  //instance Web3
  const web3 = new Web3(host);
  const privateKey = "secret...";
  const account = web3.eth.accounts.privateKeyToAccount(privateKey);

  const contractJsonPath = path.resolve(__dirname, "SimpleStorage.json");
  const contractJson = JSON.parse(fs.readFileSync(contractJsonPath));

  const contractAbi = contractJson.abi;
  const contractBinPath = path.resolve(__dirname, "SimpleStorage.bin");
  const contractBin = fs.readFileSync(contractBinPath);

  const contractConstructorInit =
    "000000000000000000000000000000000000000000000000000000000000002F";

  const txnCount = await web3.eth.getTransactionCount(account.address);

  const rawTxOptions = {
    nonce: web3.utils.numberToHex(txnCount),
    from: account.address,
    to: null,
    value: "0x00",
    data: "0x" + contractBin + contractConstructorInit,
    gasPrice: "0x0",
    gasLimit: "0x24A22",
  };

  console.log("Creating transaction...");
  const tx = new Tx(rawTxOptions);
  console.log("Signing transaction...");
  tx.sign(Buffer.from(privateKey.slice(2), 'hex'));
  console.log("Sending transaction...");
  const serializedTx = tx.serialize();

  const pTx = await web3.eth.sendSignedTransaction(
    "0x" + serializedTx.toString("hex").toString("hex")
  );

  console.log("tx transactionHash: " + pTx.transactionHash);
  console.log("tx contractAddress: " + pTx.contractAddress);
}

//calling main
deployContract()
  .then(() => {
    console.log("Contrat sucesfully deployed");
  })
  .catch((error) => {
    console.error("Error during contract's deployment :", error);
  });

Expected behavior: The smart contract to be deployed successfully when running the public_tx.js file

Actual behavior:

Creating transaction...
Signing transaction...
Sending transaction...
Erreur lors du déploiement du contrat : TransactionRevertInstructionError: Transaction has been reverted by the EVM
    at /home/.../smartcontracts/node_modules/web3-eth/lib/commonjs/utils/get_transaction_error.js:48:21
    at Generator.next (<anonymous>)
    at /home/.../smartcontracts/node_modules/web3-eth/lib/commonjs/utils/get_transaction_error.js:24:71
    at new Promise (<anonymous>)
    at __awaiter (/home/.../smartcontracts/node_modules/web3-eth/lib/commonjs/utils/get_transaction_error.js:20:12)
    at getTransactionError (/home/.../smartcontracts/node_modules/web3-eth/lib/commonjs/utils/get_transaction_error.js:33:12)
    at SendTxHelper.<anonymous> (/home/.../smartcontracts/node_modules/web3-eth/lib/commonjs/utils/send_tx_helper.js:74:84)
    at Generator.next (<anonymous>)
    at fulfilled (/home/.../smartcontracts/node_modules/web3-eth/lib/commonjs/utils/send_tx_helper.js:5:58)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  innerError: undefined,
  reason: 'Internal error',
  signature: undefined,
  receipt: undefined,
  data: undefined,
  code: 402
}

II tried to change the gas and log every data in the script and it seems correctly initialized. So I don't know if it's an error of logic in the public_tx.js or a problem of configuration of my besu network. (or something else)

Frequency: Always

Versions

Smart contract information

non-fungible-nelson commented 11 months ago

Hi there - in your custom genesis, have you specified milestone blocks that are up to date for the op-codes you're using in the smart contract?

There is some detail here: https://besu.hyperledger.org/public-networks/reference/genesis-items#milestone-blocks

If you are attempting to use an older milestone (i.e. before a certain fork), the EVM will revert the tx if it encounters a newer piece of functionality in the smart contract.

jeremyGrelaud commented 11 months ago

Indeed I used "BerlinBlock" and I see that the latest EVM version seems to be Shangai. Is it possible to directly change this configuration in the genesis file or do we need to make some kind of transition ?

I'll try tomorrow to see if the problem was linked to an incompatible EVM version.

Edit : I tried specifying the right compiler version in my smart contract and using the evm-version option of the solc command to generate the abi, bin and json files but I end up getting the same error (my transaction reverted by the EVM). image

By Following this tutorial (https://github.com/lacchain/besu-pro-testnet/blob/master/DEPLOY_APPLICATIONS.md#web3) I was able to deploy a contract on my private blockchain just like I did with remix and metamask. I just add to adjust the amount of gas for my transaction. I wonder If the problem in the case of the issue is due to gas or compiler version ...

tasty007 commented 5 months ago

Indeed I used "BerlinBlock" and I see that the latest EVM version seems to be Shangai. Is it possible to directly change this configuration in the genesis file or do we need to make some kind of transition ?

I'll try tomorrow to see if the problem was linked to an incompatible EVM version.

Edit : I tried specifying the right compiler version in my smart contract and using the evm-version option of the solc command to generate the abi, bin and json files but I end up getting the same error (my transaction reverted by the EVM). image

By Following this tutorial (https://github.com/lacchain/besu-pro-testnet/blob/master/DEPLOY_APPLICATIONS.md#web3) I was able to deploy a contract on my private blockchain just like I did with remix and metamask. I just add to adjust the amount of gas for my transaction. I wonder If the problem in the case of the issue is due to gas or compiler version ...

Hello, I also encountered the same problem, looking at the EVM source code, found that the virtual machine has executed the operation code REVERT and returned... Now I don't know how to deal with it, can you help me?