def test_block_fromjson(): sender, recipient = Key(), Key() trx = Transaction(sender.address, sender.public_key, recipient.address, 'Hello world') trx.sign(sender.private_key) block = Node.mine_block(0, 0, [trx]) block2 = Block.from_json(block.json()) assert block.index == block2.index
def test_transaction_fromjson(): sender, recipient = Key(), Key() trx = Transaction(sender.address, sender.public_key, recipient.address, 'Hello world') trx.sign(sender.private_key) doc = trx.json(True) trx2 = Transaction.from_json(doc) assert trx.signature == trx2.signature
def post(self): doc = json.loads(self.request.body) trx = Transaction(doc['sender_addr'], doc['sender_public'], doc['recipient_addr'], doc['payload']) trx.sign(doc['sender_private']) #: broadcast the new transaction broadcast_trx(doc) new_block = node.add_transaction(trx) #: broadcast new blocks self.finish(ok()) if new_block: logging.info("mined new block: %s", new_block.index) broadcast_block(new_block.json())
def make_chain(): chain = Chain() sender, recipient = Key(), Key() trx = Transaction(sender.address, sender.public_key, recipient.address, "Hi") trx.sign(sender.private_key) genesis = Node.mine_block(0, 0, [trx]) chain.add_block(genesis) for _ in range(1, 2): trx = Transaction(sender.address, sender.public_key, recipient.address, "Hi") trx.sign(sender.private_key) block = Node.mine_block(chain.height, chain.last_block.hash, [trx]) chain.add_block(block) return chain
def test_block_validation(): sender, recipient = Key(), Key() trx = Transaction(sender.address, sender.public_key, recipient.address, 'Hello world') trx.sign(sender.private_key) assert Node.mine_block(0, 0, [trx]).is_valid()
def test_transaction_validation(): sender, recipient = Key(), Key() trx = Transaction(sender.address, sender.public_key, recipient.address, 'Hello world') trx.sign(sender.private_key) assert trx.is_valid()