Skip to content

Commit

Permalink
feat!(runtime): Separate prepaid transactions into specific `pallet-g…
Browse files Browse the repository at this point in the history
…ear-voucher` call (#3401)
  • Loading branch information
breathx authored Oct 17, 2023
1 parent 175f69b commit 4d332e7
Show file tree
Hide file tree
Showing 34 changed files with 437 additions and 597 deletions.
10 changes: 5 additions & 5 deletions examples/fungible-token/tests/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async fn send_messages_in_parallel(
api: &GearApi,
batch_size: usize,
treads_number: usize,
messages: &[(ProgramId, Vec<u8>, u64, u128, bool)],
messages: &[(ProgramId, Vec<u8>, u64, u128)],
) -> Result<Vec<MessageId>> {
// TODO: currently have problem with transaction priorities from one user.
// Fix this after loader become a lib #2781
Expand Down Expand Up @@ -190,9 +190,9 @@ async fn stress_test() -> Result<()> {
}

// Converting batch
let batch: Vec<(_, Vec<u8>, u64, _, _)> = batch
let batch: Vec<(_, Vec<u8>, u64, _)> = batch
.iter()
.map(|x| (program_id, x.encode(), MAX_GAS_LIMIT, 0, false))
.map(|x| (program_id, x.encode(), MAX_GAS_LIMIT, 0))
.collect();

// Sending batch
Expand Down Expand Up @@ -256,9 +256,9 @@ async fn stress_transfer() -> Result<()> {
));
}

let messages: Vec<(_, Vec<u8>, u64, _, _)> = actions
let messages: Vec<(_, Vec<u8>, u64, _)> = actions
.into_iter()
.map(|action| (program_id, action.encode(), MAX_GAS_LIMIT, 0, false))
.map(|action| (program_id, action.encode(), MAX_GAS_LIMIT, 0))
.collect();

let message_ids = send_messages_in_parallel(&api, BATCH_CHUNK_SIZE, 1, &messages)
Expand Down
5 changes: 0 additions & 5 deletions gcli/src/cmd/reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use gsdk::signer::Signer;
/// - `payload`: data expected by the original sender.
/// - `gas_limit`: maximum amount of gas the program can spend before it is halted.
/// - `value`: balance to be transferred to the program once it's been created.
/// - `prepaid`: a flag indicating whether the tx fee and gas are paid from a voucher.
///
/// - `DispatchMessageEnqueued(H256)` when dispatch message is placed in the queue.
#[derive(Parser, Debug)]
Expand All @@ -47,9 +46,6 @@ pub struct Reply {
/// Reply value
#[arg(short, long, default_value = "0")]
value: u128,
/// Use pre-issued voucher
#[arg(long)]
pub prepaid: bool,
}

impl Reply {
Expand All @@ -63,7 +59,6 @@ impl Reply {
self.payload.to_vec()?,
self.gas_limit,
self.value,
self.prepaid,
)
.await?;

Expand Down
5 changes: 0 additions & 5 deletions gcli/src/cmd/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ use gsdk::signer::Signer;
/// - `payload`: in case of a program destination, parameters of the `handle` function.
/// - `gas_limit`: maximum amount of gas the program can spend before it is halted.
/// - `value`: balance to be transferred to the program once it's been created.
/// - `prepaid`: a flag indicating whether the tx fee and gas are paid from a voucher.
///
/// Emits the following events:
/// - `DispatchMessageEnqueued(MessageInfo)` when dispatch message is placed in the queue.
Expand All @@ -52,9 +51,6 @@ pub struct Send {
/// Send value
#[arg(short, long, default_value = "0")]
pub value: u128,
/// Use pre-issued voucher
#[arg(long)]
pub prepaid: bool,
}

impl Send {
Expand All @@ -66,7 +62,6 @@ impl Send {
self.payload.to_vec()?,
self.gas_limit,
self.value,
self.prepaid,
)
.await?;

Expand Down
28 changes: 9 additions & 19 deletions gclient/src/api/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,14 +723,13 @@ impl GearApi {
payload: impl AsRef<[u8]>,
gas_limit: u64,
value: u128,
prepaid: bool,
) -> Result<(MessageId, H256)> {
let payload = payload.as_ref().to_vec();

let tx = self
.0
.calls
.send_message(destination, payload, gas_limit, value, prepaid)
.send_message(destination, payload, gas_limit, value)
.await?;

for event in tx.wait_for_success().await?.iter() {
Expand All @@ -756,17 +755,16 @@ impl GearApi {
/// to one program.
pub async fn send_message_bytes_batch(
&self,
args: impl IntoIterator<Item = (ProgramId, impl AsRef<[u8]>, u64, u128, bool)>,
args: impl IntoIterator<Item = (ProgramId, impl AsRef<[u8]>, u64, u128)>,
) -> Result<(Vec<Result<(MessageId, ProgramId)>>, H256)> {
let calls: Vec<_> = args
.into_iter()
.map(|(destination, payload, gas_limit, value, prepaid)| {
.map(|(destination, payload, gas_limit, value)| {
RuntimeCall::Gear(GearCall::send_message {
destination: destination.into(),
payload: payload.as_ref().to_vec(),
gas_limit,
value,
prepaid,
})
})
.collect();
Expand Down Expand Up @@ -806,9 +804,8 @@ impl GearApi {
payload: impl Encode,
gas_limit: u64,
value: u128,
prepaid: bool,
) -> Result<(MessageId, H256)> {
self.send_message_bytes(destination, payload.encode(), gas_limit, value, prepaid)
self.send_message_bytes(destination, payload.encode(), gas_limit, value)
.await
}

Expand Down Expand Up @@ -837,7 +834,6 @@ impl GearApi {
payload: impl AsRef<[u8]>,
gas_limit: u64,
value: u128,
prepaid: bool,
) -> Result<(MessageId, u128, H256)> {
let payload = payload.as_ref().to_vec();

Expand All @@ -846,7 +842,7 @@ impl GearApi {
let tx = self
.0
.calls
.send_reply(reply_to_id, payload, gas_limit, value, prepaid)
.send_reply(reply_to_id, payload, gas_limit, value)
.await?;

let events = tx.wait_for_success().await?;
Expand Down Expand Up @@ -879,13 +875,9 @@ impl GearApi {
/// program id is also returned in the resulting tuple.
pub async fn send_reply_bytes_batch(
&self,
args: impl IntoIterator<Item = (MessageId, impl AsRef<[u8]>, u64, u128, bool)> + Clone,
args: impl IntoIterator<Item = (MessageId, impl AsRef<[u8]>, u64, u128)> + Clone,
) -> Result<(Vec<Result<(MessageId, ProgramId, u128)>>, H256)> {
let message_ids: Vec<_> = args
.clone()
.into_iter()
.map(|(mid, _, _, _, _)| mid)
.collect();
let message_ids: Vec<_> = args.clone().into_iter().map(|(mid, _, _, _)| mid).collect();

let messages = futures::future::try_join_all(
message_ids.iter().map(|mid| self.get_mailbox_message(*mid)),
Expand All @@ -900,13 +892,12 @@ impl GearApi {

let calls: Vec<_> = args
.into_iter()
.map(|(reply_to_id, payload, gas_limit, value, prepaid)| {
.map(|(reply_to_id, payload, gas_limit, value)| {
RuntimeCall::Gear(GearCall::send_reply {
reply_to_id: reply_to_id.into(),
payload: payload.as_ref().to_vec(),
gas_limit,
value,
prepaid,
})
})
.collect();
Expand Down Expand Up @@ -952,9 +943,8 @@ impl GearApi {
payload: impl Encode,
gas_limit: u64,
value: u128,
prepaid: bool,
) -> Result<(MessageId, u128, H256)> {
self.send_reply_bytes(reply_to_id, payload.encode(), gas_limit, value, prepaid)
self.send_reply_bytes(reply_to_id, payload.encode(), gas_limit, value)
.await
}

Expand Down
2 changes: 1 addition & 1 deletion gclient/tests/memory_dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async fn charge_10(
.calculate_handle_gas(None, program_id, payload.clone(), 0, true)
.await?;
let (message_id, _hash) = api
.send_message_bytes(program_id, payload, gas_info.min_limit, 0, false)
.send_message_bytes(program_id, payload, gas_info.min_limit, 0)
.await?;
assert!(listener.message_processed(message_id).await?.succeed());

Expand Down
2 changes: 0 additions & 2 deletions gclient/tests/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ async fn program_migrated_to_another_node() {
MULTIPLICATOR_VALUE_PAYLOAD,
dest_node_gas_limit,
0,
false,
)
.await
.expect("Unable to send message to destination program");
Expand Down Expand Up @@ -209,7 +208,6 @@ async fn program_with_gas_reservation_migrated_to_another_node() {
demo_reserve_gas::HandleAction::ReplyFromReservation,
dest_node_gas_limit,
0,
false,
)
.await
.expect("Unable to send message to destination program");
Expand Down
1 change: 0 additions & 1 deletion gclient/tests/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ async fn get_state_request() -> anyhow::Result<()> {
btree::Request::Insert(key, value).encode(),
gas_limit,
0,
false,
)
});

Expand Down
2 changes: 1 addition & 1 deletion gclient/tests/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ async fn get_mailbox() -> anyhow::Result<()> {
.calculate_handle_gas(None, program_id, vec![], 0, true)
.await?;

let messages = vec![(program_id, vec![], gas_info.min_limit * 10, 0, false); 5];
let messages = vec![(program_id, vec![], gas_info.min_limit * 10, 0); 5];

let (messages, _hash) = api.send_message_bytes_batch(messages).await?;

Expand Down
41 changes: 26 additions & 15 deletions gsdk/src/metadata/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2485,18 +2485,11 @@ pub mod runtime_types {
#[doc = "is not a program in uninitialized state. If the opposite holds true,"]
#[doc = "the message is not enqueued for processing."]
#[doc = ""]
#[doc = "If `prepaid` flag is set, the transaction fee and the gas cost will be"]
#[doc = "charged against a `voucher` that must have been issued for the sender"]
#[doc = "in conjunction with the `destination` program. That means that the"]
#[doc = "synthetic account corresponding to the (`AccountId`, `ProgramId`) pair must"]
#[doc = "exist and have sufficient funds in it. Otherwise, the call is invalidated."]
#[doc = ""]
#[doc = "Parameters:"]
#[doc = "- `destination`: the message destination."]
#[doc = "- `payload`: in case of a program destination, parameters of the `handle` function."]
#[doc = "- `gas_limit`: maximum amount of gas the program can spend before it is halted."]
#[doc = "- `value`: balance to be transferred to the program once it's been created."]
#[doc = "- `prepaid`: a flag that indicates whether a voucher should be used."]
#[doc = ""]
#[doc = "Emits the following events:"]
#[doc = "- `DispatchMessageEnqueued(MessageInfo)` when dispatch message is placed in the queue."]
Expand All @@ -2505,7 +2498,6 @@ pub mod runtime_types {
payload: ::std::vec::Vec<::core::primitive::u8>,
gas_limit: ::core::primitive::u64,
value: ::core::primitive::u128,
prepaid: ::core::primitive::bool,
},
#[codec(index = 4)]
#[doc = "Send reply on message in `Mailbox`."]
Expand All @@ -2521,18 +2513,11 @@ pub mod runtime_types {
#[doc = ""]
#[doc = "NOTE: only user who is destination of the message, can claim value"]
#[doc = "or reply on the message from mailbox."]
#[doc = ""]
#[doc = "If `prepaid` flag is set, the transaction fee and the gas cost will be"]
#[doc = "charged against a `voucher` that must have been issued for the sender"]
#[doc = "in conjunction with the mailboxed message source program. That means that the"]
#[doc = "synthetic account corresponding to the (`AccountId`, `ProgramId`) pair must"]
#[doc = "exist and have sufficient funds in it. Otherwise, the call is invalidated."]
send_reply {
reply_to_id: runtime_types::gear_core::ids::MessageId,
payload: ::std::vec::Vec<::core::primitive::u8>,
gas_limit: ::core::primitive::u64,
value: ::core::primitive::u128,
prepaid: ::core::primitive::bool,
},
#[codec(index = 5)]
#[doc = "Claim value from message in `Mailbox`."]
Expand Down Expand Up @@ -3317,6 +3302,13 @@ pub mod runtime_types {
program: runtime_types::gear_core::ids::ProgramId,
value: ::core::primitive::u128,
},
#[codec(index = 1)]
#[doc = "Dispatch allowed with voucher call."]
call {
call: runtime_types::pallet_gear_voucher::PrepaidCall<
::core::primitive::u128,
>,
},
}
#[derive(Debug, crate::gp::Decode, crate::gp::DecodeAsType, crate::gp::Encode)]
#[doc = "\n\t\t\tCustom [dispatch errors](https://docs.substrate.io/main-docs/build/events-errors/)\n\t\t\tof this pallet.\n\t\t\t"]
Expand All @@ -3338,6 +3330,23 @@ pub mod runtime_types {
},
}
}
#[derive(Debug, crate::gp::Decode, crate::gp::DecodeAsType, crate::gp::Encode)]
pub enum PrepaidCall<_0> {
#[codec(index = 0)]
SendMessage {
destination: runtime_types::gear_core::ids::ProgramId,
payload: ::std::vec::Vec<::core::primitive::u8>,
gas_limit: ::core::primitive::u64,
value: _0,
},
#[codec(index = 1)]
SendReply {
reply_to_id: runtime_types::gear_core::ids::MessageId,
payload: ::std::vec::Vec<::core::primitive::u8>,
gas_limit: ::core::primitive::u64,
value: _0,
},
}
}
pub mod pallet_grandpa {
use super::runtime_types;
Expand Down Expand Up @@ -9455,12 +9464,14 @@ pub mod calls {
#[doc = "Calls of pallet `GearVoucher`."]
pub enum GearVoucherCall {
Issue,
Call,
}
impl CallInfo for GearVoucherCall {
const PALLET: &'static str = "GearVoucher";
fn call_name(&self) -> &'static str {
match self {
Self::Issue => "issue",
Self::Call => "call",
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions gsdk/src/metadata/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,31 +219,27 @@ fn gear_call_to_scale_value(call: GearCall) -> Value {
payload,
gas_limit,
value,
prepaid,
} => Value::named_variant(
"send_message",
[
("destination", Value::from_bytes(destination.0)),
("payload", Value::from_bytes(payload)),
("gas_limit", Value::u128(gas_limit as u128)),
("value", Value::u128(value as u128)),
("prepaid", Value::bool(prepaid)),
],
),
GearCall::send_reply {
reply_to_id,
payload,
gas_limit,
value,
prepaid,
} => Value::named_variant(
"send_reply",
[
("reply_to_id", Value::from_bytes(reply_to_id.0)),
("payload", Value::from_bytes(payload)),
("gas_limit", Value::u128(gas_limit as u128)),
("value", Value::u128(value as u128)),
("prepaid", Value::bool(prepaid)),
],
),
GearCall::claim_value { message_id } => Value::named_variant(
Expand Down
4 changes: 0 additions & 4 deletions gsdk/src/signer/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ impl SignerCalls {
payload: Vec<u8>,
gas_limit: u64,
value: u128,
prepaid: bool,
) -> Result<TxInBlock> {
self.0
.run_tx(
Expand All @@ -103,7 +102,6 @@ impl SignerCalls {
Value::from_bytes(payload),
Value::u128(gas_limit as u128),
Value::u128(value),
Value::bool(prepaid),
],
)
.await
Expand All @@ -116,7 +114,6 @@ impl SignerCalls {
payload: Vec<u8>,
gas_limit: u64,
value: u128,
prepaid: bool,
) -> Result<TxInBlock> {
self.0
.run_tx(
Expand All @@ -126,7 +123,6 @@ impl SignerCalls {
Value::from_bytes(payload),
Value::u128(gas_limit as u128),
Value::u128(value),
Value::bool(prepaid),
],
)
.await
Expand Down
Loading

0 comments on commit 4d332e7

Please sign in to comment.