Skip to content

Commit

Permalink
Make Address::from_contract_id pub (#989)
Browse files Browse the repository at this point in the history
### What
Make Address::from_contract_id public and accessible to contracts.

### Why
I recently hid the function because it presents some problems and
constraints if used in contracts. From an portability point-of-view to
be able to run a contract already built on multiple networks contracts
would not have any hardcoded global state.

However @orbitlens pointed out that from an optimization standpoint it
may be preferred to have global state hardcoded even if that means
recompiling the contract for each network it is deployed on, or for each
specific deployment. The why is because no rent payments are required on
the additional data entries, and that there may be some savings in not
having to load a storage ledger entry.

We haven't yet done the work to confirm if it is indeed cheaper, but it
seems reasonable not to block developers from trying out different
workflows. The SDK doesn't need to be strongly opinionated on
everything, and this is an area where it feels like the SDK might be
being too parental.

Discussion about this occurred on Discord at:

https://discord.com/channels/897514728459468821/1118888566349631561/1119051351528321054

### Merging

This change is intended to be merged to `main`, and then backported into
a patch release based on the most recent release so that it can be
enjoyed today.

(cherry picked from commit 6d4b0b5)
  • Loading branch information
leighmcculloch committed Jun 16, 2023
1 parent 1eefa1e commit 0e07326
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
9 changes: 4 additions & 5 deletions soroban-sdk/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,10 @@ impl Address {
/// Prefer using the `Address` directly as input or output argument. Only
/// use this in special cases, for example to get an Address of a freshly
/// deployed contract.
pub(crate) fn from_contract_id(contract_id: &BytesN<32>) -> Self {
///
/// TODO: Replace this function in its pub form with a function that accepts
/// a strkey instead. Dependent on https://github.com/stellar/rs-stellar-strkey/issues/56.
pub fn from_contract_id(contract_id: &BytesN<32>) -> Self {
let env = contract_id.env();
unsafe {
Self::unchecked_new(
Expand Down Expand Up @@ -310,10 +313,6 @@ impl crate::testutils::Address for Address {
Self::try_from_val(env, &sc_addr).unwrap()
}

fn from_contract_id(contract_id: &crate::BytesN<32>) -> crate::Address {
Self::from_contract_id(contract_id)
}

fn contract_id(&self) -> crate::BytesN<32> {
self.contract_id()
}
Expand Down
3 changes: 0 additions & 3 deletions soroban-sdk/src/testutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,6 @@ pub trait Address {
/// the underlying Address value.
fn random(env: &Env) -> crate::Address;

/// Creates an `Address` corresponding to the provided contract identifier.
fn from_contract_id(contract_id: &crate::BytesN<32>) -> crate::Address;

/// Get the contract ID of an Address as a BytesN<32>.
///
/// ### Panics
Expand Down

0 comments on commit 0e07326

Please sign in to comment.