def run_sample(): """Testing code. """ # Mine a sample block. b = Block(timestamp=datetime.datetime.now(), transactions=[], previous_hash=get_genisis().hash_block()) mine_till_found(b)
}, open("walletA.json", "w")) json.dump({ "public": wallets[1][1], "private": wallets[1][0] }, open("walletB.json", "w")) print(repr(wallets)) blocksToSubmit = [] #mine initial block reward = Transaction(id=gen_uuid(), owner="mined", receiver=wallets[0][1], coins=REWARD, signature="") lastBlock = construct_and_mine([reward], get_genisis()) blocksToSubmit.append(lastBlock) balances = (10, 0) for i in range(ITERS): weight = randint(10, 20) txns = [ paySomeone(wallets[0][1], wallets[0][0], wallets[1][1], balances[0]) for _ in range(weight) ] lastBlock = construct_and_mine(txns, lastBlock) blocksToSubmit.append(lastBlock) wallets = (wallets[1], wallets[0]) balances = (balances[1] + weight * balances[0], balances[0] * (1 - weight))
def run_miner(): """Run the main miner loop. """ global blockchain global public global private new_reward = REWARD while True: # Load transaction queue and blockchain from server. new = [] blockchain = Blockchain() blockchain.add_block( Block( timestamp=datetime.datetime.now(), transactions=[], previous_hash=get_genisis().hash_block(), nonce=12834 ), cheat=True ) server = load_blockchain() txns = load_transactions() # Is this _the_ new block? # or did the server swoop us :( new_chain = load_blockchain() num_blocks = 1333 + server.head.height for i in range (num_blocks): reward = Transaction( id = gen_uuid(), owner = "mined", receiver = public, coins = REWARD, signature = None ) reward.signature = sign(reward.comp(), private) txns = [reward] b = Block( timestamp = datetime.datetime.now(), transactions = txns, previous_hash = blockchain.head.hash_block() ) blockchain.add_block(b, cheat=True) # Let's mine this block. reward = Transaction( id = gen_uuid(), owner = "mined", receiver = public, coins = REWARD, signature = None ) reward.signature = sign(reward.comp(), private) txns = [reward] # Construct a new block. b = Block( timestamp = datetime.datetime.now(), transactions = txns, previous_hash = b.hash_block() ) print(blockchain.head.height) mine_till_found(b) # WE MINED THIS BLOCK YAY. # AND WE WIN. resp = get_route('add', data=str(b)) if resp['success']: print "Block added!" delete_queue(txns) else: print "Couldn't add block:", resp['message']