def random_tx(hash160s, utxos):
    '''Create a random TX paying to some of the hash160s using some of the
    UTXOS.  Return the TX.  UTXOs is updated for the effects of the TX.
    '''
    inputs = []
    n_inputs = min(randrange(1, 4), len(utxos))
    input_value = 0
    # Create inputs spending random UTXOs.  total the inpu
    for n in range(n_inputs):
        prevout = choice(list(utxos))
        hashX, value = utxos.pop(prevout)
        inputs.append(TxInput(prevout[0], prevout[1], b'', 4294967295))
        input_value += value

    # Add a generation/coinbase like input that is present in some coins
    inputs.append(TxInput(bytes(32), 4294967295, b'', 4294967295))

    fee = min(input_value, randrange(500))
    input_value -= fee
    outputs = []
    n_outputs = randrange(1, 4)
    for n in range(n_outputs):
        value = randrange(input_value + 1)
        input_value -= value
        pk_script = coin.hash160_to_P2PKH_script(choice(hash160s))
        outputs.append(TxOutput(value, pk_script))

    tx = Tx(2, inputs, outputs, 0)
    tx_bytes = tx.serialize()
    tx_hash = tx_hash_fn(tx_bytes)
    for n, output in enumerate(tx.outputs):
        utxos[(tx_hash, n)] = (coin.hashX_from_script(output.pk_script),
                               output.value)
    return tx, tx_hash, tx_bytes
Example #2
0
def test_claim_update_validator(block_processor):
    claim_id = claim_id_hash(b'claimtx', 42)
    prev_hash, prev_idx = b'previous_claim_txid', 42
    input = TxInput(prev_hash, prev_idx, b'script', 1)
    claim = ClaimUpdate(b'name', claim_id, b'new value')
    assert not block_processor.get_update_input(claim, [input])

    block_processor.put_claim_info(
        claim_id,
        ClaimInfo(b'name', b'value', prev_hash, prev_idx, 20, b'address', 1,
                  None))

    assert block_processor.get_update_input(claim, [input])