Skip to content

Commit

Permalink
grpc api
Browse files Browse the repository at this point in the history
  • Loading branch information
modship committed Sep 26, 2024
1 parent 4f7524d commit 5b68c00
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 42 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ massa_versioning = { path = "./massa-versioning" }
massa_wallet = { path = "./massa-wallet" }

# Massa projects dependencies
massa-proto-rs = { git = "https://github.com/massalabs/massa-proto-rs", "rev" = "8eb7aa8384a407b588e190965c8e501cebdd38e3" }
massa-sc-runtime = { git = "https://github.com/massalabs/massa-sc-runtime", "rev" = "b582bcd83120cfe980211ea7bcdaf994a784c714" }
massa-proto-rs = { git = "https://github.com/massalabs/massa-proto-rs", "rev" = "a274d8b374f627f16a0113b6bbc974bf5934dcc7" }
massa-sc-runtime = { git = "https://github.com/massalabs/massa-sc-runtime", "rev" = "f51d2472212be10838b2318aade1bc14d49f90d9" }


peernet = { git = "https://github.com/massalabs/PeerNet", "rev" = "04b05ddd320fbe76cc858115af7b5fc28bdb8310" }
Expand Down
2 changes: 1 addition & 1 deletion massa-api-exports/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub struct DeferredCallsQuoteResponse {
/// if the slot is bookable
pub available: bool,
/// the cost for booking the call
pub price: u64,
pub price: Amount,
}

/// response for deferred call
Expand Down
33 changes: 16 additions & 17 deletions massa-api/src/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1160,18 +1160,21 @@ impl MassaRpcServer for API<Public> {
&self,
req: Vec<DeferredCallsQuoteRequest>,
) -> RpcResult<Vec<DeferredCallsQuoteResponse>> {
// TODO limit
if req.len() as u64 > self.0.api_settings.max_arguments {
return Err(ApiError::BadRequest("too many arguments".into()).into());
}

let queries: Vec<ExecutionQueryRequestItem> = req
.into_iter()
.map(|call| {
// add the gas cost of vm allocation
let effective_gas_request = call
.max_gas_request
.saturating_add(self.0.api_settings.deferred_calls_config.call_cst_gas_cost);
ExecutionQueryRequestItem::DeferredCallQuote(
call.target_slot,
effective_gas_request,
)
ExecutionQueryRequestItem::DeferredCallQuote {
target_slot: call.target_slot,
max_gas_request: effective_gas_request,
}
})
.collect();

Expand All @@ -1193,7 +1196,6 @@ impl MassaRpcServer for API<Public> {
available,
price,
}),

Ok(_) => Err(ApiError::InternalServerError(
"unexpected response type".to_string(),
)),
Expand All @@ -1208,14 +1210,9 @@ impl MassaRpcServer for API<Public> {
&self,
arg: Vec<String>,
) -> RpcResult<Vec<DeferredCallResponse>> {
// TODO limit
// if id_str.len() > self.0.api_settings.max_deferred_call_id_length as usize {
// return Err(ApiError::BadRequest(format!(
// "deferred call id too long: {}",
// id_str
// ))
// .into());
// }
if arg.len() as u64 > self.0.api_settings.max_arguments {
return Err(ApiError::BadRequest("too many arguments".into()).into());
}

let requests: Vec<ExecutionQueryRequestItem> = arg
.into_iter()
Expand Down Expand Up @@ -1253,11 +1250,13 @@ impl MassaRpcServer for API<Public> {
&self,
slots: Vec<Slot>,
) -> RpcResult<Vec<DeferredCallsSlotResponse>> {
// TODO limit slot count
if slots.len() as u64 > self.0.api_settings.max_arguments {
return Err(ApiError::BadRequest("too many arguments".into()).into());
}

let requests: Vec<ExecutionQueryRequestItem> = slots
.into_iter()
.map(ExecutionQueryRequestItem::DeferredCallSlotCalls)
.map(ExecutionQueryRequestItem::DeferredCallsBySlot)
.collect();

let mut slot_calls = Vec::new();
Expand All @@ -1270,7 +1269,7 @@ impl MassaRpcServer for API<Public> {
.into_iter()
{
match exec {
Ok(ExecutionQueryResponseItem::DeferredCallSlotCalls(slot, result)) => {
Ok(ExecutionQueryResponseItem::DeferredCallsBySlot(slot, result)) => {
let calls = result
.into_iter()
.map(|(id, call)| DeferredCallResponse {
Expand Down
1 change: 1 addition & 0 deletions massa-deferred-calls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ massa_models = { workspace = true }
massa_serialization = { workspace = true }
serde_with = { workspace = true }
serde_json = { workspace = true }
massa-proto-rs = { workspace = true, "features" = ["tonic"] }

[dev-dependencies]
tempfile = { workspace = true }
Expand Down
17 changes: 17 additions & 0 deletions massa-deferred-calls/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use massa_models::{
serialization::{StringDeserializer, StringSerializer, VecU8Deserializer, VecU8Serializer},
slot::{Slot, SlotDeserializer, SlotSerializer},
};
use massa_proto_rs::massa::api::v1 as grpc_api;
use massa_serialization::{
BoolDeserializer, BoolSerializer, Deserializer, SerializeError, Serializer,
U16VarIntDeserializer, U16VarIntSerializer, U64VarIntDeserializer, U64VarIntSerializer,
Expand Down Expand Up @@ -75,6 +76,22 @@ impl DeferredCall {
}
}

impl From<DeferredCall> for grpc_api::DeferredCallInfoEntry {
fn from(call: DeferredCall) -> Self {
grpc_api::DeferredCallInfoEntry {
sender_address: call.sender_address.to_string(),
target_slot: Some(call.target_slot.into()),
target_address: call.target_address.to_string(),
target_function: call.target_function,
parameters: call.parameters,
coins: Some(call.coins.into()),
max_gas: call.max_gas,
fee: Some(call.fee.into()),
cancelled: call.cancelled,
}
}
}

/// Serializer for `AsyncCall`
#[derive(Clone)]
pub struct DeferredCallSerializer {
Expand Down
63 changes: 59 additions & 4 deletions massa-execution-exports/src/mapping_grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
};
use grpc_api::execution_query_request_item as exec;
use massa_models::address::Address;
use massa_models::deferred_calls::DeferredCallId;
use massa_models::error::ModelsError;
use massa_models::execution::EventFilter;
use massa_models::mapping_grpc::to_denunciation_index;
Expand Down Expand Up @@ -133,6 +134,29 @@ pub fn to_querystate_filter(
let event_filter = to_event_filter(value.filters)?;
Ok(ExecutionQueryRequestItem::Events(event_filter))
}
exec::RequestItem::DeferredCallQuote(value) => {
Ok(ExecutionQueryRequestItem::DeferredCallQuote {
target_slot: value
.target_slot
.ok_or(ModelsError::ErrorRaised(
"target slot is required".to_string(),
))?
.into(),
max_gas_request: value.max_gas_request,
})
}
exec::RequestItem::DeferredCallInfo(info) => {
let id = DeferredCallId::from_str(&info.call_id)?;
Ok(ExecutionQueryRequestItem::DeferredCallInfo(id))
}
exec::RequestItem::DeferredCallsBySlot(value) => {
Ok(ExecutionQueryRequestItem::DeferredCallsBySlot(
value
.slot
.ok_or(ModelsError::ErrorRaised("slot is required".to_string()))?
.into(),
))
}
}
} else {
Err(ModelsError::ErrorRaised("no filter provided".to_string()))
Expand Down Expand Up @@ -265,10 +289,41 @@ fn to_execution_query_result(
},
)
}
ExecutionQueryResponseItem::DeferredCallQuote(_, _, _, _) => todo!(),
ExecutionQueryResponseItem::DeferredCallInfo(_, _) => todo!(),
ExecutionQueryResponseItem::DeferredCallSlotCalls(_deferred_call_id, _deferred_call) => {
todo!()
ExecutionQueryResponseItem::DeferredCallQuote(
target_slot,
max_gas_request,
available,
price,
) => grpc_api::execution_query_response_item::ResponseItem::DeferredCallQuote(
grpc_api::DeferredCallQuoteResponse {
target_slot: Some(target_slot.into()),
max_gas_request,
available,
price: Some(price.into()),
},
),
ExecutionQueryResponseItem::DeferredCallInfo(call_id, call) => {
grpc_api::execution_query_response_item::ResponseItem::DeferredCallInfo(
grpc_api::DeferredCallInfoResponse {
call_id: call_id.to_string(),
call: Some(call.into()),
},
)
}
ExecutionQueryResponseItem::DeferredCallsBySlot(slot, calls) => {
let arr = calls
.into_iter()
.map(|(id, call)| grpc_api::DeferredCallInfoResponse {
call_id: id.to_string(),
call: Some(call.into()),
})
.collect();
grpc_api::execution_query_response_item::ResponseItem::DeferredCallsBySlot(
grpc_api::DeferredCallsBySlotResponse {
slot: Some(slot.into()),
calls: arr,
},
)
}
};

Expand Down
13 changes: 9 additions & 4 deletions massa-execution-exports/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,16 @@ pub enum ExecutionQueryRequestItem {
OpExecutionStatusFinal(OperationId),

/// gets the deferred call quote (candidate) for a slot, returns ExecutionQueryResponseItem::DeferredCallQuote(available, price)
DeferredCallQuote(Slot, u64),
DeferredCallQuote {
/// slot to query
target_slot: Slot,
/// gas request
max_gas_request: u64,
},
/// get info of deferred calls
DeferredCallInfo(DeferredCallId),
/// retrieves the deferred call for given slot
DeferredCallSlotCalls(Slot),
DeferredCallsBySlot(Slot),

/// gets the execution status (candidate) for an denunciation, returns ExecutionQueryResponseItem::ExecutionStatus(status)
DenunciationExecutionStatusCandidate(DenunciationIndex),
Expand Down Expand Up @@ -150,11 +155,11 @@ pub enum ExecutionQueryResponseItem {
/// list of keys
KeyList(BTreeSet<Vec<u8>>),
/// deferred call quote (target_slot, gas_request, available, price)
DeferredCallQuote(Slot, u64, bool, u64),
DeferredCallQuote(Slot, u64, bool, Amount),
/// deferred call info value
DeferredCallInfo(DeferredCallId, DeferredCall),
/// deferred call slot calls value
DeferredCallSlotCalls(Slot, BTreeMap<DeferredCallId, DeferredCall>),
DeferredCallsBySlot(Slot, BTreeMap<DeferredCallId, DeferredCall>),
/// deferred credits value
DeferredCredits(BTreeMap<Slot, Amount>),
/// execution status value
Expand Down
17 changes: 8 additions & 9 deletions massa-execution-worker/src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,13 @@ impl ExecutionController for ExecutionControllerImpl {
execution_lock.get_filtered_sc_output_event(filter),
))
}
ExecutionQueryRequestItem::DeferredCallQuote(slot, max_request_gas) => {
let (target_slot, gas_request, available, price) =
execution_lock.deferred_call_quote(slot, max_request_gas);
ExecutionQueryRequestItem::DeferredCallQuote {
target_slot,
max_gas_request,
} => {
let result = execution_lock.deferred_call_quote(target_slot, max_gas_request);
Ok(ExecutionQueryResponseItem::DeferredCallQuote(
target_slot,
gas_request,
available,
price,
result.0, result.1, result.2, result.3,
))
}
ExecutionQueryRequestItem::DeferredCallInfo(deferred_call_id) => execution_lock
Expand All @@ -352,9 +351,9 @@ impl ExecutionController for ExecutionControllerImpl {
.map(|call| {
ExecutionQueryResponseItem::DeferredCallInfo(deferred_call_id, call)
}),
ExecutionQueryRequestItem::DeferredCallSlotCalls(slot) => {
ExecutionQueryRequestItem::DeferredCallsBySlot(slot) => {
let res = execution_lock.get_deferred_calls_by_slot(slot);
Ok(ExecutionQueryResponseItem::DeferredCallSlotCalls(slot, res))
Ok(ExecutionQueryResponseItem::DeferredCallsBySlot(slot, res))
}
};
resp.responses.push(resp_item);
Expand Down
6 changes: 3 additions & 3 deletions massa-execution-worker/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2442,14 +2442,14 @@ impl ExecutionState {
&self,
target_slot: Slot,
max_request_gas: u64,
) -> (Slot, u64, bool, u64) {
) -> (Slot, u64, bool, Amount) {
let gas_request =
max_request_gas.saturating_add(self.config.deferred_calls_config.call_cst_gas_cost);
let context = context_guard!(self);

match context.deferred_calls_compute_call_fee(target_slot, gas_request, context.slot) {
Ok(fee) => (target_slot, gas_request, true, fee.to_raw()),
Err(_) => (target_slot, gas_request, false, 0),
Ok(fee) => (target_slot, gas_request, true, fee),
Err(_) => (target_slot, gas_request, false, Amount::zero()),
}
}

Expand Down

0 comments on commit 5b68c00

Please sign in to comment.