為什麼交易傳送失敗了

Conflux中文社群發表於2021-12-27

在 Conflux 網路,通過 cfx_sendRawTransaction 方法傳送交易時,如果交易構造不對,傳送將會失敗。其中一些錯誤比較常見比如:

  • 使用了已被執行過的 nonce
  • 使用了已經被髮送到交易池中的 nonce

另外還有幾種傳送失敗的情況:

  • chainId 使用不匹配
  • epochHeight 太大
  • gas 超過 1500w (half of block gas limit)
  • gas 小余 21000
  • data 過大 (超過 200K)
  • gasPrice 設定為 0
  • 簽名錯誤
  • 交易池滿

如下是交易傳送失敗時 cfx_sendRawTransaction 方法返回的 RPC 錯誤

nonce 使用錯誤

使用了已經被執行的 nonce

{
    "jsonrpc": "2.0",
    "id": "15922956697249514502",
    "error": {
        "code": -32602,
        "message": "Invalid parameters: tx",
        "data": "\"Transaction 0x4a2cfa73267139d965ab86d41f2af16db09e62ff92a5abffd7f8e743f36f327c is discarded due to a too stale nonce\""
    }
}

此種情況需改為當前可以用的(未用的) nonce

使用了已經被髮送到交易池中的 nonce

{
    "jsonrpc": "2.0",
    "id": "15922956697249514502",
    "error": {
        "code": -32602,
        "message": "Invalid parameters: tx",
        "data": "\"tx already exist\""
    }
  }

{
    "jsonrpc": "2.0",
    "id": "15922956697249514502",
    "error": {
        "code": -32602,
        "message": "Invalid parameters: tx",
        "data": "Tx with same nonce already inserted. To replace it, you need to specify a gas price > {}"
    }
}

對於這兩種情況代表交易已經被髮到交易池中了,如果想更新或替換交易的話,可以使用同樣的 nonce, 修改對應的欄位,並提高 gasPrice 重新傳送

使用了過大的 nonce

傳送交易的 nonce 不能幣使用者當前 nonce 過大,如果超過 2000 將遇到如下錯誤:

{
    "jsonrpc": "2.0",
    "id": "15922956697249514502",
    "error": {
        "code": -32602,
        "message": "Invalid parameters: tx",
        "data": "\"Transaction 0xc875a03e1ce01268931a1a428d8f8313714ab5eb9c2b626bd327af7e5c3e8c03 is discarded due to in too distant future\""
    }
  }

gas

如果交易的 gas 太小(<21000)或太大(>1500w)會返回如下錯誤:

{
    "jsonrpc": "2.0",
    "id": "15922956697249514502",
    "error": {
        "code": -32602,
        "message": "Invalid parameters: tx",
        "data": "\"NotEnoughBaseGas { required: 21000, got: 2000 }\""
    }
}
{
    "jsonrpc": "2.0",
    "id": "15922956697249514502",
    "error": {
        "code": -32602,
        "message": "Invalid parameters: tx",
        "data": "\"transaction gas 20000000 exceeds the maximum value 15000000, the half of pivot block gas limit\""
    }
}

gasPrice

交易的 gasPrice 不能設定為 0:

{
    "jsonrpc": "2.0",
    "id": "15922956697249514502",
    "error": {
        "code": -32602,
        "message": "Invalid parameters: tx",
        "data": "\"ZeroGasPrice\""
    }
}

data

交易有大小限制,最大不能超過 200k

epochHeight

如果交易的 epochHeight 跟當前網路的 epochNumber 相比小余超過 10w 會遇到如下錯誤:

{
    "jsonrpc": "2.0",
    "id": "15922956697249514502",
    "error": {
        "code": -32602,
        "message": "Invalid parameters: tx",
        "data": "\"EpochHeightOutOfBound { block_height: 53800739, set: 0, transaction_epoch_bound: 100000 }\""
    }
}

chainId 使用錯誤

{
    "jsonrpc": "2.0",
    "id": "15922956697249514502",
    "error": {
        "code": -32602,
        "message": "Invalid parameters: tx",
        "data": "\"ChainIdMismatch { expected: 1, got: 2 }\""
    }
}

編碼或簽名錯誤

{
    "jsonrpc": "2.0",
    "id": "15922956697249514502",
    "error": {
        "code": -32602,
        "message": "Invalid parameters: raw",
        "data": "\"RlpIncorrectListLen\""
    }
}
{
    "jsonrpc": "2.0",
    "id": "15922956697249514502",
    "error": {
        "code": -32602,
        "message": "Invalid parameters: tx",
        "data": "Can not recover pubkey for Ethereum like tx"
    }
}

交易池滿

{
    "jsonrpc": "2.0",
    "id": "15922956697249514502",
    "error": {
        "code": -32602,
        "message": "Invalid parameters: tx",
        "data": "txpool is full"
    }
}

{
    "jsonrpc": "2.0",
    "id": "15922956697249514502",
    "error": {
        "code": -32602,
        "message": "Invalid parameters: tx",
        "data": "Failed imported to deferred pool: Transaction Pool is full"
    }
}

對於此種情況,可等待一會重新傳送交易,提高交易的 gasPrice 有助於提高傳送的機率

其他

節點處於 catch-up mode

{
    "jsonrpc": "2.0",
    "id": "15922956697249514502",
    "error": {
        "code": -32077,
        "message": "Request rejected due to still in the catch up mode.",
        "data": null
    }
}

等節點資料同步到最新之後再傳送

內部錯誤

{
    "jsonrpc": "2.0",
    "id": "15922956697249514502",
    "error": {
        "code": -32602,
        "message": "Invalid parameters: tx",
        "data": "Failed to read account_cache from storage: {}"
    }
}

相關文章