예제 #1
0
    def test_block_to_dict(self):
        wallet = Wallet()
        transaction = Transaction(wallet.pubkey, "CityU", 1)
        signature = wallet.sign_transaction(transaction)
        transaction.add_signature(signature)
        block = Block(0, [transaction.to_json()], datetime.datetime.now().strftime("%m/%d/%Y, %H:%M:%S"), "0")
        content = block.to_dict()

        self.assertIsInstance(content, dict)
        self.assertEqual(list(content.keys()), ["index", "timestamp", "previous_hash", "merkle_root", "nonce", "difficulty"])
예제 #2
0
def lightweight():
    fullchain = [json.loads(block) for block in blockchain.chain]
    lightweight = []
    for block in fullchain:
        block_object = Block(block['index'], block['transaction'], block['timestamp'], block['previous_hash'])
        block_object.merkle_root = block['merkle_root']
        block_object.nonce = block['nonce']
        block_object.difficulty = block['difficulty']
        lightweight.append(block_object.to_dict())
    response = {
        'chain': json.dumps(lightweight),
        'length': len(lightweight)
    }
    return jsonify(response), 200