ptr += Transaction.SIZE return Block(header=header, txs=txs) def get(self, key: str): for tx in self.txs: if tx.key == key: return tx.value return None class InvalidBlock(Exception): pass GENESIS_TX = Transaction("Welcome to Nami Blockchain", "@tuankiet65_nicememe") GENESIS_BLOCK_HEADER = BlockHeader(timestamp=1534613194, prev_hash=int256_to_bytes(0x0), target=int256_to_bytes(FIXED_TARGET), tx_hash=sha256(GENESIS_TX.hash()), nonce=0x69696969) GENESIS_BLOCK = Block(header=GENESIS_BLOCK_HEADER, txs=[GENESIS_TX]) if __name__ == "__main__": block = GENESIS_BLOCK print(block.to_bytes().hex()) print(block.hash_hex())
def add_transaction(self, sender, receiver, amount, account): transaction = Transaction(sender, receiver, amount, time.time()) transaction.sign(account) self.transactions_pool.append(transaction)
def transaction(): transaction = Transaction("4/20/20", "Deposit", 100, -1) return transaction
def checkRestoreWithMultipleObjectsInUndoRedo(self): from ZODB.FileStorage import FileStorage # Undo creates backpointers in (at least) FileStorage. ZODB 3.2.1 # FileStorage._data_find() had an off-by-8 error, neglecting to # account for the size of the backpointer when searching a # transaction with multiple data records. The results were # unpredictable. For example, it could raise a Python exception # due to passing a negative offset to file.seek(), or could # claim that a transaction didn't have data for an oid despite # that it actually did. # # The former failure mode was seen in real life, in a ZRS secondary # doing recovery. On my box today, the second failure mode is # what happens in this test (with an unpatched _data_find, of # course). Note that the error can only "bite" if more than one # data record is in a transaction, and the oid we're looking for # follows at least one data record with a backpointer. # # Unfortunately, _data_find() is a low-level implementation detail, # and this test does some horrid white-box abuse to test it. is_filestorage = isinstance(self._storage, FileStorage) db = DB(self._storage) c = db.open() r = c.root() # Create some objects. r["obj1"] = MinPO(1) r["obj2"] = MinPO(1) transaction.commit() # Add x attributes to them. r["obj1"].x = 'x1' r["obj2"].x = 'x2' transaction.commit() r = db.open().root() self.assertEqual(r["obj1"].x, 'x1') self.assertEqual(r["obj2"].x, 'x2') # Dirty tricks. if is_filestorage: obj1_oid = r["obj1"]._p_oid obj2_oid = r["obj2"]._p_oid # This will be the offset of the next transaction, which # will contain two backpointers. pos = self._storage.getSize() # Undo the attribute creation. info = self._storage.undoInfo() tid = info[0]['id'] t = Transaction() self._storage.tpc_begin(t) oids = self._storage.undo(tid, t) self._storage.tpc_vote(t) self._storage.tpc_finish(t) r = db.open().root() self.assertRaises(AttributeError, getattr, r["obj1"], 'x') self.assertRaises(AttributeError, getattr, r["obj2"], 'x') if is_filestorage: # _data_find should find data records for both objects in that # transaction. Without the patch, the second assert failed # (it claimed it couldn't find a data record for obj2) on my # box, but other failure modes were possible. self.assertTrue(self._storage._data_find(pos, obj1_oid, '') > 0) self.assertTrue(self._storage._data_find(pos, obj2_oid, '') > 0) # The offset of the next ("redo") transaction. pos = self._storage.getSize() # Undo the undo (restore the attributes). info = self._storage.undoInfo() tid = info[0]['id'] t = Transaction() self._storage.tpc_begin(t) oids = self._storage.undo(tid, t) self._storage.tpc_vote(t) self._storage.tpc_finish(t) r = db.open().root() self.assertEqual(r["obj1"].x, 'x1') self.assertEqual(r["obj2"].x, 'x2') if is_filestorage: # Again _data_find should find both objects in this txn, and # again the second assert failed on my box. self.assertTrue(self._storage._data_find(pos, obj1_oid, '') > 0) self.assertTrue(self._storage._data_find(pos, obj2_oid, '') > 0) # Indirectly provoke .restore(). .restore in turn indirectly # provokes _data_find too, but not usefully for the purposes of # the specific bug this test aims at: copyTransactionsFrom() uses # storage iterators that chase backpointers themselves, and # return the data they point at instead. The result is that # _data_find didn't actually see anything dangerous in this # part of the test. self._dst.copyTransactionsFrom(self._storage) self.compare(self._storage, self._dst)
def new_tx(self, *args, **kwargs): name = str(self.tx_count) self.tx_count += 1 return Transaction(self, name, *args, **kwargs)
def checkTransactionalUndoIterator(self): # check that data_txn set in iterator makes sense if not hasattr(self._storage, "iterator"): return s = self._storage BATCHES = 4 OBJECTS = 4 orig = [] for i in range(BATCHES): t = Transaction() tid = p64(i + 1) s.tpc_begin(t, tid) for j in range(OBJECTS): oid = s.new_oid() obj = MinPO(i * OBJECTS + j) s.store(oid, None, zodb_pickle(obj), '', t) orig.append((tid, oid)) s.tpc_vote(t) s.tpc_finish(t) orig = [(tid, oid, s.getTid(oid)) for tid, oid in orig] i = 0 for tid, oid, revid in orig: self._dostore(oid, revid=revid, data=MinPO(revid), description="update %s" % i) # Undo the OBJECTS transactions that modified objects created # in the ith original transaction. def undo(i): info = s.undoInfo() t = Transaction() s.tpc_begin(t) base = i * OBJECTS + i for j in range(OBJECTS): tid = info[base + j]['id'] s.undo(tid, t) s.tpc_vote(t) s.tpc_finish(t) for i in range(BATCHES): undo(i) # There are now (2 + OBJECTS) * BATCHES transactions: # BATCHES original transactions, followed by # OBJECTS * BATCHES modifications, followed by # BATCHES undos transactions = s.iterator() eq = self.assertEqual for i in range(BATCHES): txn = transactions.next() tid = p64(i + 1) eq(txn.tid, tid) L1 = [(rec.oid, rec.tid, rec.data_txn) for rec in txn] L2 = [(oid, revid, None) for _tid, oid, revid in orig if _tid == tid] eq(L1, L2) for i in range(BATCHES * OBJECTS): txn = transactions.next() eq(len([rec for rec in txn if rec.data_txn is None]), 1) for i in range(BATCHES): txn = transactions.next() # The undos are performed in reverse order. otid = p64(BATCHES - i) L1 = [(rec.oid, rec.data_txn) for rec in txn] L2 = [(oid, otid) for _tid, oid, revid in orig if _tid == otid] L1.sort() L2.sort() eq(L1, L2) self.assertRaises(StopIteration, transactions.next)
def decoderawtransaction(self, raw): tx = Transaction(raw) return tx.deserialize()
import time from transaction import Transaction from block import Block from key import BitcoinAccount wallet = BitcoinAccount() difficulty = 4 first_block = Block(0, "") tx = Transaction("mohamed", "justine", 50, time.time()) first_block.add_transaction(tx) first_block.mine(difficulty) print("First block is: ") print(first_block) last_hash = first_block.hashval second_block = Block(1, last_hash) second_block.mine(difficulty) print("Second block is: ") print(second_block)
def add_transaction(self, data: str, participant_a: AbstractParticipant, participant_b: AbstractParticipant) -> Transaction: transaction = Transaction(data, participant_a, participant_b) self.pending_transactions.append(transaction) return self.last_block['index'] + 1
def create_transaction(self, customer: Customer, date: str, amount: float) -> Transaction: new_transaction = Transaction(customer.public_key, self.public_key, date, amount) customer.sign(new_transaction) self.sign(new_transaction) return new_transaction
def addTransaction(self, timestamp, fromWallet, toWallet, transactionAmount): self.transactions.append( Transaction(timestamp, fromWallet, toWallet, transactionAmount))
def reward(): reward = Vout(get_account()['address'], REWARD) tx = Transaction([], reward) return tx
def setUp(self): self.transaction = Transaction('Alex', 'Yann', 'signature', 15)
#Create 100000 transactions n = 1000 transactions = [] value_date = date(2018, 9, 23) maturity_date = date(2020, 9, 30) interest_type = InterestType.FIXED interest_rate = 0.06 amort_type = AmortizationType.LINEAR amort_period = Periodicity.MONTHLY interest_period = Periodicity.MONTHLY i = 0 while i < n: transactions.append( Transaction(100000, value_date, maturity_date, interest_type, interest_rate, interest_period, amort_type, amort_period)) e = CashflowEngine() e.generate_principal_cashflows(transactions[i]) i += 1 if i % 1000 == 0: print("processed so far:\t" + str(i)) #i = 0 #while i < len(p_cashflows): # print(p_cashflows[i]) # i += 1 end = datetime.now() print("ENDING at:\t" + str(end))
def broadcast(self, tx, timeout=30): """Broadcast a transaction to the network. """ tx = Transaction(tx) return self.network.broadcast(tx, timeout)
def buy(buyer, products): # products: list of product of the same type, such as the iPhone XS product_id = products[0].product_id product_name = products[0].product_name product_price = products[0].stock_price seller_id = products[0].seller_id promotion_id = -1 # flag to check if a promotion applied most_valuable_customer_id = mysql.find_most_valuable_customer( seller_id) # get the seller for product from catalogue for product in products: # # random choose seller # seller_index = choice(Market.catalogue[product.product_name]) # index = Market.catalogue[product.product_name].index(seller_index) # seller = Market.catalogue[product.product_name][index] # find seller in the market catalogue for x in Market.catalogue.get(product.product_name): if x.id == seller_id: seller = x # find the buyer who has spent the most in the same seller and offer promotion with id 1 if most_valuable_customer_id == buyer.id: product_price = product.stock_price * 0.95 promotion_id = 1 # call seller's sold function seller.sold(product) # deduct price from user's balance buyer.deduct(product.stock_price) # track user GoogleAds.track_user_purchase(buyer, product) # update the stock of product products[0].stock_quantity = products[0].stock_quantity - len(products) if products[0].stock_quantity < 0: # print("stock < 0") pass else: logging.info( "[Market]: Update the stock quantity to %d of product %s of seller %s %d", products[0].stock_quantity, product_name, seller.name, seller_id) # update the stock of product in database mysql.update_stock(product_id, seller_id, products[0].stock_quantity, products[0].stock_cost, seller.wallet) dt = datetime.datetime.now() transaction = Transaction(datetime=dt, seller_id=seller.id, customer_id=buyer.id, product_id=product_id, quantity=len(products), total_amount=product_price * len(products)) logging.info( "[Market]:Transaction between Seller %s and Customer %s with the product %s at %s " "in year %s and quarter %s", seller.name, buyer.name, product_name, transaction.timestamp, transaction.year, transaction.quarter) # add the profit to seller's wallet seller.wallet += product_price * len(products) # if a promotion has been applied in calculating, then update the transaction promotion id property if promotion_id != -1: transaction.set_promotion_id(promotion_id) # if a customer makes a single transaction > $2000, offer a $50 off if transaction.total_amount >= 2000: transaction.total_amount -= 50 # if no promotion has been applied to this customer, then update the transaction promotion to id 5 if promotion_id == -1: transaction.set_promotion_id(5) # write to database mysql.save_txn(transaction)
def undo(self, tid, note): t = Transaction() t.note(note) oids = self._begin_undos_vote(t, tid) self._storage.tpc_finish(t) return oids
""" opened_file = open(filename, "w") print(" Export File Opened") print(" Beginning Export") for i in (ts if ats == [] else ats): try: opened_file.write(repr(i) + "\n") except Exception as e: print(" An error has occurred: {}".format(e)) opened_file.write(repr(var) + "\n") opened_file.close() print(" Export File Closed") if __name__ == "__main__": t0 = Transaction(2015, 11, 24, "Cash", "Accounts Recievable", "Job", 400) t1 = Transaction(2015, 11, 24, "Fast Food", "Cash", 'McDonald\'s', 300) t2 = Transaction(2015, 11, 24, "Fast Food", "Debit", "McDonald's", 200) t3 = Transaction(2015, 10, 24, "Fast Food", "Cash", "Wendy's", 500) t4 = Transaction(2015, 10, 23, "Fast Food", "Gifts", "Wendy's", 400) t5 = Transaction(2015, 10, 22, "Drinks", "Savings", "Coffee", 200) ts = [t0, t1, t2, t3, t4, t5] a0 = Account( 'Cash', 2, [ Transaction(2015, 11, 24, "Cash", "Accounts Recievable", "Job", 400, '$'), Transaction(2015, 11, 24, "Fast Food", "Cash", "McDonald's", 300, '$'), Transaction(2015, 10, 24, "Fast Food", "Cash", "Wendy's", 500, '$') ], {
def signrawtransaction(self, raw_tx, input_info, private_keys): tx = Transaction(raw_tx) self.wallet.signrawtransaction(tx, input_info, private_keys, self.password) return tx
def __init__(self, io, host='localhost', port=7474, cleanoutdb=False, debug=False , retries=300, readonly=False, encryption_required=False, use_network=True): 'Initialize and construct a global database instance' #print >> sys.stderr, 'CALLING NEW initglobal' CMAdb.log = logging.getLogger('cma') CMAdb.debug = debug CMAdb.io = io from hbring import HbRing try: syslog = logging.handlers.SysLogHandler(address='/dev/log' , facility=logging.handlers.SysLogHandler.LOG_DAEMON) except EnvironmentError: # Docker doesn't really get along with logging - sigh... syslog = logging.StreamHandler() syslog.setFormatter(logging.Formatter('%(name)s %(levelname)s: %(message)s')) CMAdb.log.addHandler(syslog) CMAdb.log.setLevel(logging.DEBUG) hostport = '%s:%s' % (host, port) url = ('https://%s/db/data/' % (hostport)) url = ('http://%s/db/data/' % (hostport)) Neo4jCreds().authenticate(hostport) neodb = neo4j.Graph(url) self.db = neodb if cleanoutdb: CMAdb.log.info('Re-initializing the NEO4j database') self.delete_all() self.db = neodb CMAdb.use_network = use_network trycount=0 while True: try: CMAdb.cdb = CMAdb(db=neodb) # Neo4j started. All is well with the world. break except (RuntimeError, IOError, py2neo.GraphError) as exc: print >> sys.stderr, 'TRYING AGAIN [%s]...[%s]' % (url, str(exc)) trycount += 1 if trycount > retries: print >> sys.stderr, ('Neo4j still not started - giving up.') CMAdb.log.critical('Neo4j still not started - giving up.') raise RuntimeError('Neo4j not running - giving up [%s]' % str(exc)) if (trycount % 60) == 1: print >> sys.stderr, ('Waiting for Neo4j [%s] to start [%s].' % (url, str(exc))) CMAdb.log.warning('Waiting for Neo4j [%s] to start [%s].' % (url, str(exc))) # Let's try again in a second... time.sleep(1) Store.debug = debug Store.log = CMAdb.log CMAdb.store = Store(neodb, CMAconsts.uniqueindexes, CMAconsts.classkeymap , readonly=readonly) if not readonly: for classname in GraphNode.classmap: GraphNode.initclasstypeobj(CMAdb.store, classname) from transaction import Transaction CMAdb.transaction = Transaction(encryption_required=encryption_required) #print >> sys.stderr, 'CMAdb:', CMAdb #print >> sys.stderr, 'CMAdb.store(cmadb.py):', CMAdb.store CMAdb.TheOneRing = CMAdb.store.load_or_create(HbRing, name='The_One_Ring' , ringtype=HbRing.THEONERING) CMAdb.transaction.commit_trans(io) #print >> sys.stderr, 'COMMITTING Store' #print >> sys.stderr, 'Transaction Commit results:', CMAdb.store.commit() CMAdb.store.commit() #print >> sys.stderr, 'Store COMMITTED' else: CMAdb.transaction = None
def sendrawtransaction(self, raw): tx = Transaction(raw) r, h = self.wallet.sendtx( tx ) return h
def mine_block(self): """Create a new block and add open transactions to it.""" # Fetch the currently last block of the blockchain if self.public_key is None: return None last_my_turn = 0 for block in self.__chain[::-1]: last_my_turn += 1 if block.transactions: t = block.transactions[-1] if t.recipient == self.public_key: break if len(self.chain) > len(self.__peer_nodes) >= last_my_turn: return "NOT_TURN" last_block = self.__chain[-1] # Hash the last block (=> to be able to compare it to the stored hash # value) hashed_block = hash_block(last_block) proof = self.proof_of_work() # Miners should be rewarded, so let's create a reward transaction # reward_transaction = { # 'sender': 'MINING', # 'recipient': owner, # 'amount': MINING_REWARD # } reward_transaction = Transaction( 'MINING', self.public_key, '', MINING_REWARD) # Copy transaction instead of manipulating the original # open_transactions list # This ensures that if for some reason the mining should fail, # we don't have the reward transaction stored in the open transactions copied_transactions = self.__open_transactions[:] for tx in copied_transactions: if not Wallet.verify_transaction(tx): return None copied_transactions.append(reward_transaction) block = Block(len(self.__chain), hashed_block, copied_transactions, proof) consensus = 0 for node in self.__peer_nodes: url = 'http://{}/broadcast-block'.format(node) converted_block = block.__dict__.copy() converted_block['transactions'] = [ tx.__dict__ for tx in converted_block['transactions']] try: response = requests.post(url, json={'block': converted_block}) if response.status_code == 400 or response.status_code == 500: print('Block declined, needs resolving') if response.status_code == 409: self.resolve_conflicts = True if response.status_code in [201, 200]: consensus += 1 except requests.exceptions.ConnectionError: continue if consensus < len(self.__peer_nodes) / 2: return "Consensus Failed" self.__chain.append(block) self.__open_transactions = [] self.save_data() return block
import sys from transaction import Transaction if __name__ == '__main__': transaction = Transaction(None) line = sys.stdin.readline().rstrip() while line != "END": try: args = line.split() if args[0] == "SET": key = args[1] value = args[2] transaction.set(key, value) elif args[0] == "GET": key = args[1] value = transaction.get(key) print value if value != None else "NULL" elif args[0] == "UNSET": key = args[1] transaction.unset(key) elif args[0] == "NUMEQUALTO": value = args[1] print str(transaction.numEqualTo(value)) elif args[0] == "BEGIN": transaction = transaction.createChild() elif args[0] == "COMMIT": transaction = transaction.commit({}) elif args[0] == "ROLLBACK": if transaction.parent != None: transaction = transaction.parent
def test_make_transaction(wallet: Wallet, output_public_key : str, amount: float): transaction = Transaction(input=wallet.get_public_key_in_bytes(), outputs=[output_public_key,amount]) signed_transaction = wallet.sign(transaction) assert signed_transaction.is_signed_correctly()
def append_transaction(self, transaction): self.current_block.transactions.append( Transaction(transaction['sender'], transaction['destination'], transaction['contents'], transaction['signature'], transaction['contents_hash'], time.time()))
#!/usr/bin/env python from influxdb import InfluxDBClient from subprocess import check_output from datetime import datetime import asyncio import websockets import traceback import json import time from transaction import Transaction clients = set() transaction = Transaction() async def run_async_function_with_interval(function, parameter, interval): while True: try: await function(parameter) except Exception: print(traceback.format_exc()) finally: await asyncio.sleep(1) async def get_and_send_data(data_function): data = await data_function() await send_to_clients(data)
def create_transaction(self, sender, receiver, amount): #remember to broadcast it timestamp = datetime.datetime.fromtimestamp( time.time()).strftime('%Y-%m-%d %H:%M:%S') return Transaction(timestamp, sender, receiver, amount)
def deserialize(self, tx): """Deserialize a serialized transaction""" return Transaction(tx).deserialize()
def deserialize(self, tx): """Deserialize a serialized transaction""" tx = Transaction(tx) return tx.deserialize()
def add_transaction(self, sender, receiver, amount): transaction = Transaction(sender, receiver, amount, time.time()) self.tx_pool.append(transaction)