Skip to content

Commit

Permalink
feat: move ContractSpec to spec tools crate
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed Jan 19, 2024
1 parent 6a19e18 commit 1fcc467
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 90 deletions.
1 change: 1 addition & 0 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 cmd/crates/soroban-spec-tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ crate-type = ["rlib"]
soroban-spec = { workspace = true }
stellar-strkey = { workspace = true }
stellar-xdr = { workspace = true, features = ["curr", "std", "serde"] }
soroban-env-host = { workspace = true }

serde_json = { workspace = true }
itertools = { workspace = true }
ethnum = { workspace = true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use soroban_env_host::xdr::{
StringM, WriteXdr,
};

pub struct ContractSpec {
pub struct Spec {
pub env_meta_base64: Option<String>,
pub env_meta: Vec<ScEnvMetaEntry>,
pub meta_base64: Option<String>,
Expand Down Expand Up @@ -38,7 +38,7 @@ pub enum Error {
Parser(#[from] wasmparser::BinaryReaderError),
}

impl ContractSpec {
impl Spec {
pub fn new(bytes: &[u8]) -> Result<Self, Error> {
let mut env_meta: Option<&[u8]> = None;
let mut meta: Option<&[u8]> = None;
Expand Down Expand Up @@ -87,7 +87,7 @@ impl ContractSpec {
vec![]
};

Ok(ContractSpec {
Ok(Spec {
env_meta_base64,
env_meta,
meta_base64,
Expand All @@ -108,7 +108,7 @@ impl ContractSpec {
}
}

impl Display for ContractSpec {
impl Display for Spec {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(env_meta) = &self.env_meta_base64 {
writeln!(f, "Env Meta: {env_meta}")?;
Expand Down
1 change: 1 addition & 0 deletions cmd/crates/soroban-spec-tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use stellar_xdr::curr::{
UInt128Parts, UInt256Parts, Uint256, VecM,
};

pub mod contract;
pub mod utils;

#[derive(thiserror::Error, Debug)]
Expand Down
4 changes: 2 additions & 2 deletions cmd/crates/soroban-test/tests/it/help.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use soroban_cli::commands::contract;
use soroban_test::TestEnv;

use crate::util::{invoke_custom as invoke, CUSTOM_TYPES};
use crate::util::{invoke_custom as invoke, CUSTOM_TYPES, DEFAULT_CONTRACT_ID};

async fn invoke_custom(func: &str, args: &str) -> Result<String, contract::invoke::Error> {
let e = &TestEnv::default();
invoke(e, "1", func, args, &CUSTOM_TYPES.path()).await
invoke(e, DEFAULT_CONTRACT_ID, func, args, &CUSTOM_TYPES.path()).await
}

#[tokio::test]
Expand Down
7 changes: 5 additions & 2 deletions cmd/crates/soroban-test/tests/it/integration/dotenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn current_env_not_overwritten() {
write_env_file(e, &contract_id());

e.new_assert_cmd("contract")
.env("SOROBAN_CONTRACT_ID", "2")
.env("SOROBAN_CONTRACT_ID", "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4")
.arg("invoke")
.arg("--")
.arg("hello")
Expand All @@ -51,7 +51,10 @@ fn cli_args_have_priority() {
deploy_hello(e);
write_env_file(e, &contract_id());
e.new_assert_cmd("contract")
.env("SOROBAN_CONTRACT_ID", "2")
.env(
"SOROBAN_CONTRACT_ID",
"CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4",
)
.arg("invoke")
.arg("--id")
.arg(TEST_CONTRACT_ID)
Expand Down
2 changes: 2 additions & 0 deletions cmd/crates/soroban-test/tests/it/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,5 @@ pub async fn invoke_custom(
i.invoke(&soroban_cli::commands::global::Args::default())
.await
}

pub const DEFAULT_CONTRACT_ID: &str = "CDR6QKTWZQYW6YUJ7UP7XXZRLWQPFRV6SWBLQS4ZQOSAF4BOUD77OO5Z";
16 changes: 7 additions & 9 deletions cmd/soroban-cli/src/commands/contract/bindings/typescript.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use std::{ffi::OsString, fmt::Debug, path::PathBuf};

use clap::{command, Parser};
use soroban_spec_tools::contract as contract_spec;
use soroban_spec_typescript::{self as typescript, boilerplate::Project};

use crate::wasm;
use crate::{
commands::{
config::locator,
contract::{self, fetch},
network::{self, Network},
},
utils::contract_spec::{self, ContractSpec},
use crate::commands::{
config::locator,
contract::{self, fetch},
network::{self, Network},
};
use crate::wasm;

#[derive(Parser, Debug, Clone)]
#[group(skip)]
Expand Down Expand Up @@ -78,7 +76,7 @@ impl Cmd {
network: self.network.clone(),
};
let bytes = fetch.get_bytes().await?;
ContractSpec::new(&bytes)?.spec
contract_spec::Spec::new(&bytes)?.spec
};
if self.output_dir.is_file() {
return Err(Error::IsFile(self.output_dir.clone()));
Expand Down
3 changes: 2 additions & 1 deletion cmd/soroban-cli/src/commands/contract/inspect.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clap::{command, Parser};
use soroban_env_host::xdr;
use soroban_spec_tools::contract;
use std::{fmt::Debug, path::PathBuf};
use tracing::debug;

Expand Down Expand Up @@ -28,7 +29,7 @@ pub enum Error {
#[error(transparent)]
Xdr(#[from] xdr::Error),
#[error(transparent)]
Spec(#[from] crate::utils::contract_spec::Error),
Spec(#[from] contract::Error),
}

impl Cmd {
Expand Down
2 changes: 1 addition & 1 deletion cmd/soroban-cli/src/commands/contract/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl Cmd {
}
}

fn get_contract_meta_sdk_version(wasm_spec: &utils::contract_spec::ContractSpec) -> Option<String> {
fn get_contract_meta_sdk_version(wasm_spec: &soroban_spec_tools::contract::Spec) -> Option<String> {
let rs_sdk_version_option = if let Some(_meta) = &wasm_spec.meta_base64 {
wasm_spec.meta.iter().find(|entry| match entry {
ScMetaEntry::ScMetaV0(ScMetaV0 { key, .. }) => {
Expand Down
15 changes: 5 additions & 10 deletions cmd/soroban-cli/src/commands/contract/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,8 @@ use super::super::{
config::{self, locator},
events,
};
use crate::{
commands::global,
rpc::{self, Client},
utils::{self, contract_spec},
Pwd,
};
use soroban_spec_tools::Spec;
use crate::{commands::global, rpc, Pwd};
use soroban_spec_tools::{contract, Spec};

#[derive(Parser, Debug, Default, Clone)]
#[allow(clippy::struct_excessive_bools)]
Expand Down Expand Up @@ -140,7 +135,7 @@ pub enum Error {
#[error(transparent)]
StrKey(#[from] stellar_strkey::DecodeError),
#[error(transparent)]
ContractSpec(#[from] contract_spec::Error),
ContractSpec(#[from] contract::Error),
#[error("")]
MissingFileArg(PathBuf),
}
Expand Down Expand Up @@ -275,7 +270,7 @@ impl Cmd {
// For testing wasm arg parsing
let _ = self.build_host_function_parameters(contract_id, spec_entries)?;
}
let client = Client::new(&network.rpc_url)?;
let client = rpc::Client::new(&network.rpc_url)?;
client
.verify_network_passphrase(Some(&network.network_passphrase))
.await?;
Expand Down Expand Up @@ -344,7 +339,7 @@ impl Cmd {

impl Cmd {
fn contract_id(&self) -> Result<[u8; 32], Error> {
utils::contract_id_from_str(&self.contract_id)
soroban_spec_tools::utils::contract_id_from_str(&self.contract_id)
.map_err(|e| Error::CannotParseContractId(self.contract_id.clone(), e))
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/soroban-cli/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl Args {
} else if let Some(wasm_hash) = &self.wasm_hash {
return Ok(vec![LedgerKey::ContractCode(LedgerKeyContractCode {
hash: xdr::Hash(
utils::contract_id_from_str(wasm_hash)
soroban_spec_tools::utils::contract_id_from_str(wasm_hash)
.map_err(|e| Error::CannotParseContractId(wasm_hash.clone(), e))?,
),
})]);
Expand Down
13 changes: 6 additions & 7 deletions cmd/soroban-cli/src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use soroban_env_host::xdr::{
};
use soroban_sdk::token;
use soroban_sdk::xdr::Limits;
use soroban_spec_tools::contract::Spec;
use std::{
fmt::Display,
str::FromStr,
Expand All @@ -25,7 +26,7 @@ use termcolor::{Color, ColorChoice, StandardStream, WriteColor};
use termcolor_output::colored;
use tokio::time::sleep;

use crate::utils::contract_spec;
// use soroban_spec_tools::contract::Spec;

mod txn;

Expand Down Expand Up @@ -88,7 +89,7 @@ pub enum Error {
#[error("unexpected contract code data type: {0:?}")]
UnexpectedContractCodeDataType(LedgerEntryData),
#[error(transparent)]
CouldNotParseContractSpec(#[from] contract_spec::Error),
CouldNotParseContractSpec(#[from] soroban_spec_tools::contract::Error),
#[error("unexpected contract code got token")]
UnexpectedToken(ContractDataEntry),
#[error(transparent)]
Expand Down Expand Up @@ -898,11 +899,9 @@ soroban config identity fund {address} --helper-url <url>"#
xdr::ScVal::ContractInstance(xdr::ScContractInstance {
executable: xdr::ContractExecutable::Wasm(hash),
..
}) => Ok(contract_spec::ContractSpec::new(
&self.get_remote_wasm_from_hash(hash).await?,
)
.map_err(Error::CouldNotParseContractSpec)?
.spec),
}) => Ok(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
57 changes: 8 additions & 49 deletions cmd/soroban-cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use soroban_env_host::xdr::{
TransactionV1Envelope, WriteXdr,
};

pub mod contract_spec;
pub use soroban_spec_tools::contract as contract_spec;

/// # Errors
///
Expand Down Expand Up @@ -55,16 +55,17 @@ pub fn sign_transaction(
///
/// Might return an error
pub fn contract_id_from_str(contract_id: &str) -> Result<[u8; 32], stellar_strkey::DecodeError> {
stellar_strkey::Contract::from_string(contract_id)
.map(|strkey| strkey.0)
.or_else(|_| {
Ok(
if let Ok(strkey) = stellar_strkey::Contract::from_string(contract_id) {
strkey.0
} else {
// strkey failed, try to parse it as a hex string, for backwards compatibility.
soroban_spec_tools::utils::padded_hex_from_str(contract_id, 32)
.map_err(|_| stellar_strkey::DecodeError::Invalid)?
.try_into()
.map_err(|_| stellar_strkey::DecodeError::Invalid)
})
.map_err(|_| stellar_strkey::DecodeError::Invalid)
.map_err(|_| stellar_strkey::DecodeError::Invalid)?
},
)
}

/// # Errors
Expand Down Expand Up @@ -198,47 +199,5 @@ mod tests {
),
Err(err) => panic!("Failed to parse contract id: {err}"),
}

// hex
match contract_id_from_str(
"363eaa3867841fbad0f4ed88c779e4fe66e56a2470dc98c0ec9c073d05c7b103",
) {
Ok(contract_id) => assert_eq!(
contract_id,
[
0x36, 0x3e, 0xaa, 0x38, 0x67, 0x84, 0x1f, 0xba, 0xd0, 0xf4, 0xed, 0x88, 0xc7,
0x79, 0xe4, 0xfe, 0x66, 0xe5, 0x6a, 0x24, 0x70, 0xdc, 0x98, 0xc0, 0xec, 0x9c,
0x07, 0x3d, 0x05, 0xc7, 0xb1, 0x03,
]
),
Err(err) => panic!("Failed to parse contract id: {err}"),
}

// unpadded-hex
match contract_id_from_str("1") {
Ok(contract_id) => assert_eq!(
contract_id,
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
]
),
Err(err) => panic!("Failed to parse contract id: {err}"),
}

// invalid hex
match contract_id_from_str("foobar") {
Ok(_) => panic!("Expected parsing to fail"),
Err(err) => assert_eq!(err, stellar_strkey::DecodeError::Invalid),
}

// hex too long (33 bytes)
match contract_id_from_str(
"000000000000000000000000000000000000000000000000000000000000000000",
) {
Ok(_) => panic!("Expected parsing to fail"),
Err(err) => assert_eq!(err, stellar_strkey::DecodeError::Invalid),
}
}
}
9 changes: 5 additions & 4 deletions cmd/soroban-cli/src/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use clap::arg;
use soroban_env_host::xdr::{self, LedgerKey, LedgerKeyContractCode};
use soroban_spec_tools::contract::{self, Spec};
use std::{
fs, io,
path::{Path, PathBuf},
};

use crate::utils::{self, contract_spec::ContractSpec};
use crate::utils::{self};

#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand All @@ -25,7 +26,7 @@ pub enum Error {
#[error(transparent)]
Parser(#[from] wasmparser::BinaryReaderError),
#[error(transparent)]
ContractSpec(#[from] crate::utils::contract_spec::Error),
ContractSpec(#[from] contract::Error),
}

#[derive(Debug, clap::Args, Clone)]
Expand Down Expand Up @@ -60,9 +61,9 @@ impl Args {

/// # Errors
/// May fail to read wasm file or parse xdr section
pub fn parse(&self) -> Result<ContractSpec, Error> {
pub fn parse(&self) -> Result<Spec, Error> {
let contents = self.read()?;
Ok(ContractSpec::new(&contents)?)
Ok(Spec::new(&contents)?)
}
}

Expand Down

0 comments on commit 1fcc467

Please sign in to comment.