Go-Ethereum

通過 web3 獲取 txpool 中的待處理交易

  • April 20, 2022

我想監控我的待處理交易的記憶體池。我知道人們可以通過(例如在這裡eth_subscribe回答)來監控待處理的交易。但是我的一些交易被卡在了eth_subscribe 中。txpool, and they don’t seem to show up in the result of`

我對 mempool、txpool 和從eth_subscribe.

  1. 如何檢索已經存在的交易txpool
  2. eth_subscribe只返回 txid。其中一些無法被eth.getTransaction. 這意味著什麼?
  3. 從返回的事務與eth_subscribe中的不同txpool。目前尚不清楚它們是否構成超集txpool

實際上,Web3 確實有一種方法可以返回您要查找的內容,您可以在此處找到詳細資訊:https ://web3py.readthedocs.io/en/fix_docs_build/web3.txpool.html#web3.txpool.TxPool.content

一個簡單的呼叫如下:

w3 = Web3(HTTPProvider("http://provider.com"))
w3.geth.txpool.content()

這可以很好地為您提供 dict 中待處理事務的所有數據,如下所示:

{
'pending': {
   '0x0216d5032f356960cd3749c31ab34eeff21b3395': {
   806: [{
       'blockHash': "0x0000000000000000000000000000000000000000000000000000000000000000",
       'blockNumber': None,
       'from': "0x0216d5032f356960cd3749c31ab34eeff21b3395",
       'gas': "0x5208",
       'gasPrice': "0xba43b7400",
       'hash': "0xaf953a2d01f55cfe080c0c94150a60105e8ac3d51153058a1f03dd239dd08586",
       'input': "0x",
       'nonce': "0x326",
       'to': "0x7f69a91a3cf4be60020fb58b893b7cbb65376db8",
       'transactionIndex': None,
       'value': "0x19a99f0cf456000"
   }]
   },
   '0x24d407e5a0b506e1cb2fae163100b5de01f5193c': {
   34: [{
       'blockHash': "0x0000000000000000000000000000000000000000000000000000000000000000",
       'blockNumber': None,
       'from': "0x24d407e5a0b506e1cb2fae163100b5de01f5193c",
       'gas': "0x44c72",
       'gasPrice': "0x4a817c800",
       'hash': "0xb5b8b853af32226755a65ba0602f7ed0e8be2211516153b75e9ed640a7d359fe",
       'input': "0xb61d27f600000000000000000000000024d407e5a0b506e1cb2fae163100b5de01f5193c00000000000000000000000000000000000000000000000053444835ec580000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
       'nonce': "0x22",
       'to': "0x7320785200f74861b69c49e4ab32399a71b34f1a",
       'transactionIndex': None,
       'value': "0x0"
   }]
   }
},
'queued': {
   '0x976a3fc5d6f7d259ebfb4cc2ae75115475e9867c': {
   3: [{
       'blockHash': "0x0000000000000000000000000000000000000000000000000000000000000000",
       'blockNumber': None,
       'from': "0x976a3fc5d6f7d259ebfb4cc2ae75115475e9867c",
       'gas': "0x15f90",
       'gasPrice': "0x4a817c800",
       'hash': "0x57b30c59fc39a50e1cba90e3099286dfa5aaf60294a629240b5bbec6e2e66576",
       'input': "0x",
       'nonce': "0x3",
       'to': "0x346fb27de7e7370008f5da379f74dd49f5f2f80f",
       'transactionIndex': None,
       'value': "0x1f161421c8e0000"
   }]
   },
   '0x9b11bf0459b0c4b2f87f8cebca4cfc26f294b63a': {
   2: [{
       'blockHash': "0x0000000000000000000000000000000000000000000000000000000000000000",
       'blockNumber': None,
       'from': "0x9b11bf0459b0c4b2f87f8cebca4cfc26f294b63a",
       'gas': "0x15f90",
       'gasPrice': "0xba43b7400",
       'hash': "0x3a3c0698552eec2455ed3190eac3996feccc806970a4a056106deaf6ceb1e5e3",
       'input': "0x",
       'nonce': "0x2",
       'to': "0x24a461f25ee6a318bdef7f33de634a67bb67ac9d",
       'transactionIndex': None,
       'value': "0xebec21ee1da40000"
   }],
   6: [{
       'blockHash': "0x0000000000000000000000000000000000000000000000000000000000000000",
       'blockNumber': None,
       'from': "0x9b11bf0459b0c4b2f87f8cebca4cfc26f294b63a",
       'gas': "0x15f90",
       'gasPrice': "0x4a817c800",
       'hash': "0xbbcd1e45eae3b859203a04be7d6e1d7b03b222ec1d66dfcc8011dd39794b147e",
       'input': "0x",
       'nonce': "0x6",
       'to': "0x6368f3f8c2b42435d6c136757382e4a59436a681",
       'transactionIndex': None,
       'value': "0xf9a951af55470000"
   }, {
       'blockHash': "0x0000000000000000000000000000000000000000000000000000000000000000",
       'blockNumber': None,
       'from': "0x9b11bf0459b0c4b2f87f8cebca4cfc26f294b63a",
       'gas': "0x15f90",
       'gasPrice': "0x4a817c800",
       'hash': "0x60803251d43f072904dc3a2d6a084701cd35b4985790baaf8a8f76696041b272",
       'input': "0x",
       'nonce': "0x6",
       'to': "0x8db7b4e0ecb095fbd01dffa62010801296a9ac78",
       'transactionIndex': None,
       'value': "0xebe866f5f0a06000"
   }],
   }
}
}

我提供了 Python 中的範例,因為您沒有指定您使用的任何語言。

如果你感到困惑,我建議你使用 JSON-RPC

curl -X POST -d '{"jsonrpc":"2.0","method":"txpool_status","params":[],"id":1}' http://ip:port
curl -X POST -d '{"jsonrpc":"2.0","method":"txpool_content","params":[],"id":1}' http://ip:port
curl -X POST -d '{"jsonrpc":"2.0","method":"txpool_inspect","params":[],"id":1}' http://ip:port

如果您熟悉使用您使用的語言創建 POST 請求,請將上述 curl 命令更改為它們並使用。

引用自:https://ethereum.stackexchange.com/questions/61560