def initialize_users(): # Need to create a datastructure that stores the user users = [] transactions = [] for i in range(utils.number_of_users): coinsList = [] newUser = User(i) for j in range(10): cID = str(i * 10 + j) signature = sign_message(scrooge_private, str(cID)) encoded = base64.b64encode(signature) no_bytes = encoded.decode('utf-8') coin_dict = {} coin_dict[cID] = no_bytes newUser.add_coin(coin_dict) transaction = {} transaction['previous_transaction'] = None transaction['sender'] = 'scrooge' transaction['receiver'] = newUser.user_id transaction['coin_id'] = json.dumps(coin_dict) transactions.append(transaction) utils.printLog( "User " + str(i) + "\n" + str(newUser.private_key.public_key().public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo))) utils.printLog("10 coins") users.append(newUser) return users, transactions
def create_block(print_transactions=False): block = OrderedDict() for index in range(10): item = queue[index] for key in item: if (print_transactions): utils.printLog("Adding to block Transaction ID: ", key) block[key] = item[key] block['previous_block'] = utils.previous_block return block
def create_transaction(self): number = self.user_id if(len(self.coins)>0): while(number==self.user_id): number = random.randint(0, utils.number_of_users-1) transaction = {} coin_entry = random.randint(0, len(self.coins)-1) cID = self.coins[coin_entry] transaction['previous_transaction'] = utils.find_previous_transaction(cID) transaction['sender'] = self.user_id transaction['receiver'] = number transaction['coin_id'] = cID dictionary, t_hash = utils.sign_and_hash('transaction', transaction, self.private_key) utils.printLog("Creating Transaction ID: ",t_hash, " User ",transaction['sender'], " Sending CoinID ",next(iter(transaction['coin_id']))," to User ",transaction['receiver'], " Previous transaction ID ",transaction['previous_transaction']) return dictionary return None
def check_double(transaction, index): get_cID = transaction['coin_id'] get_sender = transaction['sender'] for index2, signed_transaction in enumerate(queue): for key in signed_transaction: q_transaction = signed_transaction[key]['transaction'] if (get_cID == q_transaction['coin_id'] and get_sender == q_transaction['sender'] and index > index2): utils.printLog("Double Spending, User ", get_sender, " Tried to pay coin ID", get_cID, "to Users ", transaction['receiver'], " and ", q_transaction['receiver']) queue.remove(queue[index]) index -= 1 return True return False
users, init_transactions = initialize_users() # for transaction in init_transactions: for transaction in init_transactions: dictionary, _ = sign_and_hash("transaction", transaction, scrooge_private) queue.append(dictionary) if (len(queue) == 10): block = create_block() queue = [] signed_block, _ = sign_and_hash("block", block, scrooge_private) for key in signed_block: utils.blockchain[key] = signed_block[key] while True: if is_pressed(' '): utils.printLog("Created ", len(utils.blockchain), " blocks") SystemExit(0) break random_user_id = random.randint(0, utils.number_of_users - 1) transaction = users[random_user_id].create_transaction() if (transaction is not None): queue.append(transaction) if (len(queue) >= 10): doChecks(check_double_spending=True) if (len(queue) >= 10): utils.printLog("Creating a block") block = create_block(print_transactions=True) queue = queue[10:] signed_block, _ = sign_and_hash("block", block, scrooge_private) for key in signed_block: utils.blockchain[key] = signed_block[key]