Exemplo n.º 1
0
    def run_test(self):
        node = self.nodes[0]
        node.add_p2p_connection(P2PDataStore())
        p2pGetter = node.add_p2p_connection(P2PBlockGetter())

        self.log.info("Submitting block with invalid auxpow...")
        tip = node.getbestblockhash()
        blk, blkHash = self.createBlock()
        blk = self.addAuxpow(blk, blkHash, False)
        assert_equal(node.submitblock(blk.serialize().hex()), "high-hash")
        assert_equal(node.getbestblockhash(), tip)

        self.log.info("Submitting block with valid auxpow...")
        blk = self.addAuxpow(blk, blkHash, True)
        assert_equal(node.submitblock(blk.serialize().hex()), None)
        assert_equal(node.getbestblockhash(), blkHash)

        self.log.info("Retrieving block through RPC...")
        gotHex = node.getblock(blkHash, 0)
        gotBlk = CBlock()
        gotBlk.deserialize(BytesIO(hex_str_to_bytes(gotHex)))
        gotBlk.rehash()
        assert_equal("%064x" % int(gotBlk.hash, 16), blkHash)

        self.log.info("Retrieving block through P2P...")
        gotBlk = p2pGetter.getBlock(blkHash)
        gotBlk.rehash()
        assert_equal("%064x" % int(gotBlk.hash, 16), blkHash)

        self.log.info("Sending block with invalid auxpow over P2P...")
        tip = node.getbestblockhash()
        blk, blkHash = self.createBlock()
        blk = self.addAuxpow(blk, blkHash, False)
        node.p2p.send_blocks_and_test([blk],
                                      node,
                                      force_send=True,
                                      success=False,
                                      reject_reason="high-hash")
        assert_equal(node.getbestblockhash(), tip)

        self.log.info("Sending the same block with valid auxpow...")
        blk = self.addAuxpow(blk, blkHash, True)
        node.p2p.send_blocks_and_test([blk], node, success=True)
        assert_equal(node.getbestblockhash(), blkHash)

        self.log.info("Retrieving block through RPC...")
        gotHex = node.getblock(blkHash, 0)
        gotBlk = CBlock()
        gotBlk.deserialize(BytesIO(hex_str_to_bytes(gotHex)))
        gotBlk.rehash()
        assert_equal("%064x" % int(gotBlk.hash, 16), blkHash)

        self.log.info("Retrieving block through P2P...")
        gotBlk = p2pGetter.getBlock(blkHash)
        gotBlk.rehash()
        assert_equal("%064x" % int(gotBlk.hash, 16), blkHash)
Exemplo n.º 2
0
    def test_transaction_serialization(self):
        legacy_addr = self.nodes[0].getnewaddress("", "legacy")
        p2sh_addr = self.nodes[0].getnewaddress("", "p2sh-segwit")
        bech32_addr = self.nodes[0].getnewaddress("", "bech32")
        self.unknown_addr = self.nodes[1].getnewaddress()

        # directly seed types of utxos required
        self.nodes[0].generatetoaddress(1, legacy_addr)
        self.nodes[0].generatetoaddress(1, p2sh_addr)
        self.nodes[0].generatetoaddress(1, bech32_addr)
        self.nodes[0].generatetoaddress(101, self.unknown_addr)

        # grab utxos filtering by age
        legacy_utxo = self.nodes[0].listunspent(104, 104)[0]
        p2sh_utxo = self.nodes[0].listunspent(103, 103)[0]
        bech32_utxo = self.nodes[0].listunspent(102, 102)[0]

        submitted_txids = []
        self.log.info("Testing legacy UTXO")
        submitted_txids.append(
            self.assert_tx_format_also_signed(legacy_utxo, segwit=False))
        self.log.info("Testing p2sh UTXO")
        submitted_txids.append(
            self.assert_tx_format_also_signed(p2sh_utxo, segwit=True))
        self.log.info("Testing bech32 UTXO")
        submitted_txids.append(
            self.assert_tx_format_also_signed(bech32_utxo, segwit=True))

        blockhash = self.nodes[0].generate(1)[0]
        hexblock = self.nodes[0].getblock(blockhash, 0)
        block_details = self.nodes[0].getblock(blockhash, 2)
        block = CBlock()
        block.deserialize(BytesIO(hex_str_to_bytes(hexblock)))
        assert (len(block.vtx) == len(submitted_txids) + 1)
        assert_equal(len(block_details["tx"]), len(block.vtx))
        for tx1, tx2 in zip(block.vtx[1:], block_details["tx"][1:]):
            # no tuple wildcard, just re-used tx2 on first one
            assert ((tx1.rehash(), tx2["wtxid"]) in submitted_txids)
            assert ((tx2["txid"], tx2["hash"]) in submitted_txids)
            assert ((tx2["txid"], tx2["wtxid"]) in submitted_txids)
        block.rehash()
        assert_equal(block.hash, self.nodes[0].getbestblockhash())
Exemplo n.º 3
0
    def test_transaction_serialization(self):
        legacy_addr = self.nodes[0].getnewaddress("", "legacy")
        p2sh_addr = self.nodes[0].getnewaddress("", "p2sh-segwit")
        bech32_addr = self.nodes[0].getnewaddress("", "bech32")
        self.unknown_addr = self.nodes[1].getnewaddress()

        # directly seed types of utxos required
        self.nodes[0].generatetoaddress(1, legacy_addr)
        self.nodes[0].generatetoaddress(1, p2sh_addr)
        self.nodes[0].generatetoaddress(1, bech32_addr)
        self.nodes[0].generatetoaddress(101, self.unknown_addr)

        # grab utxos filtering by age
        legacy_utxo = self.nodes[0].listunspent(104, 104)[0]
        p2sh_utxo = self.nodes[0].listunspent(103, 103)[0]
        bech32_utxo = self.nodes[0].listunspent(102, 102)[0]

        submitted_txids = []
        self.log.info("Testing legacy UTXO")
        submitted_txids.append(self.assert_tx_format_also_signed(legacy_utxo, segwit=False))
        self.log.info("Testing p2sh UTXO")
        submitted_txids.append(self.assert_tx_format_also_signed(p2sh_utxo, segwit=True))
        self.log.info("Testing bech32 UTXO")
        submitted_txids.append(self.assert_tx_format_also_signed(bech32_utxo, segwit=True))

        blockhash = self.nodes[0].generate(1)[0]
        hexblock = self.nodes[0].getblock(blockhash, 0)
        block_details = self.nodes[0].getblock(blockhash, 2)
        block = CBlock()
        block.deserialize(BytesIO(hex_str_to_bytes(hexblock)))
        assert(len(block.vtx) == len(submitted_txids) + 1)
        assert_equal(len(block_details["tx"]), len(block.vtx))
        for tx1, tx2 in zip(block.vtx[1:], block_details["tx"][1:]):
            # no tuple wildcard, just re-used tx2 on first one
            assert((tx1.rehash(), tx2["wtxid"]) in submitted_txids)
            assert((tx2["txid"], tx2["hash"]) in submitted_txids)
            assert((tx2["txid"], tx2["wtxid"]) in submitted_txids)
        block.rehash()
        assert_equal(block.hash, self.nodes[0].getbestblockhash())