def test_fraudulent_tx_source_other_user(self): blockchain = Blockchain() victim = generate_key() miner_key = generate_key() miner = Miner(blockchain) miner.reward_addr = miner_key.publickey() emptyblock = miner._mine() transfer = Wallet(miner_key, blockchain) transfer.send(10, victim.publickey()) self.assertEqual(len(emptyblock.transactions), 1) self._test_fraudulent_tx(victim, blockchain)
def wallet_send(data): l = json.loads(data) key = RSAKey(RSAPublicKey.load(l.get('public_key')), RSAPrivateKey.load(l.get('private_key'))) wallet = Wallet(key, blockchain) try: wallet.send(l.get('amount'), l.get('recipient')) except InsufficientFundsException: return json.dumps({ "successful": False, "message": "Insufficient funds" }) return json.dumps({"successful": True})
def test_chained_tx(self): blockchain = Blockchain() member1 = generate_key() member2 = generate_key() member3 = generate_key() miner = Miner(blockchain) miner.reward_addr = member1.publickey() _ = miner._mine() wallet1 = Wallet(member1, blockchain) wallet1.send(10, member2.publickey()) wallet2 = Wallet(member2, blockchain) wallet2.send(10, member3.publickey()) wallet3 = Wallet(member3, blockchain) self.assertEqual(wallet1.balance(), 0) self.assertEqual(wallet2.balance(), 0) self.assertEqual(wallet3.balance(), 10)
def test_value_transfer(self): """ Test successful transaction """ SENDER_ORIG_VALUE = MINING_REWARD # Mining reward SEND_AMOUNT = 5 blockchain = Blockchain() sender = generate_key() receiver = generate_key() miner_key = generate_key() # This is an empty blockchain so create value out of thin air. miner = Miner(blockchain) miner.reward_addr = sender.publickey() emptyblock = miner._mine() self.assertEqual(len(emptyblock.transactions), 1) # First test with unconfirmed transactions for i in range(1, 3): wallet = Wallet(sender, blockchain) wallet.send(SEND_AMOUNT, receiver.publickey()) # Scan the blockchain for the transaction that just happened. receiver_owns = blockchain.scan_unspent_transactions( receiver.publickey()) value_received = sum( map(lambda x: x.get('value', 0), receiver_owns)) self.assertEqual(value_received, SEND_AMOUNT * i) sender_owns = blockchain.scan_unspent_transactions( sender.publickey()) value_owned_by_sender = sum( map(lambda x: x.get('value', 0), sender_owns)) self.assertEqual(value_owned_by_sender, SENDER_ORIG_VALUE - SEND_AMOUNT * i) # Mine the transactions miner = Miner(blockchain) miner.reward_addr = miner_key.publickey() newb = miner._mine() # Test with confirmed transactions receiver_owns = blockchain.scan_unspent_transactions( receiver.publickey()) value_received = sum(map(lambda x: x.get('value', 0), receiver_owns)) self.assertEqual(value_received, SEND_AMOUNT * 2) sender_owns = blockchain.scan_unspent_transactions(sender.publickey()) value_owned_by_sender = sum( map(lambda x: x.get('value', 0), sender_owns)) self.assertEqual(value_owned_by_sender, SENDER_ORIG_VALUE - SEND_AMOUNT * 2) # Check whether miner received the award miner_owns = blockchain.scan_unspent_transactions( miner_key.publickey()) value_owned_by_miner = sum(map(lambda x: x.get('value', 0), miner_owns)) self.assertEqual(value_owned_by_miner, MINING_REWARD) self.assertEqual(len(newb.transactions), 3)