def get_transaction_by_rid(self, selector, wif=None, bulletin_secret=None, rid=False, raw=False, theirs=False, my=False, public_key=None): # from block import Block # from transaction import Transaction from yadacoin.crypt import Crypt if not rid: ds = bulletin_secret selectors = [ TU.hash(ds + selector), TU.hash(selector + ds) ] else: if not isinstance(selector, list): selectors = [selector, ] else: selectors = selector def txn_gen(): res = self.mongo.db.blocks.find( {"transactions": {"$elemMatch": {"relationship": {"$ne": ""}, "rid": {"$in": selectors}}}}) for x in res: yield x res = self.mongo.db.fastgraph_transactions.find( {"txn": {"$elemMatch": {"relationship": {"$ne": ""}, "rid": {"$in": selectors}}}}) for x in res: yield x for block in txn_gen(): for transaction in block.get('transactions'): if theirs and public_key == transaction['public_key']: continue if my and public_key != transaction['public_key']: continue if not raw: try: cipher = Crypt(wif) decrypted = cipher.decrypt(transaction['relationship']) relationship = json.loads(decrypted.decode('latin1')) transaction['relationship'] = relationship except: continue if 'rid' in transaction and transaction['rid'] in selectors: return transaction
def get_transactions_by_rid(self, selector, bulletin_secret, wif=None, rid=False, raw=False, returnheight=True, lt_block_height=None, requested_rid=False): # selectors is old code before we got an RID by sorting the bulletin secrets # from block import Block # from transaction import Transaction from yadacoin.crypt import Crypt if not rid: ds = bulletin_secret selectors = [ TU.hash(ds + selector), TU.hash(selector + ds) ] else: if not isinstance(selector, list): selectors = [selector, ] else: selectors = selector transactions_by_rid_cache = self.mongo.db.transactions_by_rid_cache.find( { 'raw': raw, 'rid': rid, 'bulletin_secret': bulletin_secret, 'returnheight': returnheight, 'selector': {'$in': selectors}, 'requested_rid': requested_rid } ).sort([('height', -1)]) latest_block = self.config.BU.get_latest_block() transactions = [] if lt_block_height: query = {"transactions.rid": {"$in": selectors}, "transactions": {"$elemMatch": {"relationship": {"$ne": ""}}}, 'index': {'$lte': lt_block_height}} if requested_rid: query["transactions.requested_rid"] = {"$in": selectors} blocks = self.mongo.db.blocks.find(query) else: if transactions_by_rid_cache.count(): transactions_by_rid_cache = transactions_by_rid_cache[0] block_height = transactions_by_rid_cache['height'] else: block_height = 0 query = {"transactions.rid": {"$in": selectors}, "transactions": {"$elemMatch": {"relationship": {"$ne": ""}}}, 'index': {'$gt': block_height}} if requested_rid: query = { "$or": [ { "transactions.rid": { "$in": selectors } }, { "transactions.requested_rid": { "$in": selectors } } ], "transactions": { "$elemMatch": { "relationship": { "$ne": "" } } }, 'index': { '$gt': block_height } } else: query = { "transactions.rid": { "$in": selectors }, "transactions": { "$elemMatch": { "relationship": { "$ne": "" } } }, 'index': { '$gt': block_height } } blocks = self.mongo.db.blocks.find(query) cipher = Crypt(self.config.wif) for block in blocks: for transaction in block.get('transactions'): if 'relationship' in transaction and transaction['relationship']: if returnheight: transaction['height'] = block['index'] if not raw: try: decrypted = cipher.decrypt(transaction['relationship']) relationship = json.loads(decrypted.decode('latin1')) transaction['relationship'] = relationship except: continue for selector in selectors: self.app_log.debug('caching transactions_by_rid at height: {}'.format(block['index'])) self.mongo.db.transactions_by_rid_cache.insert( { 'raw': raw, 'rid': rid, 'bulletin_secret': bulletin_secret, 'returnheight': returnheight, 'selector': selector, 'txn': transaction, 'height': block['index'], 'requested_rid': requested_rid } ) transactions.append(transaction) if not transactions: for selector in selectors: self.mongo.db.transactions_by_rid_cache.insert( { 'raw': raw, 'rid': rid, 'bulletin_secret': bulletin_secret, 'returnheight': returnheight, 'selector': selector, 'height': latest_block['index'], 'requested_rid': requested_rid } ) for ftxn in self.mongo.db.fastgraph_transactions.find({'txn.rid': {'$in': selectors}}): if 'txn' in ftxn: yield ftxn['txn'] last_id = '' for x in self.mongo.db.transactions_by_rid_cache.find({ 'raw': raw, 'rid': rid, 'returnheight': returnheight, 'selector': {'$in': selectors}, 'requested_rid': requested_rid }).sort([('txn.id', 1)]): if 'txn' in x and x['txn']['id'] != last_id: last_id = x['txn']['id'] yield x['txn']