From 0e073268daf98ffcdc56ea9bde35f74d51220bd6 Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Fri, 16 Jun 2023 08:54:42 -0700 Subject: [PATCH] Make Address::from_contract_id pub (#989) ### 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 6d4b0b510d02584ba8731e447cf1a203000eab42) --- soroban-sdk/src/address.rs | 9 ++++----- soroban-sdk/src/testutils.rs | 3 --- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/soroban-sdk/src/address.rs b/soroban-sdk/src/address.rs index abc6c50e3..2af976474 100644 --- a/soroban-sdk/src/address.rs +++ b/soroban-sdk/src/address.rs @@ -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( @@ -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() } diff --git a/soroban-sdk/src/testutils.rs b/soroban-sdk/src/testutils.rs index 3afa0c59c..44694fc71 100644 --- a/soroban-sdk/src/testutils.rs +++ b/soroban-sdk/src/testutils.rs @@ -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