Exemplo n.º 1
0
def test_block():
    transactions = []
    for x in range(10):
        tx_in = TxIn(
            prevout=OutPoint(),
            scriptSig=script.serialize([f"{x}{x}"]),
            sequence=0xFFFFFFFF,
            txinwitness=[],
        )
        tx_out = TxOut(
            value=50 * 10**8,
            scriptPubKey=script.serialize([f"{x}{x}"]),
        )
        tx = TxData(
            version=1,
            locktime=0,
            vin=[tx_in],
            vout=[tx_out],
        )
        transactions.append(tx)
    header = BlockHeader(
        version=1,
        previousblockhash="00" * 32,
        merkleroot="00" * 32,
        time=1,
        bits=b"\x23\x00\x00\x01",
        nonce=1,
    )
    header.merkleroot = _generate_merkle_root(transactions)
    msg = Block(BlockData(header, transactions))
    msg_bytes = bytes.fromhex("00" * 4) + msg.serialize()
    assert msg == Block.deserialize(get_payload(msg_bytes)[1])
Exemplo n.º 2
0
def create_genesis(time, nonce, difficulty, version, reward):
    script_sig = script.serialize(
        [
            "FFFF001D",
            b"\x04",
            "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks".encode(),
        ]
    )
    script_pub_key = script.serialize(
        [
            "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f",
            "OP_CHECKSIG",
        ]
    )
    tx_in = TxIn(
        prevout=OutPoint(),
        scriptSig=script_sig,
        sequence=0xFFFFFFFF,
        txinwitness=[],
    )
    tx_out = TxOut(
        value=reward,
        scriptPubKey=script_pub_key,
    )
    tx = Tx(
        version=1,
        locktime=0,
        vin=[tx_in],
        vout=[tx_out],
    )
    header = BlockHeader(
        version=version,
        previousblockhash="00" * 32,
        merkleroot="00" * 32,
        time=time,
        bits=difficulty.to_bytes(4, "big"),
        nonce=nonce,
    )
    header.merkleroot = _generate_merkle_root([tx])
    return header