def genesis_block(self): transactions = [] my_trans = [] for peer in self.peers_database: if peer.pk is not None: for count in range(CHAIN_SIZE // 2): if peer.pk == self.peer_data.pk: trans = Transaction(outputs=[(peer.pk, BASE_VALUE)], timestamp=count) my_trans.append(trans) transactions.append(trans) else: transactions.append( Transaction(outputs=[(peer.pk, BASE_VALUE)], timestamp=count)) if self.mode == 'client': for tx in my_trans: input_utxo = tx.get_outputs()[0] input_utxo.set_prev_tx_hash(tx) input_utxo.sign(self.sk) self.__wallet.append(input_utxo) genesis_block = Block(transactions=transactions, previous_hash="genesis", height=0, timestamp=0) self.blockchain = Blockchain(block=genesis_block)
def create_god_transaction(to_pk): """Creates first ("God") transaction in chain history using seed coins""" god_pk, god_sk = signature.generate_keys() tx = Transaction(god_pk, to_pk, SEED_COIN_SUPPLY) tx.sign(god_sk) return tx
def generate_transaction(): sender_address = request.form['sender_address'] sender_private_key = request.form['sender_private_key'] recipient_address = request.form['recipient_address'] value = request.form['amount'] transaction = Transaction(sender_address, sender_private_key, recipient_address, value) array = {} array['address'] = sender_address array['public_key'] = '' array['private_key'] = sender_private_key array['timestamp'] = 1 check_wallet_out = Wallet(array) check_wallet_out = check_wallet_out.ischeck_address() array['address'] = recipient_address check_wallet_in = Wallet(array) check_wallet_in = check_wallet_in.ischeck_address() try: response = { 'transaction': transaction.to_dict(), 'signature': transaction.sign_transaction() } if (check_wallet_out and check_wallet_in): return jsonify(response), 200 else: return "Not address or private key", 500 except: return "Not address or private key", 500
def test_transaction_signing(self): u1 = Client() u2 = Client() outputs = [(u2.public_key, 5)] tx = Transaction(u1.public_key, u1.get_sk(), [], outputs, False) tx.sign_transaction() self.assertNotEqual(None, tx.get_signature())
def run(type_op): highest_block = self.blockchain.get_highest_block() accounts, txs, validators, contract_accounts = self.blockchain.get_state( Block.calc_hash(highest_block)) new_state, shard_num = Transaction.run_contract(contract_accounts[from_vk], vk, type_op) new_tx = Transaction(0, vk, from_vk, 0, 0, shard_num=shard_num) new_tx.new_state = new_state publish(new_tx)
def test_transaction_outputs(self): u1 = Client() u2 = Client() outputs = [(u2.public_key, 5)] tx = Transaction(u1.public_key, u1.get_sk(), [], outputs, False) utxo_op = tx.get_outputs() self.assertEqual(len(outputs), 1) self.assertEqual(utxo_op[0].get_recipient_pk(), u2.public_key)
def generate_tx(self, outputs, prev_tx_hash, output_index): for utxo in self.__wallet: if utxo.get_transaction_hash() == prev_tx_hash and utxo.get_index() == output_index: utxo.sign(self.__secret_key) tx = Transaction(originator_sk=self.__secret_key, originator_pk=self.public_key, inputs=[utxo], outputs=outputs, witnesses_included=True) tx.sign_transaction() return tx
def generate_tx(self, outputs, utxo): utxo.sign(self.sk) tx = Transaction(peer_data=self.peer_data, inputs=[utxo], outputs=outputs, witnesses_included=True) msg = str(tx.to_dict()) signature = sign(msg, self.sk) tx.sign_transaction(signature) return tx
def main(): blockchain_ip = "103.22.220.153" from_node = 1 to_node = 0 value = 0 data = "TEXT" t1 = Transaction(blockchain_ip, from_node, to_node) t1.transact(value, data) print(t1.transaction_history)
def test_transaction_verification(self): u1 = Client() u2 = Client() outputs = [(u2.public_key, 5)] tx = Transaction(u1.public_key, u1.get_sk(), [], outputs, False) tx.sign_transaction() u3 = Client() result = u3.validate_transaction(tx) self.assertFalse( result ) # because of the absense of inputs, it's considered an overspending transaction
def test_transaction_dumping(self): u1 = Client() u2 = Client() outputs = [(u2.public_key, 5)] tx = Transaction(u1.public_key, u1.get_sk(), [], outputs, False) dictionary = tx.to_dict() self.assertEqual(dictionary['witnesses_included'], False) self.assertEqual(dictionary['originator'], u1.public_key) self.assertEqual(dictionary['witnesses'], []) self.assertEqual(dictionary['ip_counter'], 0) self.assertEqual(dictionary['inputs'], []) self.assertEqual(dictionary['op_counter'], 1) self.assertEqual(dictionary['outputs'], tx.get_outputs()) self.assertEqual(dictionary['time'], tx.get_timestamp())
def genesis_block(self): transactions = [] for i in range(1, 51): transactions.append(Transaction(outputs=[(i, BASE_VALUE)])) genesis_block = Block(transactions=transactions, previous_hash="genesis") self.__blockchain = Blockchain(block=genesis_block)
def run_contract(self): accounts, txs, validators, contract_accounts = self.blockCallBack.blockchain.get_state( Block.calc_hash(self.blockCallBack.blockchain.get_highest_block())) if self.vk.to_string().hex() in contract_accounts: tx = Transaction(0, self.vk.to_string().hex(), RUN_ADDR, 0, 0) self.txCallBack.publish(tx) return True return False
def mine_block(self, last_block: Block, all_txs, benef="", max_txs_per_block=300) -> Block: """ Calculates headers for a new block with parent block as last_block finds mutually valid set of txs from all_txs - txs included till parent :param last_block: :param all_txs: :param benef: :return: """ max_hash_val = int(MAX_HASH, 16) target_hash_val = max_hash_val // last_block.diff new_diff = last_block.diff if time.time() - last_block.timestamp > MINE_RATE: new_diff -= 1 else: new_diff += 1 if new_diff < 1: new_diff = 1 accounts_state, applied_txs, existing_validators, existing_contract_accounts = self.get_state( Block.calc_hash(last_block)) new_txs = [] self.tx_pool_sem.acquire() # loop to get new txs to include in mined block. effectively all_txs - applied_txs for tx in all_txs: if len(new_txs) > max_txs_per_block: break found = False for ttx in applied_txs: if ttx.__dict__ == tx.__dict__: found = True break if not found: new_txs.append(tx) self.tx_pool_sem.release() if new_txs: val_txs, new_state, new_validators, new_contract_accounts = Transaction.get_mutually_valid_txs( new_txs, accounts_state, existing_contract_accounts, benef, existing_validators) if len(val_txs) > 0: block = Block(timestamp=time.time(), parent_hash=Block.calc_hash(last_block), benef=benef, num=last_block.num + 1, diff=new_diff, txs=val_txs) print("Attempting to mine, headers calculated, number of txs = ", len(val_txs)) curr_nonce = 0 while int(Block.calc_hash(block), 16) > target_hash_val: if curr_nonce > MAX_NONCE: print("nonce exceeded max") # print(hex(target_hash_val), Block.calc_hash(block)) curr_nonce += 1 block.nonce = curr_nonce return block else: return None else: return None
def from_dict(d): b = Block() txs = d["txs"] b.__dict__ = d b.txs = [] for d_tx in txs: tx = Transaction.from_dict(d_tx) b.txs.append(tx) return b
def sync_overall(self, save=False): print(" * Start syncing...") best_chain = self.sync_local() best_chain_is_local_chain = True for peer in PEERS: peer_blockchain_url = peer + 'blockchain.json' try: r = requests.get(peer_blockchain_url) peer_blockchain_dict = r.json() print(' * Syncing from %s:' % (peer_blockchain_url)) peer_blocks = [] for peer_block in peer_blockchain_dict: peer_blocks.append( Block(index=peer_block['index'], timestamp=peer_block['timestamp'], transactions=peer_block['transactions'], previous_hash=peer_block['previous_hash'], diff=peer_block['diff'], hash=peer_block['hash'], nonce=peer_block['nonce'])) peer_blockchain = Blockchain() peer_blockchain.chain = peer_blocks #sync transaction Transaction.sync_transaction() #sync wallet Transaction.sync_wallet() if peer_blockchain.is_valid_chain() and len( peer_blockchain.chain) > len(best_chain.chain): best_chain = peer_blockchain best_chain_is_local_chain = False except requests.ConnectionError: print("Peer %s is not running and can not be synced" % (peer)) else: print(" * Syncing complete from peer %s" % (peer)) if not best_chain_is_local_chain: best_chain.save() return best_chain
def message(self, pubnub, message): Logger.pubnub_log(message) tx = Transaction.from_dict(message.message) # if tx.verify_sign(): self.blockchain.tx_pool_sem.acquire() self.tx_pool.add(tx) self.blockchain.tx_pool_sem.release() if tx.to_vk == RUN_ADDR: highest_block = self.blockchain.get_highest_block() accounts, txs, validators, contract_accounts = self.blockchain.get_state(Block.calc_hash(highest_block)) if tx.from_vk in contract_accounts and self.vk in validators: def f(contract_accounts, vk, publish, from_vk): def check_completion(type_op): # type_op is either "m", "s" or "r" Logger.p("Checking completion," + type_op) highest_block = self.blockchain.get_highest_block() accounts, txs, validators, contract_accounts = self.blockchain.get_state( Block.calc_hash(highest_block)) state = contract_accounts[tx.from_vk].state print(state) for elm in state: if (type(elm) == tuple or type(elm) == list) and elm[1] == type_op: continue else: return False return True def run(type_op): highest_block = self.blockchain.get_highest_block() accounts, txs, validators, contract_accounts = self.blockchain.get_state( Block.calc_hash(highest_block)) new_state, shard_num = Transaction.run_contract(contract_accounts[from_vk], vk, type_op) new_tx = Transaction(0, vk, from_vk, 0, 0, shard_num=shard_num) new_tx.new_state = new_state publish(new_tx) run("m") while not check_completion("m"): time.sleep(SLEEP_BETWEEN_COMPLETION_CHECK) run("s") while not check_completion("s"): time.sleep(SLEEP_BETWEEN_COMPLETION_CHECK) run("r") Logger.p("Creating and starting new thread") Thread(target=f, args=(contract_accounts, self.vk, self.publish, tx.from_vk)).start() else: Logger.p("Tx has RUN_ADDR but not a contract account or you are not in validator set") else: Logger.p("Tx does not have RUN_ADDR") Logger.p("signature verified, tx added to tx pool")
def _get_state_helper(self, block_hash) -> (Accounts, Set[Transaction], Set[Account], Dict): """ Applies all transactions from genesis till given block and returns the final state ,all the transactions in the entire chain, the validators registered till that block and the contract accounts until then :param block_hash: :return: Accounts """ if self.chain[block_hash].num == 0: # if genesis, init account state, applied txs, validators # and contract accounts to nulls return Accounts(), set(), set(), {} accounts, txs, validators, existing_contract_accounts = self._get_state_helper( self.chain[block_hash].parent_hash) txs = txs.union(set(self.chain[block_hash].txs)) b, new_accounts, temp_vals, new_contract_accounts = Transaction.apply_txs(self.chain[block_hash].txs, accounts, existing_contract_accounts, self.chain[block_hash].benef, validators) validators = validators.union(temp_vals) assert b return new_accounts, txs, validators, new_contract_accounts
def send_tx(self, to, val, fee, mapper_code="", shard_num=-1, shuffler_code="", reducer_code=""): tx = Transaction(0, self.vk.to_string().hex(), to, val, fee, shard_num=shard_num) tx.mapper_code = mapper_code tx.reducer_code = reducer_code tx.shuffler_code = shuffler_code self.txCallBack.publish(tx)
def transaction(): """Return transaction object.""" return Transaction()
from transaction.transaction import Transaction blockchain_ip = "103.22.220.153" hexstring = "0x46e899a7c0b31e324bff6b31def97027b431ffbbb294cca975f7c5b2230053ad" print(Transaction.get_input_data(blockchain_ip, hexstring))
def test_transaction_creation(self): u1 = Client() u2 = Client() outputs = [(u2.public_key, 5)] tx = Transaction(u1.public_key, u1.get_sk(), [], outputs, False) self.assertIsInstance(tx, Transaction)
def process(self, ops): """ Process a list of operations. Author: Yi Zhang Date: 11/18/2017 - Param: :ops (Operation[]): The list of operations needed to be processed """ for op in ops: if op.OP == "begin": # initialize transaction t = Transaction("NORMAL", op.NAME) self.transactions.append(t) elif op.OP == "beginRO": # initialize RO trans self.database.register_read_only(op.NAME) t = Transaction("READ", op.NAME) self.transactions.append(t) elif op.OP == "R": # read # check the status of this trans trans = self._find_transaction(op.T) if trans == None: print("Transaction not found: " + str(op.T)) elif trans.get_status() == "RUNNING": rs = self.database.read(op.T, int(op.X[1:])) if rs == -1: # abort rl = self.database.abort(op.T) trans.set_status("ABORTED") self._resume(rl) elif rs == -2: # blocked trans.set_status("BLOCKED") trans.buffer.append(op) self._clean_deadlocks() else: # success trans.variables[int(op.X[1:])] = rs elif trans.get_status() == "BLOCKED": trans.buffer.append(op) elif op.OP == "W": # write # check the status of this trans trans = self._find_transaction(op.T) if trans == None: print("Transaction not found: " + str(op.T)) elif trans.get_status() == "RUNNING": # send to database rs = self.database.write(op.T, int(op.X[1:]), op.V) if rs == -2: # need to wait trans.set_status("BLOCKED") trans.buffer.append(op) self._clean_deadlocks() elif trans.get_status() == "BLOCKED": trans.buffer.append(op) elif op.OP == "dump": if op.SITE != -1: self.database.dump(server=op.SITE) elif op.VAR != "": self.database.dump(var=int(op.VAR[1:])) else: self.database.dump() elif op.OP == "end": # check the status of this trans trans = self._find_transaction(op.NAME) if trans == None: print("Transaction not found: " + str(op.NAME)) elif trans.get_status() == "RUNNING": # send to database rs = self.database.end(op.NAME) if rs is None: # abort rl = self.database.abort(op.NAME) trans.set_status("ABORTED") self._resume(rl) else: trans.set_status("COMMITED") self._print_trans(trans.name) self._resume(rs) elif trans.get_status() == "BLOCKED": trans.buffer.append(op) elif op.OP == "fail": to_be_abort = self.database.fail(op.SITE) for t in to_be_abort: r = self.database.abort(t) trans = self._find_transaction(t) trans.set_status("ABORTED") self._resume(r) else: # recover self.database.recover(op.SITE) n_transactions = [] for t in self.transactions: if t.get_status() == "COMMITED" or t.get_status() == "ABORTED": self.history.append(t) else: n_transactions.append(t) self.transactions = n_transactions
@app.route('/someone_mined_new_block', methods=['POST']) def import_block_from_other_node(): print('------------------------------') print(' * Someone has minned new block, receiving...') new_block_dict = json.loads(request.data) print(new_block_dict['hash']) mine.sched.add_job(mine.validate_possible_block, args=[new_block_dict], id='validate_possible_block') return 'I received a block' #transaction transaction = Transaction() @app.route('/transaction') def transaction_index(): return render_template('transaction/index.html') @app.route('/hash') def gettransactionID(): response = transaction.gettransactionID( '0100000001c997a5e56e104102fa209c6a852dd90660a20b2d9c352423edce25857fcd3704000000004847304402204e45e16932b8af514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5fb8cd410220181522ec8eca07de4860a4acdd12909d831cc56cbbac4622082221a8768d1d0901ffffffff0200ca9a3b00000000434104ae1a62fe09c5f51b13905f07f06b99a2f7159b2225f374cd378d71302fa28414e7aab37397f554a7df5f142c21c1b7303b8a0626f1baded5c72a704f7e6cd84cac00286bee0000000043410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac00000000' ) return response
print("Firing up a new node!") print("Your public key is:") print(pk) print("Your secret key is:") print(sk) # Generate God keys to create seed transaction god_pk, god_sk = signature.generate_keys() # Create two blockchains and implement fork choice new_blockchain = None for i in range(4): if not new_blockchain: # Must mine the Genesis node tx = Transaction(god_pk, pk, SEED_COIN_SUPPLY) tx.sign(god_sk) new_blockchain = Blockchain([tx]) else: to_pk = input("Give seed money to:") amount = int(input("Amount:")) tx = Transaction(pk, to_pk, amount) # All transactions sent from God node tx.sign(sk) new_blockchain.add_transactions([tx]) print("Creating second blockchain...") new_blockchain_2 = None for i in range(3): if not new_blockchain_2: # Must mine the Genesis node tx = Transaction(god_pk, pk, SEED_COIN_SUPPLY)
def handle_queries(): global user database = None active_transaction = None while True: query = str(input(">> ")) operation = bqo.findoperation(query) if operation == Operation.EXIT: break if "CREATE DATABASE" in query.upper(): create.execute(database, query, user) continue elif operation == Operation.GRANT: grant.execute(query, user) continue elif operation == Operation.REVOKE: revoke.execute(query, user) continue elif operation == Operation.SHW_DTBS: infoqueries.showdatabases() continue if database is None and operation is not Operation.USE: print("Database not selected\n") continue if "GENERATE ERD" in query.upper(): erd.generating_erd(database) continue if active_transaction != None: active_transaction.execute(query, operation) if operation == Operation.COMMIT or operation == Operation.ROLLBACK: active_transaction = None continue logger.get_event_logger().info(f"The query entered is : {query}") start_time = time.time() if operation == Operation.SELECT: select.execute(database, query) elif operation == Operation.INSERT: insert.execute(database, query) elif operation == Operation.UPDATE: update.execute(database, query) elif operation == Operation.DELETE: delete.execute(database, query) elif operation == Operation.DROP: drop.execute(database, query) elif operation == Operation.CREATE: create.execute(database, query, user) elif operation == Operation.USE: database = use.execute(query, user) elif operation == Operation.SHW_TBLS: infoqueries.showtables(database) elif operation == Operation.SHW_DTBS: infoqueries.showdatabases() elif operation == Operation.DESC: infoqueries.describe(database, query) elif operation == Operation.STRT_TRNAS: active_transaction = Transaction(database) print("Transaction Started") logger.get_event_logger().info(f"Transaction started") elif operation == Operation.COMMIT: print("No active Transaction") logger.get_event_logger().info(f"No active transaction") elif operation == Operation.ROLLBACK: print("No active Transaction") logger.get_event_logger().info(f"No active transaction") else: print("Invalid Query") logger.get_event_logger().error(f"The query entered is invalid") end_time = time.time() total_time = end_time - start_time logger.get_general_logger().info( f"The total execution time of the query \"{query}\" is : {total_time}" ) print()