Skip to content
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

Rename 'get'/'put' Methods in Clarity #4521

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 32 additions & 28 deletions clarity/src/vm/database/clarity_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,22 +472,26 @@ impl<'a> ClarityDatabase<'a> {
self.store.set_block_hash(bhh, query_pending_data)
}

pub fn put<T: ClaritySerializable>(&mut self, key: &str, value: &T) -> Result<()> {
self.store.put(&key, &value.serialize())
pub fn put_data<T: ClaritySerializable>(&mut self, key: &str, value: &T) -> Result<()> {
self.store.put_data(&key, &value.serialize())
}

/// Like `put()`, but returns the serialized byte size of the stored value
pub fn put_with_size<T: ClaritySerializable>(&mut self, key: &str, value: &T) -> Result<u64> {
pub fn put_data_with_size<T: ClaritySerializable>(
&mut self,
key: &str,
value: &T,
) -> Result<u64> {
let serialized = value.serialize();
self.store.put(&key, &serialized)?;
self.store.put_data(&key, &serialized)?;
Ok(byte_len_of_serialization(&serialized))
}

pub fn get<T>(&mut self, key: &str) -> Result<Option<T>>
pub fn get_data<T>(&mut self, key: &str) -> Result<Option<T>>
where
T: ClarityDeserializable<T>,
{
self.store.get::<T>(key)
self.store.get_data::<T>(key)
}

pub fn put_value(&mut self, key: &str, value: Value, epoch: &StacksEpochId) -> Result<()> {
Expand Down Expand Up @@ -524,7 +528,7 @@ impl<'a> ClarityDatabase<'a> {

let size = serialized.len() as u64;
let hex_serialized = to_hex(serialized.as_slice());
self.store.put(&key, &hex_serialized)?;
self.store.put_data(&key, &hex_serialized)?;

Ok(pre_sanitized_size.unwrap_or(size))
}
Expand All @@ -540,11 +544,11 @@ impl<'a> ClarityDatabase<'a> {
.map_err(|e| InterpreterError::DBError(e.to_string()).into())
}

pub fn get_with_proof<T>(&mut self, key: &str) -> Result<Option<(T, Vec<u8>)>>
pub fn get_data_with_proof<T>(&mut self, key: &str) -> Result<Option<(T, Vec<u8>)>>
where
T: ClarityDeserializable<T>,
{
self.store.get_with_proof(key)
self.store.get_data_with_proof(key)
}

pub fn make_key_for_trip(
Expand Down Expand Up @@ -787,7 +791,7 @@ impl<'a> ClarityDatabase<'a> {
/// The instantiation of subsequent epochs may bump up the epoch version in the clarity DB if
/// Clarity is updated in that epoch.
pub fn get_clarity_epoch_version(&mut self) -> Result<StacksEpochId> {
let out = match self.get(Self::clarity_state_epoch_key())? {
let out = match self.get_data(Self::clarity_state_epoch_key())? {
Some(x) => u32::try_into(x).map_err(|_| {
InterpreterError::Expect("Bad Clarity epoch version in stored Clarity state".into())
})?,
Expand All @@ -798,7 +802,7 @@ impl<'a> ClarityDatabase<'a> {

/// Should be called _after_ all of the epoch's initialization has been invoked
pub fn set_clarity_epoch_version(&mut self, epoch: StacksEpochId) -> Result<()> {
self.put(Self::clarity_state_epoch_key(), &(epoch as u32))
self.put_data(Self::clarity_state_epoch_key(), &(epoch as u32))
}

/// Returns the _current_ total liquid ustx
Expand Down Expand Up @@ -1131,12 +1135,12 @@ impl<'a> ClarityDatabase<'a> {

pub fn get_stx_btc_ops_processed(&mut self) -> Result<u64> {
Ok(self
.get("vm_pox::stx_btc_ops::processed_blocks")?
.get_data("vm_pox::stx_btc_ops::processed_blocks")?
.unwrap_or(0))
}

pub fn set_stx_btc_ops_processed(&mut self, processed: u64) -> Result<()> {
self.put("vm_pox::stx_btc_ops::processed_blocks", &processed)
self.put_data("vm_pox::stx_btc_ops::processed_blocks", &processed)
}
}

Expand All @@ -1158,7 +1162,7 @@ impl<'a> ClarityDatabase<'a> {
) -> Result<()> {
let key = ClarityDatabase::make_microblock_pubkey_height_key(pubkey_hash);
let value = format!("{}", &height);
self.put(&key, &value)
self.put_data(&key, &value)
}

pub fn get_cc_special_cases_handler(&self) -> Option<SpecialCaseHandler> {
Expand Down Expand Up @@ -1195,15 +1199,15 @@ impl<'a> ClarityDatabase<'a> {
})?;

let value_str = to_hex(&value_bytes);
self.put(&key, &value_str)
self.put_data(&key, &value_str)
}

pub fn get_microblock_pubkey_hash_height(
&mut self,
pubkey_hash: &Hash160,
) -> Result<Option<u32>> {
let key = ClarityDatabase::make_microblock_pubkey_height_key(pubkey_hash);
self.get(&key)?
self.get_data(&key)?
.map(|height_str: String| {
height_str.parse::<u32>().map_err(|_| {
InterpreterError::Expect(
Expand All @@ -1221,7 +1225,7 @@ impl<'a> ClarityDatabase<'a> {
height: u32,
) -> Result<Option<(StandardPrincipalData, u16)>> {
let key = ClarityDatabase::make_microblock_poison_key(height);
self.get(&key)?
self.get_data(&key)?
.map(|reporter_hex_str: String| {
let reporter_value = Value::try_deserialize_hex_untyped(&reporter_hex_str)
.map_err(|_| {
Expand Down Expand Up @@ -1776,7 +1780,7 @@ impl<'a> ClarityDatabase<'a> {
StoreType::CirculatingSupply,
token_name,
);
self.put(&supply_key, &(0_u128))?;
self.put_data(&supply_key, &(0_u128))?;

Ok(data)
}
Expand Down Expand Up @@ -1830,7 +1834,7 @@ impl<'a> ClarityDatabase<'a> {
StoreType::CirculatingSupply,
token_name,
);
let current_supply: u128 = self.get(&key)?.ok_or_else(|| {
let current_supply: u128 = self.get_data(&key)?.ok_or_else(|| {
InterpreterError::Expect("ERROR: Clarity VM failed to track token supply.".into())
})?;

Expand All @@ -1844,7 +1848,7 @@ impl<'a> ClarityDatabase<'a> {
}
}

self.put(&key, &new_supply)
self.put_data(&key, &new_supply)
}

pub fn checked_decrease_token_supply(
Expand All @@ -1858,7 +1862,7 @@ impl<'a> ClarityDatabase<'a> {
StoreType::CirculatingSupply,
token_name,
);
let current_supply: u128 = self.get(&key)?.ok_or_else(|| {
let current_supply: u128 = self.get_data(&key)?.ok_or_else(|| {
InterpreterError::Expect("ERROR: Clarity VM failed to track token supply.".into())
})?;

Expand All @@ -1868,7 +1872,7 @@ impl<'a> ClarityDatabase<'a> {

let new_supply = current_supply - amount;

self.put(&key, &new_supply)
self.put_data(&key, &new_supply)
}

pub fn get_ft_balance(
Expand All @@ -1889,7 +1893,7 @@ impl<'a> ClarityDatabase<'a> {
&principal.serialize(),
);

let result = self.get(&key)?;
let result = self.get_data(&key)?;
match result {
None => Ok(0),
Some(balance) => Ok(balance),
Expand All @@ -1909,7 +1913,7 @@ impl<'a> ClarityDatabase<'a> {
token_name,
&principal.serialize(),
);
self.put(&key, &balance)
self.put_data(&key, &balance)
}

pub fn get_ft_supply(
Expand All @@ -1922,7 +1926,7 @@ impl<'a> ClarityDatabase<'a> {
StoreType::CirculatingSupply,
token_name,
);
let supply = self.get(&key)?.ok_or_else(|| {
let supply = self.get_data(&key)?.ok_or_else(|| {
InterpreterError::Expect("ERROR: Clarity VM failed to track token supply.".into())
})?;
Ok(supply)
Expand Down Expand Up @@ -2098,7 +2102,7 @@ impl<'a> ClarityDatabase<'a> {
pub fn get_account_stx_balance(&mut self, principal: &PrincipalData) -> Result<STXBalance> {
let key = ClarityDatabase::make_key_for_account_balance(principal);
debug!("Fetching account balance"; "principal" => %principal.to_string());
let result = self.get(&key)?;
let result = self.get_data(&key)?;
Ok(match result {
None => STXBalance::zero(),
Some(balance) => balance,
Expand All @@ -2107,7 +2111,7 @@ impl<'a> ClarityDatabase<'a> {

pub fn get_account_nonce(&mut self, principal: &PrincipalData) -> Result<u64> {
let key = ClarityDatabase::make_key_for_account_nonce(principal);
let result = self.get(&key)?;
let result = self.get_data(&key)?;
Ok(match result {
None => 0,
Some(nonce) => nonce,
Expand All @@ -2116,7 +2120,7 @@ impl<'a> ClarityDatabase<'a> {

pub fn set_account_nonce(&mut self, principal: &PrincipalData, nonce: u64) -> Result<()> {
let key = ClarityDatabase::make_key_for_account_nonce(principal);
self.put(&key, &nonce)
self.put_data(&key, &nonce)
}
}

Expand Down
22 changes: 11 additions & 11 deletions clarity/src/vm/database/clarity_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ pub type SpecialCaseHandler = &'static dyn Fn(
// attempt to continue processing in the event of an unexpected storage error.
pub trait ClarityBackingStore {
/// put K-V data into the committed datastore
fn put_all(&mut self, items: Vec<(String, String)>) -> Result<()>;
fn put_all_data(&mut self, items: Vec<(String, String)>) -> Result<()>;
/// fetch K-V out of the committed datastore
fn get(&mut self, key: &str) -> Result<Option<String>>;
fn get_data(&mut self, key: &str) -> Result<Option<String>>;
/// fetch K-V out of the committed datastore, along with the byte representation
/// of the Merkle proof for that key-value pair
fn get_with_proof(&mut self, key: &str) -> Result<Option<(String, Vec<u8>)>>;
fn get_data_with_proof(&mut self, key: &str) -> Result<Option<(String, Vec<u8>)>>;
fn has_entry(&mut self, key: &str) -> Result<bool> {
Ok(self.get(key)?.is_some())
Ok(self.get_data(key)?.is_some())
}

/// change the current MARF context to service reads from a different chain_tip
Expand Down Expand Up @@ -109,7 +109,7 @@ pub trait ClarityBackingStore {
) -> Result<(StacksBlockId, Sha512Trunc256Sum)> {
let key = make_contract_hash_key(contract);
let contract_commitment = self
.get(&key)?
.get_data(&key)?
.map(|x| ContractCommitment::deserialize(&x))
.ok_or_else(|| CheckErrors::NoSuchContract(contract.to_string()))?;
let ContractCommitment {
Expand Down Expand Up @@ -232,11 +232,11 @@ impl ClarityBackingStore for NullBackingStore {
panic!("NullBackingStore can't set block hash")
}

fn get(&mut self, _key: &str) -> Result<Option<String>> {
fn get_data(&mut self, _key: &str) -> Result<Option<String>> {
panic!("NullBackingStore can't retrieve data")
}

fn get_with_proof(&mut self, _key: &str) -> Result<Option<(String, Vec<u8>)>> {
fn get_data_with_proof(&mut self, _key: &str) -> Result<Option<(String, Vec<u8>)>> {
panic!("NullBackingStore can't retrieve data")
}

Expand All @@ -260,7 +260,7 @@ impl ClarityBackingStore for NullBackingStore {
panic!("NullBackingStore can't get current block height")
}

fn put_all(&mut self, mut _items: Vec<(String, String)>) -> Result<()> {
fn put_all_data(&mut self, mut _items: Vec<(String, String)>) -> Result<()> {
panic!("NullBackingStore cannot put")
}
}
Expand Down Expand Up @@ -301,11 +301,11 @@ impl ClarityBackingStore for MemoryBackingStore {
Err(RuntimeErrorType::UnknownBlockHeaderHash(BlockHeaderHash(bhh.0)).into())
}

fn get(&mut self, key: &str) -> Result<Option<String>> {
fn get_data(&mut self, key: &str) -> Result<Option<String>> {
SqliteConnection::get(self.get_side_store(), key)
}

fn get_with_proof(&mut self, key: &str) -> Result<Option<(String, Vec<u8>)>> {
fn get_data_with_proof(&mut self, key: &str) -> Result<Option<(String, Vec<u8>)>> {
Ok(SqliteConnection::get(self.get_side_store(), key)?.map(|x| (x, vec![])))
}

Expand Down Expand Up @@ -337,7 +337,7 @@ impl ClarityBackingStore for MemoryBackingStore {
None
}

fn put_all(&mut self, items: Vec<(String, String)>) -> Result<()> {
fn put_all_data(&mut self, items: Vec<(String, String)>) -> Result<()> {
for (key, value) in items.into_iter() {
SqliteConnection::put(self.get_side_store(), &key, &value)?;
}
Expand Down
25 changes: 14 additions & 11 deletions clarity/src/vm/database/key_value_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ impl<'a> RollbackWrapper<'a> {
let all_edits =
rollback_check_pre_bottom_commit(last_item.edits, &mut self.lookup_map)?;
if all_edits.len() > 0 {
self.store.put_all(all_edits).map_err(|e| {
self.store.put_all_data(all_edits).map_err(|e| {
InterpreterError::Expect(format!(
"ERROR: Failed to commit data to sql store: {e:?}"
))
Expand All @@ -308,7 +308,7 @@ impl<'a> RollbackWrapper<'a> {
}
}

fn inner_put<T>(
fn inner_put_data<T>(
lookup_map: &mut HashMap<T, Vec<String>>,
edits: &mut Vec<(T, RollbackValueCheck)>,
key: T,
Expand All @@ -322,14 +322,14 @@ fn inner_put<T>(
}

impl<'a> RollbackWrapper<'a> {
pub fn put(&mut self, key: &str, value: &str) -> InterpreterResult<()> {
pub fn put_data(&mut self, key: &str, value: &str) -> InterpreterResult<()> {
let current = self.stack.last_mut().ok_or_else(|| {
InterpreterError::Expect(
"ERROR: Clarity VM attempted PUT on non-nested context.".into(),
)
})?;

Ok(inner_put(
Ok(inner_put_data(
&mut self.lookup_map,
&mut current.edits,
key.to_string(),
Expand Down Expand Up @@ -359,17 +359,17 @@ impl<'a> RollbackWrapper<'a> {

/// this function will only return commitment proofs for values _already_ materialized
/// in the underlying store. otherwise it returns None.
pub fn get_with_proof<T>(&mut self, key: &str) -> InterpreterResult<Option<(T, Vec<u8>)>>
pub fn get_data_with_proof<T>(&mut self, key: &str) -> InterpreterResult<Option<(T, Vec<u8>)>>
where
T: ClarityDeserializable<T>,
{
self.store
.get_with_proof(key)?
.get_data_with_proof(key)?
.map(|(value, proof)| Ok((T::deserialize(&value)?, proof)))
.transpose()
}

pub fn get<T>(&mut self, key: &str) -> InterpreterResult<Option<T>>
pub fn get_data<T>(&mut self, key: &str) -> InterpreterResult<Option<T>>
where
T: ClarityDeserializable<T>,
{
Expand All @@ -386,7 +386,10 @@ impl<'a> RollbackWrapper<'a> {
}
}
// otherwise, lookup from store
self.store.get(key)?.map(|x| T::deserialize(&x)).transpose()
self.store
.get_data(key)?
.map(|x| T::deserialize(&x))
.transpose()
}

pub fn deserialize_value(
Expand Down Expand Up @@ -423,7 +426,7 @@ impl<'a> RollbackWrapper<'a> {
return Ok(Some(Self::deserialize_value(x, expected, epoch)?));
}
}
let stored_data = self.store.get(key).map_err(|_| {
let stored_data = self.store.get_data(key).map_err(|_| {
SerializationError::DeserializationError("ERROR: Clarity backing store failure".into())
})?;
match stored_data {
Expand All @@ -449,7 +452,7 @@ impl<'a> RollbackWrapper<'a> {
) -> InterpreterResult<()> {
let key = make_contract_hash_key(contract);
let value = self.store.make_contract_commitment(content_hash);
self.put(&key, &value)
self.put_data(&key, &value)
}

pub fn insert_metadata(
Expand All @@ -466,7 +469,7 @@ impl<'a> RollbackWrapper<'a> {

let metadata_key = (contract.clone(), key.to_string());

Ok(inner_put(
Ok(inner_put_data(
&mut self.metadata_lookup_map,
&mut current.metadata_edits,
metadata_key,
Expand Down