def _add_transaction_stub(txn_model: transaction_model.TransactionModel) -> None: """store a stub for a transaction in the index to prevent 404s from getting immediately after posting Args: txn_model: txn model to stub in the index """ redisearch.put_document(txn_model.txn_type, txn_model.txn_id, txn_model.export_as_search_index(stub=True)) redisearch.put_document(redisearch.Indexes.transaction.value, f"txn-{txn_model.txn_id}", {"block_id": "0"})
def store_full_txns(block_model: "l1_block_model.L1BlockModel") -> None: """ Store the transactions object as a single file per block in storage. Also updates the indexes for each indexed transaction in ES with block information. """ _log.info("[TRANSACTION DAO] Putting transaction to storage") storage.put(f"{FOLDER}/{block_model.block_id}", block_model.export_as_full_transactions().encode("utf-8")) # Could optimize by grouping indexing of transactions in the block with matchking txn_types using redisearch.put_many_documents for txn in block_model.transactions: redisearch.put_document(redisearch.Indexes.transaction.value, f"txn-{txn.txn_id}", {"block_id": txn.block_id}, upsert=True) try: redisearch.put_document(txn.txn_type, txn.txn_id, txn.export_as_search_index(), upsert=True) except redis.exceptions.ResponseError as e: # If the index doesn't exist, we don't care that we couldn't place the index (transaction type is probably deleted) if str(e) != "Unknown index name": raise else: _log.warning( f"Txn type {txn.txn_type} for txn {txn.txn_id} failed to index. (Transaction type may simply be deleted?) Ignoring" )
def test_put_document_success(self): mock_add_document = MagicMock() redisearch._get_redisearch_index_client = MagicMock( return_value=MagicMock(add_document=mock_add_document)) redisearch.put_document(index="banana", doc_name="doc1", fields={"fruit": "apple"}) mock_add_document.assert_called_once_with("doc1", replace=False, fruit="apple", partial=False)
def insert_block(block: "model.BlockModel") -> None: """ Insert new block into blockchain and ref to block's hash """ # Create ref to this block for the next block last_block_ref = {"block_id": block.block_id, "proof": block.proof} # Upload stripped block redisearch.put_document(redisearch.Indexes.block.value, block.block_id, block.export_as_search_index()) storage.put_object_as_json(f"{FOLDER}/{block.block_id}", block.export_as_at_rest()) # Upload ref storage.put_object_as_json(f"{FOLDER}/{LAST_CLOSED_KEY}", last_block_ref)
def broadcast_to_public_chain(l5_block: l5_block_model.L5BlockModel) -> None: _log.info("[L5] Preparing to broadcast") # Hash the block and publish the block to a public network public_hash = keys.get_my_keys().hash_l5_for_public_broadcast(l5_block) transaction_hash = _interchain_client.publish_l5_hash_to_public_network( public_hash) _log.info( "[L5] After Publish to public network, setting new broadcast time") _log.info(f"[L5] transaction_hash {transaction_hash}") # Append transaction hash to list, add network and last block sent at l5_block.transaction_hash += [transaction_hash] l5_block.block_last_sent_at = _interchain_client.get_current_block() l5_block.network = INTERCHAIN_NETWORK storage_key = f"BLOCK/{l5_block.block_id}" _log.info(f"[L5] Adding to storage at {storage_key} and creating index") storage.put_object_as_json(storage_key, l5_block.export_as_at_rest()) redisearch.put_document(redisearch.Indexes.block.value, l5_block.block_id, l5_block.export_as_search_index())
def add_smart_contract_index( contract: smart_contract_model.SmartContractModel) -> None: """Add the index for a smart contract""" redisearch.put_document(redisearch.Indexes.smartcontract.value, contract.id, {"sc_name": contract.txn_type}, upsert=True)