Skip to content

Commit

Permalink
wrap api result to struct
Browse files Browse the repository at this point in the history
  • Loading branch information
modship committed Jan 9, 2025
1 parent 06f80f7 commit 1705251
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 22 deletions.
13 changes: 12 additions & 1 deletion massa-api-exports/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl std::fmt::Display for AddressFilter {

/// Datastore keys entry query input structure
#[derive(Clone, Debug, Deserialize)]
pub struct GetAddressDatastoreKeys {
pub struct GetAddressDatastoreKeysRequest {
/// The address for which to get the datastore keys
pub address: Address,
/// The prefix to filter the keys
Expand All @@ -194,3 +194,14 @@ pub struct GetAddressDatastoreKeys {
/// The count of keys to return
pub count: Option<u32>,
}

/// Response object for datastore keys query
#[derive(Clone, Debug, Serialize)]
pub struct GetAddressDatastoreKeysResponse {
/// The address for which to get the datastore keys
pub address: Address,
/// final true means final
pub is_final: bool,
/// keys list
pub keys: Vec<Vec<u8>>,
}
7 changes: 4 additions & 3 deletions massa-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ use jsonrpsee::proc_macros::rpc;
use jsonrpsee::server::middleware::http::HostFilterLayer;
use jsonrpsee::server::{BatchRequestConfig, PingConfig, ServerBuilder, ServerHandle};
use jsonrpsee::RpcModule;
use massa_api_exports::address::GetAddressDatastoreKeysResponse;
use massa_api_exports::execution::{
DeferredCallResponse, DeferredCallsQuoteRequest, DeferredCallsQuoteResponse,
DeferredCallsSlotResponse, Transfer,
};
use massa_api_exports::{
address::{AddressFilter, AddressInfo, GetAddressDatastoreKeys},
address::{AddressFilter, AddressInfo, GetAddressDatastoreKeysRequest},
block::{BlockInfo, BlockSummary},
config::APIConfig,
datastore::{DatastoreEntryInput, DatastoreEntryOutput},
Expand Down Expand Up @@ -430,8 +431,8 @@ pub trait MassaRpc {
#[method(name = "get_address_datastore_keys")]
async fn get_address_datastore_keys(
&self,
arg: Vec<GetAddressDatastoreKeys>,
) -> RpcResult<Vec<Vec<Vec<u8>>>>;
arg: Vec<GetAddressDatastoreKeysRequest>,
) -> RpcResult<Vec<GetAddressDatastoreKeysResponse>>;
}

fn wrong_api<T>() -> RpcResult<T> {
Expand Down
8 changes: 5 additions & 3 deletions massa-api/src/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use crate::{MassaRpcServer, Private, RpcServer, StopHandle, Value, API};
use async_trait::async_trait;
use jsonrpsee::core::{client::Error as JsonRpseeError, RpcResult};
use massa_api_exports::{
address::{AddressFilter, AddressInfo, GetAddressDatastoreKeys},
address::{
AddressFilter, AddressInfo, GetAddressDatastoreKeysRequest, GetAddressDatastoreKeysResponse,
},
block::{BlockInfo, BlockSummary},
config::APIConfig,
datastore::{DatastoreEntryInput, DatastoreEntryOutput},
Expand Down Expand Up @@ -381,8 +383,8 @@ impl MassaRpcServer for API<Private> {

async fn get_address_datastore_keys(
&self,
_arg: Vec<GetAddressDatastoreKeys>,
) -> RpcResult<Vec<Vec<Vec<u8>>>> {
_arg: Vec<GetAddressDatastoreKeysRequest>,
) -> RpcResult<Vec<GetAddressDatastoreKeysResponse>> {
crate::wrong_api::<Vec<_>>()
}
}
Expand Down
25 changes: 17 additions & 8 deletions massa-api/src/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use async_trait::async_trait;
use itertools::{izip, Itertools};
use jsonrpsee::core::{client::Error as JsonRpseeError, RpcResult};
use massa_api_exports::{
address::{AddressFilter, AddressInfo, GetAddressDatastoreKeys},
address::{
AddressFilter, AddressInfo, GetAddressDatastoreKeysRequest, GetAddressDatastoreKeysResponse,
},
block::{BlockInfo, BlockInfoContent, BlockSummary},
config::APIConfig,
datastore::{DatastoreEntryInput, DatastoreEntryOutput},
Expand Down Expand Up @@ -957,8 +959,8 @@ impl MassaRpcServer for API<Public> {

async fn get_address_datastore_keys(
&self,
arg: Vec<GetAddressDatastoreKeys>,
) -> RpcResult<Vec<Vec<Vec<u8>>>> {
arg: Vec<GetAddressDatastoreKeysRequest>,
) -> RpcResult<Vec<GetAddressDatastoreKeysResponse>> {
let requests: Vec<ExecutionQueryRequestItem> = arg
.into_iter()
.map(|request| {
Expand All @@ -968,7 +970,9 @@ impl MassaRpcServer for API<Public> {
)
})
.collect();
let mut result: Vec<Vec<Vec<u8>>> = Vec::with_capacity(requests.len());
// let mut result: Vec<Vec<Vec<u8>>> = Vec::with_capacity(requests.len());
let mut result2: Vec<GetAddressDatastoreKeysResponse> = Vec::with_capacity(requests.len());

let response = self
.0
.execution_controller
Expand All @@ -977,8 +981,13 @@ impl MassaRpcServer for API<Public> {
for response_item in response.responses {
match response_item {
Ok(item) => match item {
ExecutionQueryResponseItem::KeyList(keys) => {
result.push(keys.into_iter().collect());
ExecutionQueryResponseItem::AddressDatastoreKeys(keys, address, is_final) => {
result2.push(GetAddressDatastoreKeysResponse {
address,
is_final,
keys: keys.into_iter().collect(),
});
// result.push(keys.into_iter().collect());
}
_ => {
return Err(
Expand All @@ -992,7 +1001,7 @@ impl MassaRpcServer for API<Public> {
}
}

Ok(result)
Ok(result2)
}

/// get addresses
Expand Down Expand Up @@ -1558,7 +1567,7 @@ fn check_input_operation(

/// Convert GetAddressDatastoreKeys to ExecutionQueryRequestItem
fn from_get_address_datastore_keys(
value: GetAddressDatastoreKeys,
value: GetAddressDatastoreKeysRequest,
max_datastore_query_config: Option<u32>,
) -> ExecutionQueryRequestItem {
// take the minimum of the limit and the max_datastore_query_config if it is set
Expand Down
2 changes: 1 addition & 1 deletion massa-execution-exports/src/mapping_grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ fn to_execution_query_result(
ExecutionQueryResponseItem::DatastoreValue(result) => {
grpc_api::execution_query_response_item::ResponseItem::Bytes(result)
}
ExecutionQueryResponseItem::KeyList(result) => {
ExecutionQueryResponseItem::AddressDatastoreKeys(result, _address, _is_final) => {
grpc_api::execution_query_response_item::ResponseItem::VecBytes(
grpc_model::ArrayOfBytesWrapper {
items: result.into_iter().collect(),
Expand Down
8 changes: 4 additions & 4 deletions massa-execution-exports/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub enum ExecutionQueryRequestItem {
AddressBytecodeCandidate(Address),
/// gets the bytecode (final) of an address, returns ExecutionQueryResponseItem::Bytecode(bytecode) or an error if the address is not found
AddressBytecodeFinal(Address),
/// gets the datastore keys (candidate) of an address, returns ExecutionQueryResponseItem::KeyList(keys) or an error if the address is not found
/// gets the datastore keys (candidate) of an address, returns ExecutionQueryResponseItem::AddressDatastoreKeys(keys, addr, is_final) or an error if the address is not found
AddressDatastoreKeysCandidate {
/// Address for which to query the datastore
address: Address,
Expand All @@ -80,7 +80,7 @@ pub enum ExecutionQueryRequestItem {
/// Maximum number of keys to return
count: Option<u32>,
},
/// gets the datastore keys (final) of an address, returns ExecutionQueryResponseItem::KeyList(keys) or an error if the address is not found
/// gets the datastore keys (final) of an address, returns ExecutionQueryResponseItem::AddressDatastoreKeys(keys, addr, is_final) or an error if the address is not found
AddressDatastoreKeysFinal {
/// Address for which to query the datastore
address: Address,
Expand Down Expand Up @@ -163,8 +163,8 @@ pub enum ExecutionQueryResponseItem {
Bytecode(Bytecode),
/// datastore value
DatastoreValue(Vec<u8>),
/// list of keys
KeyList(BTreeSet<Vec<u8>>),
/// list of keys (keys, address, is_final)
AddressDatastoreKeys(BTreeSet<Vec<u8>>, Address, bool),
/// deferred call quote (target_slot, gas_request, available, price)
DeferredCallQuote(Slot, u64, bool, Amount),
/// deferred call info value
Expand Down
8 changes: 6 additions & 2 deletions massa-execution-worker/src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ impl ExecutionController for ExecutionControllerImpl {
&address, &prefix, start_key, count,
);
match speculative_v {
Some(keys) => Ok(ExecutionQueryResponseItem::KeyList(keys)),
Some(keys) => Ok(ExecutionQueryResponseItem::AddressDatastoreKeys(
keys, address, false,
)),
None => Err(ExecutionQueryError::NotFound(format!(
"Account {}",
address
Expand All @@ -222,7 +224,9 @@ impl ExecutionController for ExecutionControllerImpl {
let final_v = execution_lock
.get_final_datastore_keys(&address, &prefix, start_key, count);
match final_v {
Some(keys) => Ok(ExecutionQueryResponseItem::KeyList(keys)),
Some(keys) => Ok(ExecutionQueryResponseItem::AddressDatastoreKeys(
keys, address, true,
)),
None => Err(ExecutionQueryError::NotFound(format!(
"Account {}",
address
Expand Down

0 comments on commit 1705251

Please sign in to comment.