コード例 #1
0
def test_invalid_rev_block_3():
    utxo_set = UTXOSet()
    rev_block = RevBlock("", [], [])
    rev_block.pow = "00" * 32
    rev_block.removable = [OutPoint("aa" * 32, 0).hex]
    with pytest.raises(Exception):
        utxo_set.reverse_block(rev_block)
コード例 #2
0
def test_invalid_rev_block_2():
    utxo_set = UTXOSet()
    rev_block = RevBlock("", [], [])
    rev_block.pow = "00" * 32
    rev_block.old_txout = [[OutPoint("00" * 32, 0).hex, TxOut()]]
    with pytest.raises(Exception):
        utxo_set.reverse_block(rev_block)
コード例 #3
0
    def add_blocks(self, blocks):

        last_block = self.get_last_blocks()

        for i, block in enumerate(blocks):
            if not self.get_block(block.header.pow):  # first new block
                blocks = blocks[i:]
                break

        try:  # tries to add enough blocks to be in the best chain

            last_valid_block = self.get_block(blocks[0].header.previous_pow)
            if last_valid_block:  # if it is not the first block
                last_valid_index = self.get_block(last_valid_block[0])[2]
                last_index = self.get_last_blocks()[0][2]
                if last_index - last_valid_index > 0:
                    reverse_blocks = self.get_last_blocks(last_index -
                                                          last_valid_index)
                    for rev_block in reverse_blocks:
                        filename = os.path.join(self.base_dir, "rev",
                                                rev_block[0] + ".rev")
                        with open(filename, "rb") as f:
                            rev_block = RevBlock.deserialize(f.read())
                        self._reverse_block(rev_block)

                    previous_work = work_from_chain(
                        [rev[0] for rev in reverse_blocks])
                    current_chain = []
                    blocks = (i for i in blocks)  # change to iterator
                    for block in blocks:
                        self._add_block(block)
                        current_chain.append(block.header.pow)
                        if work_from_chain(current_chain) > previous_work:
                            break  # already in best chain

            self.main_utxo_set.db.commit()
            self.db.commit()
        except:
            self.main_utxo_set.db.rollback()
            self.db.rollback()
            return False

        for block in blocks:
            try:
                self._add_block(block)
                self.main_utxo_set.db.commit()
                self.db.commit()
            except:
                self.main_utxo_set.db.rollback()
                self.db.rollback()

        if last_block == self.get_last_blocks():
            return False
        else:
            return True
コード例 #4
0
 def add_block(self, block):
     rev_block = RevBlock(block.header.pow, [], [])
     if not self.validate_block(block):
         raise Exception
     for i, tx_out in enumerate(block.transactions[0].outputs):
         complete_id = OutPoint(block.transactions[0].txid, i).hex
         self.add_utxo(complete_id, tx_out)
         rev_block.removable.append(complete_id)
     for tx in block.transactions[1:]:
         for i, tx_out in enumerate(tx.outputs):
             complete_id = OutPoint(tx.txid, i).hex
             self.add_utxo(complete_id, tx_out)
             rev_block.removable.append(complete_id)
         for i, tx_in in enumerate(tx.inputs):
             complete_id = tx_in.prevout.hex
             rev_block.old_txout.append(
                 [complete_id, self.get_utxo(complete_id)])
             self.remove_utxo(complete_id)
     return rev_block
コード例 #5
0
ファイル: test_block.py プロジェクト: giacomocaironi/toykoin
def test_reverse_serialization():
    rev_block_bytes = b"U\x91\xfb\x04\xe7t\x1c4\xc5_\xef\xd9\x00\xa6Nc\x9c5[\xd9\xa4\x86:\xeb\xdahH\x8c\xfeY\xb1\x8e\x00\x01\xb8eq\x0e\x05\x8a\xca\x8c\x02\xf2\xae\xfa)\xd1\x0bZP\x94L<9\xbc\x11N1\xb5\xc9CZ\x89\xdb\x1e\x00\x00\x00\n\x00\x00\x00\x02T\x0b\xe4\x00\x00\x00\x00\x02U\xa1\xdf\xbd.g5{(\x18\xf0P\x9f\x9a?\xca/j\xc4\x99\xf1<\xba0\xfd\xb5\x18|\x9c>\x1f\xbc\x00\x00\xc4|v\xb4\x07\x08\x08\x9fQ\xc8?\x9d\xd6\x81b\x16Y)0\x800^\x98\x9d\xfa\xae.4\xfft\x7f\x13\x00\x00"
    assert RevBlock.deserialize(rev_block_bytes).serialize() == rev_block_bytes
コード例 #6
0
ファイル: test_block.py プロジェクト: giacomocaironi/toykoin
def test_rev_block_invalid_3():
    rev_block = RevBlock("", [], [])
    rev_block.pow = "00" * 32
    rev_block.old_txout = [[OutPoint("00" * 32, 0).hex, TxOut()]]
    assert not rev_block.is_valid()
コード例 #7
0
ファイル: test_block.py プロジェクト: giacomocaironi/toykoin
def test_rev_block_invalid_1():
    rev_block = RevBlock("", [], [])
    assert not rev_block.is_valid()