|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2026 RBB S.r.l |
| 3 | +# Copyright (c) 2017-2021 The Bitcoin Core developers |
| 4 | + |
| 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 mempool events test |
| 18 | +
|
| 19 | +Check that: |
| 20 | +* We can create 2 wallets with same mnemonic, |
| 21 | +* get an address |
| 22 | +* send coins to the wallet's address |
| 23 | +* sync the wallet with the node |
| 24 | +* check balance in both wallets |
| 25 | +* send coins from Acc 0 to Acc 1 without creating a block |
| 26 | +* the second wallet should get the new Tx from mempool events |
| 27 | +* second wallet can create a new unconfirmed Tx on top of the on in mempool |
| 28 | +""" |
| 29 | + |
| 30 | +import asyncio |
| 31 | + |
| 32 | +from test_framework.mintlayer import (ATOMS_PER_COIN, block_input_data_obj, |
| 33 | + make_tx, reward_input) |
| 34 | +from test_framework.test_framework import BitcoinTestFramework |
| 35 | +from test_framework.util import assert_equal, assert_in |
| 36 | +from test_framework.wallet_cli_controller import (DEFAULT_ACCOUNT_INDEX, |
| 37 | + WalletCliController) |
| 38 | + |
| 39 | + |
| 40 | +class WalletMempoolEvents(BitcoinTestFramework): |
| 41 | + |
| 42 | + def set_test_params(self): |
| 43 | + self.setup_clean_chain = True |
| 44 | + self.num_nodes = 1 |
| 45 | + self.extra_args = [ |
| 46 | + [ |
| 47 | + "--blockprod-min-peers-to-produce-blocks=0", |
| 48 | + ] |
| 49 | + ] |
| 50 | + |
| 51 | + def setup_network(self): |
| 52 | + self.setup_nodes() |
| 53 | + self.sync_all(self.nodes[0:1]) |
| 54 | + |
| 55 | + def generate_block(self, transactions=[]): |
| 56 | + node = self.nodes[0] |
| 57 | + |
| 58 | + block_input_data = {"PoW": {"reward_destination": "AnyoneCanSpend"}} |
| 59 | + block_input_data = block_input_data_obj.encode(block_input_data).to_hex()[2:] |
| 60 | + |
| 61 | + # create a new block, taking transactions from mempool |
| 62 | + block = node.blockprod_generate_block( |
| 63 | + block_input_data, transactions, [], "FillSpaceFromMempool" |
| 64 | + ) |
| 65 | + node.chainstate_submit_block(block) |
| 66 | + block_id = node.chainstate_best_block_id() |
| 67 | + |
| 68 | + # Wait for mempool to sync |
| 69 | + self.wait_until( |
| 70 | + lambda: node.mempool_local_best_block_id() == block_id, timeout=5 |
| 71 | + ) |
| 72 | + |
| 73 | + return block_id |
| 74 | + |
| 75 | + def run_test(self): |
| 76 | + asyncio.run(self.async_test()) |
| 77 | + |
| 78 | + async def async_test(self): |
| 79 | + node = self.nodes[0] |
| 80 | + async with WalletCliController(node, self.config, self.log) as wallet, \ |
| 81 | + WalletCliController(node, self.config, self.log) as wallet2: |
| 82 | + # new wallet |
| 83 | + await wallet.create_wallet() |
| 84 | + # create wallet2 with the same mnemonic |
| 85 | + mnemonic = await wallet.show_seed_phrase() |
| 86 | + assert mnemonic is not None |
| 87 | + assert_in("Wallet recovered successfully", await wallet2.recover_wallet(mnemonic)) |
| 88 | + |
| 89 | + # check it is on genesis |
| 90 | + best_block_height = await wallet.get_best_block_height() |
| 91 | + self.log.info(f"best block height = {best_block_height}") |
| 92 | + assert_equal(best_block_height, "0") |
| 93 | + best_block_height = await wallet2.get_best_block_height() |
| 94 | + assert_equal(best_block_height, "0") |
| 95 | + |
| 96 | + # new address |
| 97 | + pub_key_bytes = await wallet.new_public_key() |
| 98 | + assert_equal(len(pub_key_bytes), 33) |
| 99 | + |
| 100 | + # Get chain tip |
| 101 | + tip_id = node.chainstate_best_block_id() |
| 102 | + |
| 103 | + # Submit a valid transaction |
| 104 | + token_fee = 1000 |
| 105 | + coins_to_send = 1 |
| 106 | + token_fee_output = { |
| 107 | + "Transfer": [ |
| 108 | + {"Coin": token_fee * ATOMS_PER_COIN}, |
| 109 | + { |
| 110 | + "PublicKey": { |
| 111 | + "key": {"Secp256k1Schnorr": {"pubkey_data": pub_key_bytes}} |
| 112 | + } |
| 113 | + }, |
| 114 | + ], |
| 115 | + } |
| 116 | + tx_fee_output = { |
| 117 | + "Transfer": [ |
| 118 | + {"Coin": coins_to_send * ATOMS_PER_COIN}, |
| 119 | + { |
| 120 | + "PublicKey": { |
| 121 | + "key": {"Secp256k1Schnorr": {"pubkey_data": pub_key_bytes}} |
| 122 | + } |
| 123 | + }, |
| 124 | + ], |
| 125 | + } |
| 126 | + encoded_tx, tx_id = make_tx( |
| 127 | + [reward_input(tip_id)], [token_fee_output] + [tx_fee_output] * 2, 0 |
| 128 | + ) |
| 129 | + |
| 130 | + self.log.debug(f"Encoded transaction {tx_id}: {encoded_tx}") |
| 131 | + |
| 132 | + assert_in("No transaction found", await wallet.get_transaction(tx_id)) |
| 133 | + |
| 134 | + node.mempool_submit_transaction(encoded_tx, {}) |
| 135 | + assert node.mempool_contains_tx(tx_id) |
| 136 | + |
| 137 | + self.generate_block() |
| 138 | + assert not node.mempool_contains_tx(tx_id) |
| 139 | + |
| 140 | + # sync the wallet |
| 141 | + assert_in("Success", await wallet.sync()) |
| 142 | + assert_in("Success", await wallet2.sync()) |
| 143 | + |
| 144 | + acc0_address = await wallet.new_address() |
| 145 | + |
| 146 | + # both wallets have the same balances after syncing the new block |
| 147 | + assert_in( |
| 148 | + f"Coins amount: {coins_to_send * 2 + token_fee}", |
| 149 | + await wallet.get_balance(), |
| 150 | + ) |
| 151 | + assert_in( |
| 152 | + f"Coins amount: {coins_to_send * 2 + token_fee}", |
| 153 | + await wallet2.get_balance(), |
| 154 | + ) |
| 155 | + |
| 156 | + # create new account and get an address |
| 157 | + assert_in("Success", await wallet.create_new_account()) |
| 158 | + assert_in("Success", await wallet2.create_new_account()) |
| 159 | + assert_in("Success", await wallet.select_account(1)) |
| 160 | + acc1_address = await wallet.new_address() |
| 161 | + |
| 162 | + # go back to Acc 0 and send 1 coin to Acc 1 |
| 163 | + coins_to_send = 2 |
| 164 | + assert_in("Success", await wallet.select_account(DEFAULT_ACCOUNT_INDEX)) |
| 165 | + assert_in( |
| 166 | + "The transaction was submitted successfully", |
| 167 | + await wallet.send_to_address(acc1_address, coins_to_send), |
| 168 | + ) |
| 169 | + |
| 170 | + # check mempool has 1 transaction now |
| 171 | + transactions = node.mempool_transactions() |
| 172 | + assert_equal(len(transactions), 1) |
| 173 | + transfer_tx = transactions[0] |
| 174 | + |
| 175 | + # check wallet 1 has it as pending |
| 176 | + pending_txs = await wallet.list_pending_transactions() |
| 177 | + assert_equal(1, len(pending_txs)) |
| 178 | + transfer_tx_id = pending_txs[0] |
| 179 | + |
| 180 | + # check wallet 2 also received it from mempool events |
| 181 | + pending_txs = await wallet2.list_pending_transactions() |
| 182 | + assert_equal(1, len(pending_txs)) |
| 183 | + assert_equal(transfer_tx_id, pending_txs[0]) |
| 184 | + |
| 185 | + assert_in("Success", await wallet.select_account(1)) |
| 186 | + # wallet 2 should automatically recover Acc 1 |
| 187 | + assert_in("Success", await wallet2.select_account(1)) |
| 188 | + |
| 189 | + # check both balances have `coins_to_send` coins in-mempool state |
| 190 | + assert_in( |
| 191 | + f"Coins amount: {coins_to_send}", |
| 192 | + await wallet.get_balance(utxo_states=['in-mempool']), |
| 193 | + ) |
| 194 | + assert_in( |
| 195 | + f"Coins amount: {coins_to_send}", |
| 196 | + await wallet2.get_balance(utxo_states=['in-mempool']), |
| 197 | + ) |
| 198 | + |
| 199 | + # check wallet2 can send 1 coin back to Acc0 from the not yet confirmed tx in mempool |
| 200 | + assert_in( |
| 201 | + "The transaction was submitted successfully", |
| 202 | + await wallet2.send_to_address(acc0_address, 1), |
| 203 | + ) |
| 204 | + |
| 205 | + self.generate_block() |
| 206 | + |
| 207 | + assert_in("Success", await wallet.sync()) |
| 208 | + assert_in("Success", await wallet2.sync()) |
| 209 | + |
| 210 | + |
| 211 | +if __name__ == "__main__": |
| 212 | + WalletMempoolEvents().main() |
0 commit comments