Skip to content

Commit

Permalink
Merge branch 'nightly' into preston/sov-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
preston-evans98 committed Jul 31, 2023
2 parents e0887f6 + 3835042 commit faa7480
Show file tree
Hide file tree
Showing 24 changed files with 176 additions and 137 deletions.
3 changes: 2 additions & 1 deletion examples/demo-stf/src/sov-cli/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,8 @@ mod test {
let balance = demo
.runtime
.bank
.balance_of(user_address, token_address, &mut working_set);
.balance_of(user_address, token_address, &mut working_set)
.unwrap();

balance.amount
}
Expand Down
12 changes: 6 additions & 6 deletions examples/demo-stf/src/tests/stf_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub mod test {
let storage = ProverStorage::with_path(path).unwrap();
let mut working_set = WorkingSet::new(storage);

let resp = runtime.election.results(&mut working_set);
let resp = runtime.election.results(&mut working_set).unwrap();

assert_eq!(
resp,
Expand All @@ -67,7 +67,7 @@ pub mod test {
count: 3
}))
);
let resp = runtime.value_setter.query_value(&mut working_set);
let resp = runtime.value_setter.query_value(&mut working_set).unwrap();

assert_eq!(resp, sov_value_setter::Response { value: Some(33) });
}
Expand Down Expand Up @@ -112,7 +112,7 @@ pub mod test {
let runtime = &mut Runtime::<DefaultContext>::default();
let mut working_set = WorkingSet::new(demo.current_storage.clone());

let resp = runtime.election.results(&mut working_set);
let resp = runtime.election.results(&mut working_set).unwrap();

assert_eq!(
resp,
Expand All @@ -122,7 +122,7 @@ pub mod test {
}))
);

let resp = runtime.value_setter.query_value(&mut working_set);
let resp = runtime.value_setter.query_value(&mut working_set).unwrap();

assert_eq!(resp, sov_value_setter::Response { value: Some(33) });
}
Expand Down Expand Up @@ -170,14 +170,14 @@ pub mod test {
let storage = ProverStorage::with_path(path).unwrap();
let mut working_set = WorkingSet::new(storage);

let resp = runtime.election.results(&mut working_set);
let resp = runtime.election.results(&mut working_set).unwrap();

assert_eq!(
resp,
sov_election::GetResultResponse::Err("Election is not frozen".to_owned())
);

let resp = runtime.value_setter.query_value(&mut working_set);
let resp = runtime.value_setter.query_value(&mut working_set).unwrap();

assert_eq!(resp, sov_value_setter::Response { value: None });
}
Expand Down
17 changes: 10 additions & 7 deletions examples/demo-stf/src/tests/tx_revert_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ fn test_tx_revert() {
let mut working_set = WorkingSet::new(storage);

// We sent 4 vote messages but one of them is invalid and should be reverted.
let resp = runtime.election.number_of_votes(&mut working_set);
let resp = runtime.election.number_of_votes(&mut working_set).unwrap();

assert_eq!(resp, sov_election::GetNbOfVotesResponse::Result(3));

let resp = runtime.election.results(&mut working_set);
let resp = runtime.election.results(&mut working_set).unwrap();

assert_eq!(
resp,
Expand All @@ -83,7 +83,8 @@ fn test_tx_revert() {

let resp = runtime
.sequencer_registry
.sequencer_address(DEMO_SEQUENCER_DA_ADDRESS.to_vec(), &mut working_set);
.sequencer_address(DEMO_SEQUENCER_DA_ADDRESS.to_vec(), &mut working_set)
.unwrap();
// Sequencer is not excluded from list of allowed!
assert_eq!(Some(sequencer_rollup_address), resp.address);
}
Expand Down Expand Up @@ -166,13 +167,14 @@ fn test_nonce_incremented_on_revert() {
let mut working_set = WorkingSet::new(storage);

// No votes actually recorded, because there was invalid vote
let resp = runtime.election.number_of_votes(&mut working_set);
let resp = runtime.election.number_of_votes(&mut working_set).unwrap();

assert_eq!(resp, sov_election::GetNbOfVotesResponse::Result(0));

let nonce = match runtime
.accounts
.get_account(voter.pub_key(), &mut working_set)
.unwrap()
{
Response::AccountExists { nonce, .. } => nonce,
Response::AccountEmpty => 0,
Expand Down Expand Up @@ -230,7 +232,7 @@ fn test_tx_bad_sig() {
let storage = ProverStorage::with_path(path).unwrap();
let mut working_set = WorkingSet::new(storage);

let resp = runtime.election.results(&mut working_set);
let resp = runtime.election.results(&mut working_set).unwrap();

assert_eq!(
resp,
Expand Down Expand Up @@ -306,7 +308,7 @@ fn test_tx_bad_serialization() {
let storage = ProverStorage::with_path(path).unwrap();
let mut working_set = WorkingSet::new(storage);

let resp = runtime.election.results(&mut working_set);
let resp = runtime.election.results(&mut working_set).unwrap();

assert_eq!(
resp,
Expand All @@ -316,7 +318,8 @@ fn test_tx_bad_serialization() {
// Sequencer is not in list of allowed sequencers
let allowed_sequencer = runtime
.sequencer_registry
.sequencer_address(SEQUENCER_DA_ADDRESS.to_vec(), &mut working_set);
.sequencer_address(SEQUENCER_DA_ADDRESS.to_vec(), &mut working_set)
.unwrap();
assert!(allowed_sequencer.address.is_none());

// Balance of sequencer is not increased
Expand Down
6 changes: 3 additions & 3 deletions module-system/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ impl<C: sov_modules_api::Context> Bank<C> {
user_address: C::Address,
token_address: C::Address,
working_set: &mut WorkingSet<C::Storage>,
) -> BalanceResponse {
BalanceResponse {
) -> RpcResult<BalanceResponse> {
Ok(BalanceResponse {
amount: self.get_balance_of(user_address, token_address, working_set),
}
})
}
}
```
Expand Down
4 changes: 2 additions & 2 deletions module-system/RPC_WALKTHROUGH.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl<C: Context> Bank<C> {
user_address: C::Address,
token_address: C::Address,
working_set: &mut WorkingSet<C::Storage>,
) -> BalanceResponse {
) -> RpcResult<BalanceResponse> {
...
}

Expand All @@ -40,7 +40,7 @@ impl<C: Context> Bank<C> {
&self,
token_address: C::Address,
working_set: &mut WorkingSet<C::Storage>,
) -> TotalSupplyResponse {
) -> RpcResult<TotalSupplyResponse> {
...
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use jsonrpsee::core::RpcResult;
use sov_modules_api::macros::rpc_gen;
use sov_state::WorkingSet;

Expand All @@ -18,7 +19,10 @@ pub enum GetNbOfVotesResponse {
#[rpc_gen(client, server, namespace = "election")]
impl<C: sov_modules_api::Context> Election<C> {
#[rpc_method(name = "results")]
pub fn results(&self, working_set: &mut WorkingSet<C::Storage>) -> GetResultResponse {
pub fn results(
&self,
working_set: &mut WorkingSet<C::Storage>,
) -> RpcResult<GetResultResponse> {
let is_frozen = self.is_frozen.get(working_set).unwrap_or_default();

if is_frozen {
Expand All @@ -29,18 +33,18 @@ impl<C: sov_modules_api::Context> Election<C> {
.into_iter()
.max_by(|c1, c2| c1.count.cmp(&c2.count));

GetResultResponse::Result(candidate)
Ok(GetResultResponse::Result(candidate))
} else {
GetResultResponse::Err("Election is not frozen".to_owned())
Ok(GetResultResponse::Err("Election is not frozen".to_owned()))
}
}

#[rpc_method(name = "numberOfVotes")]
pub fn number_of_votes(
&self,
working_set: &mut WorkingSet<C::Storage>,
) -> GetNbOfVotesResponse {
) -> RpcResult<GetNbOfVotesResponse> {
let number_of_votes = self.number_of_votes.get(working_set).unwrap_or_default();
GetNbOfVotesResponse::Result(number_of_votes)
Ok(GetNbOfVotesResponse::Result(number_of_votes))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fn test_module<C: Context>(admin: C::Address, working_set: &mut WorkingSet<C::St

// Get result
{
let query_response: GetResultResponse = election.results(working_set);
let query_response: GetResultResponse = election.results(working_set).unwrap();

assert_eq!(
query_response,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use jsonrpsee::core::RpcResult;
use sov_modules_api::macros::rpc_gen;
use sov_state::WorkingSet;

Expand All @@ -12,9 +13,9 @@ pub struct Response {
impl<C: sov_modules_api::Context> ValueSetter<C> {
/// Queries the state of the module.
#[rpc_method(name = "queryValue")]
pub fn query_value(&self, working_set: &mut WorkingSet<C::Storage>) -> Response {
Response {
pub fn query_value(&self, working_set: &mut WorkingSet<C::Storage>) -> RpcResult<Response> {
Ok(Response {
value: self.value.get(working_set),
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn test_value_setter_helper<C: Context>(

// Test query
{
let query_response = module.query_value(working_set);
let query_response = module.query_value(working_set).unwrap();

assert_eq!(
query::Response {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![allow(missing_docs)]
use jsonrpsee::core::RpcResult;
use sov_modules_api::macros::rpc_gen;
use sov_modules_api::AddressBech32;
use sov_state::WorkingSet;
Expand Down Expand Up @@ -26,13 +27,15 @@ impl<C: sov_modules_api::Context> Accounts<C> {
&self,
pub_key: C::PublicKey,
working_set: &mut WorkingSet<C::Storage>,
) -> Response {
match self.accounts.get(&pub_key, working_set) {
) -> RpcResult<Response> {
let response = match self.accounts.get(&pub_key, working_set) {
Some(Account { addr, nonce }) => Response::AccountExists {
addr: addr.into(),
nonce,
},
None => Response::AccountEmpty,
}
};

Ok(response)
}
}
14 changes: 10 additions & 4 deletions module-system/module-implementations/sov-accounts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ fn test_config_account() {
.init_module(&account_config, native_working_set)
.unwrap();

let query_response = accounts.get_account(init_pub_key, native_working_set);
let query_response = accounts
.get_account(init_pub_key, native_working_set)
.unwrap();

assert_eq!(
query_response,
Expand Down Expand Up @@ -55,7 +57,9 @@ fn test_update_account() {
.create_default_account(sender.clone(), native_working_set)
.unwrap();

let query_response = accounts.get_account(sender.clone(), native_working_set);
let query_response = accounts
.get_account(sender.clone(), native_working_set)
.unwrap();

assert_eq!(
query_response,
Expand All @@ -80,12 +84,14 @@ fn test_update_account() {
.unwrap();

// Account corresponding to the old public key does not exist
let query_response = accounts.get_account(sender, native_working_set);
let query_response = accounts.get_account(sender, native_working_set).unwrap();

assert_eq!(query_response, query::Response::AccountEmpty);

// New account with the new public key and an old address is created.
let query_response = accounts.get_account(new_pub_key, native_working_set);
let query_response = accounts
.get_account(new_pub_key, native_working_set)
.unwrap();

assert_eq!(
query_response,
Expand Down
13 changes: 7 additions & 6 deletions module-system/module-implementations/sov-bank/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use jsonrpsee::core::RpcResult;
use sov_modules_api::macros::rpc_gen;
use sov_state::WorkingSet;

Expand All @@ -21,24 +22,24 @@ impl<C: sov_modules_api::Context> Bank<C> {
user_address: C::Address,
token_address: C::Address,
working_set: &mut WorkingSet<C::Storage>,
) -> BalanceResponse {
BalanceResponse {
) -> RpcResult<BalanceResponse> {
Ok(BalanceResponse {
amount: self.get_balance_of(user_address, token_address, working_set),
}
})
}

#[rpc_method(name = "supplyOf")]
pub fn supply_of(
&self,
token_address: C::Address,
working_set: &mut WorkingSet<C::Storage>,
) -> TotalSupplyResponse {
TotalSupplyResponse {
) -> RpcResult<TotalSupplyResponse> {
Ok(TotalSupplyResponse {
amount: self
.tokens
.get(&token_address, working_set)
.map(|token| token.total_supply),
}
})
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ fn burn_deployed_tokens() {
assert!(working_set.events().is_empty());

let query_total_supply = |working_set: &mut WorkingSet<Storage>| -> Option<u64> {
let total_supply: TotalSupplyResponse = bank.supply_of(token_address.clone(), working_set);
let total_supply: TotalSupplyResponse =
bank.supply_of(token_address.clone(), working_set).unwrap();
total_supply.amount
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,12 @@ fn freeze_token() {
minter_address: new_holder.clone(),
};

let query_total_supply =
|token_address: Address, working_set: &mut WorkingSet<Storage>| -> Option<u64> {
let total_supply: TotalSupplyResponse = bank.supply_of(token_address, working_set);
total_supply.amount
};
let query_total_supply = |token_address: Address,
working_set: &mut WorkingSet<Storage>|
-> Option<u64> {
let total_supply: TotalSupplyResponse = bank.supply_of(token_address, working_set).unwrap();
total_supply.amount
};

let minted = bank.call(mint_message, &minter_context, &mut working_set);
assert!(minted.is_err());
Expand Down
11 changes: 6 additions & 5 deletions module-system/module-implementations/sov-bank/tests/mint_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ fn mint_token() {
// No events at the moment. If there are, needs to be checked
assert!(working_set.events().is_empty());

let query_total_supply =
|token_address: Address, working_set: &mut WorkingSet<Storage>| -> Option<u64> {
let total_supply: TotalSupplyResponse = bank.supply_of(token_address, working_set);
total_supply.amount
};
let query_total_supply = |token_address: Address,
working_set: &mut WorkingSet<Storage>|
-> Option<u64> {
let total_supply: TotalSupplyResponse = bank.supply_of(token_address, working_set).unwrap();
total_supply.amount
};

let query_user_balance =
|user_address: Address, working_set: &mut WorkingSet<Storage>| -> Option<u64> {
Expand Down
Loading

0 comments on commit faa7480

Please sign in to comment.