以太坊交易池(txpool)的管理
txpool對應的啟動引數
我們先來了解一下,針對txpool有哪些引數項可以設定,然後著重分析。
--txpool.nolocals 為本地提交交易禁用價格豁免
--txpool.journal value 本地交易的磁碟日誌:用於節點重啟 (預設: "transactions.rlp")
--txpool.rejournal value 重新生成本地交易日誌的時間間隔 (預設: 1小時)
--txpool.pricelimit value 加入交易池的最小的gas價格限制(預設: 1)
--txpool.pricebump value 價格波動百分比(相對之前已有交易) (預設: 10)
--txpool.accountslots value 每個帳戶保證可執行的最少交易槽數量 (預設: 16)
--txpool.globalslots value 所有帳戶可執行的最大交易槽數量 (預設: 4096)
--txpool.accountqueue value 每個帳戶允許的最多非可執行交易槽數量 (預設: 64)
--txpool.globalqueue value 所有帳戶非可執行交易最大槽數量 (預設: 1024)
--txpool.lifetime value 非可執行交易最大入隊時間(預設: 3小時)
txpool內容的檢視
> txpool.content
{
pending: {},
queued: {}
}
很顯然,txpool中由兩部分構成pending和queued組成。那麼他們兩者有什麼分別呢。最明顯的是一個為待打包狀態,一個為佇列中。這裡我們發起了兩筆不同的交易:
> eth.sendTransaction({from:"0xdae19174969a7404e222c24b6726e4d089c12768",to:"0x5929a871f57a1C5F7E4eA304CAe92DACD1C1556b",value:web3.toWei(0.01,"ether"),gasPrice:21000000000,nonce:2});
"0x7db7883bb23a31deb9f01b5e6fb28363b1aee1b9b6797ea8b5706be170a1187c"
> eth.sendTransaction({from:"0xdae19174969a7404e222c24b6726e4d089c12768",to:"0x5929a871f57a1C5F7E4eA304CAe92DACD1C1556b",value:web3.toWei(0.01,"ether")});
"0x2784a79a8c454c72700e7be3b31c1c98ceaea232ca4992a6830b0fc999ebb653"
很顯然,第一筆交易指定了nonce為2,第二筆交易未指定nonce值,因為此地址沒有發起過交易那麼nonce值預設為0。這時我們再看一下txpool中的內容:
> txpool.content
{
pending: {
0xdAE19174969A7404e222c24B6726E4D089c12768: {
0: {
blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
blockNumber: null,
from: "0xdae19174969a7404e222c24b6726e4d089c12768",
gas: "0x15f90",
gasPrice: "0x1",
hash: "0x2784a79a8c454c72700e7be3b31c1c98ceaea232ca4992a6830b0fc999ebb653",
input: "0x",
nonce: "0x0",
r: "0xdabcd46d8d0b61e468d9f10119d544437f89cd094c35a89e5cbed298faf52c4a",
s: "0x3670f23ecfb0a12e982a60438640fe042eefc50646a077de0244a8d67a84af9e",
to: "0x5929a871f57a1c5f7e4ea304cae92dacd1c1556b",
transactionIndex: "0x0",
v: "0xa95",
value: "0x2386f26fc10000"
}
}
},
queued: {
0xdAE19174969A7404e222c24B6726E4D089c12768: {
2: {
blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
blockNumber: null,
from: "0xdae19174969a7404e222c24b6726e4d089c12768",
gas: "0x15f90",
gasPrice: "0x4e3b29200",
hash: "0x7db7883bb23a31deb9f01b5e6fb28363b1aee1b9b6797ea8b5706be170a1187c",
input: "0x",
nonce: "0x2",
r: "0xa8953a87c326c02da9d7a712d6c7ac0cd415cbc71ea0c24423f9e01b1fec65bd",
s: "0x3faefc3a0db585a67f02996a7167890e41ff5fd8fd4be6efff3bea7a797fad29",
to: "0x5929a871f57a1c5f7e4ea304cae92dacd1c1556b",
transactionIndex: "0x0",
v: "0xa96",
value: "0x2386f26fc10000"
}
}
}
}
現在txpool中有兩筆交易,其中nonce為0的在pending中,nonce為2的在queued中。為什麼只有nonce不同的兩筆交易,在txpool中的位置卻不同呢?
txpool的處理流程
首先,如果不傳入nonce值,那麼geth節點會預設計算當前地址已經發起了的交易中最大的nonce值為多少,然後將其+1,然後將此交易放置在pending中,等待節點打包。
其次,如果傳入的nonce值過大,在進入txpool中檢查到它之前的nonce並沒有使用過,那麼此筆交易不會發送到pending中,而且放置在queued中。只有當前面的nonce補齊之後,才會進入到pending中。那麼,我們再發一筆交易把nonce補齊看看:
> eth.sendTransaction({from:"0xdae19174969a7404e222c24b6726e4d089c12768",to:"0x5929a871f57a1C5F7E4eA304CAe92DACD1C1556b",value:web3.toWei(0.01,"ether")});
"0x7ee17d38405c01bab4eec4d9dc62a6bba98283e243a2d9132187706485878ef5"
> txpool.content
{
pending: {
0xdAE19174969A7404e222c24B6726E4D089c12768: {
0: {
blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
blockNumber: null,
from: "0xdae19174969a7404e222c24b6726e4d089c12768",
gas: "0x15f90",
gasPrice: "0x1",
hash: "0x2784a79a8c454c72700e7be3b31c1c98ceaea232ca4992a6830b0fc999ebb653",
input: "0x",
nonce: "0x0",
r: "0xdabcd46d8d0b61e468d9f10119d544437f89cd094c35a89e5cbed298faf52c4a",
s: "0x3670f23ecfb0a12e982a60438640fe042eefc50646a077de0244a8d67a84af9e",
to: "0x5929a871f57a1c5f7e4ea304cae92dacd1c1556b",
transactionIndex: "0x0",
v: "0xa95",
value: "0x2386f26fc10000"
},
1: {
blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
blockNumber: null,
from: "0xdae19174969a7404e222c24b6726e4d089c12768",
gas: "0x15f90",
gasPrice: "0x1",
hash: "0x7ee17d38405c01bab4eec4d9dc62a6bba98283e243a2d9132187706485878ef5",
input: "0x",
nonce: "0x1",
r: "0xe03fb4d94b0ff04107c855bfd88a84ecdefb03f4c9b0cea5341591aa69d4751e",
s: "0x4d2f60f4045e5492cd4818145cec73c78b00e0cff57026c4528d91a82dee76e1",
to: "0x5929a871f57a1c5f7e4ea304cae92dacd1c1556b",
transactionIndex: "0x0",
v: "0xa96",
value: "0x2386f26fc10000"
},
2: {
blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
blockNumber: null,
from: "0xdae19174969a7404e222c24b6726e4d089c12768",
gas: "0x15f90",
gasPrice: "0x4e3b29200",
hash: "0x7db7883bb23a31deb9f01b5e6fb28363b1aee1b9b6797ea8b5706be170a1187c",
input: "0x",
nonce: "0x2",
r: "0xa8953a87c326c02da9d7a712d6c7ac0cd415cbc71ea0c24423f9e01b1fec65bd",
s: "0x3faefc3a0db585a67f02996a7167890e41ff5fd8fd4be6efff3bea7a797fad29",
to: "0x5929a871f57a1c5f7e4ea304cae92dacd1c1556b",
transactionIndex: "0x0",
v: "0xa96",
value: "0x2386f26fc10000"
}
}
},
queued: {}
}
很明顯,當中間的nonce被補齊之後,原來處於queued當中的交易被放置到了pending中。
經驗之談
前文提到的如何處理過期交易中提到了補齊nonce和設定—txpool.lifetime也是基於今天這批文章講述的基礎邏輯。除此之外,我們還要了解一下—txpool.accountqueue引數,它定義了每個賬戶在本節點queued中存放的最多的交易個數,預設是64個交易。
另外為了避免手續費過低導致交易一直存在於txpool當中佔用記憶體,可以通過console設定手續費的最低值:
>miner.setGasPrice(51000000000)
true
或者在啟動引數上新增:
--gasprice "51000000000"