Пример #1
0
def test_serialization():
    from serialization import namedtuple_cls_registry
    assert len(namedtuple_cls_registry) > 0

    json_str = genesis_block.serialize()
    deserialized_block = Block.deserialize(json_str)

    assert hasattr(deserialized_block, 'mine')
    assert deserialized_block.version == 0
Пример #2
0
    def load_blockchain(self):
        from models import CentralBank

        try:
            with open(self.BLOCKCHAIN_PATH) as f:
                blocks_data = json.loads(f.read())

                if not (isinstance(blocks_data, dict) or isinstance(blocks_data, list)):
                    raise Exception("blockchain.json file is not valid")

                if isinstance(blocks_data, dict):
                    blocks_data = [blocks_data]

            self.blockchain = 'pending'
            self.central_bank = CentralBank.get_central_bank()
            if not self.central_bank.has_valid_configuration():
                raise Exception('central bank configuration is not properly set')

            self.blockchain = BlockChain(self.central_bank.difficulty)

            self.all_utxos = {}

            for block_data in blocks_data:
                block = Block.deserialize(block_data)
                self.blockchain.append_block(block, mine_block=False)

                for transaction in block.transactions:

                    for inp in transaction.inputs:
                        del self.all_utxos[inp.transaction_output_id]

                    for output in transaction.outputs:
                        self.all_utxos[output.id] = output

        except Exception as exp:
            print("Error occurred while loading blockchain")
            print(exp)

            self.blockchain = None
            return

        self.blockchain.print()
Пример #3
0
 def test_serialize(self):
     s = json.dumps(self.b1.serialize())
     b = Block.deserialize(s)
     assert (b.equals(self.b1))
Пример #4
0
 def test_get_block(self):
     response = get_json(self.app.get('block/1'))
     block = Block.deserialize(response)
     assert (block.equals(self.chain.chain[1]))