-
Notifications
You must be signed in to change notification settings - Fork 59
Add external read-only contract storage access #1691
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,81 @@ | ||
| use crate::{Env, Host, HostError, StorageType, Val}; | ||
| use crate::{ | ||
| storage::InstanceStorageMap, | ||
| xdr::{LedgerEntryData, ScErrorCode, ScErrorType}, | ||
| AddressObject, Host, HostError, StorageType, Val, | ||
| }; | ||
|
|
||
| impl Host { | ||
| pub(crate) fn try_get_contract_data( | ||
| fn contract_data_entry_value(&self, entry: &LedgerEntryData) -> Result<Val, HostError> { | ||
| match entry { | ||
| LedgerEntryData::ContractData(e) => self.to_valid_host_val(&e.val), | ||
| _ => Err(self.err( | ||
| ScErrorType::Storage, | ||
| ScErrorCode::InternalError, | ||
| "expected contract data ledger entry", | ||
| &[], | ||
| )), | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn try_get_contract_data_value( | ||
| &self, | ||
| k: Val, | ||
| t: StorageType, | ||
| ) -> Result<Option<Val>, HostError> { | ||
| if self.has_contract_data(k, t)?.into() { | ||
| Ok(Some(self.get_contract_data(k, t)?)) | ||
| } else { | ||
| Ok(None) | ||
| match t { | ||
| StorageType::Temporary | StorageType::Persistent => { | ||
| let key = self.storage_key_from_val(k, t.try_into()?)?; | ||
| let entry = self | ||
| .try_borrow_storage_mut()? | ||
| .try_get(&key, self, Some(k))?; | ||
| entry | ||
| .map(|entry| self.contract_data_entry_value(&entry.data)) | ||
| .transpose() | ||
| } | ||
| StorageType::Instance => { | ||
| self.with_instance_storage(|s| s.map.get(&k, self).map(|v| v.copied())) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn try_get_external_contract_data_value( | ||
| &self, | ||
| contract: AddressObject, | ||
| k: Val, | ||
| t: StorageType, | ||
| ) -> Result<Option<Val>, HostError> { | ||
| match t { | ||
| StorageType::Temporary | StorageType::Persistent => { | ||
| let key = self.storage_key_from_address_val(contract, k, t.try_into()?)?; | ||
| let entry = self | ||
| .try_borrow_storage_mut()? | ||
| .try_get(&key, self, Some(k))?; | ||
| entry | ||
| .map(|entry| self.contract_data_entry_value(&entry.data)) | ||
| .transpose() | ||
| } | ||
| StorageType::Instance => self.try_get_external_instance_data_value(contract, k), | ||
| } | ||
| } | ||
|
Comment on lines
+20
to
+59
|
||
|
|
||
| fn try_get_external_instance_data_value( | ||
| &self, | ||
| contract: AddressObject, | ||
| k: Val, | ||
| ) -> Result<Option<Val>, HostError> { | ||
| let contract_id = self.contract_id_from_address(contract)?; | ||
| let instance_key = self.contract_instance_ledger_key(&contract_id)?; | ||
| let entry = self | ||
| .try_borrow_storage_mut()? | ||
| .try_get(&instance_key, self, None)?; | ||
|
Comment on lines
+68
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
| let Some(entry) = entry else { | ||
| return Ok(None); | ||
| }; | ||
| let instance = self.extract_contract_instance_from_ledger_entry(&entry)?; | ||
| let instance_storage = InstanceStorageMap::from_instance_xdr(&instance, self)?; | ||
| instance_storage | ||
| .map | ||
| .get(&k, self) | ||
| .map(|value| value.copied()) | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This interface is rather awkward and inconsistent with the existing storage functions. I think we should either:
try_function implementationtry_function saves us a lot of instructions vs has + get, then we should return an 'option' value from the function instead of writing the output to linear memory. But I really think just dropping this is easier and not significantly more expensive.