trufflesuite / ganache

:warning: The Truffle Suite is being sunset. For information on ongoing support, migration options and FAQs, visit the Consensys blog. Thank you for all the support over the years.
https://consensys.io/blog/consensys-announces-the-sunset-of-truffle-and-ganache-and-new-hardhat?utm_source=github&utm_medium=referral&utm_campaign=2023_Sep_truffle-sunset-2023_announcement_
MIT License
2.62k stars 681 forks source link

Fork sendTransactions - Returned error: header not found #1671

Open saadbhd opened 2 years ago

saadbhd commented 2 years ago

I'm trying to replay and to test sendTransactions of some pending transactions.

I fork on the last block with unlocking the adresses needed

Capture d’écran, le 2021-11-26 à 00 38 14

but Im always getting one of this transactions :

davidmurdoch commented 2 years ago

Can you try with ganache@7.0.0-beta.1?

npm uninstall ganache-cli -g
npm install ganache@beta -g
saadbhd commented 2 years ago

Its not solving the problem

davidmurdoch commented 2 years ago

Can you paste the complete ganache output here?

saadbhd commented 2 years ago
Capture d’écran, le 2021-11-26 à 01 35 20
davidmurdoch commented 2 years ago

Your are still using ganache-cli. Try with the ganache beta.

saadbhd commented 2 years ago

When i run it from the terminal , i got this

Capture d’écran, le 2021-11-26 à 01 51 49

but from the js file I use var ganacheCLI = require( 'ganache-cli'); , and then forking using ganacheCLI

davidmurdoch commented 2 years ago

From JavaScript install ganache@beta in the project'npm_modules:

npm install ganache@beta

Then import it with:

var ganacheCLI = require( 'ganache'); 
saadbhd commented 2 years ago
Capture d’écran, le 2021-11-26 à 02 20 35

Still Having the same issue !!

davidmurdoch commented 2 years ago

What block number are you forking from?

saadbhd commented 2 years ago

Im not specifying the block number in order to get the last one , i need to test the tx on last possible block

davidmurdoch commented 2 years ago

What is blockNb.toString() in your example?

saadbhd commented 2 years ago

ah Sorry ! its web3.eth.getBlockNumber(), but now Im using the fork without specifying the block i remove it from the request

davidmurdoch commented 2 years ago

We don't use the absolute latest block number by default because nodes will report that they know the latest block number, but the block hasn't actually been fully synced yet.

The header not found error can occur when a node hasn't synced a block.

The other time this can happen is when your aren't using an "archive node" and the transaction needs to interact with data from older blocks. Is your moralis node an archive node?

saadbhd commented 2 years ago

no its node an archive node because the archive node in moralis is not up to date.

saadbhd commented 2 years ago

its wotking now sometimes ... but I got a new error

Ganache eth_getTransactionReceipt notice: the transaction with hash

0xf3f06b4f73d61f91840cb0fec968e9b27afd943841779c650ac519782786fd47 has not yet been mined

I tried to do before calling the 'eth_getTransactionReceipt' :

Capture d’écran, le 2021-11-26 à 03 07 06

but nothing !!

davidmurdoch commented 2 years ago

That's not an error, but a notice that you've asked for the receipt before it was available.

You may want to enable ganache v7's legacyInstamine mode, which will ensure the transaction is included in a block before the hash is returned:

Ganache.provider({ miner:{ legacyInstamine: true }})

Details on this breaking change: https://github.com/trufflesuite/ganache/releases/tag/ganache%407.0.0-alpha.0#user-content-v7.0.0-alpha.0-transaction-hashes-are-now-returned-before-the-transaction-receipt-is-available

davidmurdoch commented 2 years ago

Also, generally, forking requires an archive node, as ganache may need to fetch state from old blocks in order to run new transactions.

It could be an interesting feature to allow for multiple forking urls, where if one fails we can retry with another.

saadbhd commented 2 years ago

AHHH okey !! thank you so much for your help and advice !!

now I can get the transaction receipt but all my transaction have status: "0x0" which mean fail and that's weird because they are successful on bscscan

davidmurdoch commented 2 years ago

Can you provide a full reproduction script so we track down what ganache is doing wrong?

saadbhd commented 2 years ago

Yes sure : some Im forking one lastblock-1 I build my approveTX : from _transaction : which is a pendingtransaction in mainnet After Im trying to see if it will pass or not in the localweb3 (fork).

  var localWeb3 = await  new Web3(ganacheBETA.provider({fork: "https://speedy-nodes-nyc.moralis.io/af7342cac22d80aa835f8f48/bsc/mainnet/",  _chainId: 1,  _chainIdRpc: 1 , miner: { legacyInstamine: true },fork_block_number : blockNb-1 ,unlocked_accounts: [_transaction.from]}), null, {transactionConfirmationBlocks: 1})

  approveTX = {
    data: _transaction.input,
    from: _transaction.from,
    gasPrice: web3_ws.utils.toHex(_transaction.gasPrice)  ,
    nonce: _transaction.nonce,
    to: _transaction.to,
    value: web3_ws.utils.toHex(_transaction.value),  
  }

 localWeb3.currentProvider.send({
      method: "eth_sendTransaction",
      params: [approveTX],
      jsonrpc: "2.0",
      id: 1, } , (err,res)=>{
        console.log("eth_sendTransaction",approveTX,_transaction.hash,err,res,res["result"])

          localWeb3.currentProvider.send({
            method: "eth_getTransactionByHash",
            params: [res["result"]],
            jsonrpc: "2.0",
            id: 3, } , (err1,res1)=>{
              console.log("eth_getTransactionByHash",approveTX,_transaction.hash,err1,res1,res)
            })  

        localWeb3.currentProvider.send({
          method: "eth_getTransactionReceipt",
          params: [res["result"]],
          jsonrpc: "2.0",
          id: 4, } , (err2,res2)=>{
            console.log("eth_getTransactionReceipt",approveTX,_transaction.hash,err2,res2,res)
          })    

      } )

I noticed that the from address and the value change, that is to say they are not the same in the initial transaction (mainnet) and the one sent after the fork

davidmurdoch commented 2 years ago

Thanks for the additional information. Hm, the from address and value changing sounds like a bug in ganache. When I get back into the office next week I'll look into this (I'm actually on Holiday right now).

saadbhd commented 2 years ago

all right and even the gasPrice is changing !

okey Im waiting for your help thank you !!

davidmurdoch commented 2 years ago

Oh, I was just thinking... I know nothing about BSC, but if it doesn't follow Ethereum's hardforks then things might not behave as expected. Do you know if BSC implemented EIP-1559 (part of Ethereum's London hardfork)?

saadbhd commented 2 years ago

I really don't Know.

I was trying to see from where it can be the problem and now I'm having issue with : "sender account not recognized" even if I'm unlocking the adress of the _transaction.from

davidmurdoch commented 2 years ago

Another tip then I'm actually taking some time off! If you send the signed transaction (via eth_sendRawTransaction) you won't need to unlock the account and can bypass that whole code path.

saadbhd commented 2 years ago

I tried to send the signed transaction I cannot sign it me because I don't have the private key of the from adress)

but I get this :

UnhandledPromiseRejectionWarning: Error: Incompatible EIP155-based V 148 and chain id 1. See the second parameter of the Transaction constructor to set the chain id.

davidmurdoch commented 2 years ago

You can use the v, r, and s params of the transaction to create the raw transaction.

Start the chain with chain id of 148 so it will pass EIP-155 validation. You only need to set chainId, with ganache 7, not _chainId or _chainIdRpc.

saadbhd commented 2 years ago

I still Have the problem, even with changing the chainId to 148

And when I try to change it here :

Capture d’écran, le 2021-11-26 à 15 50 20

I got chain ID 148 not supported .

saadbhd commented 2 years ago

I Tried this to have the raw :

Capture d’écran, le 2021-11-26 à 16 11 36

but when sending eth_sendRawTransaction , I got this error now 😱:

Error: Invalid signature v value

davidmurdoch commented 2 years ago

There is a typo in your code. You use approveTX.approveTX.

saadbhd commented 2 years ago

Yes it was a mistake that a fixed after sending the commment,

To, be clear this is my all code :

Capture d’écran, le 2021-11-26 à 16 25 56

and I'm still having the

Error: Invalid signature v value\n

saadbhd commented 2 years ago

another thing is that when a compare the signed transaction with first one using this site : https://app.mycrypto.com/broadcast-transaction

I dont get the same txHash and same from adress. (same r,v,s and data ... )

davidmurdoch commented 2 years ago

If you have the raw transaction's hex you can just send that to ganache.

saadbhd commented 2 years ago

No I dans have it

davidmurdoch commented 2 years ago

Just FYI: it makes it very difficult to try reproduce your issue because you are pasting screenshots instead of code.

saadbhd commented 2 years ago

I have the JSON var TRANSACTION : with some infos : from,to hash .....

And I tried to build the raw from that. but when i send it to ganache i have the signature v value error

davidmurdoch commented 2 years ago

Can you paste the complete JSON here?

saadbhd commented 2 years ago

this is my function : _transaction a get it from the pendingTransaction subscribe

function   simulateParentTX (_transaction){
    try{
    return new  Promise (async function  (resolve, reject){  
      blockNb = await web3_ws.eth.getBlockNumber();
      console.log("before fork ",blockNb)

      var localWeb3 = await  new Web3(ganacheBETA.provider({fork: "https://speedy-nodes-nyc.moralis.io/af7342cac22d80aa835f8f48/bsc/mainnet/",  chainId:1337, miner: { legacyInstamine: true },fork_block_number : blockNb ,unlocked_accounts: [_transaction.from]}), null, {transactionConfirmationBlocks: 1})

      localWeb3.eth.defaultAccount = _transaction.from
      console.log("sendrr",localWeb3.eth.defaultAccount)

      const params = [_transaction.nonce, _transaction.gasPrice, _transaction.gas, _transaction.to, _transaction.value, _transaction.input];
      const signedTransaction = rlp.encode([...params, _transaction.v, _transaction.r, _transaction.s]);
      var raw = "0x"+signedTransaction.toString('hex')
      console.log("signed:",raw,_transaction);

     localWeb3.currentProvider.send({
          method: "eth_sendRawTransaction",
          params: [raw],
          jsonrpc: "2.0",
          id: 1, } , (err,res)=>{
            console.log("eth_sendRawTransaction",_transaction.hash,err,res,res["result"])

              localWeb3.currentProvider.send({
                method: "eth_getTransactionByHash",
                params: [res["result"]],
                jsonrpc: "2.0",
                id: 3, } , (err1,res1)=>{
                  console.log("eth_getTransactionByHash",_transaction.hash,err1,res1,res)
                })  

            localWeb3.currentProvider.send({
              method: "eth_getTransactionReceipt",
              params: [res["result"]],
              jsonrpc: "2.0",
              id: 4, } , (err2,res2)=>{
                console.log("eth_getTransactionReceipt",_transaction.hash,err2,res2,res)
              })    

          } )

      })
  }catch(error){console.log("catch tx..:",error)}
}
davidmurdoch commented 2 years ago

Can you just send me the transaction? console.log(JSON.stringify(_transaction))

saadbhd commented 2 years ago

an example of the transaction 👍

{"blockHash":null,"blockNumber":null,"from":"0x0253d7D17305a551a0788A45A4B99BED4fC56c64","gas":182410,"gasPrice":"5000000000","hash":"0xac5c6d55e90e62bc14965f9bad96a297e8587d2b7dc647c571b7ac0859b44d0f","input":"0x18cbafe50000000000000000000000000000000000000000000000000edd3f2aecab76f9000000000000000000000000000000000000000000000000003045097adb20c200000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000253d7d17305a551a0788a45a4b99bed4fc56c640000000000000000000000000000000000000000000000000000000061a109ac000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000e1656e45f18ec6747f5a8496fd39b50b38396d000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c","nonce":100,"to":"0x10ED43C718714eb63d5aA57B78B54704E256024E","transactionIndex":null,"value":"0","type":0,"v":"0x93","r":"0xa75d2e46fba1eaf7a758d8678173141a95fc0e5dac6c2325ab83cbcd357ad780","s":"0x237efc27822c9ddb2f71e386333e19869ceadfbb746d2066240ccfa1a2ebcbdf"}

davidmurdoch commented 2 years ago

I can't get that transaction to hash correctly either; the signature doesn't seem to match the given from address. The only thing I can think of is that Binance must be using a different algorithms.

Oh, I made a mistake about the chainId you should be using earlier, Binance's mainnet chainId is 56.

saadbhd commented 2 years ago

okey , me to I can't get to hash correctly and its blocking me 😭😱

saadbhd commented 2 years ago

hey @davidmurdoch please any updates about the problem

fainashalts commented 2 years ago

Hi @saadbhd it looks like BSC may not compliant with EVM standards on hashing transactions. Unfortunately I'm not sure there's much we can do to resolve this on our end.

We'd love to support BSC if possible, and will put this in our backlog to investigate. Apologies that it can't be a high priority right now. If you figure this out on your end, we'd love to hear about what you learn!