|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2026 RBB S.r.l |
| 3 | +# Copyright (c) 2017-2021 The Bitcoin Core developers |
| 4 | +# opensource@mintlayer.org |
| 5 | +# SPDX-License-Identifier: MIT |
| 6 | +# Licensed under the MIT License; |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# https://github.com/mintlayer/mintlayer-core/blob/master/LICENSE |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +"""Wallet scan mempool on startup test |
| 18 | +
|
| 19 | +Check that: |
| 20 | +* We create 2 wallets with same mnemonic, |
| 21 | +* get an address from the first wallet |
| 22 | +* send coins to the wallet's address |
| 23 | +* sync both wallets with the node |
| 24 | +* check balance in both wallets |
| 25 | +* close the second wallet |
| 26 | +* from the first wallet send coins from Acc 0 to Acc 1 without creating a block |
| 27 | +* reopen the second wallet and create a third wallet with the same mnemonic |
| 28 | +* they both should get the new Tx from the mempool upon creation/opening |
| 29 | +* second wallet can create a new unconfirmed Tx on top of the Tx in mempool |
| 30 | +""" |
| 31 | + |
| 32 | +import asyncio |
| 33 | + |
| 34 | +from test_framework.mintlayer import (ATOMS_PER_COIN, block_input_data_obj, |
| 35 | + make_tx, reward_input) |
| 36 | +from test_framework.test_framework import BitcoinTestFramework |
| 37 | +from test_framework.util import assert_equal, assert_in |
| 38 | +from test_framework.wallet_cli_controller import (DEFAULT_ACCOUNT_INDEX, |
| 39 | + WalletCliController) |
| 40 | + |
| 41 | + |
| 42 | +class WalletMempoolScanning(BitcoinTestFramework): |
| 43 | + |
| 44 | + def set_test_params(self): |
| 45 | + self.setup_clean_chain = True |
| 46 | + self.num_nodes = 1 |
| 47 | + self.extra_args = [ |
| 48 | + [ |
| 49 | + "--blockprod-min-peers-to-produce-blocks=0", |
| 50 | + ] |
| 51 | + ] |
| 52 | + |
| 53 | + def setup_network(self): |
| 54 | + self.setup_nodes() |
| 55 | + self.sync_all(self.nodes[0:1]) |
| 56 | + |
| 57 | + def generate_block(self, transactions=[]): |
| 58 | + node = self.nodes[0] |
| 59 | + |
| 60 | + block_input_data = {"PoW": {"reward_destination": "AnyoneCanSpend"}} |
| 61 | + block_input_data = block_input_data_obj.encode(block_input_data).to_hex()[2:] |
| 62 | + |
| 63 | + # create a new block, taking transactions from mempool |
| 64 | + block = node.blockprod_generate_block( |
| 65 | + block_input_data, transactions, [], "FillSpaceFromMempool" |
| 66 | + ) |
| 67 | + node.chainstate_submit_block(block) |
| 68 | + block_id = node.chainstate_best_block_id() |
| 69 | + |
| 70 | + # Wait for mempool to sync |
| 71 | + self.wait_until( |
| 72 | + lambda: node.mempool_local_best_block_id() == block_id, timeout=5 |
| 73 | + ) |
| 74 | + |
| 75 | + return block_id |
| 76 | + |
| 77 | + def run_test(self): |
| 78 | + asyncio.run(self.async_test()) |
| 79 | + |
| 80 | + async def async_test(self): |
| 81 | + node = self.nodes[0] |
| 82 | + async with WalletCliController( |
| 83 | + node, self.config, self.log |
| 84 | + ) as wallet, WalletCliController(node, self.config, self.log) as wallet2: |
| 85 | + # new wallet |
| 86 | + await wallet.create_wallet() |
| 87 | + # create wallet2 with the same mnemonic |
| 88 | + mnemonic = await wallet.show_seed_phrase() |
| 89 | + assert mnemonic is not None |
| 90 | + wallet2_name = "wallet2" |
| 91 | + assert_in( |
| 92 | + "Wallet recovered successfully", |
| 93 | + await wallet2.recover_wallet(mnemonic, name=wallet2_name), |
| 94 | + ) |
| 95 | + |
| 96 | + # check it is on genesis |
| 97 | + best_block_height = await wallet.get_best_block_height() |
| 98 | + self.log.info(f"best block height = {best_block_height}") |
| 99 | + assert_equal(best_block_height, "0") |
| 100 | + best_block_height = await wallet2.get_best_block_height() |
| 101 | + assert_equal(best_block_height, "0") |
| 102 | + |
| 103 | + # new address |
| 104 | + pub_key_bytes = await wallet.new_public_key() |
| 105 | + assert_equal(len(pub_key_bytes), 33) |
| 106 | + |
| 107 | + # Get chain tip |
| 108 | + tip_id = node.chainstate_best_block_id() |
| 109 | + |
| 110 | + # Submit a valid transaction |
| 111 | + token_fee = 1000 |
| 112 | + coins_to_send = 1 |
| 113 | + token_fee_output = { |
| 114 | + "Transfer": [ |
| 115 | + {"Coin": token_fee * ATOMS_PER_COIN}, |
| 116 | + { |
| 117 | + "PublicKey": { |
| 118 | + "key": {"Secp256k1Schnorr": {"pubkey_data": pub_key_bytes}} |
| 119 | + } |
| 120 | + }, |
| 121 | + ], |
| 122 | + } |
| 123 | + tx_fee_output = { |
| 124 | + "Transfer": [ |
| 125 | + {"Coin": coins_to_send * ATOMS_PER_COIN}, |
| 126 | + { |
| 127 | + "PublicKey": { |
| 128 | + "key": {"Secp256k1Schnorr": {"pubkey_data": pub_key_bytes}} |
| 129 | + } |
| 130 | + }, |
| 131 | + ], |
| 132 | + } |
| 133 | + encoded_tx, tx_id = make_tx( |
| 134 | + [reward_input(tip_id)], [token_fee_output] + [tx_fee_output] * 2, 0 |
| 135 | + ) |
| 136 | + |
| 137 | + self.log.debug(f"Encoded transaction {tx_id}: {encoded_tx}") |
| 138 | + |
| 139 | + assert_in("No transaction found", await wallet.get_transaction(tx_id)) |
| 140 | + |
| 141 | + node.mempool_submit_transaction(encoded_tx, {}) |
| 142 | + assert node.mempool_contains_tx(tx_id) |
| 143 | + |
| 144 | + self.generate_block() |
| 145 | + assert not node.mempool_contains_tx(tx_id) |
| 146 | + |
| 147 | + # sync the wallet |
| 148 | + assert_in("Success", await wallet.sync()) |
| 149 | + assert_in("Success", await wallet2.sync()) |
| 150 | + |
| 151 | + acc0_address = await wallet.new_address() |
| 152 | + |
| 153 | + # both wallets have the same balances after syncing the new block |
| 154 | + assert_in( |
| 155 | + f"Coins amount: {coins_to_send * 2 + token_fee}", |
| 156 | + await wallet.get_balance(), |
| 157 | + ) |
| 158 | + assert_in( |
| 159 | + f"Coins amount: {coins_to_send * 2 + token_fee}", |
| 160 | + await wallet2.get_balance(), |
| 161 | + ) |
| 162 | + |
| 163 | + # create new account and get an address |
| 164 | + assert_in("Success", await wallet.create_new_account()) |
| 165 | + assert_in("Success", await wallet2.create_new_account()) |
| 166 | + assert_in("Success", await wallet.select_account(1)) |
| 167 | + acc1_address = await wallet.new_address() |
| 168 | + |
| 169 | + # close wallet2 |
| 170 | + await wallet2.close_wallet() |
| 171 | + |
| 172 | + # go back to Acc 0 and send 1 coin to Acc 1 |
| 173 | + coins_to_send = 2 |
| 174 | + assert_in("Success", await wallet.select_account(DEFAULT_ACCOUNT_INDEX)) |
| 175 | + assert_in( |
| 176 | + "The transaction was submitted successfully", |
| 177 | + await wallet.send_to_address(acc1_address, coins_to_send), |
| 178 | + ) |
| 179 | + |
| 180 | + # check mempool has 1 transaction now |
| 181 | + transactions = node.mempool_transactions() |
| 182 | + assert_equal(len(transactions), 1) |
| 183 | + |
| 184 | + # check wallet 1 has it as pending |
| 185 | + pending_txs = await wallet.list_pending_transactions() |
| 186 | + assert_equal(1, len(pending_txs)) |
| 187 | + transfer_tx_id = pending_txs[0] |
| 188 | + |
| 189 | + # reopen wallet2 and sync it will scan the mempool on first sync |
| 190 | + await wallet2.open_wallet(wallet2_name) |
| 191 | + assert_in("Success", await wallet2.sync()) |
| 192 | + |
| 193 | + # check wallet 2 has the new tx from scanning the mempool |
| 194 | + pending_txs = await wallet2.list_pending_transactions() |
| 195 | + assert_equal(1, len(pending_txs)) |
| 196 | + assert_equal(transfer_tx_id, pending_txs[0]) |
| 197 | + |
| 198 | + assert_in("Success", await wallet.select_account(1)) |
| 199 | + # wallet 2 should automatically recover Acc 1 |
| 200 | + assert_in("Success", await wallet2.select_account(1)) |
| 201 | + |
| 202 | + # check both balances have `coins_to_send` coins in-mempool state |
| 203 | + assert_in( |
| 204 | + f"Coins amount: {coins_to_send}", |
| 205 | + await wallet.get_balance(utxo_states=["in-mempool"]), |
| 206 | + ) |
| 207 | + assert_in( |
| 208 | + f"Coins amount: {coins_to_send}", |
| 209 | + await wallet2.get_balance(utxo_states=["in-mempool"]), |
| 210 | + ) |
| 211 | + |
| 212 | + # check wallet2 can send 1 coin back to Acc0 from the not yet confirmed tx in mempool |
| 213 | + assert_in( |
| 214 | + "The transaction was submitted successfully", |
| 215 | + await wallet2.send_to_address(acc0_address, 1), |
| 216 | + ) |
| 217 | + |
| 218 | + # close wallet2 and recover a new one with the same mnemonic |
| 219 | + await wallet2.close_wallet() |
| 220 | + assert_in( |
| 221 | + "Wallet recovered successfully", |
| 222 | + await wallet2.recover_wallet(mnemonic), |
| 223 | + ) |
| 224 | + # sync the new wallet2 |
| 225 | + assert_in("Success", await wallet2.sync()) |
| 226 | + # check wallet 2 has the new tx from scanning the mempool |
| 227 | + pending_txs = await wallet2.list_pending_transactions() |
| 228 | + assert_equal(2, len(pending_txs)) |
| 229 | + assert_in(transfer_tx_id, pending_txs) |
| 230 | + |
| 231 | + self.generate_block() |
| 232 | + |
| 233 | + assert_in("Success", await wallet.sync()) |
| 234 | + assert_in("Success", await wallet2.sync()) |
| 235 | + |
| 236 | + |
| 237 | +if __name__ == "__main__": |
| 238 | + WalletMempoolScanning().main() |
0 commit comments