[JAVA] Check if Ethereum transactions are not included in the block

Article content

I wrote an article that uses web3j to publish a signed transaction, but that way the transaction wasn't populated into the block.

Send signed transaction using Web3j

I will leave the correction method including the confirmation method.

Check transaction pool

Check "txpool" in geth's JavaScript console.

> txpool
{
  content: {
    pending: {
      0x945Cd603A6754cB13C3D61d8fe240990f86f9f8A: {
        19: {...}
      }
    },
    queued: {
      0x66B4e7bE902300F9a15D900822Bbd8803Be87391: {
        1: {...},
        100: {...},
        5: {...}
      },
      0x945Cd603A6754cB13C3D61d8fe240990f86f9f8A: {
        100: {...}
      }
    }
  },
  inspect: {
    pending: {
      0x945Cd603A6754cB13C3D61d8fe240990f86f9f8A: {
        19: "0xf5ad7542173e8944D1aE16B4394eAA34cfdA4814: 0 wei + 200000 gas × 500 wei"
      }
    },
    queued: {
      0x66B4e7bE902300F9a15D900822Bbd8803Be87391: {
        1: "0x945Cd603A6754cB13C3D61d8fe240990f86f9f8A: 0 wei + 4712388 gas × 100 wei",
        100: "0x945Cd603A6754cB13C3D61d8fe240990f86f9f8A: 0 wei + 4712388 gas × 100 wei",
        5: "0x945Cd603A6754cB13C3D61d8fe240990f86f9f8A: 0 wei + 4712388 gas × 100 wei"
      },
      0x945Cd603A6754cB13C3D61d8fe240990f86f9f8A: {
        100: "0x66B4e7bE902300F9a15D900822Bbd8803Be87391: 1000000000000000 wei + 200000 gas × 100 wei"
      }
    }
  },
  status: {
    pending: 1,
    queued: 4
  },
  getContent: function(callback),
  getInspect: function(callback),
  getStatus: function(callback)
}

The point here is "queued". When a transaction is created, it goes into queued, various checks such as signature check are performed, and if there is no problem with the check, it moves to "pending" and waits for it to be stored in the block.

It was explained in detail on this site. About Ethereum's Pending Transaction and Transaction Pool

How to send a signed transaction using Web3j

Send signed transaction using Web3j I modified the sendSignedTransaction I wrote in this article.

SendTransaction.java


  public TransactionReceipt sendSignedTransaction(Credentials credentials, String password, String toAddress) {

      TransactionReceipt receipt = null;
      try {
        //Get nonce value
        EthGetTransactionCount ethTransactionCount =
            web3j.ethGetTransactionCount(credentials.getAddress(), DefaultBlockParameterName.PENDING).send();
        BigInteger nonce = ethTransactionCount.getTransactionCount();

        // "eth_sendTransaction"Create an object to pass to the argument of
        RawTransaction rawTransaction = RawTransaction.createEtherTransaction(
            nonce,                        // nonce
            BigInteger.valueOf(100),     // gasPrice
            BigInteger.valueOf(4712388), // gasLimit
            toAddress,                    // to
            BigInteger.valueOf(1010000)  // value
            );

        //Sign transaction
        byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
        String hexValue = Numeric.toHexString(signedMessage);

        EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).send();
        String transactionHash = ethSendTransaction.getTransactionHash();

        //Send transaction
        Optional<TransactionReceipt> transactionReceipt = null;
        int retry = 0;

        if(transactionHash != null) {
          do {
            System.out.printf("%3d checking if transaction " + transactionHash + " is mined....\n" ,retry);
            EthGetTransactionReceipt ethGetTransactionReceiptResp = web3j.ethGetTransactionReceipt(transactionHash).send();
            transactionReceipt = ethGetTransactionReceiptResp.getTransactionReceipt();

            Thread.sleep(3000);
            retry++;
          }while(!transactionReceipt.isPresent() && retry < 100);
        } else {
          System.out.println("Transaction Send failed...");
          System.out.println("Message:" + ethSendTransaction.getError().getMessage());
          System.out.println("Data   :" + ethSendTransaction.getError().getData());
        }
      }catch(IOException | InterruptedException ex) {
        ex.printStackTrace();
      }catch(Exception ex) {
        ex.printStackTrace();
      }
    return receipt;
  }

The mistake was that the nonce value set in RawTransaction was a fixed value, so it was caught in the transaction check process and remained in the queued.

I checked the transaction information immediately after sending the Raw Transaction.

INFO [09-17|20:52:21.455] Submitted transaction                    fullhash=0x84b89a096fcb1a5381319cf08348f633ebe2dc2ef93ba48b0c05d5b460110970 recipient=0x66B4e7bE902300F9a15D900822Bbd8803Be87391
> eth.getTransaction("0x84b89a096fcb1a5381319cf08348f633ebe2dc2ef93ba48b0c05d5b460110970")
{
  blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  blockNumber: null,
  from: "0x945cd603a6754cb13c3d61d8fe240990f86f9f8a",
  gas: 4712388,
  gasPrice: 100,
  hash: "0x84b89a096fcb1a5381319cf08348f633ebe2dc2ef93ba48b0c05d5b460110970",
  input: "0x",
  nonce: 24,
  r: "0xd6bb90deea18af081795bc335c3323389c830721d5d7db54f6ef6c19f3425358",
  s: "0x4e99740c21f5ef2f3debafcff0256340dfff07e00a86801eb90d72804439d602",
  to: "0x66b4e7be902300f9a15d900822bbd8803be87391",
  transactionIndex: 0,
  v: "0x1c",
  value: 1010000
}
> txpool.status
{
  pending: 1,
  queued: 0
}

Transactions have accumulated in pending. I checked it even after mining.

> eth.getTransaction("0x84b89a096fcb1a5381319cf08348f633ebe2dc2ef93ba48b0c05d5b460110970")
{
  blockHash: "0x3a150aebfffbe81804f4d5b193e3a22aea79654c3bd5c53d2a3df8d7d8417c34",
  blockNumber: 3066,
  from: "0x945cd603a6754cb13c3d61d8fe240990f86f9f8a",
  gas: 4712388,
  gasPrice: 100,
  hash: "0x84b89a096fcb1a5381319cf08348f633ebe2dc2ef93ba48b0c05d5b460110970",
  input: "0x",
  nonce: 24,
  r: "0xd6bb90deea18af081795bc335c3323389c830721d5d7db54f6ef6c19f3425358",
  s: "0x4e99740c21f5ef2f3debafcff0256340dfff07e00a86801eb90d72804439d602",
  to: "0x66b4e7be902300f9a15d900822bbd8803be87391",
  transactionIndex: 0,
  v: "0x1c",
  value: 1010000
}

> txpool.status
{
  pending: 0,
  queued: 0
}

Stored in a block and pending changed to 0.

Impressions

I realized that it is necessary to accurately understand the contents to be set in the transaction and the flow from the creation of the transaction to the inclusion in the block.

Recommended Posts

Check if Ethereum transactions are not included in the block
If the submodule assets are not found in the Play Framework
What to do if the changes are not reflected in the jar manifest file
How to check if the characters entered in the Swift Text Field are email addresses
Line breaks in the entered text are not displayed in view
What to do when the changes in the Servlet are not reflected
Determine if the strings to be compared are the same in Java
What are the rules in JUnit?
What to do if the prefix c is not bound in JSP
Process to check if "some fields are duplicated" from the Bean List
[HTTP] Status code included in the HTTP response
must not return in the for statement
Add if not in Set, error message if
[Rails] If CSS is not applied in collection_select, check if class can be specified.