def test_make_block_no_ancestors(self): """ when there are no ancestors, there should be no inheritance and hash should equal the input hash """ contents = blockchain.make_block(os.path.abspath(version3dir), [], 'test') self.assertEqual(contents['Inheritance'], []) self.assertEqual(contents['Hash'], 'test')
def run(): print('connect_to_network') connect_to_network() # print('connect_fake_peers_to_network') # connect_fake_peers_to_network(5) global peers peers = get_peers() peers = re.findall('(\d+.\d+.\d+.\d+)', str(peers)) thread = Thread(target=starting_web_interface_thread, args=()) thread.start() sleep(1) global chain chain = json.loads(get_chain(peers)) print(chain) global state state = get_state(peers) state = json.loads(state) print(state) print('Starting Transaction Thread') thread = Thread(target=transaction_threaded_function, args=()) thread.start() print(peers) while True: sleep(1) global transactions if len(transactions) >= block_transaction_limit: global found_block found_block = False hash = start_mining(chain[len(chain) - 1]) if hash == 'none': continue new_block = blockchain.make_block(transactions, chain) if blockchain.check_block_validity(new_block, chain[len(chain) - 1], state): for peer in peers: conn = http.client.HTTPConnection(peer, 12345) conn.request('POST', '/', json.dumps(new_block, sort_keys=True)) transactions = [] else: print('Block not valid')
def test_make_block(self): """ when there are ancestors, inheritance should reflect that and there should be a hash that is not equal to the input hash, because of the addition of the inheritance """ p, raw = blockchain.get_ancestors_from_file(self.input_file) paths = blockchain.ancestors_to_relative_blockchain_paths(raw) contents = blockchain.make_block(os.path.abspath(version3dir), raw, '') self.assertEqual(len(contents['Inheritance']), 2) self.assertTrue(KEY in contents['Inheritance'][0]) self.assertTrue(KEY2 in contents['Inheritance'][1]) self.assertNotEqual(contents['Hash'], '')
def fill_chain(state, txn_buffer): while len(txn_buffer) > 0: txn_list = [] while (len(txn_buffer) > 0) and (len(txn_list) < BLOCK_SIZE_LIMIT): new_txn = txn_buffer.pop() valid_txn = blockchain.is_valid_txn( new_txn, state) # This will return False if txn is invalid if valid_txn: # If we got a valid state, not 'False' txn_list.append(new_txn) state = blockchain.update_state(new_txn, state) else: print( "ignored transaction! State: {state}, transaction: {tnx}". format(state=json.dumps(state), tnx=json.dumps(new_txn))) sys.stdout.flush() continue ## Make a block my_block = blockchain.make_block(txn_list, chain) chain.append(my_block)
my_block = blockchain.make_block(txn_list, chain) chain.append(my_block) state = INIT_STATE # Define the initial state chain = [make_genesis_block(state)] txn_buffer = [ blockchain.make_transaction(state.keys()) for _ in range(TNX_BUFFER_COUNT) ] fill_chain(state, txn_buffer) print(blockchain.check_chain(chain)) # get block from other node other_node_chain = copy.copy(chain) other_node_txns = [blockchain.make_transaction(state.keys()) for i in range(5)] new_block = blockchain.make_block(other_node_txns, other_node_chain) print("Blockchain on Node A is currently {num} blocks long".format( num=len(chain))) try: print("New Block Received; checking validity...") state = blockchain.check_block_validity( new_block, chain[-1], state ) # Update the state- this will throw an error if the block is invalid! chain.append(new_block) except: print("Invalid block; ignoring and waiting for the next block...") print("Blockchain on Node A is now {num} blocks long".format(num=len(chain)))