天天看點

合約錯誤:org.web3j.protocol.exceptions.TransactionException:Error processing request:unknown transaction

在執行一個 ERC20 智能合約的transfer調用時發生的org.web3j的一個交易錯誤:

org.web3j.protocol.exceptions.TransactionException: Error processing request: unknown transaction

Credentials credentials = null;
         try {
             this.initWeb3Client();
             credentials = WalletUtils.loadCredentials(pwd, path);
             MyToken mt =  MyToken.load(ADDRESS,web3j,
                     credentials, BigInteger.valueOf(20_000_000_000L),BigInteger.valueOf(4_300_000L));
             TransactionReceipt tr = oso.transfer(address,value).send();
             System.out.println("hash:"+tr.getTransactionHash());
         } catch (IOException e) {
             e.printStackTrace();
         } catch (CipherException e) {
             e.printStackTrace();
         } catch (Exception e) {
             e.printStackTrace();
         }           

交易被成功的送出到了Rinkeby測試鍊上,但是抛出了這個錯誤資訊:

org.web3j.protocol.exceptions.TransactionException: Error processing request: unknown transaction
    at org.web3j.tx.response.TransactionReceiptProcessor.sendTransactionReceiptRequest(TransactionReceiptProcessor.java:32)
    at org.web3j.tx.response.PollingTransactionReceiptProcessor.getTransactionReceipt(PollingTransactionReceiptProcessor.java:37)
    at org.web3j.tx.response.PollingTransactionReceiptProcessor.waitForTransactionReceipt(PollingTransactionReceiptProcessor.java:29)
    at org.web3j.tx.TransactionManager.processResponse(TransactionManager.java:72)
    at org.web3j.tx.TransactionManager.executeTransaction(TransactionManager.java:51)
    at org.web3j.tx.ManagedTransaction.send(ManagedTransaction.java:70)
    at org.web3j.tx.Contract.executeTransaction(Contract.java:223)
    at org.web3j.tx.Contract.executeTransaction(Contract.java:207)
    at org.web3j.tx.Contract.executeTransaction(Contract.java:201)
    at org.web3j.tx.Contract.lambda$executeRemoteCallTransaction$3(Contract.java:240)
    at org.web3j.protocol.core.RemoteCall.send(RemoteCall.java:30)
    at com.novel.balbit.ports.contract.MyTokenTest.transfer(MyTokenTest.java:358)
    at com.novel.balbit.ports.contract.MyTokenTest.main(MyTokenTest.java:396)           

問題可能解決方法

這個問題應該發生在

TransactionReceiptProcessor

内部。

當調用

web3j.ethGetTransactionReceipt(transactionHash).send()

時内部會執行

waitForTransactionReceipt

你的節點可能還沒有徹底準備好,無法給你提供一個有效的

TransactionReceipt

你可以通過手動建構交易來解決這個問題:

Web3j web3 = Web3j.build(new HttpService());  // defaults to http://localhost:8545/
Credentials credentials = WalletUtils.loadCredentials("password", "/path/to/walletfile");

// get the next available nonce
EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
             address, DefaultBlockParameterName.LATEST).sendAsync().get();
BigInteger nonce = ethGetTransactionCount.getTransactionCount();

// create our transaction
RawTransaction rawTransaction  = RawTransaction.createEtherTransaction(
             nonce, <gas price>, <gas limit>, <toAddress>, <value>);

// sign & send our transaction
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
String hexValue = Hex.toHexString(signedMessage);
// FROM here you can get the tx hash.
EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).send();           

雖然可以使用自定義的交易管理器,但盡量嘗試更改輪詢的次數來增加獲得

txhash

的機率。

public static final int DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH = 40;
   public static final long DEFAULT_POLLING_FREQUENCY = 1000 * 15;           

例如:

new Transfer(client, new ClientTransactionManager(client ,fromAddress, 100))
           

另外一個明确的方法:

val txManager  = new RawTransactionManager(client,credentials,100,1000 * 15)
     val transfer   = new Transfer(client,txManager)

     transfer.sendFunds(
         walletAddress,
         amount,
         currency.convert)           

原文《以太坊常見問題和錯誤》中的:

http://cw.hubwiz.com/card/c/ethereum-FAQ/1/1/5/

另外推薦一些之前的教程:

  • python以太坊 ,主要是針對python圍繞web3.py進行區塊鍊以太坊應用開發的講解。
  • web3j ,主要是針對java和android程式員圍繞web3j庫進行區塊鍊以太坊開發的講解。
  • php以太坊 ,主要是介紹使用php進行智能合約開發互動,進行賬号建立、交易、轉賬、代币開發以及過濾器和事件等内容。
  • 以太坊開發 ,主要是介紹使用node.js、mongodb、區塊鍊、ipfs實作去中心化電商DApp實戰,适合進階。
  • 以太坊教程 ,主要介紹智能合約與dapp應用開發,适合入門。