def form_chains(self): self.chains = [] cboard = self.current_board for color in (BLACK, WHITE): for block in cboard.iterate_blocks(color): block.chain = None for block1 in cboard.iterate_blocks(color): if block1.chain: continue chain = Chain() self.chains.append(chain) chain.add_block(block1) block_added = True while block_added: block_added = False for block2 in cboard.iterate_blocks(color): if block2.chain: continue for block3 in chain.blocks.values(): if len( cboard.block_connection_status( block3.get_origin(), block2.get_origin())) >= 2: chain.add_block(block2) block_added = True break
def test_many_transaction_in_genesis_block_fails(self): b = self._create_genesis_block(self.w1) b.add_transaction(self.w1.send_funds(self.w2.address, 1)) c = Chain() c.difficulty = 2 c.add_block(b) self.assertFalse(c.is_valid())
def sync_local(): local_chain = Chain([]) #We're assuming that the folder and at least initial block exists if os.path.exists(CHAINDATA_DIR): for filepath in glob.glob(os.path.join(CHAINDATA_DIR, '*.json')): with open(filepath, 'r') as block_file: try: block_info = json.load(block_file) except: print filepath local_block = Block(block_info) local_chain.add_block(local_block) return local_chain
def sync_local(sync_es=False): local_chain = Chain([]) if os.path.exists(CHAINDATA_DIR): for filepath in sorted(glob.glob(os.path.join(CHAINDATA_DIR, '*.json'))): with open(filepath, 'r') as block_file: try: block_info = json.load(block_file) except: raise Exception("block error") if sync_es: es.insert_document(block_info) local_block = Block(block_info) local_chain.add_block(local_block) return local_chain
def verify_block(self, block): # what the f**k new_block = Block() new_block.transactions = [] for t in block['transactions']: t_new = Transaction(t['sender'], t['destination'], t['contents'], t['signature'], t['contents_hash'], t['timestamp']) new_block.transactions.append(t_new) new_block.timestamp = block['timestamp'] new_block.previous_hash = block['previous_hash'] new_block.nonce = block['nonce'] new_block.block_hash = block['block_hash'] new_block.pow_hash = block['pow_hash'] if Verification.verify_all_signatures(new_block): Chain.add_block(new_block) return True else: return False
def run(): # Create chain to hold ledger chain = Chain() print("\n## Created chain with origin block. ##\n") print(chain) print("\n") # Add blocks chain.add_block("This is the first real block!") print(" >> Added block 1. <<\n") chain.add_block({'name': 'new block', 'type':'test'}) print(" >> Added block 2. <<\n") chain.add_block({'name': 'some other type of data', 'series':['a', 'b', 'c']}) print(" >> Added block 3. <<\n") # Let's take a look at our chain for i in range(len(chain.ledger)): print(chain.ledger[i]) print("\n")
def main(): logging.basicConfig(level=logging.DEBUG) UTXOs = {} # Unspend transaction outputs, used by wallets and transactions c = Chain() w1 = Wallet(UTXOs) w2 = Wallet(UTXOs) # Make the 1st transaction and generate all coins in the blockchain genesis_block = Block() genesis_transaction = w1.send_funds(w1.address, 100) genesis_block.add_transaction(genesis_transaction) c.add_block(genesis_block) # Let's spend some coins b2 = Block() b2.add_transaction(w1.send_funds(w2.address, 1)) b2.add_transaction(w1.send_funds(w2.address, 2)) c.add_block(b2) # Mine a block just for fun c.add_block(Block()) # Content of the blockchain c.print() print('Chain is valid:', c.is_valid()) print('w1 balance:', w1.balance) print('w2 balance:', w2.balance)
def test_add_genesis_block(self): b = self._create_genesis_block(self.w1) c = Chain() c.add_block(b) self.assertTrue(c.is_valid())
# Show Advertised Transactions to start Verification print('Verification Start:') transacts = miner1.show() # Start Mining print('Mining...') h2 = miner1.mine(transacts.copy()) # Start of Consensus Mechanism. If Consensus happens, the block gets added to the Chain. print('Trying to Achieve Consensus:') if (miner2.consensus(h2) == 1 and miner3.consensus(h2) == 1): print('Verified.') print('Creating Block') print('Adding Block to Chain') blockchain.add_block(h2) else: print("Can't Verify.") # Show information in the Blockchain Now print('Printing Complete Blockchain:') c = 1 for i in blockchain.chn: print('Block', c) print('Transactions') for j in i._transactions: print(j.sender, 'to', j.receiver, 'Amount:', j.amount, 'Fees:', j.fees) print('Own Hash', i.get_hash()) print('Previous Hash', i.get_prev()) print() c += 1
##################################### block_one = Block(block_one_dir) block_two = Block(block_two_dir) blockchain = Chain([block_zero, block_one, block_two]) print blockchain print blockchain.is_valid() print len(blockchain) assert blockchain.is_valid() assert len(blockchain) == 3 empty_chain = Chain([]) assert len(empty_chain) == 0 empty_chain.add_block(block_zero) assert len(empty_chain) == 1 #empty_chain = Chain([]) #assert len(empty_chain) == 0 print empty_chain print len(empty_chain) sys.exit(0) #..... assert blockchain == another_blockchain assert not blockchain != another_blockchain assert blockchain <= another_blockchain assert blockchain >= another_blockchain
# # App main module (script) # # 13.09.2018 Created by: zhenya ################################################################################ from chain import Chain block_one_transactions = {"sender": "Alice", "receiver": "Bob", "amount": "50"} block_two_transactions = {"sender": "Bob", "receiver": "Cole", "amount": "25"} block_three_transactions = { "sender": "Alice", "receiver": "Cole", "amount": "35" } fake_transactions = { "sender": "Bob", "receiver": "Cole, Alice", "amount": "25" } local_blockchain = Chain() local_blockchain.print_blocks() local_blockchain.add_block(block_one_transactions) local_blockchain.add_block(block_two_transactions) local_blockchain.add_block(block_three_transactions) local_blockchain.print_blocks() local_blockchain.blocks[2].transactions = fake_transactions local_blockchain.validate_chain()
prevnumber = chain.state.block_number for i in range(0, 20): print("iteration " + str(i)) block = Block( BlockHeader(timestamp=int(time.time()), prevhash=prevhash, number=prevnumber + 1)) t = trie.Trie(EphemDB()) for j in range(0, 3): transaction = transactions.Transaction(i * 3 + j, '', "192.172.9.1/28", 0, 'data', 1, 1, 1) block.transactions.append(transaction) t.update(rlp.encode(j), rlp.encode(transaction)) block.header.tx_root = t.root_hash chain.add_block(block) prevhash = block.hash prevnumber = prevnumber + 1 chain.process_time_queue() time.sleep(2) # para que timestamp (padre) != timestamp (hijo) block = chain.get_block_by_number(2) print("nonces for block " + str(2) + ": (" + str(len(block.transactions)) + " transactions in block)") for tx in block.transactions: print(tx.nonce) rand = randint(0, 20) block = chain.get_block_by_number(rand) print("type of second block: " + str(type(block))) print("nonces for block " + str(rand) + ": (" + str(len(block.transactions)) +
assert not block_one_invalid_obj.is_valid() ##################################### # # Bringing Chains into play # ##################################### blockchain = Chain([block_zero, block_one, block_two]) assert blockchain.is_valid() assert len(blockchain) == 3 empty_chain = Chain([]) assert len(empty_chain) == 0 empty_chain.add_block(block_zero) assert len(empty_chain) == 1 empty_chain = Chain([]) assert len(empty_chain) == 0 another_blockchain = Chain([another_block_zero, another_block_one, another_block_two]) assert another_blockchain.is_valid() assert len(another_blockchain) == 3 assert blockchain == another_blockchain assert not blockchain != another_blockchain assert blockchain <= another_blockchain assert blockchain >= another_blockchain assert not blockchain > another_blockchain assert not another_blockchain < blockchain
# print(f.length()) # f.show_tail() # print(f.find(Block('3'))) # print(f.find('3')) # print(f.find(Block('8'))) # print(f.find('8')) # for i in f: # print(i.data) # print(hash(i.data)) c = Chain() c.add_transaction('1') c.add_transaction('2') c.add_transaction('3') c.add_block() print(c.last_block().data) print(c.last_block().hash) print(c.last_block().prev_hash) for i in c.last_block().data: print(i.data) c.add_transaction('4') c.add_transaction('5') c.add_transaction('6') c.add_block() print(c.last_block().data) print(c.last_block().hash) print(c.last_block().prev_hash) for i in c.last_block().data: print(i.data)