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

Handle restorePreamble in cli prepare_and_send helper #892

Closed
wants to merge 7 commits into from
Closed
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
47 changes: 41 additions & 6 deletions cmd/soroban-cli/src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use soroban_env_host::{
xdr::{
self, AccountEntry, AccountId, ContractDataEntry, DiagnosticEvent, Error as XdrError,
LedgerEntryData, LedgerFootprint, LedgerKey, LedgerKeyAccount, PublicKey, ReadXdr,
SorobanAuthorizationEntry, Transaction, TransactionEnvelope, TransactionMeta,
TransactionMetaV3, TransactionResult, TransactionV1Envelope, Uint256, VecM, WriteXdr,
SequenceNumber, SorobanAuthorizationEntry, Transaction, TransactionEnvelope,
TransactionMeta, TransactionMetaV3, TransactionResult, TransactionV1Envelope, Uint256,
VecM, WriteXdr,
},
};
use soroban_sdk::token;
Expand All @@ -27,7 +28,7 @@ use tokio::time::sleep;
use crate::utils::{self, contract_spec};

mod transaction;
use transaction::{assemble, sign_soroban_authorizations};
use transaction::{assemble, build_restore_txn, sign_soroban_authorizations};

const VERSION: Option<&str> = option_env!("CARGO_PKG_VERSION");

Expand Down Expand Up @@ -201,6 +202,17 @@ pub struct SimulateHostFunctionResult {
pub xdr: String,
}

#[derive(serde::Deserialize, serde::Serialize, Debug)]
pub struct SimulateTransactionResponseRestorePreamble {
#[serde(rename = "transactionData")]
pub transaction_data: String,
#[serde(
rename = "minResourceFee",
deserialize_with = "deserialize_number_from_string"
)]
pub min_resource_fee: u32,
}

#[derive(serde::Deserialize, serde::Serialize, Debug)]
pub struct SimulateTransactionResponse {
#[serde(skip_serializing_if = "Option::is_none", default)]
Expand All @@ -222,6 +234,8 @@ pub struct SimulateTransactionResponse {
deserialize_with = "deserialize_number_from_string"
)]
pub latest_ledger: u32,
#[serde(rename = "restorePreamble")]
restore_preamble: Option<SimulateTransactionResponseRestorePreamble>,
}

#[derive(serde::Deserialize, serde::Serialize, Debug)]
Expand Down Expand Up @@ -604,15 +618,24 @@ soroban config identity fund {address} --helper-url <url>"#
&self,
tx: &Transaction,
log_events: Option<LogEvents>,
) -> Result<Transaction, Error> {
) -> Result<
(
Transaction,
Option<SimulateTransactionResponseRestorePreamble>,
),
Error,
> {
tracing::trace!(?tx);
let sim_response = self
.simulate_transaction(&TransactionEnvelope::Tx(TransactionV1Envelope {
tx: tx.clone(),
signatures: VecM::default(),
}))
.await?;
assemble(tx, &sim_response, log_events)
Ok((
assemble(tx, &sim_response, log_events)?,
sim_response.restore_preamble,
))
}

pub async fn prepare_and_send_transaction(
Expand All @@ -624,9 +647,20 @@ soroban config identity fund {address} --helper-url <url>"#
log_events: Option<LogEvents>,
) -> Result<(TransactionResult, TransactionMeta, Vec<DiagnosticEvent>), Error> {
let GetLatestLedgerResponse { sequence, .. } = self.get_latest_ledger().await?;
let unsigned_tx = self
let (mut unsigned_tx, restore_preamble) = self
.prepare_transaction(tx_without_preflight, log_events)
.await?;
if let Some(restore) = restore_preamble {
// Build and submit the restore transaction
self.send_transaction(&utils::sign_transaction(
source_key,
&build_restore_txn(&unsigned_tx, &restore)?,
network_passphrase,
)?)
.await?;
// Increment the original txn's seq_num so it doesn't conflict
unsigned_tx.seq_num = SequenceNumber(unsigned_tx.seq_num.0 + 1);
}
let (part_signed_tx, signed_auth_entries) = sign_soroban_authorizations(
&unsigned_tx,
source_key,
Expand All @@ -640,6 +674,7 @@ soroban config identity fund {address} --helper-url <url>"#
// re-simulate to calculate the new fees
self.prepare_transaction(&part_signed_tx, log_events)
.await?
.0
};
let tx = utils::sign_transaction(source_key, &fee_ready_txn, network_passphrase)?;
self.send_transaction(&tx).await
Expand Down
40 changes: 35 additions & 5 deletions cmd/soroban-cli/src/rpc/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use ed25519_dalek::Signer;
use sha2::{Digest, Sha256};
use soroban_env_host::xdr::{
AccountId, DiagnosticEvent, Hash, HashIdPreimage, HashIdPreimageSorobanAuthorization,
OperationBody, PublicKey, ReadXdr, ScAddress, ScMap, ScSymbol, ScVal,
SorobanAddressCredentials, SorobanAuthorizationEntry, SorobanCredentials,
SorobanTransactionData, Transaction, TransactionExt, Uint256, VecM, WriteXdr,
AccountId, DiagnosticEvent, ExtensionPoint, Hash, HashIdPreimage,
HashIdPreimageSorobanAuthorization, Memo, Operation, OperationBody, Preconditions, PublicKey,
ReadXdr, RestoreFootprintOp, ScAddress, ScMap, ScSymbol, ScVal, SorobanAddressCredentials,
SorobanAuthorizationEntry, SorobanCredentials, SorobanTransactionData, Transaction,
TransactionExt, Uint256, VecM, WriteXdr,
};

use crate::rpc::{Error, LogEvents, SimulateTransactionResponse};
use crate::rpc::{
Error, LogEvents, SimulateTransactionResponse, SimulateTransactionResponseRestorePreamble,
};

// Apply the result of a simulateTransaction onto a transaction envelope, preparing it for
// submission to the network.
Expand Down Expand Up @@ -232,6 +235,30 @@ pub fn sign_soroban_authorization_entry(
Ok(auth)
}

pub fn build_restore_txn(
parent: &Transaction,
restore: &SimulateTransactionResponseRestorePreamble,
) -> Result<Transaction, Error> {
let transaction_data =
SorobanTransactionData::from_xdr_base64(restore.transaction_data.clone())?;
Ok(Transaction {
source_account: parent.source_account.clone(),
fee: parent.fee + restore.min_resource_fee,
seq_num: parent.seq_num.clone(),
cond: Preconditions::None,
memo: Memo::None,
operations: vec![Operation {
source_account: None,
body: OperationBody::RestoreFootprint(RestoreFootprintOp {
ext: ExtensionPoint::V0,
}),
}]
.try_into()
.unwrap(),
ext: TransactionExt::V1(transaction_data),
})
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -299,6 +326,7 @@ mod tests {
mem_bytes: "0".to_string(),
},
latest_ledger: 3,
restore_preamble: None,
}
}

Expand Down Expand Up @@ -416,6 +444,7 @@ mod tests {
mem_bytes: "0".to_string(),
},
latest_ledger: 3,
restore_preamble: None,
},
None,
);
Expand Down Expand Up @@ -443,6 +472,7 @@ mod tests {
mem_bytes: "0".to_string(),
},
latest_ledger: 3,
restore_preamble: None,
},
None,
);
Expand Down