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

Add error message on system contract #1494

Merged
merged 10 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
12 changes: 6 additions & 6 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ path = "cmd/soroban-cli"
[workspace.dependencies.soroban-rpc]
package = "stellar-rpc-client"
version = "21.4.0"
git = "https://github.com/stellar/rs-stellar-rpc-client"
rev = "495001a0118802fa807714c3ef25f5893bc6a0bb"
Ifropc marked this conversation as resolved.
Show resolved Hide resolved

[workspace.dependencies.stellar-xdr]
version = "21.2.0"
Expand Down
21 changes: 20 additions & 1 deletion cmd/soroban-cli/src/commands/contract/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ use std::str::FromStr;
use std::{fmt::Debug, fs, io};

use clap::{arg, command, Parser};
use stellar_xdr::curr::{ContractDataEntry, ContractExecutable, ScVal};

use crate::commands::contract::fetch::Error::{ContractIsStellarAsset, UnexpectedContractToken};
use crate::commands::{global, NetworkRunnable};
use crate::config::{
self, locator,
network::{self, Network},
};
use crate::utils::rpc::get_remote_wasm_from_hash;
use crate::{
rpc::{self, Client},
Pwd,
Expand Down Expand Up @@ -64,6 +67,13 @@ pub enum Error {
Network(#[from] network::Error),
#[error("cannot create contract directory for {0:?}")]
CannotCreateContractDir(PathBuf),
#[error("unexpected contract data {0:?}")]
UnexpectedContractToken(ContractDataEntry),
#[error(
"cannot fetch wasm for contract because the contract is \
a network built-in asset contract that does not have a downloadable code binary"
)]
ContractIsStellarAsset(),
}

impl From<Infallible> for Error {
Expand Down Expand Up @@ -126,6 +136,15 @@ impl NetworkRunnable for Cmd {
client
.verify_network_passphrase(Some(&network.network_passphrase))
.await?;
Ok(client.get_remote_wasm(&contract_id).await?)
let data_entry = client.get_contract_data(&contract_id).await?;
if let ScVal::ContractInstance(contract) = &data_entry.val {
return match &contract.executable {
ContractExecutable::Wasm(hash) => {
Ok(get_remote_wasm_from_hash(&client, &hash).await?)
}
ContractExecutable::StellarAsset => Err(ContractIsStellarAsset()),
};
}
return Err(UnexpectedContractToken(data_entry));
}
}
3 changes: 2 additions & 1 deletion cmd/soroban-cli/src/get_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub use soroban_spec_tools::contract as contract_spec;
use crate::commands::global;
use crate::config::{self, data, locator, network};
use crate::rpc;
use crate::utils::rpc::get_remote_wasm_from_hash;

#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand Down Expand Up @@ -65,7 +66,7 @@ pub async fn get_remote_contract_spec(
if let Ok(entries) = data::read_spec(&hash_str) {
entries
} else {
let raw_wasm = client.get_remote_wasm_from_hash(hash).await?;
let raw_wasm = get_remote_wasm_from_hash(&client, &hash).await?;
let res = contract_spec::Spec::new(&raw_wasm)?;
let res = res.spec;
if global_args.map_or(true, |a| !a.no_cache) {
Expand Down
26 changes: 26 additions & 0 deletions cmd/soroban-cli/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use ed25519_dalek::Signer;
use sha2::{Digest, Sha256};
use soroban_env_host::xdr;
use stellar_strkey::ed25519::PrivateKey;

use soroban_env_host::xdr::{
Expand All @@ -8,6 +9,8 @@ use soroban_env_host::xdr::{
TransactionSignaturePayload, TransactionSignaturePayloadTaggedTransaction,
TransactionV1Envelope, WriteXdr,
};
use soroban_rpc::{Client, Error};
use stellar_xdr::curr::{LedgerEntryData, LedgerKey, ReadXdr};

pub use soroban_spec_tools::contract as contract_spec;

Expand Down Expand Up @@ -129,6 +132,29 @@ pub fn contract_id_hash_from_asset(
Ok(Hash(Sha256::digest(preimage_xdr).into()))
}

pub mod rpc {
use soroban_env_host::xdr;
use soroban_rpc::{Client, Error};
use stellar_xdr::curr::{Hash, LedgerEntryData, LedgerKey, Limits, ReadXdr};

pub async fn get_remote_wasm_from_hash(client: &Client, hash: &Hash) -> Result<Vec<u8>, Error> {
let code_key = LedgerKey::ContractCode(xdr::LedgerKeyContractCode { hash: hash.clone() });
let contract_data = client.get_ledger_entries(&[code_key]).await?;
let entries = contract_data.entries.unwrap_or_default();
if entries.is_empty() {
return Err(Error::NotFound(
"Contract Code".to_string(),
hex::encode(hash),
));
}
let contract_data_entry = &entries[0];
match LedgerEntryData::from_xdr_base64(&contract_data_entry.xdr, Limits::none())? {
LedgerEntryData::ContractCode(xdr::ContractCodeEntry { code, .. }) => Ok(code.into()),
scval => Err(Error::UnexpectedContractCodeDataType(scval)),
}
}
}

pub mod parsing {

use regex::Regex;
Expand Down
Loading