Skip to content

Commit

Permalink
feat: added view_receipt_record rpc endpoint (#266)
Browse files Browse the repository at this point in the history
* feat: added `view_receipt_record` rpc endpoint

* chore: removed leftovers

* feat: used appropriate metrics

* feat: added proper response structure

* docs(view_receipt_record): added new custom method
  • Loading branch information
frolvanya authored Jun 3, 2024
1 parent f889e24 commit dacba79
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 10 deletions.
31 changes: 31 additions & 0 deletions docs/CUSTOM_RPC_METHODS.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,34 @@ Next page response:
```
In the last page response `next_page_token` field will be `null`.

# view_receipt_record

The `view_receipt_record` method is a custom method that allows you to view the record of the receipt by its ID.

## How to use it
### Example

Request:
```json
{
"jsonrpc": "2.0",
"id": "dontcare",
"method": "view_receipt_record",
"params": {
"receipt_id": "6aB1XxfnhuQ83FWHb5xyqssGnaD5CUQgxHpbAVJFRrPe"
}
}
```
Response:
```json
{
"id": "dontcare",
"jsonrpc": "2.0",
"result": {
"block_height": 118875440,
"parent_transaction_hash": "6iJgcM5iZrWuhG4ZpUyX6ivtMQUho2S1JRdBYdY7Y7vX",
"receipt_id": "6aB1XxfnhuQ83FWHb5xyqssGnaD5CUQgxHpbAVJFRrPe",
"shard_id": 0
}
}
```
1 change: 1 addition & 0 deletions readnode-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ pub struct QueryData<T: borsh::BorshDeserialize> {
pub block_height: near_indexer_primitives::types::BlockHeight,
pub block_hash: CryptoHash,
}

pub struct ReceiptRecord {
pub receipt_id: CryptoHash,
pub parent_transaction_hash: CryptoHash,
Expand Down
4 changes: 4 additions & 0 deletions rpc-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ async fn main() -> anyhow::Result<()> {
"view_state_paginated",
modules::state::methods::view_state_paginated,
)
.with_method(
"view_receipt_record",
modules::receipts::methods::view_receipt_record,
)
// requests methods
.with_method("query", modules::queries::methods::query)
// basic requests methods
Expand Down
50 changes: 40 additions & 10 deletions rpc-server/src/modules/receipts/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ pub async fn receipt(
Ok(result.map_err(near_jsonrpc::primitives::errors::RpcError::from)?)
}

/// Fetches a receipt record by it's ID
#[cfg_attr(feature = "tracing-instrumentation", tracing::instrument(skip(data)))]
pub async fn view_receipt_record(
data: Data<ServerContext>,
Params(params): Params<serde_json::Value>,
) -> Result<crate::modules::receipts::RpcReceiptRecordResponse, RPCError> {
tracing::debug!("`view_receipt_record` call. Params: {:?}", params);
let receipt_request =
near_jsonrpc::primitives::types::receipts::RpcReceiptRequest::parse(params)?;

let result = fetch_receipt_record(&data, &receipt_request, "view_receipt_record").await;

Ok(result
.map_err(near_jsonrpc::primitives::errors::RpcError::from)?
.into())
}

#[cfg_attr(feature = "tracing-instrumentation", tracing::instrument(skip(data)))]
async fn fetch_receipt(
data: &Data<ServerContext>,
Expand All @@ -41,16 +58,7 @@ async fn fetch_receipt(
> {
let receipt_id = request.receipt_reference.receipt_id;

let receipt_record = data
.db_manager
.get_receipt_by_id(receipt_id, "EXPERIMENTAL_receipt")
.await
.map_err(|err| {
tracing::warn!("Error in `receipt` call: {:?}", err);
near_jsonrpc::primitives::types::receipts::RpcReceiptError::UnknownReceipt {
receipt_id,
}
})?;
let receipt_record = fetch_receipt_record(data, request, "EXPERIMENTAL_receipt").await?;

// Getting the raw Vec<u8> of the TransactionDetails from ScyllaDB
let transaction_details = data
Expand Down Expand Up @@ -85,3 +93,25 @@ async fn fetch_receipt(

Ok(near_jsonrpc::primitives::types::receipts::RpcReceiptResponse { receipt_view })
}

#[cfg_attr(feature = "tracing-instrumentation", tracing::instrument(skip(data)))]
async fn fetch_receipt_record(
data: &Data<ServerContext>,
request: &near_jsonrpc::primitives::types::receipts::RpcReceiptRequest,
method_name: &str,
) -> Result<
readnode_primitives::ReceiptRecord,
near_jsonrpc::primitives::types::receipts::RpcReceiptError,
> {
let receipt_id = request.receipt_reference.receipt_id;

data.db_manager
.get_receipt_by_id(receipt_id, method_name)
.await
.map_err(|err| {
tracing::warn!("Error in `{}` call: {:?}", method_name, err);
near_jsonrpc::primitives::types::receipts::RpcReceiptError::UnknownReceipt {
receipt_id,
}
})
}
19 changes: 19 additions & 0 deletions rpc-server/src/modules/receipts/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
pub mod methods;

#[derive(serde::Serialize, serde::Deserialize, Debug)]
pub struct RpcReceiptRecordResponse {
pub receipt_id: near_indexer_primitives::CryptoHash,
pub parent_transaction_hash: near_indexer_primitives::CryptoHash,
pub block_height: near_indexer_primitives::types::BlockHeight,
pub shard_id: near_indexer_primitives::types::ShardId,
}

impl From<readnode_primitives::ReceiptRecord> for RpcReceiptRecordResponse {
fn from(receipt: readnode_primitives::ReceiptRecord) -> Self {
Self {
receipt_id: receipt.receipt_id,
parent_transaction_hash: receipt.parent_transaction_hash,
block_height: receipt.block_height,
shard_id: receipt.shard_id,
}
}
}

0 comments on commit dacba79

Please sign in to comment.