|
| 1 | +# Copyright 2023 Hathor Labs |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from typing import Optional, cast |
| 16 | + |
| 17 | +from hathor.crypto.util import decode_address |
| 18 | +from hathor.manager import HathorManager |
| 19 | +from hathor.transaction import Transaction |
| 20 | + |
| 21 | + |
| 22 | +def gen_new_tx(manager, address, value, verify=True): |
| 23 | + from hathor.transaction import Transaction |
| 24 | + from hathor.wallet.base_wallet import WalletOutputInfo |
| 25 | + |
| 26 | + outputs = [] |
| 27 | + outputs.append(WalletOutputInfo(address=decode_address(address), value=int(value), timelock=None)) |
| 28 | + |
| 29 | + tx = manager.wallet.prepare_transaction_compute_inputs(Transaction, outputs, manager.tx_storage) |
| 30 | + tx.storage = manager.tx_storage |
| 31 | + |
| 32 | + max_ts_spent_tx = max(tx.get_spent_tx(txin).timestamp for txin in tx.inputs) |
| 33 | + tx.timestamp = max(max_ts_spent_tx + 1, int(manager.reactor.seconds())) |
| 34 | + |
| 35 | + tx.weight = 1 |
| 36 | + tx.parents = manager.get_new_tx_parents(tx.timestamp) |
| 37 | + tx.resolve() |
| 38 | + if verify: |
| 39 | + tx.verify() |
| 40 | + return tx |
| 41 | + |
| 42 | + |
| 43 | +def add_new_blocks(manager, num_blocks, advance_clock=None, *, parent_block_hash=None, |
| 44 | + block_data=b'', weight=None, address=None): |
| 45 | + """ Create, resolve and propagate some blocks |
| 46 | +
|
| 47 | + :param manager: Manager object to handle the creation |
| 48 | + :type manager: :py:class:`hathor.manager.HathorManager` |
| 49 | +
|
| 50 | + :param num_blocks: Quantity of blocks to be created |
| 51 | + :type num_blocks: int |
| 52 | +
|
| 53 | + :return: Blocks created |
| 54 | + :rtype: list[Block] |
| 55 | + """ |
| 56 | + blocks = [] |
| 57 | + for _ in range(num_blocks): |
| 58 | + blocks.append( |
| 59 | + add_new_block(manager, advance_clock, parent_block_hash=parent_block_hash, |
| 60 | + data=block_data, weight=weight, address=address) |
| 61 | + ) |
| 62 | + if parent_block_hash: |
| 63 | + parent_block_hash = blocks[-1].hash |
| 64 | + return blocks |
| 65 | + |
| 66 | + |
| 67 | +def add_new_block(manager, advance_clock=None, *, parent_block_hash=None, |
| 68 | + data=b'', weight=None, address=None, propagate=True): |
| 69 | + """ Create, resolve and propagate a new block |
| 70 | +
|
| 71 | + :param manager: Manager object to handle the creation |
| 72 | + :type manager: :py:class:`hathor.manager.HathorManager` |
| 73 | +
|
| 74 | + :return: Block created |
| 75 | + :rtype: :py:class:`hathor.transaction.block.Block` |
| 76 | + """ |
| 77 | + block = manager.generate_mining_block(parent_block_hash=parent_block_hash, data=data, address=address) |
| 78 | + if weight is not None: |
| 79 | + block.weight = weight |
| 80 | + block.resolve() |
| 81 | + block.validate_full() |
| 82 | + if propagate: |
| 83 | + manager.propagate_tx(block, fails_silently=False) |
| 84 | + if advance_clock: |
| 85 | + manager.reactor.advance(advance_clock) |
| 86 | + return block |
| 87 | + |
| 88 | + |
| 89 | +class NoCandidatesError(Exception): |
| 90 | + pass |
| 91 | + |
| 92 | + |
| 93 | +def gen_new_double_spending(manager: HathorManager, *, use_same_parents: bool = False, |
| 94 | + tx: Optional[Transaction] = None, weight: float = 1) -> Transaction: |
| 95 | + if tx is None: |
| 96 | + tx_candidates = manager.get_new_tx_parents() |
| 97 | + genesis = manager.tx_storage.get_all_genesis() |
| 98 | + genesis_txs = [tx for tx in genesis if not tx.is_block] |
| 99 | + # XXX: it isn't possible to double-spend a genesis transaction, thus we remove it from tx_candidates |
| 100 | + for genesis_tx in genesis_txs: |
| 101 | + if genesis_tx.hash in tx_candidates: |
| 102 | + tx_candidates.remove(genesis_tx.hash) |
| 103 | + if not tx_candidates: |
| 104 | + raise NoCandidatesError() |
| 105 | + # assert tx_candidates, 'Must not be empty, otherwise test was wrongly set up' |
| 106 | + tx_hash = manager.rng.choice(tx_candidates) |
| 107 | + tx = cast(Transaction, manager.tx_storage.get_transaction(tx_hash)) |
| 108 | + |
| 109 | + txin = manager.rng.choice(tx.inputs) |
| 110 | + |
| 111 | + from hathor.transaction.scripts import P2PKH, parse_address_script |
| 112 | + spent_tx = tx.get_spent_tx(txin) |
| 113 | + spent_txout = spent_tx.outputs[txin.index] |
| 114 | + p2pkh = parse_address_script(spent_txout.script) |
| 115 | + assert isinstance(p2pkh, P2PKH) |
| 116 | + |
| 117 | + from hathor.wallet.base_wallet import WalletInputInfo, WalletOutputInfo |
| 118 | + value = spent_txout.value |
| 119 | + wallet = manager.wallet |
| 120 | + assert wallet is not None |
| 121 | + private_key = wallet.get_private_key(p2pkh.address) |
| 122 | + inputs = [WalletInputInfo(tx_id=txin.tx_id, index=txin.index, private_key=private_key)] |
| 123 | + |
| 124 | + address = wallet.get_unused_address(mark_as_used=True) |
| 125 | + outputs = [WalletOutputInfo(address=decode_address(address), value=int(value), timelock=None)] |
| 126 | + |
| 127 | + tx2 = wallet.prepare_transaction(Transaction, inputs, outputs) |
| 128 | + tx2.storage = manager.tx_storage |
| 129 | + tx2.weight = weight |
| 130 | + tx2.timestamp = max(tx.timestamp + 1, int(manager.reactor.seconds())) |
| 131 | + |
| 132 | + if use_same_parents: |
| 133 | + tx2.parents = list(tx.parents) |
| 134 | + else: |
| 135 | + tx2.parents = manager.get_new_tx_parents(tx2.timestamp) |
| 136 | + |
| 137 | + tx2.resolve() |
| 138 | + return tx2 |
0 commit comments