Skip to content

Commit

Permalink
feat: add spec cache, speeding up invoke
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal authored and gitbutler-client committed Mar 11, 2024
1 parent dcf0ab0 commit df57e3b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
25 changes: 25 additions & 0 deletions cmd/soroban-cli/src/commands/config/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub enum Error {
Http(#[from] http::uri::InvalidUri),
#[error(transparent)]
Ulid(#[from] ulid::DecodeError),
#[error(transparent)]
Xdr(#[from] xdr::Error),
}

pub const XDG_DATA_HOME: &str = "XDG_DATA_HOME";
Expand All @@ -37,6 +39,12 @@ pub fn actions_dir() -> Result<std::path::PathBuf, Error> {
Ok(dir)
}

pub fn spec_dir() -> Result<std::path::PathBuf, Error> {
let dir = project_dir()?.data_local_dir().join("spec");
std::fs::create_dir_all(&dir)?;
Ok(dir)
}

pub fn write(action: Action, rpc_url: Uri) -> Result<ulid::Ulid, Error> {
let data = Data {
action,
Expand All @@ -54,6 +62,23 @@ pub fn read(id: &ulid::Ulid) -> Result<(Action, Uri), Error> {
Ok((data.action, http::Uri::from_str(&data.rpc_url)?))
}

pub fn write_spec(hash: &str, spec_entries: &[xdr::ScSpecEntry]) -> Result<(), Error> {
let file = spec_dir()?.join(hash);
tracing::trace!("writing spec to {:?}", file);
let mut contents: Vec<u8> = Vec::new();
for entry in spec_entries {
contents.extend(entry.to_xdr(xdr::Limits::none())?);
}
std::fs::write(file, contents)?;
Ok(())
}

pub fn read_spec(hash: &str) -> Result<Vec<xdr::ScSpecEntry>, Error> {
let file = spec_dir()?.join(hash);
tracing::trace!("reading spec from {:?}", file);
Ok(soroban_spec::read::parse_raw(&std::fs::read(file)?)?)
}

pub fn list_ulids() -> Result<Vec<ulid::Ulid>, Error> {
let dir = actions_dir()?;
let mut list = std::fs::read_dir(dir)?
Expand Down
4 changes: 3 additions & 1 deletion cmd/soroban-cli/src/commands/contract/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ impl NetworkRunnable for Cmd {
.run_against_rpc_server(args, None)
.await?;
}

if args.map_or(true, |a| !a.no_cache) {
data::write_spec(&hash.to_string(), &wasm_spec.spec)?;
}
Ok(hash)
}
}
Expand Down
33 changes: 27 additions & 6 deletions cmd/soroban-cli/src/commands/contract/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use heck::ToKebabCase;

use soroban_env_host::{
xdr::{
self, Error as XdrError, Hash, HostFunction, InvokeContractArgs, InvokeHostFunctionOp,
LedgerEntryData, LedgerFootprint, Memo, MuxedAccount, Operation, OperationBody,
Preconditions, ScAddress, ScSpecEntry, ScSpecFunctionV0, ScSpecTypeDef, ScVal, ScVec,
SequenceNumber, SorobanAuthorizationEntry, SorobanResources, Transaction, TransactionExt,
Uint256, VecM,
self, ContractDataEntry, Error as XdrError, Hash, HostFunction, InvokeContractArgs,
InvokeHostFunctionOp, LedgerEntryData, LedgerFootprint, Memo, MuxedAccount, Operation,
OperationBody, Preconditions, ScAddress, ScSpecEntry, ScSpecFunctionV0, ScSpecTypeDef,
ScVal, ScVec, SequenceNumber, SorobanAuthorizationEntry, SorobanResources, Transaction,
TransactionExt, Uint256, VecM,
},
HostError,
};
Expand Down Expand Up @@ -331,8 +331,29 @@ impl NetworkRunnable for Cmd {
let account_details = client.get_account(&public_strkey).await?;
let sequence: i64 = account_details.seq_num.into();

let ContractDataEntry {
val:
xdr::ScVal::ContractInstance(xdr::ScContractInstance {
executable: xdr::ContractExecutable::Wasm(hash),
..
}),
..
} = client.get_contract_data(&contract_id).await?
else {
return Err(Error::MissingResult);
};
let hash = hash.to_string();

// Get the contract
let spec_entries = client.get_remote_contract_spec(&contract_id).await?;
let spec_entries = if let Ok(entries) = data::read_spec(&hash) {
entries
} else {
let res = client.get_remote_contract_spec(&contract_id).await?;
if global_args.map_or(true, |a| !a.no_cache) {
data::write_spec(&hash, &res)?;
}
res
};

// Get the ledger footprint
let (function, spec, host_function_params, signers) =
Expand Down

0 comments on commit df57e3b

Please sign in to comment.