Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cli][easy] Add serialize-signed-transaction and serialize-unsigned transaction flags for sui client ptb command #16905

Merged
merged 3 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/sui/src/client_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ use crate::key_identity::{get_identity_address, KeyIdentity};
#[cfg(test)]
mod profiler_tests;

#[macro_export]
macro_rules! serialize_or_execute {
($tx_data:expr, $serialize_unsigned:expr, $serialize_signed:expr, $context:expr, $result_variant:ident) => {{
assert!(
Expand Down
6 changes: 6 additions & 0 deletions crates/sui/src/client_ptb/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pub const GAS_BUDGET: &str = "gas-budget";
pub const SUMMARY: &str = "summary";
pub const GAS_COIN: &str = "gas-coin";
pub const JSON: &str = "json";
pub const SERIALIZE_UNSIGNED: &str = "serialize-unsigned-transaction";
pub const SERIALIZE_SIGNED: &str = "serialize-signed-transaction";

// Types
pub const U8: &str = "u8";
Expand Down Expand Up @@ -67,6 +69,8 @@ pub const COMMANDS: &[&str] = &[
SUMMARY,
GAS_COIN,
JSON,
SERIALIZE_UNSIGNED,
SERIALIZE_SIGNED,
];

pub fn is_keyword(s: &str) -> bool {
Expand Down Expand Up @@ -97,6 +101,8 @@ pub struct Program {
pub struct ProgramMetadata {
pub preview_set: bool,
pub summary_set: bool,
pub serialize_unsigned_set: bool,
pub serialize_signed_set: bool,
pub gas_object_id: Option<Spanned<ObjectID>>,
pub json_set: bool,
pub gas_budget: Spanned<u64>,
Expand Down
8 changes: 8 additions & 0 deletions crates/sui/src/client_ptb/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ struct ProgramParsingState {
preview_set: bool,
summary_set: bool,
warn_shadows_set: bool,
serialize_unsigned_set: bool,
serialize_signed_set: bool,
json_set: bool,
gas_object_id: Option<Spanned<ObjectID>>,
gas_budget: Option<Spanned<u64>>,
Expand All @@ -56,6 +58,8 @@ impl<'a, I: Iterator<Item = &'a str>> ProgramParser<'a, I> {
preview_set: false,
summary_set: false,
warn_shadows_set: false,
serialize_unsigned_set: false,
serialize_signed_set: false,
json_set: false,
gas_object_id: None,
gas_budget: None,
Expand Down Expand Up @@ -99,6 +103,8 @@ impl<'a, I: Iterator<Item = &'a str>> ProgramParser<'a, I> {
}

match lexeme {
L(T::Command, A::SERIALIZE_UNSIGNED) => flag!(serialize_unsigned_set),
L(T::Command, A::SERIALIZE_SIGNED) => flag!(serialize_signed_set),
L(T::Command, A::SUMMARY) => flag!(summary_set),
L(T::Command, A::JSON) => flag!(json_set),
L(T::Command, A::PREVIEW) => flag!(preview_set),
Expand Down Expand Up @@ -201,6 +207,8 @@ impl<'a, I: Iterator<Item = &'a str>> ProgramParser<'a, I> {
A::ProgramMetadata {
preview_set: self.state.preview_set,
summary_set: self.state.summary_set,
serialize_unsigned_set: self.state.serialize_unsigned_set,
serialize_signed_set: self.state.serialize_signed_set,
gas_object_id: self.state.gas_object_id,
json_set: self.state.json_set,
gas_budget,
Expand Down
34 changes: 31 additions & 3 deletions crates/sui/src/client_ptb/ptb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use crate::{
client_commands::SuiClientCommandResult,
client_ptb::{
ast::{ParsedProgram, Program},
builder::PTBBuilder,
Expand All @@ -12,6 +13,8 @@ use crate::{
sp,
};

use super::{ast::ProgramMetadata, lexer::Lexer, parser::ProgramParser};
use crate::serialize_or_execute;
use anyhow::{anyhow, Error};
use clap::{arg, Args, ValueHint};
use move_core_types::account_address::AccountAddress;
Expand All @@ -27,11 +30,11 @@ use sui_types::{
digests::TransactionDigest,
gas::GasCostSummary,
quorum_driver_types::ExecuteTransactionRequestType,
transaction::{ProgrammableTransaction, Transaction, TransactionData},
transaction::{
ProgrammableTransaction, SenderSignedData, Transaction, TransactionData, TransactionDataAPI,
},
};

use super::{ast::ProgramMetadata, lexer::Lexer, parser::ProgramParser};

#[derive(Clone, Debug, Args)]
#[clap(disable_help_flag = true)]
pub struct PTB {
Expand Down Expand Up @@ -88,6 +91,10 @@ impl PTB {
Ok(parsed) => parsed,
};

if program_metadata.serialize_unsigned_set && program_metadata.serialize_signed_set {
anyhow::bail!("Cannot serialize both signed and unsigned PTBs");
}

if program_metadata.preview_set {
println!(
"{}",
Expand Down Expand Up @@ -157,6 +164,17 @@ impl PTB {
program_metadata.gas_budget.value,
gas_price,
);

if program_metadata.serialize_unsigned_set {
serialize_or_execute!(tx_data, true, false, context, PTB).print(true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use serialize_or_execute to do more of what's happening below? (Execute the transaction and display it?) It seems like that would help with consistency in general.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can do that once we add the summary flag to other commands. Then I can probably have everything within this macro for PTB.

return Ok(());
}

if program_metadata.serialize_signed_set {
serialize_or_execute!(tx_data, false, true, context, PTB).print(true);
return Ok(());
}

// sign the tx
let signature =
context
Expand Down Expand Up @@ -382,6 +400,16 @@ pub fn ptb_description() -> clap::Command {
--"preview"
"Preview the list of PTB transactions instead of executing them."
))
.arg(arg!(
--"serialize-unsigned-transaction"
"Instead of executing the transaction, serialize the bcs bytes of the unsigned \
transaction data using base64 encoding."
))
.arg(arg!(
--"serialize-signed-transaction"
"Instead of executing the transaction, serialize the bcs bytes of the signed \
transaction data using base64 encoding."
))
.arg(arg!(
--"summary"
"Show only a short summary (digest, execution status, gas cost). \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ expression: parsed
ProgramMetadata {
preview_set: false,
summary_set: false,
serialize_unsigned_set: false,
serialize_signed_set: false,
gas_object_id: None,
json_set: false,
gas_budget: Spanned {
Expand Down Expand Up @@ -62,6 +64,8 @@ expression: parsed
ProgramMetadata {
preview_set: false,
summary_set: false,
serialize_unsigned_set: false,
serialize_signed_set: false,
gas_object_id: None,
json_set: false,
gas_budget: Spanned {
Expand Down Expand Up @@ -106,6 +110,8 @@ expression: parsed
ProgramMetadata {
preview_set: false,
summary_set: false,
serialize_unsigned_set: false,
serialize_signed_set: false,
gas_object_id: None,
json_set: false,
gas_budget: Spanned {
Expand Down Expand Up @@ -169,6 +175,8 @@ expression: parsed
ProgramMetadata {
preview_set: false,
summary_set: false,
serialize_unsigned_set: false,
serialize_signed_set: false,
gas_object_id: None,
json_set: false,
gas_budget: Spanned {
Expand Down Expand Up @@ -223,6 +231,8 @@ expression: parsed
ProgramMetadata {
preview_set: false,
summary_set: false,
serialize_unsigned_set: false,
serialize_signed_set: false,
gas_object_id: None,
json_set: false,
gas_budget: Spanned {
Expand Down Expand Up @@ -292,6 +302,8 @@ expression: parsed
ProgramMetadata {
preview_set: false,
summary_set: false,
serialize_unsigned_set: false,
serialize_signed_set: false,
gas_object_id: None,
json_set: false,
gas_budget: Spanned {
Expand Down Expand Up @@ -355,6 +367,8 @@ expression: parsed
ProgramMetadata {
preview_set: false,
summary_set: false,
serialize_unsigned_set: false,
serialize_signed_set: false,
gas_object_id: None,
json_set: false,
gas_budget: Spanned {
Expand Down Expand Up @@ -409,6 +423,8 @@ expression: parsed
ProgramMetadata {
preview_set: false,
summary_set: false,
serialize_unsigned_set: false,
serialize_signed_set: false,
gas_object_id: None,
json_set: false,
gas_budget: Spanned {
Expand Down Expand Up @@ -472,6 +488,8 @@ expression: parsed
ProgramMetadata {
preview_set: false,
summary_set: false,
serialize_unsigned_set: false,
serialize_signed_set: false,
gas_object_id: None,
json_set: false,
gas_budget: Spanned {
Expand Down Expand Up @@ -526,6 +544,8 @@ expression: parsed
ProgramMetadata {
preview_set: false,
summary_set: false,
serialize_unsigned_set: false,
serialize_signed_set: false,
gas_object_id: None,
json_set: false,
gas_budget: Spanned {
Expand Down Expand Up @@ -568,6 +588,8 @@ expression: parsed
ProgramMetadata {
preview_set: false,
summary_set: false,
serialize_unsigned_set: false,
serialize_signed_set: false,
gas_object_id: None,
json_set: false,
gas_budget: Spanned {
Expand Down Expand Up @@ -629,6 +651,8 @@ expression: parsed
ProgramMetadata {
preview_set: false,
summary_set: false,
serialize_unsigned_set: false,
serialize_signed_set: false,
gas_object_id: None,
json_set: false,
gas_budget: Spanned {
Expand Down Expand Up @@ -737,6 +761,8 @@ expression: parsed
ProgramMetadata {
preview_set: false,
summary_set: false,
serialize_unsigned_set: false,
serialize_signed_set: false,
gas_object_id: None,
json_set: false,
gas_budget: Spanned {
Expand Down Expand Up @@ -822,6 +848,8 @@ expression: parsed
ProgramMetadata {
preview_set: false,
summary_set: false,
serialize_unsigned_set: false,
serialize_signed_set: false,
gas_object_id: None,
json_set: false,
gas_budget: Spanned {
Expand Down Expand Up @@ -907,6 +935,8 @@ expression: parsed
ProgramMetadata {
preview_set: false,
summary_set: false,
serialize_unsigned_set: false,
serialize_signed_set: false,
gas_object_id: None,
json_set: false,
gas_budget: Spanned {
Expand Down Expand Up @@ -943,6 +973,8 @@ expression: parsed
ProgramMetadata {
preview_set: false,
summary_set: false,
serialize_unsigned_set: false,
serialize_signed_set: false,
gas_object_id: None,
json_set: false,
gas_budget: Spanned {
Expand Down Expand Up @@ -1004,6 +1036,8 @@ expression: parsed
ProgramMetadata {
preview_set: false,
summary_set: false,
serialize_unsigned_set: false,
serialize_signed_set: false,
gas_object_id: None,
json_set: false,
gas_budget: Spanned {
Expand Down Expand Up @@ -1050,6 +1084,8 @@ expression: parsed
ProgramMetadata {
preview_set: false,
summary_set: false,
serialize_unsigned_set: false,
serialize_signed_set: false,
gas_object_id: None,
json_set: false,
gas_budget: Spanned {
Expand Down Expand Up @@ -1096,6 +1132,8 @@ expression: parsed
ProgramMetadata {
preview_set: false,
summary_set: false,
serialize_unsigned_set: false,
serialize_signed_set: false,
gas_object_id: None,
json_set: false,
gas_budget: Spanned {
Expand Down Expand Up @@ -1170,6 +1208,8 @@ expression: parsed
ProgramMetadata {
preview_set: false,
summary_set: false,
serialize_unsigned_set: false,
serialize_signed_set: false,
gas_object_id: None,
json_set: false,
gas_budget: Spanned {
Expand Down Expand Up @@ -1222,6 +1262,8 @@ expression: parsed
ProgramMetadata {
preview_set: false,
summary_set: false,
serialize_unsigned_set: false,
serialize_signed_set: false,
gas_object_id: None,
json_set: false,
gas_budget: Spanned {
Expand Down Expand Up @@ -1278,6 +1320,8 @@ expression: parsed
ProgramMetadata {
preview_set: false,
summary_set: false,
serialize_unsigned_set: false,
serialize_signed_set: false,
gas_object_id: None,
json_set: false,
gas_budget: Spanned {
Expand All @@ -1297,6 +1341,8 @@ expression: parsed
ProgramMetadata {
preview_set: false,
summary_set: false,
serialize_unsigned_set: false,
serialize_signed_set: false,
gas_object_id: Some(
Spanned {
span: Span {
Expand Down Expand Up @@ -1324,6 +1370,8 @@ expression: parsed
ProgramMetadata {
preview_set: false,
summary_set: true,
serialize_unsigned_set: false,
serialize_signed_set: false,
gas_object_id: None,
json_set: false,
gas_budget: Spanned {
Expand All @@ -1343,6 +1391,8 @@ expression: parsed
ProgramMetadata {
preview_set: false,
summary_set: false,
serialize_unsigned_set: false,
serialize_signed_set: false,
gas_object_id: None,
json_set: true,
gas_budget: Spanned {
Expand All @@ -1362,6 +1412,8 @@ expression: parsed
ProgramMetadata {
preview_set: true,
summary_set: false,
serialize_unsigned_set: false,
serialize_signed_set: false,
gas_object_id: None,
json_set: false,
gas_budget: Spanned {
Expand All @@ -1381,6 +1433,8 @@ expression: parsed
ProgramMetadata {
preview_set: false,
summary_set: false,
serialize_unsigned_set: false,
serialize_signed_set: false,
gas_object_id: None,
json_set: false,
gas_budget: Spanned {
Expand Down
Loading
Loading