def test_coin_creation(self): scrooge = Scrooge() coins = [ Scroogecoin(value=2, wallet_id=scrooge.wallet.id), Scroogecoin(value=5, wallet_id=scrooge.wallet.id) ] scrooge.create_coins(coins) self.assertTrue( isinstance(scrooge.blockchain.blocks[1].transaction, CoinCreation))
def test_process_payment_without_signature(self): """ Put coins in Scrooge's wallet, and transfer them to the same wallet without signing """ scrooge = Scrooge() coin = Scroogecoin(value=2, wallet_id=scrooge.wallet.id) created_coins = scrooge.create_coins([coin]).transaction.created_coins payment = Payment(created_coins=[coin], consumed_coins=created_coins) payment_result = scrooge.process_payment(payment, []) self.assertEqual(payment_result, None)
def test_process_payment_with_signature(self): """ Put coins in Scrooge's wallet, and transfer them to the same wallet """ scrooge = Scrooge() coin = Scroogecoin(value=2, wallet_id=scrooge.wallet.id) created_coins = scrooge.create_coins([coin]).transaction.created_coins payment = Payment(created_coins=[coin], consumed_coins=created_coins) signature = scrooge.wallet.sign(encoded_hash_object(payment)) payment_result = scrooge.process_payment( payment, [(scrooge.wallet.verifying_key, signature)]) self.assertFalse(payment_result == None)
def test_get_coins(self): scrooge = Scrooge() wallet1 = Wallet() wallet2 = Wallet() coins = [ Scroogecoin(value=20, wallet_id=wallet1.id), Scroogecoin(value=100, wallet_id=wallet2.id), Scroogecoin(value=30, wallet_id=wallet1.id), ] scrooge.create_coins(coins) wallet_coins = wallet1.get_coins(scrooge.blockchain) self.assertEqual(len(wallet_coins), 2) self.assertEqual(wallet_coins[0].value + wallet_coins[1].value, 50)
def test_devide_coin_in_two_coins(self): """ Check that the coin division is done correctly """ scrooge = Scrooge() wallet = Wallet() coin = Scroogecoin(value=20, wallet_id=wallet.id) created_block = scrooge.create_coins([coin]) created_coins = created_block.transaction.created_coins new_coins = wallet.devide_coin(coin=created_coins[0], value=15, scrooge=scrooge) self.assertTrue(len(new_coins) == 2) self.assertTrue(new_coins[0].wallet_id == wallet.id and new_coins[1].wallet_id == wallet.id) self.assertEqual(new_coins[0].value + new_coins[1].value, 20)
def test_genesis_is_included(self): scrooge = Scrooge() self.assertEqual(len(scrooge.blockchain.blocks), 1)
from wallet import Wallet import keyboard import random import time from os import _exit NUMBER_OF_WALLETS = 100 MIN_TRANSFET_AMOUNT = 1 MAX_TRANSFER_AMOUNT = 10 if __name__ == '__main__': open(OUTPUT_PATH, 'w').close() wallets = [Wallet() for _ in range(NUMBER_OF_WALLETS)] scrooge = Scrooge(wallets) def on_space_press(_): print(bcolors.OKGREEN, bcolors.BOLD, "Blockchain log saved to:", OUTPUT_PATH, bcolors.ENDC, bcolors.ENDC, sep="") print(bcolors.WARNING, "Terminating simulation...", bcolors.ENDC, sep="") _exit(0)
from scrooge import Scrooge from user import User from tools import OUTPUT_PATH, logger import keyboard import random import time from os import _exit NUMBER_OF_USERS = 10 if __name__ == '__main__': open(OUTPUT_PATH, 'w').close() users = [User() for _ in range(NUMBER_OF_USERS)] scrooge = Scrooge(users) def on_space_press(_): logger("Wrap up! Check your `./output/log.txt` file for logs.") scrooge.sign_last_block() # logger('\nLast block successfully signed:\t'+scrooge.current_building_block.signature) _exit(0) keyboard.on_press_key("space", on_space_press) while (True): sender_index = random.randint(0, NUMBER_OF_USERS - 1) receiver_index = random.randint(0, NUMBER_OF_USERS - 1) users[sender_index].create_transaction(users[receiver_index], scrooge) time.sleep(0.2)
from scrooge_coin import Scroogecoin from scrooge_utils import encoded_hash_object from transaction import Transaction from user import User keep_going = True LOGGER = SafeWriter("log.txt", "w") def key_capture_thread(): global keep_going input() keep_going = False scrooge = Scrooge() users = {} for i in range(100): users[i] = User() coins = [Scroogecoin(user_id=users[i].id) for _ in range(10)] scrooge.create_coins(coins) for user in users.values(): LOGGER.write(f'User ID: {user.id}, balance = {len(user.get_balance(scrooge.ledger))}') threading.Thread(target=key_capture_thread, args=(), name='key_capture_thread', daemon=True).start() while keep_going: user_a = random.choice(list(users.values())) user_b = random.choice(list(users.values())) amount = random.randint(1, 10)
from scrooge import Scrooge from user import User import random import sys import keyboard sys.stdout = open('out.txt', 'w') print('Initializing Scrooge') scrooge = Scrooge() for i in range(0, 10): print('Creating User ' + str(i)) new_user = User(scrooge) scrooge.users[str(new_user.key.publickey().export_key())]=new_user for user_key in scrooge.users.keys(): scrooge.create_coins(10, user_key) users = list(scrooge.users.items()) print('=============================================\n') print('Printing users') print('=============================================\n') for user in scrooge.users.values(): print(user) print('=============================================\n') print('Simulating transactions. Some of these transactions are double spending at random.') print('This simulation does not end. You can terminate by pressing on space') print('=============================================\n') while True: try: if keyboard.is_pressed('space'): print('You pressed on space! Terminating') sys.exit()