From 100ed7781a4a9c969bdcd7b010cf5742d1912ed3 Mon Sep 17 00:00:00 2001 From: Ashok Menon Date: Wed, 18 Sep 2024 22:49:53 +0100 Subject: [PATCH] feat(json-rpc): get transaction block raw effects ## Description Add the ability to get transaction effects in BCS form, from JSON-RPC's read path, by passing the `showRawEffects` option. ## Test plan ``` sui$ cargo build --bin sui --features indexer sui$ $SUI start --force-regenesis --with-indexer --with-graphql --with-faucet ``` Then in another session: ``` sui$ $SUI client faucet ``` Find the transaction `$DIGEST` of the faucet transaction, and then fetch it with: ``` curl -LX POST "http://localhost:9000" \ --header 'Content-Type: application/json' \ --data-raw '{ "jsonrpc": "2.0", "method": "sui_getTransactionBlock", "id": 1, "params": ["'$DIGEST'", { "showRawEffects": true }] }' | jq . ``` And corroborate it against the following GraphQL query: ``` query ($digest: String!) { transactionBlock(digest: $digest) { effects { bcs } } } ``` Which can be requested at `localhost:9125`. --- crates/sui-json-rpc/src/read_api.rs | 45 +++++++++++++++++------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/crates/sui-json-rpc/src/read_api.rs b/crates/sui-json-rpc/src/read_api.rs index 809322187d490..4bfc9bfb29f3e 100644 --- a/crates/sui-json-rpc/src/read_api.rs +++ b/crates/sui-json-rpc/src/read_api.rs @@ -1325,28 +1325,36 @@ fn convert_to_response( let mut response = SuiTransactionBlockResponse::new(cache.digest); response.errors = cache.errors; - if opts.show_raw_input && cache.transaction.is_some() { - let sender_signed_data = cache.transaction.as_ref().unwrap().data(); - let raw_tx = bcs::to_bytes(sender_signed_data) - .map_err(|e| anyhow!("Failed to serialize raw transaction with error: {}", e))?; // TODO: is this a client or server error? - response.raw_transaction = raw_tx; - } + if let Some(transaction) = cache.transaction { + if opts.show_raw_input { + response.raw_transaction = bcs::to_bytes(transaction.data()).map_err(|e| { + // TODO: is this a client or server error? + anyhow!("Failed to serialize raw transaction with error: {e}") + })?; + } - if opts.show_input && cache.transaction.is_some() { - let tx_block = - SuiTransactionBlock::try_from(cache.transaction.unwrap().into_data(), module_cache)?; - response.transaction = Some(tx_block); + if opts.show_input { + response.transaction = Some(SuiTransactionBlock::try_from( + transaction.into_data(), + module_cache, + )?); + } } - if opts.show_effects && cache.effects.is_some() { - let effects = cache.effects.unwrap().try_into().map_err(|e| { - anyhow!( + if let Some(effects) = cache.effects { + if opts.show_raw_effects { + response.raw_effects = bcs::to_bytes(&effects).map_err(|e| { // TODO: is this a client or server error? - "Failed to convert transaction block effects with error: {}", - e - ) - })?; - response.effects = Some(effects); + anyhow!("Failed to serialize transaction block effects with error: {e}") + })?; + } + + if opts.show_effects { + response.effects = Some(effects.try_into().map_err(|e| { + // TODO: is this a client or server error? + anyhow!("Failed to convert transaction block effects with error: {e}") + })?); + } } response.checkpoint = cache.checkpoint_seq; @@ -1363,6 +1371,7 @@ fn convert_to_response( if opts.show_object_changes { response.object_changes = cache.object_changes; } + Ok(response) }