Skip to content

Commit

Permalink
Change State API: StorageValue::new(..) takes a reference (#368)
Browse files Browse the repository at this point in the history
* StorageValue::new() takes a reference

* Update docs
  • Loading branch information
bkolad authored May 31, 2023
1 parent 5c1c5c0 commit ee0c631
Show file tree
Hide file tree
Showing 26 changed files with 64 additions and 66 deletions.
8 changes: 4 additions & 4 deletions examples/demo-nft-module/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,12 @@ impl<C: Context> NonFungibleToken<C> {
config: &<Self as sov_modules_api::Module>::Config,
working_set: &mut WorkingSet<C::Storage>,
) -> Result<()> {
self.admin.set(config.admin.clone(), working_set);
self.admin.set(&config.admin, working_set);
for (id, owner) in config.owners.iter() {
if self.owners.get(id, working_set).is_some() {
bail!("Token id {} already exists", id);
}
self.owners.set(id, owner.clone(), working_set);
self.owners.set(id, owner, working_set);
}
Ok(())
}
Expand All @@ -268,7 +268,7 @@ impl<C: Context> NonFungibleToken<C> {
bail!("Token with id {} already exists", id);
}

self.owners.set(&id, context.sender().clone(), working_set);
self.owners.set(&id, context.sender(), working_set);

working_set.add_event("NFT mint", &format!("A token with id {id} was minted"));
Ok(CallResponse::default())
Expand All @@ -290,7 +290,7 @@ impl<C: Context> NonFungibleToken<C> {
if &token_owner != context.sender() {
bail!("Only token owner can transfer token");
}
self.owners.set(&id, to, working_set);
self.owners.set(&id, &to, working_set);
working_set.add_event(
"NFT transfer",
&format!("A token with id {id} was transferred"),
Expand Down
4 changes: 2 additions & 2 deletions examples/demo-nft-module/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<C: Context> NonFungibleToken<C> {
bail!("Token with id {} already exists", id);
}

self.owners.set(&id, context.sender().clone(), working_set);
self.owners.set(&id, context.sender(), working_set);

working_set.add_event("NFT mint", &format!("A token with id {id} was minted"));
Ok(CallResponse::default())
Expand All @@ -58,7 +58,7 @@ impl<C: Context> NonFungibleToken<C> {
if &token_owner != context.sender() {
bail!("Only token owner can transfer token");
}
self.owners.set(&id, to, working_set);
self.owners.set(&id, &to, working_set);
working_set.add_event(
"NFT transfer",
&format!("A token with id {id} was transferred"),
Expand Down
4 changes: 2 additions & 2 deletions examples/demo-nft-module/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ impl<C: Context> NonFungibleToken<C> {
config: &<Self as sov_modules_api::Module>::Config,
working_set: &mut WorkingSet<C::Storage>,
) -> Result<()> {
self.admin.set(config.admin.clone(), working_set);
self.admin.set(&config.admin, working_set);
for (id, owner) in config.owners.iter() {
if self.owners.get(id, working_set).is_some() {
bail!("Token id {} already exists", id);
}
self.owners.set(id, owner.clone(), working_set);
self.owners.set(id, owner, working_set);
}
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl<C: sov_modules_api::Context> Election<C> {
self.exit_if_candidates_already_set(working_set)?;

let candidates = candidate_names.into_iter().map(Candidate::new).collect();
self.candidates.set(candidates, working_set);
self.candidates.set(&candidates, working_set);
working_set.add_event("Election: set_candidates", "Candidate was set");

Ok(CallResponse::default())
Expand All @@ -48,7 +48,7 @@ impl<C: sov_modules_api::Context> Election<C> {
self.exit_if_voter_already_set(&voter_address, working_set)?;

self.allowed_voters
.set(&voter_address, Voter::fresh(), working_set);
.set(&voter_address, &Voter::fresh(), working_set);

working_set.add_event(
"Election: add_voter",
Expand Down Expand Up @@ -80,7 +80,7 @@ impl<C: sov_modules_api::Context> Election<C> {
.checked_add(1)
.ok_or(anyhow!("Vote count overflow"))?;

self.number_of_votes.set(new_number_of_votes, working_set);
self.number_of_votes.set(&new_number_of_votes, working_set);
self.exit_if_frozen(working_set)?;

let voter = self
Expand All @@ -91,7 +91,7 @@ impl<C: sov_modules_api::Context> Election<C> {
Voter::Voted => bail!("Voter tried voting a second time!"),
Voter::Fresh => {
self.allowed_voters
.set(context.sender(), Voter::voted(), working_set);
.set(context.sender(), &Voter::voted(), working_set);

let mut candidates = self.candidates.get_or_err(working_set)?;

Expand All @@ -105,7 +105,7 @@ impl<C: sov_modules_api::Context> Election<C> {
.checked_add(1)
.ok_or(anyhow!("Vote count overflow"))?;

self.candidates.set(candidates, working_set);
self.candidates.set(&candidates, working_set);

working_set.add_event(
"Election: make_vote",
Expand All @@ -123,7 +123,7 @@ impl<C: sov_modules_api::Context> Election<C> {
working_set: &mut WorkingSet<C::Storage>,
) -> Result<CallResponse> {
self.exit_if_not_admin(context, working_set)?;
self.is_frozen.set(true, working_set);
self.is_frozen.set(&true, working_set);
working_set.add_event("Election: freeze_election", "Election was frozen");
Ok(CallResponse::default())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ impl<C: sov_modules_api::Context> Election<C> {
config: &<Self as sov_modules_api::Module>::Config,
working_set: &mut WorkingSet<C::Storage>,
) -> Result<()> {
self.admin.set(config.admin.clone(), working_set);
self.is_frozen.set(false, working_set);
self.admin.set(&config.admin, working_set);
self.is_frozen.set(&false, working_set);

Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<C: sov_modules_api::Context> ValueSetter<C> {
}

// This is how we set a new value:
self.value.set(new_value, working_set);
self.value.set(&new_value, working_set);
working_set.add_event("set", &format!("value_set: {new_value:?}"));

Ok(CallResponse::default())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ impl<C: sov_modules_api::Context> ValueSetter<C> {
admin_config: &<Self as sov_modules_api::Module>::Config,
working_set: &mut WorkingSet<C::Storage>,
) -> Result<()> {
self.admin.set(admin_config.admin.clone(), working_set);
self.admin.set(&admin_config.admin, working_set);
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pub mod module_a {
pub fn update(&mut self, key: &str, value: &str, working_set: &mut WorkingSet<C::Storage>) {
working_set.add_event("module A", "update");
self.state_1_a
.set(&key.to_owned(), value.to_owned(), working_set);
self.state_2_a.set(value.to_owned(), working_set)
.set(&key.to_owned(), &value.to_owned(), working_set);
self.state_2_a.set(&value.to_owned(), working_set)
}
}
}
Expand All @@ -48,7 +48,7 @@ pub mod module_b {
pub fn update(&mut self, key: &str, value: &str, working_set: &mut WorkingSet<C::Storage>) {
working_set.add_event("module B", "update");
self.state_1_b
.set(&key.to_owned(), value.to_owned(), working_set);
.set(&key.to_owned(), &value.to_owned(), working_set);
self.mod_1_a.update("key_from_b", value, working_set);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl<C: sov_modules_api::Context> ExampleModule<C> {
_context: &C,
working_set: &mut WorkingSet<C::Storage>,
) -> Result<sov_modules_api::CallResponse> {
self.value.set(new_value, working_set);
self.value.set(&new_value, working_set);
working_set.add_event("set", &format!("value_set: {new_value:?}"));

Ok(CallResponse::default())
Expand Down
4 changes: 2 additions & 2 deletions module-system/module-implementations/sov-accounts/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ impl<C: sov_modules_api::Context> Accounts<C> {
signature.verify(&new_pub_key, UPDATE_ACCOUNT_MSG)?;

// Update the public key (account data remains the same).
self.accounts.set(&new_pub_key, account, working_set);
self.accounts.set(&new_pub_key, &account, working_set);
self.public_keys
.set(context.sender(), new_pub_key, working_set);
.set(context.sender(), &new_pub_key, working_set);
Ok(CallResponse::default())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ impl<C: sov_modules_api::Context> Accounts<C> {
nonce: 0,
};

self.accounts
.set(&pub_key, new_account.clone(), working_set);
self.accounts.set(&pub_key, &new_account, working_set);

self.public_keys.set(&default_address, pub_key, working_set);
self.public_keys
.set(&default_address, &pub_key, working_set);
Ok(new_account)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl<C: Context> TxHooks for Accounts<C> {
) -> anyhow::Result<()> {
let mut account = self.accounts.get_or_err(tx.pub_key(), working_set)?;
account.nonce += 1;
self.accounts.set(tx.pub_key(), account, working_set);
self.accounts.set(tx.pub_key(), &account, working_set);
Ok(())
}
}
8 changes: 4 additions & 4 deletions module-system/module-implementations/sov-bank/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl<C: sov_modules_api::Context> Bank<C> {
);
}

self.tokens.set(&token_address, token, working_set);
self.tokens.set(&token_address, &token, working_set);
Ok(CallResponse::default())
}

Expand All @@ -107,7 +107,7 @@ impl<C: sov_modules_api::Context> Bank<C> {
let mut token = self.tokens.get_or_err(&coins.token_address, working_set)?;
token.burn(context.sender(), coins.amount, working_set)?;
token.total_supply -= coins.amount;
self.tokens.set(&coins.token_address, token, working_set);
self.tokens.set(&coins.token_address, &token, working_set);

Ok(CallResponse::default())
}
Expand All @@ -121,7 +121,7 @@ impl<C: sov_modules_api::Context> Bank<C> {
) -> Result<CallResponse> {
let mut token = self.tokens.get_or_err(&coins.token_address, working_set)?;
token.mint(context.sender(), &minter_address, coins.amount, working_set)?;
self.tokens.set(&coins.token_address, token, working_set);
self.tokens.set(&coins.token_address, &token, working_set);

Ok(CallResponse::default())
}
Expand All @@ -134,7 +134,7 @@ impl<C: sov_modules_api::Context> Bank<C> {
) -> Result<CallResponse> {
let mut token = self.tokens.get_or_err(&token_address, working_set)?;
token.freeze(context.sender())?;
self.tokens.set(&token_address, token, working_set);
self.tokens.set(&token_address, &token, working_set);

Ok(CallResponse::default())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl<C: sov_modules_api::Context> Bank<C> {
bail!("Token address already exists");
}

self.tokens.set(&token_address, token, working_set);
self.tokens.set(&token_address, &token, working_set);
}
Ok(())
}
Expand Down
10 changes: 5 additions & 5 deletions module-system/module-implementations/sov-bank/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ impl<C: sov_modules_api::Context> Token<C> {
// We can't overflow here because the sum must be smaller or eq to `total_supply` which is u64.
let to_balance = self.balances.get(to, working_set).unwrap_or_default() + amount;

self.balances.set(from, from_balance, working_set);
self.balances.set(to, to_balance, working_set);
self.balances.set(from, &from_balance, working_set);
self.balances.set(to, &to_balance, working_set);

Ok(())
}
Expand All @@ -64,7 +64,7 @@ impl<C: sov_modules_api::Context> Token<C> {
working_set: &mut WorkingSet<C::Storage>,
) -> Result<()> {
let new_balance = self.check_balance(from, amount, working_set)?;
self.balances.set(from, new_balance, working_set);
self.balances.set(from, &new_balance, working_set);

Ok(())
}
Expand Down Expand Up @@ -101,7 +101,7 @@ impl<C: sov_modules_api::Context> Token<C> {
"Account Balance overflow in the mint method of bank module",
))?;

self.balances.set(minter_address, to_balance, working_set);
self.balances.set(minter_address, &to_balance, working_set);
self.total_supply = self
.total_supply
.checked_add(amount)
Expand Down Expand Up @@ -149,7 +149,7 @@ impl<C: sov_modules_api::Context> Token<C> {

let mut total_supply: Option<u64> = Some(0);
for (address, balance) in address_and_balances.iter() {
balances.set(address, *balance, working_set);
balances.set(address, balance, working_set);
total_supply = total_supply.and_then(|ts| ts.checked_add(*balance));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl<C: sov_modules_api::Context, Vm: Zkvm> ProverIncentives<C, Vm> {
.get(prover, working_set)
.unwrap_or_default();
let total_balance = old_balance + bond_amount;
self.bonded_provers.set(prover, total_balance, working_set);
self.bonded_provers.set(prover, &total_balance, working_set);

// Emit the bonding event
working_set.add_event(
Expand Down Expand Up @@ -90,7 +90,7 @@ impl<C: sov_modules_api::Context, Vm: Zkvm> ProverIncentives<C, Vm> {
.transfer_from(&self.address, context.sender(), coins, working_set)?;

// Update our internal tracking of the total bonded amount for the sender.
self.bonded_provers.set(context.sender(), 0, working_set);
self.bonded_provers.set(context.sender(), &0, working_set);

// Emit the unbonding event
working_set.add_event(
Expand Down Expand Up @@ -126,7 +126,7 @@ impl<C: sov_modules_api::Context, Vm: Zkvm> ProverIncentives<C, Vm> {

// Lock the prover's bond amount.
self.bonded_provers
.set(context.sender(), old_balance - minimum_bond, working_set);
.set(context.sender(), &(old_balance - minimum_bond), working_set);

// Don't return an error for invalid proofs - those are expected and shouldn't cause reverts.
if let Ok(_public_outputs) =
Expand All @@ -139,7 +139,7 @@ impl<C: sov_modules_api::Context, Vm: Zkvm> ProverIncentives<C, Vm> {
// TODO: reward the prover with newly minted tokens as appropriate based on gas fees.
// https://github.com/Sovereign-Labs/sovereign/issues/271
self.bonded_provers
.set(context.sender(), old_balance, working_set);
.set(context.sender(), &old_balance, working_set);

working_set.add_event(
"processed_valid_proof",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ impl<C: sov_modules_api::Context, Vm: Zkvm> ProverIncentives<C, Vm> {
"At least one prover must be set at genesis!"
);

self.minimum_bond.set(config.minimum_bond, working_set);
self.minimum_bond.set(&config.minimum_bond, working_set);
self.commitment_of_allowed_verifier_method.set(
crate::StoredCodeCommitment {
&crate::StoredCodeCommitment {
commitment: config.commitment_of_allowed_verifier_method.clone(),
},
working_set,
);
self.bonding_token_address
.set(config.bonding_token_address.clone(), working_set);
.set(&config.bonding_token_address, working_set);

for (prover, bond) in config.initial_provers.iter() {
self.bond_prover_helper(*bond, prover, working_set)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ impl<C: sov_modules_api::Context> Sequencer<C> {
working_set: &mut WorkingSet<C::Storage>,
) -> Result<()> {
self.seq_rollup_address
.set(config.seq_rollup_address.clone(), working_set);
.set(&config.seq_rollup_address, working_set);

self.seq_da_address
.set(config.seq_da_address.clone(), working_set);
self.seq_da_address.set(&config.seq_da_address, working_set);

self.coins_to_lock
.set(config.coins_to_lock.clone(), working_set);
self.coins_to_lock.set(&config.coins_to_lock, working_set);

Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod modules;
use modules::{first_test_module, second_test_module};
use sov_modules_api::Address;
use sov_modules_api::ModuleInfo;
use sov_modules_api::{default_context::DefaultContext, Context, Genesis, Module};
use sov_modules_api::{default_context::DefaultContext, Context, Genesis};
use sov_modules_macros::{DispatchCall, Genesis, MessageCodec};
use sov_state::ProverStorage;

Expand Down
Loading

0 comments on commit ee0c631

Please sign in to comment.