Skip to content

Commit

Permalink
chore: clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed Jan 25, 2024
1 parent a0309db commit 1b242de
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 11 deletions.
72 changes: 63 additions & 9 deletions cmd/crates/soroban-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ impl TryInto<GetTransactionResponse> for GetTransactionResponseRaw {
}

impl GetTransactionResponse {
///
/// # Errors
pub fn return_value(&self) -> Result<xdr::ScVal, Error> {
if let Some(xdr::TransactionMeta::V3(xdr::TransactionMetaV3 {
soroban_meta: Some(xdr::SorobanTransactionMeta { return_value, .. }),
Expand All @@ -190,6 +192,8 @@ impl GetTransactionResponse {
}
}

///
/// # Errors
pub fn events(&self) -> Result<Vec<DiagnosticEvent>, Error> {
if let Some(meta) = self.result_meta.as_ref() {
Ok(extract_events(meta))
Expand All @@ -198,6 +202,8 @@ impl GetTransactionResponse {
}
}

///
/// # Errors
pub fn contract_events(&self) -> Result<Vec<DiagnosticEvent>, Error> {
Ok(self
.events()?
Expand Down Expand Up @@ -310,6 +316,8 @@ pub struct SimulateTransactionResponse {
}

impl SimulateTransactionResponse {
///
/// # Errors
pub fn results(&self) -> Result<Vec<SimulateHostFunctionResult>, Error> {
self.results
.iter()
Expand All @@ -331,13 +339,17 @@ impl SimulateTransactionResponse {
.collect()
}

///
/// # Errors
pub fn events(&self) -> Result<Vec<DiagnosticEvent>, Error> {
self.events
.iter()
.map(|e| Ok(DiagnosticEvent::from_xdr_base64(e, Limits::none())?))
.collect()
}

///
/// # Errors
pub fn transaction_data(&self) -> Result<SorobanTransactionData, Error> {
Ok(SorobanTransactionData::from_xdr_base64(
&self.transaction_data,
Expand Down Expand Up @@ -434,10 +446,13 @@ impl Display for Event {
}

impl Event {
///
/// # Errors
pub fn parse_cursor(&self) -> Result<(u64, i32), Error> {
parse_cursor(&self.id)
}

///
/// # Errors
pub fn pretty_print(&self) -> Result<(), Box<dyn std::error::Error>> {
let mut stdout = StandardStream::stdout(ColorChoice::Auto);
if !stdout.supports_color() {
Expand Down Expand Up @@ -539,6 +554,8 @@ pub struct Client {
}

impl Client {
///
/// # Errors
pub fn new(base_url: &str) -> Result<Self, Error> {
// Add the port to the base URL if there is no port explicitly included
// in the URL and the scheme allows us to infer a default port.
Expand Down Expand Up @@ -569,6 +586,8 @@ impl Client {
})
}

///
/// # Errors
fn client(&self) -> Result<HttpClient, Error> {
let url = self.base_url.clone();
let mut headers = HeaderMap::new();
Expand All @@ -580,6 +599,8 @@ impl Client {
.build(url)?)
}

///
/// # Errors
pub async fn friendbot_url(&self) -> Result<String, Error> {
let network = self.get_network().await?;
tracing::trace!("{network:#?}");
Expand All @@ -590,7 +611,8 @@ impl Client {
)
})
}

///
/// # Errors
pub async fn verify_network_passphrase(&self, expected: Option<&str>) -> Result<String, Error> {
let server = self.get_network().await?.passphrase;
if let Some(expected) = expected {
Expand All @@ -604,11 +626,15 @@ impl Client {
Ok(server)
}

///
/// # Errors
pub async fn get_network(&self) -> Result<GetNetworkResponse, Error> {
tracing::trace!("Getting network");
Ok(self.client()?.request("getNetwork", rpc_params![]).await?)
}

///
/// # Errors
pub async fn get_latest_ledger(&self) -> Result<GetLatestLedgerResponse, Error> {
tracing::trace!("Getting latest ledger");
Ok(self
Expand All @@ -617,6 +643,8 @@ impl Client {
.await?)
}

///
/// # Errors
pub async fn get_account(&self, address: &str) -> Result<AccountEntry, Error> {
tracing::trace!("Getting address {}", address);
let key = LedgerKey::Account(LedgerKeyAccount {
Expand Down Expand Up @@ -648,6 +676,8 @@ soroban config identity fund {address} --helper-url <url>"#
}
}

///
/// # Errors
pub async fn send_transaction(
&self,
tx: &TransactionEnvelope,
Expand Down Expand Up @@ -718,6 +748,8 @@ soroban config identity fund {address} --helper-url <url>"#
}
}

///
/// # Errors
pub async fn simulate_transaction(
&self,
tx: &TransactionEnvelope,
Expand All @@ -740,6 +772,8 @@ soroban config identity fund {address} --helper-url <url>"#
}
}

///
/// # Errors
pub async fn send_assembled_transaction(
&self,
txn: txn::Assembled,
Expand All @@ -761,6 +795,8 @@ soroban config identity fund {address} --helper-url <url>"#
self.send_transaction(&tx).await
}

///
/// # Errors
pub async fn prepare_and_send_transaction(
&self,
tx_without_preflight: &Transaction,
Expand All @@ -782,20 +818,26 @@ soroban config identity fund {address} --helper-url <url>"#
.await
}

///
/// # Errors
pub async fn create_assembled_transaction(
&self,
txn: &Transaction,
) -> Result<txn::Assembled, Error> {
txn::Assembled::new(txn, self).await
}

///
/// # Errors
pub async fn get_transaction(&self, tx_id: &str) -> Result<GetTransactionResponseRaw, Error> {
Ok(self
.client()?
.request("getTransaction", rpc_params![tx_id])
.await?)
}

///
/// # Errors
pub async fn get_ledger_entries(
&self,
keys: &[LedgerKey],
Expand All @@ -806,14 +848,16 @@ soroban config identity fund {address} --helper-url <url>"#
if base64_result.is_err() {
return Err(Error::Xdr(XdrError::Invalid));
}
base64_keys.push(k.to_xdr_base64(Limits::none()).unwrap());
base64_keys.push(k.to_xdr_base64(Limits::none())?);
}
Ok(self
.client()?
.request("getLedgerEntries", rpc_params![base64_keys])
.await?)
}

///
/// # Errors
pub async fn get_full_ledger_entries(
&self,
ledger_keys: &[LedgerKey],
Expand Down Expand Up @@ -854,7 +898,8 @@ soroban config identity fund {address} --helper-url <url>"#
latest_ledger,
})
}

///
/// # Errors
pub async fn get_events(
&self,
start: EventStart,
Expand Down Expand Up @@ -894,6 +939,8 @@ soroban config identity fund {address} --helper-url <url>"#
Ok(self.client()?.request("getEvents", oparams).await?)
}

///
/// # Errors
pub async fn get_contract_data(
&self,
contract_id: &[u8; 32],
Expand All @@ -917,6 +964,8 @@ soroban config identity fund {address} --helper-url <url>"#
}
}

///
/// # Errors
pub async fn get_remote_wasm(&self, contract_id: &[u8; 32]) -> Result<Vec<u8>, Error> {
match self.get_contract_data(contract_id).await? {
xdr::ContractDataEntry {
Expand All @@ -931,6 +980,8 @@ soroban config identity fund {address} --helper-url <url>"#
}
}

///
/// # Errors
pub async fn get_remote_wasm_from_hash(&self, hash: xdr::Hash) -> Result<Vec<u8>, Error> {
let code_key = LedgerKey::ContractCode(xdr::LedgerKeyContractCode { hash: hash.clone() });
let contract_data = self.get_ledger_entries(&[code_key]).await?;
Expand All @@ -947,7 +998,8 @@ soroban config identity fund {address} --helper-url <url>"#
scval => Err(Error::UnexpectedContractCodeDataType(scval)),
}
}

///
/// # Errors
pub async fn get_remote_contract_spec(
&self,
contract_id: &[u8; 32],
Expand All @@ -957,9 +1009,11 @@ soroban config identity fund {address} --helper-url <url>"#
xdr::ScVal::ContractInstance(xdr::ScContractInstance {
executable: xdr::ContractExecutable::Wasm(hash),
..
}) => Ok(contract::Spec::new(&self.get_remote_wasm_from_hash(hash).await?)
.map_err(Error::CouldNotParseContractSpec)?
.spec),
}) => Ok(
contract::Spec::new(&self.get_remote_wasm_from_hash(hash).await?)
.map_err(Error::CouldNotParseContractSpec)?
.spec,
),
xdr::ScVal::ContractInstance(xdr::ScContractInstance {
executable: xdr::ContractExecutable::StellarAsset,
..
Expand Down Expand Up @@ -996,7 +1050,7 @@ fn extract_events(tx_meta: &TransactionMeta) -> Vec<DiagnosticEvent> {
}
}

pub fn parse_cursor(c: &str) -> Result<(u64, i32), Error> {
pub(crate) fn parse_cursor(c: &str) -> Result<(u64, i32), Error> {
let (toid_part, event_index) = c.split('-').collect_tuple().ok_or(Error::InvalidCursor)?;
let toid_part: u64 = toid_part.parse().map_err(|_| Error::InvalidCursor)?;
let start_index: i32 = event_index.parse().map_err(|_| Error::InvalidCursor)?;
Expand Down
23 changes: 21 additions & 2 deletions cmd/crates/soroban-rpc/src/txn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ pub struct Assembled {
}

impl Assembled {
///
/// # Errors
pub async fn new(txn: &Transaction, client: &Client) -> Result<Self, Error> {
let sim_res = Self::simulate(txn, client).await?;
let txn = assemble(txn, &sim_res)?;
Ok(Self { txn, sim_res })
}

///
/// # Errors
pub fn hash(&self, network_passphrase: &str) -> Result<[u8; 32], xdr::Error> {
let signature_payload = TransactionSignaturePayload {
network_id: Hash(Sha256::digest(network_passphrase).into()),
Expand All @@ -35,6 +39,8 @@ impl Assembled {
Ok(Sha256::digest(signature_payload.to_xdr(Limits::none())?).into())
}

///
/// # Errors
pub fn sign(
self,
key: &ed25519_dalek::SigningKey,
Expand All @@ -55,6 +61,8 @@ impl Assembled {
}))
}

///
/// # Errors
pub async fn simulate(
tx: &Transaction,
client: &Client,
Expand All @@ -67,6 +75,8 @@ impl Assembled {
.await
}

///
/// # Errors
pub async fn handle_restore(
self,
client: &Client,
Expand Down Expand Up @@ -96,6 +106,8 @@ impl Assembled {
&self.sim_res
}

///
/// # Errors
pub async fn authorize(
self,
client: &Client,
Expand Down Expand Up @@ -123,6 +135,8 @@ impl Assembled {
self
}

///
/// # Errors
pub fn auth(&self) -> VecM<SorobanAuthorizationEntry> {
self.txn
.operations
Expand All @@ -138,6 +152,8 @@ impl Assembled {
.unwrap_or_default()
}

///
/// # Errors
pub fn log(
&self,
log_events: Option<LogEvents>,
Expand Down Expand Up @@ -199,6 +215,8 @@ impl Assembled {

// Apply the result of a simulateTransaction onto a transaction envelope, preparing it for
// submission to the network.
///
/// # Errors
pub fn assemble(
raw: &Transaction,
simulation: &SimulateTransactionResponse,
Expand Down Expand Up @@ -422,6 +440,8 @@ fn sign_soroban_authorization_entry(
Ok(auth)
}

///
/// # Errors
pub fn restore(parent: &Transaction, restore: &RestorePreamble) -> Result<Transaction, Error> {
let transaction_data =
SorobanTransactionData::from_xdr_base64(&restore.transaction_data, Limits::none())?;
Expand All @@ -442,8 +462,7 @@ pub fn restore(parent: &Transaction, restore: &RestorePreamble) -> Result<Transa
ext: ExtensionPoint::V0,
}),
}]
.try_into()
.unwrap(),
.try_into()?,
ext: TransactionExt::V1(transaction_data),
})
}
Expand Down

0 comments on commit 1b242de

Please sign in to comment.