Merge bitcoin/bitcoin#27460: rpc: Add importmempool RPC

fa776e61cd Add importmempool RPC (MarcoFalke)
fa20d734a2 refactor: Add and use kernel::ImportMempoolOptions (MarcoFalke)
fa8866990d doc: Clarify the getmempoolinfo.loaded RPC field documentation (MarcoFalke)
6888886cec Remove Chainstate::LoadMempool (MarcoFalke)

Pull request description:

  Currently it is possible to import a mempool by placing it in the datadir and starting the node. However this has many issues:

  * Users aren't expected to fiddle with the datadir, possibly corrupting it
  * An existing mempool file in the datadir may be overwritten
  * The node needs to be restarted
  * Importing an untrusted file this way is dangerous, because it can corrupt the mempool

  Fix all issues by adding a new RPC.

ACKs for top commit:
  ajtowns:
    utACK fa776e61cd
  achow101:
    ACK fa776e61cd
  glozow:
    reACK fa776e61cd

Tree-SHA512: fcb1a92d6460839283c546c47a2d930c363ac1013c4c50dc5215ddf9fe5e51921d23fe0abfae0a5a7631983cfc7e2fff3788b70f95937d0a989a203be4d67546
This commit is contained in:
Andrew Chow
2023-08-15 10:03:51 -04:00
11 changed files with 160 additions and 32 deletions

View File

@@ -46,7 +46,7 @@ from test_framework.util import (
assert_greater_than_or_equal,
assert_raises_rpc_error,
)
from test_framework.wallet import MiniWallet
from test_framework.wallet import MiniWallet, COIN
class MempoolPersistTest(BitcoinTestFramework):
@@ -159,6 +159,16 @@ class MempoolPersistTest(BitcoinTestFramework):
assert self.nodes[0].getmempoolinfo()["loaded"]
assert_equal(len(self.nodes[0].getrawmempool()), 0)
self.log.debug("Import mempool at runtime to node0.")
assert_equal({}, self.nodes[0].importmempool(mempooldat0))
assert_equal(len(self.nodes[0].getrawmempool()), 7)
fees = self.nodes[0].getmempoolentry(txid=last_txid)["fees"]
assert_equal(fees["base"], fees["modified"])
assert_equal({}, self.nodes[0].importmempool(mempooldat0, {"apply_fee_delta_priority": True, "apply_unbroadcast_set": True}))
assert_equal(2, self.nodes[0].getmempoolinfo()["unbroadcastcount"])
fees = self.nodes[0].getmempoolentry(txid=last_txid)["fees"]
assert_equal(fees["base"] + Decimal("0.00001000"), fees["modified"])
self.log.debug("Stop-start node0. Verify that it has the transactions in its mempool.")
self.stop_nodes()
self.start_node(0)
@@ -186,6 +196,7 @@ class MempoolPersistTest(BitcoinTestFramework):
assert_raises_rpc_error(-1, "Unable to dump mempool to disk", self.nodes[1].savemempool)
os.rmdir(mempooldotnew1)
self.test_importmempool_union()
self.test_persist_unbroadcast()
def test_persist_unbroadcast(self):
@@ -210,6 +221,46 @@ class MempoolPersistTest(BitcoinTestFramework):
node0.mockscheduler(16 * 60) # 15 min + 1 for buffer
self.wait_until(lambda: len(conn.get_invs()) == 1)
def test_importmempool_union(self):
self.log.debug("Submit different transactions to node0 and node1's mempools")
self.start_node(0)
self.start_node(2)
tx_node0 = self.mini_wallet.send_self_transfer(from_node=self.nodes[0])
tx_node1 = self.mini_wallet.send_self_transfer(from_node=self.nodes[1])
tx_node01 = self.mini_wallet.create_self_transfer()
tx_node01_secret = self.mini_wallet.create_self_transfer()
self.nodes[0].prioritisetransaction(tx_node01["txid"], 0, COIN)
self.nodes[0].prioritisetransaction(tx_node01_secret["txid"], 0, 2 * COIN)
self.nodes[1].prioritisetransaction(tx_node01_secret["txid"], 0, 3 * COIN)
self.nodes[0].sendrawtransaction(tx_node01["hex"])
self.nodes[1].sendrawtransaction(tx_node01["hex"])
assert tx_node0["txid"] in self.nodes[0].getrawmempool()
assert not tx_node0["txid"] in self.nodes[1].getrawmempool()
assert not tx_node1["txid"] in self.nodes[0].getrawmempool()
assert tx_node1["txid"] in self.nodes[1].getrawmempool()
assert tx_node01["txid"] in self.nodes[0].getrawmempool()
assert tx_node01["txid"] in self.nodes[1].getrawmempool()
assert not tx_node01_secret["txid"] in self.nodes[0].getrawmempool()
assert not tx_node01_secret["txid"] in self.nodes[1].getrawmempool()
self.log.debug("Check that importmempool can add txns without replacing the entire mempool")
mempooldat0 = str(self.nodes[0].chain_path / "mempool.dat")
result0 = self.nodes[0].savemempool()
assert_equal(mempooldat0, result0["filename"])
assert_equal({}, self.nodes[1].importmempool(mempooldat0, {"apply_fee_delta_priority": True}))
# All transactions should be in node1's mempool now.
assert tx_node0["txid"] in self.nodes[1].getrawmempool()
assert tx_node1["txid"] in self.nodes[1].getrawmempool()
assert not tx_node1["txid"] in self.nodes[0].getrawmempool()
# For transactions that already existed, priority should be changed
entry_node01 = self.nodes[1].getmempoolentry(tx_node01["txid"])
assert_equal(entry_node01["fees"]["base"] + 1, entry_node01["fees"]["modified"])
# Deltas for not-yet-submitted transactions should be applied as well (prioritisation is stackable).
self.nodes[1].sendrawtransaction(tx_node01_secret["hex"])
entry_node01_secret = self.nodes[1].getmempoolentry(tx_node01_secret["txid"])
assert_equal(entry_node01_secret["fees"]["base"] + 5, entry_node01_secret["fees"]["modified"])
self.stop_nodes()
if __name__ == "__main__":
MempoolPersistTest().main()