以太坊交易池(txpool)的管理

txpool对应的启动参数

我们先来了解一下,针对txpool有哪些参数项可以设置,然后着重分析。

 
  1. --txpool.nolocals 为本地提交交易禁用价格豁免
  2. --txpool.journal value 本地交易的磁盘日志:用于节点重启 (默认: "transactions.rlp")
  3. --txpool.rejournal value 重新生成本地交易日志的时间间隔 (默认: 1小时)
  4. --txpool.pricelimit value 加入交易池的最小的gas价格限制(默认: 1)
  5. --txpool.pricebump value 价格波动百分比(相对之前已有交易) (默认: 10)
  6. --txpool.accountslots value 每个帐户保证可执行的最少交易槽数量 (默认: 16)
  7. --txpool.globalslots value 所有帐户可执行的最大交易槽数量 (默认: 4096)
  8. --txpool.accountqueue value 每个帐户允许的最多非可执行交易槽数量 (默认: 64)
  9. --txpool.globalqueue value 所有帐户非可执行交易最大槽数量 (默认: 1024)
  10. --txpool.lifetime value 非可执行交易最大入队时间(默认: 3小时)

txpool内容的查看

 
  1. > txpool.content
  2. {
  3. pending: {},
  4. queued: {}
  5. }

很显然,txpool中由两部分构成pending和queued组成。那么他们两者有什么分别呢。最明显的是一个为待打包状态,一个为队列中。这里我们发起了两笔不同的交易:

 
  1. > eth.sendTransaction({from:"0xdae19174969a7404e222c24b6726e4d089c12768",to:"0x5929a871f57a1C5F7E4eA304CAe92DACD1C1556b",value:web3.toWei(0.01,"ether"),gasPrice:21000000000,nonce:2});
  2. "0x7db7883bb23a31deb9f01b5e6fb28363b1aee1b9b6797ea8b5706be170a1187c"
  3.  
  4. > eth.sendTransaction({from:"0xdae19174969a7404e222c24b6726e4d089c12768",to:"0x5929a871f57a1C5F7E4eA304CAe92DACD1C1556b",value:web3.toWei(0.01,"ether")});
  5. "0x2784a79a8c454c72700e7be3b31c1c98ceaea232ca4992a6830b0fc999ebb653"

很显然,第一笔交易指定了nonce为2,第二笔交易未指定nonce值,因为此地址没有发起过交易那么nonce值默认为0。这时我们再看一下txpool中的内容:

 
  1. > txpool.content
  2. {
  3. pending: {
  4. 0xdAE19174969A7404e222c24B6726E4D089c12768: {
  5. 0: {
  6. blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  7. blockNumber: null,
  8. from: "0xdae19174969a7404e222c24b6726e4d089c12768",
  9. gas: "0x15f90",
  10. gasPrice: "0x1",
  11. hash: "0x2784a79a8c454c72700e7be3b31c1c98ceaea232ca4992a6830b0fc999ebb653",
  12. input: "0x",
  13. nonce: "0x0",
  14. r: "0xdabcd46d8d0b61e468d9f10119d544437f89cd094c35a89e5cbed298faf52c4a",
  15. s: "0x3670f23ecfb0a12e982a60438640fe042eefc50646a077de0244a8d67a84af9e",
  16. to: "0x5929a871f57a1c5f7e4ea304cae92dacd1c1556b",
  17. transactionIndex: "0x0",
  18. v: "0xa95",
  19. value: "0x2386f26fc10000"
  20. }
  21. }
  22. },
  23. queued: {
  24. 0xdAE19174969A7404e222c24B6726E4D089c12768: {
  25. 2: {
  26. blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  27. blockNumber: null,
  28. from: "0xdae19174969a7404e222c24b6726e4d089c12768",
  29. gas: "0x15f90",
  30. gasPrice: "0x4e3b29200",
  31. hash: "0x7db7883bb23a31deb9f01b5e6fb28363b1aee1b9b6797ea8b5706be170a1187c",
  32. input: "0x",
  33. nonce: "0x2",
  34. r: "0xa8953a87c326c02da9d7a712d6c7ac0cd415cbc71ea0c24423f9e01b1fec65bd",
  35. s: "0x3faefc3a0db585a67f02996a7167890e41ff5fd8fd4be6efff3bea7a797fad29",
  36. to: "0x5929a871f57a1c5f7e4ea304cae92dacd1c1556b",
  37. transactionIndex: "0x0",
  38. v: "0xa96",
  39. value: "0x2386f26fc10000"
  40. }
  41. }
  42. }
  43. }

现在txpool中有两笔交易,其中nonce为0的在pending中,nonce为2的在queued中。为什么只有nonce不同的两笔交易,在txpool中的位置却不同呢?

txpool的处理流程

首先,如果不传入nonce值,那么geth节点会默认计算当前地址已经发起了的交易中最大的nonce值为多少,然后将其+1,然后将此交易放置在pending中,等待节点打包。

其次,如果传入的nonce值过大,在进入txpool中检查到它之前的nonce并没有使用过,那么此笔交易不会发送到pending中,而且放置在queued中。只有当前面的nonce补齐之后,才会进入到pending中。那么,我们再发一笔交易把nonce补齐看看:

 
  1. > eth.sendTransaction({from:"0xdae19174969a7404e222c24b6726e4d089c12768",to:"0x5929a871f57a1C5F7E4eA304CAe92DACD1C1556b",value:web3.toWei(0.01,"ether")});
  2. "0x7ee17d38405c01bab4eec4d9dc62a6bba98283e243a2d9132187706485878ef5"
  3.  
  4. > txpool.content
  5. {
  6. pending: {
  7. 0xdAE19174969A7404e222c24B6726E4D089c12768: {
  8. 0: {
  9. blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  10. blockNumber: null,
  11. from: "0xdae19174969a7404e222c24b6726e4d089c12768",
  12. gas: "0x15f90",
  13. gasPrice: "0x1",
  14. hash: "0x2784a79a8c454c72700e7be3b31c1c98ceaea232ca4992a6830b0fc999ebb653",
  15. input: "0x",
  16. nonce: "0x0",
  17. r: "0xdabcd46d8d0b61e468d9f10119d544437f89cd094c35a89e5cbed298faf52c4a",
  18. s: "0x3670f23ecfb0a12e982a60438640fe042eefc50646a077de0244a8d67a84af9e",
  19. to: "0x5929a871f57a1c5f7e4ea304cae92dacd1c1556b",
  20. transactionIndex: "0x0",
  21. v: "0xa95",
  22. value: "0x2386f26fc10000"
  23. },
  24. 1: {
  25. blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  26. blockNumber: null,
  27. from: "0xdae19174969a7404e222c24b6726e4d089c12768",
  28. gas: "0x15f90",
  29. gasPrice: "0x1",
  30. hash: "0x7ee17d38405c01bab4eec4d9dc62a6bba98283e243a2d9132187706485878ef5",
  31. input: "0x",
  32. nonce: "0x1",
  33. r: "0xe03fb4d94b0ff04107c855bfd88a84ecdefb03f4c9b0cea5341591aa69d4751e",
  34. s: "0x4d2f60f4045e5492cd4818145cec73c78b00e0cff57026c4528d91a82dee76e1",
  35. to: "0x5929a871f57a1c5f7e4ea304cae92dacd1c1556b",
  36. transactionIndex: "0x0",
  37. v: "0xa96",
  38. value: "0x2386f26fc10000"
  39. },
  40. 2: {
  41. blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  42. blockNumber: null,
  43. from: "0xdae19174969a7404e222c24b6726e4d089c12768",
  44. gas: "0x15f90",
  45. gasPrice: "0x4e3b29200",
  46. hash: "0x7db7883bb23a31deb9f01b5e6fb28363b1aee1b9b6797ea8b5706be170a1187c",
  47. input: "0x",
  48. nonce: "0x2",
  49. r: "0xa8953a87c326c02da9d7a712d6c7ac0cd415cbc71ea0c24423f9e01b1fec65bd",
  50. s: "0x3faefc3a0db585a67f02996a7167890e41ff5fd8fd4be6efff3bea7a797fad29",
  51. to: "0x5929a871f57a1c5f7e4ea304cae92dacd1c1556b",
  52. transactionIndex: "0x0",
  53. v: "0xa96",
  54. value: "0x2386f26fc10000"
  55. }
  56. }
  57. },
  58. queued: {}
  59. }

很明显,当中间的nonce被补齐之后,原来处于queued当中的交易被放置到了pending中。

经验之谈

前文提到的如何处理过期交易中提到了补齐nonce和设置—txpool.lifetime也是基于今天这批文章讲述的基础逻辑。除此之外,我们还要了解一下—txpool.accountqueue参数,它定义了每个账户在本节点queued中存放的最多的交易个数,默认是64个交易。

另外为了避免手续费过低导致交易一直存在于txpool当中占用内存,可以通过console设置手续费的最低值:

 
  1. >miner.setGasPrice(51000000000)
  2. true

或者在启动参数上添加:

 
  1. --gasprice "51000000000"

你可能感兴趣的:(区块链)