|
| 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 Block, Transaction |
| 20 | +from hathor.types import Address, VertexId |
| 21 | + |
| 22 | + |
| 23 | +def gen_new_tx(manager: HathorManager, address: str, value: int, verify: bool = True) -> Transaction: |
| 24 | + """ |
| 25 | + Generate and return a new transaction. |
| 26 | +
|
| 27 | + Args: |
| 28 | + manager: the HathorManager to generate the transaction for |
| 29 | + address: an address for the transaction's output |
| 30 | + value: a value for the transaction's output |
| 31 | + verify: whether to verify the generated transaction |
| 32 | +
|
| 33 | + Returns: the generated transaction. |
| 34 | + """ |
| 35 | + from hathor.transaction import Transaction |
| 36 | + from hathor.wallet.base_wallet import WalletOutputInfo |
| 37 | + |
| 38 | + outputs = [] |
| 39 | + outputs.append(WalletOutputInfo(address=decode_address(address), value=int(value), timelock=None)) |
| 40 | + |
| 41 | + assert manager.wallet is not None |
| 42 | + tx = manager.wallet.prepare_transaction_compute_inputs(Transaction, outputs, manager.tx_storage) |
| 43 | + tx.storage = manager.tx_storage |
| 44 | + |
| 45 | + max_ts_spent_tx = max(tx.get_spent_tx(txin).timestamp for txin in tx.inputs) |
| 46 | + tx.timestamp = max(max_ts_spent_tx + 1, int(manager.reactor.seconds())) |
| 47 | + |
| 48 | + tx.weight = 1 |
| 49 | + tx.parents = manager.get_new_tx_parents(tx.timestamp) |
| 50 | + manager.cpu_mining_service.resolve(tx) |
| 51 | + if verify: |
| 52 | + manager.verification_service.verify(tx) |
| 53 | + return tx |
| 54 | + |
| 55 | + |
| 56 | +def add_new_blocks( |
| 57 | + manager: HathorManager, |
| 58 | + num_blocks: int, |
| 59 | + advance_clock: Optional[int] = None, |
| 60 | + *, |
| 61 | + parent_block_hash: Optional[VertexId] = None, |
| 62 | + block_data: bytes = b'', |
| 63 | + weight: Optional[float] = None, |
| 64 | + address: Optional[Address] = None, |
| 65 | + signal_bits: int | None = None, |
| 66 | +) -> list[Block]: |
| 67 | + """ Create, resolve and propagate some blocks |
| 68 | +
|
| 69 | + :param manager: Manager object to handle the creation |
| 70 | + :type manager: :py:class:`hathor.manager.HathorManager` |
| 71 | +
|
| 72 | + :param num_blocks: Quantity of blocks to be created |
| 73 | + :type num_blocks: int |
| 74 | +
|
| 75 | + :return: Blocks created |
| 76 | + :rtype: list[Block] |
| 77 | + """ |
| 78 | + blocks = [] |
| 79 | + for _ in range(num_blocks): |
| 80 | + blocks.append( |
| 81 | + add_new_block(manager, advance_clock, parent_block_hash=parent_block_hash, |
| 82 | + data=block_data, weight=weight, address=address, signal_bits=signal_bits) |
| 83 | + ) |
| 84 | + if parent_block_hash: |
| 85 | + parent_block_hash = blocks[-1].hash |
| 86 | + return blocks |
| 87 | + |
| 88 | + |
| 89 | +def add_new_block( |
| 90 | + manager: HathorManager, |
| 91 | + advance_clock: Optional[int] = None, |
| 92 | + *, |
| 93 | + parent_block_hash: Optional[VertexId] = None, |
| 94 | + data: bytes = b'', |
| 95 | + weight: Optional[float] = None, |
| 96 | + address: Optional[Address] = None, |
| 97 | + propagate: bool = True, |
| 98 | + signal_bits: int | None = None, |
| 99 | +) -> Block: |
| 100 | + """ Create, resolve and propagate a new block |
| 101 | +
|
| 102 | + :param manager: Manager object to handle the creation |
| 103 | + :type manager: :py:class:`hathor.manager.HathorManager` |
| 104 | +
|
| 105 | + :return: Block created |
| 106 | + :rtype: :py:class:`hathor.transaction.block.Block` |
| 107 | + """ |
| 108 | + block = manager.generate_mining_block(parent_block_hash=parent_block_hash, data=data, address=address) |
| 109 | + if weight is not None: |
| 110 | + block.weight = weight |
| 111 | + if signal_bits is not None: |
| 112 | + block.signal_bits = signal_bits |
| 113 | + manager.cpu_mining_service.resolve(block) |
| 114 | + manager.verification_service.validate_full(block) |
| 115 | + if propagate: |
| 116 | + manager.propagate_tx(block, fails_silently=False) |
| 117 | + if advance_clock: |
| 118 | + assert hasattr(manager.reactor, 'advance') |
| 119 | + manager.reactor.advance(advance_clock) |
| 120 | + return block |
| 121 | + |
| 122 | + |
| 123 | +class NoCandidatesError(Exception): |
| 124 | + pass |
| 125 | + |
| 126 | + |
| 127 | +def gen_new_double_spending(manager: HathorManager, *, use_same_parents: bool = False, |
| 128 | + tx: Optional[Transaction] = None, weight: float = 1) -> Transaction: |
| 129 | + """ |
| 130 | + Generate and return a double spending transaction. |
| 131 | +
|
| 132 | + Args: |
| 133 | + manager: the HathorManager to generate the transaction for |
| 134 | + use_same_parents: whether to use the same parents as the original transaction |
| 135 | + tx: the original transaction do double spend |
| 136 | + weight: the new transaction's weight |
| 137 | +
|
| 138 | + Returns: the double spending transaction. |
| 139 | + """ |
| 140 | + if tx is None: |
| 141 | + tx_candidates = manager.get_new_tx_parents() |
| 142 | + genesis = manager.tx_storage.get_all_genesis() |
| 143 | + genesis_txs = [tx for tx in genesis if not tx.is_block] |
| 144 | + # XXX: it isn't possible to double-spend a genesis transaction, thus we remove it from tx_candidates |
| 145 | + for genesis_tx in genesis_txs: |
| 146 | + if genesis_tx.hash in tx_candidates: |
| 147 | + tx_candidates.remove(genesis_tx.hash) |
| 148 | + if not tx_candidates: |
| 149 | + raise NoCandidatesError() |
| 150 | + # assert tx_candidates, 'Must not be empty, otherwise test was wrongly set up' |
| 151 | + tx_hash = manager.rng.choice(tx_candidates) |
| 152 | + tx = cast(Transaction, manager.tx_storage.get_transaction(tx_hash)) |
| 153 | + |
| 154 | + txin = manager.rng.choice(tx.inputs) |
| 155 | + |
| 156 | + from hathor.transaction.scripts import P2PKH, parse_address_script |
| 157 | + spent_tx = tx.get_spent_tx(txin) |
| 158 | + spent_txout = spent_tx.outputs[txin.index] |
| 159 | + p2pkh = parse_address_script(spent_txout.script) |
| 160 | + assert isinstance(p2pkh, P2PKH) |
| 161 | + |
| 162 | + from hathor.wallet.base_wallet import WalletInputInfo, WalletOutputInfo |
| 163 | + value = spent_txout.value |
| 164 | + wallet = manager.wallet |
| 165 | + assert wallet is not None |
| 166 | + private_key = wallet.get_private_key(p2pkh.address) |
| 167 | + inputs = [WalletInputInfo(tx_id=txin.tx_id, index=txin.index, private_key=private_key)] |
| 168 | + |
| 169 | + address = wallet.get_unused_address(mark_as_used=True) |
| 170 | + outputs = [WalletOutputInfo(address=decode_address(address), value=int(value), timelock=None)] |
| 171 | + |
| 172 | + tx2 = wallet.prepare_transaction(Transaction, inputs, outputs) |
| 173 | + tx2.storage = manager.tx_storage |
| 174 | + tx2.weight = weight |
| 175 | + tx2.timestamp = max(tx.timestamp + 1, int(manager.reactor.seconds())) |
| 176 | + |
| 177 | + if use_same_parents: |
| 178 | + tx2.parents = list(tx.parents) |
| 179 | + else: |
| 180 | + tx2.parents = manager.get_new_tx_parents(tx2.timestamp) |
| 181 | + |
| 182 | + manager.cpu_mining_service.resolve(tx2) |
| 183 | + return tx2 |
0 commit comments