From af55868022c38b61f987ad5386e6eff38a9363f3 Mon Sep 17 00:00:00 2001 From: gluax <16431709+gluax@users.noreply.github.com> Date: Mon, 22 Jul 2024 07:44:31 -0700 Subject: [PATCH 1/3] refactor: dr queries return optionals --- Cargo.lock | 2 +- Cargo.toml | 2 +- contract/src/msgs/data_requests/query.rs | 22 +++++++++------- contract/src/msgs/data_requests/state.rs | 2 +- .../src/msgs/data_requests/test_helpers.rs | 26 +++++++------------ contract/src/msgs/data_requests/tests.rs | 16 ++++++------ 6 files changed, 32 insertions(+), 38 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 164d12dd..ce0e1a62 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -687,7 +687,7 @@ dependencies = [ [[package]] name = "seda-common" version = "0.0.0" -source = "git+https://github.com/sedaprotocol/seda-common-rs.git?branch=main#3d1c3ee3e8dc286f0ddafe0fec3c8a9ef91de4cc" +source = "git+https://github.com/sedaprotocol/seda-common-rs.git?branch=refactor/consistent-optionals#f130c4d44c1c2be0b2a0734626247232566954c1" dependencies = [ "base64 0.22.1", "cosmwasm-schema", diff --git a/Cargo.toml b/Cargo.toml index 17da2fac..dee6c00f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,7 +31,7 @@ k256 = { version = "0.13", default-features = false, features = ["ecdsa"] } libfuzzer-sys = "0.4" rand = "0.8" schemars = { version = "0.8", features = ["semver"] } -seda-common = { git = "https://github.com/sedaprotocol/seda-common-rs.git", branch = "main" } +seda-common = { git = "https://github.com/sedaprotocol/seda-common-rs.git", branch = "refactor/consistent-optionals" } # leaving this in to make local development easier # seda-common = { path = "../seda-common-rs" } semver = { version = "1.0", features = ["serde"] } diff --git a/contract/src/msgs/data_requests/query.rs b/contract/src/msgs/data_requests/query.rs index d49d56bc..898984e4 100644 --- a/contract/src/msgs/data_requests/query.rs +++ b/contract/src/msgs/data_requests/query.rs @@ -4,26 +4,28 @@ impl QueryHandler for QueryMsg { fn query(self, deps: Deps, _env: Env) -> Result { let binary = match self { QueryMsg::GetDataRequest { dr_id } => { - to_json_binary(&state::may_get_request(deps.storage, &Hash::from_hex_str(&dr_id)?)?)? + to_json_binary(&state::may_load_request(deps.storage, &Hash::from_hex_str(&dr_id)?)?)? } QueryMsg::GetDataRequestCommitment { dr_id, public_key } => { - let dr = state::load_request(deps.storage, &Hash::from_hex_str(&dr_id)?)?; - to_json_binary(&dr.get_commitment(&public_key))? + let dr = state::may_load_request(deps.storage, &Hash::from_hex_str(&dr_id)?)?; + to_json_binary(&dr.as_ref().map(|dr| dr.get_commitment(&public_key)))? } QueryMsg::GetDataRequestCommitments { dr_id } => { - let dr = state::load_request(deps.storage, &Hash::from_hex_str(&dr_id)?)?; - to_json_binary(&dr.commits)? + let dr = state::may_load_request(deps.storage, &Hash::from_hex_str(&dr_id)?)?; + let comittments = dr.map(|dr| dr.commits).unwrap_or_default(); + to_json_binary(&comittments)? } QueryMsg::GetDataRequestReveal { dr_id, public_key } => { - let dr = state::load_request(deps.storage, &Hash::from_hex_str(&dr_id)?)?; - to_json_binary(&dr.get_reveal(&public_key))? + let dr = state::may_load_request(deps.storage, &Hash::from_hex_str(&dr_id)?)?; + to_json_binary(&dr.as_ref().map(|dr| dr.get_reveal(&public_key)))? } QueryMsg::GetDataRequestReveals { dr_id } => { - let dr = state::load_request(deps.storage, &Hash::from_hex_str(&dr_id)?)?; - to_json_binary(&dr.reveals)? + let dr = state::may_load_request(deps.storage, &Hash::from_hex_str(&dr_id)?)?; + let reveals = dr.map(|dr| dr.reveals).unwrap_or_default(); + to_json_binary(&reveals)? } QueryMsg::GetDataResult { dr_id } => { - to_json_binary(&state::load_result(deps.storage, &Hash::from_hex_str(&dr_id)?)?)? + to_json_binary(&state::may_load_resuslt(deps.storage, &Hash::from_hex_str(&dr_id)?)?)? } QueryMsg::GetDataRequestsByStatus { status, offset, limit } => { to_json_binary(&state::requests_by_status(deps.storage, &status, offset, limit)?)? diff --git a/contract/src/msgs/data_requests/state.rs b/contract/src/msgs/data_requests/state.rs index 12a4c23f..88d72ac6 100644 --- a/contract/src/msgs/data_requests/state.rs +++ b/contract/src/msgs/data_requests/state.rs @@ -14,7 +14,7 @@ pub fn data_request_or_result_exists(deps: Deps, dr_id: Hash) -> bool { DATA_REQUESTS.has(deps.storage, &dr_id) || DATA_RESULTS.has(deps.storage, &dr_id) } -pub fn may_get_request(store: &dyn Storage, dr_id: &Hash) -> StdResult> { +pub fn may_load_request(store: &dyn Storage, dr_id: &Hash) -> StdResult> { DATA_REQUESTS.may_get(store, dr_id) } diff --git a/contract/src/msgs/data_requests/test_helpers.rs b/contract/src/msgs/data_requests/test_helpers.rs index 46db9dcb..acc940c0 100644 --- a/contract/src/msgs/data_requests/test_helpers.rs +++ b/contract/src/msgs/data_requests/test_helpers.rs @@ -97,14 +97,6 @@ pub fn construct_result(dr: DataRequest, reveal: RevealBody, exit_code: u8) -> D } impl TestInfo { - #[track_caller] - pub fn get_dr(&self, dr_id: &str) -> Option { - self.query(query::QueryMsg::GetDataRequest { - dr_id: dr_id.to_string(), - }) - .unwrap() - } - #[track_caller] pub fn post_data_request( &mut self, @@ -132,7 +124,7 @@ impl TestInfo { #[track_caller] pub fn commit_result(&mut self, sender: &TestExecutor, dr_id: &str, commitment: Hash) -> Result<(), ContractError> { - let dr = self.get_dr(dr_id).unwrap(); + let dr = self.get_data_request(dr_id).unwrap(); let commitment = commitment.to_hex(); let msg_hash = hash([ "commit_data_result".as_bytes(), @@ -161,7 +153,7 @@ impl TestInfo { dr_id: &str, commitment: Hash, ) -> Result<(), ContractError> { - let dr = self.get_dr(dr_id).unwrap(); + let dr = self.get_data_request(dr_id).unwrap(); let commitment = commitment.to_hex(); let msg_hash = hash([ "commit_data_result".as_bytes(), @@ -190,7 +182,7 @@ impl TestInfo { dr_id: &str, reveal_body: RevealBody, ) -> Result<(), ContractError> { - let dr = self.get_dr(dr_id).unwrap(); + let dr = self.get_data_request(dr_id).unwrap(); let msg_hash = hash([ "reveal_data_result".as_bytes(), dr_id.as_bytes(), @@ -241,7 +233,7 @@ impl TestInfo { } #[track_caller] - pub fn get_data_request(&self, dr_id: &str) -> DataRequest { + pub fn get_data_request(&self, dr_id: &str) -> Option { self.query(query::QueryMsg::GetDataRequest { dr_id: dr_id.to_string(), }) @@ -249,7 +241,7 @@ impl TestInfo { } #[track_caller] - pub fn get_data_result(&self, dr_id: &str) -> DataResult { + pub fn get_data_result(&self, dr_id: &str) -> Option { self.query(query::QueryMsg::GetDataResult { dr_id: dr_id.to_string(), }) @@ -257,7 +249,7 @@ impl TestInfo { } #[track_caller] - pub fn get_data_result_commit(&self, dr_id: Hash, public_key: PublicKey) -> Option { + pub fn get_data_request_commit(&self, dr_id: Hash, public_key: PublicKey) -> Option { self.query(query::QueryMsg::GetDataRequestCommitment { dr_id: dr_id.to_hex(), public_key: public_key.to_hex(), @@ -266,12 +258,12 @@ impl TestInfo { } #[track_caller] - pub fn get_data_result_commits(&self, dr_id: Hash) -> HashMap { + pub fn get_data_request_commits(&self, dr_id: Hash) -> HashMap { self.query(query::QueryMsg::GetDataRequestCommitments { dr_id: dr_id.to_hex() }) .unwrap() } - pub fn get_data_result_reveal(&self, dr_id: Hash, public_key: PublicKey) -> Option { + pub fn get_data_request_reveal(&self, dr_id: Hash, public_key: PublicKey) -> Option { self.query(query::QueryMsg::GetDataRequestReveal { dr_id: dr_id.to_hex(), public_key: public_key.to_hex(), @@ -280,7 +272,7 @@ impl TestInfo { } #[track_caller] - pub fn get_data_result_reveals(&self, dr_id: Hash) -> HashMap { + pub fn get_data_request_reveals(&self, dr_id: Hash) -> HashMap { self.query(query::QueryMsg::GetDataRequestCommitments { dr_id: dr_id.to_hex() }) .unwrap() } diff --git a/contract/src/msgs/data_requests/tests.rs b/contract/src/msgs/data_requests/tests.rs index 43be4743..38b72e05 100644 --- a/contract/src/msgs/data_requests/tests.rs +++ b/contract/src/msgs/data_requests/tests.rs @@ -72,7 +72,7 @@ fn post_data_request() { let mut test_info = TestInfo::init(); // data request with id 0x69... does not yet exist - let value = test_info.get_dr("69a6e26b4d65f5b3010254a0aae2bf1bc8dccb4ddd27399c580eb771446e719f"); + let value = test_info.get_data_request("69a6e26b4d65f5b3010254a0aae2bf1bc8dccb4ddd27399c580eb771446e719f"); assert_eq!(None, value); // post a data request @@ -87,14 +87,14 @@ fn post_data_request() { assert!(res.is_err_and(|x| x == ContractError::DataRequestAlreadyExists)); // should be able to fetch data request with id 0x69... - let received_value = test_info.get_dr(&dr_id); + let received_value = test_info.get_data_request(&dr_id); assert_eq!(Some(test_helpers::construct_dr(dr, vec![], 1)), received_value); let await_commits = test_info.get_data_requests_by_status(DataRequestStatus::Committing, 0, 10); assert_eq!(1, await_commits.len()); assert!(await_commits.iter().any(|r| r.id == dr_id)); // nonexistent data request does not yet exist - let value = test_info.get_dr("00f0f00f0f00f0f0000000f0fff0ff0ff0ffff0fff00000f000ff000000f000f"); + let value = test_info.get_data_request("00f0f00f0f00f0f0000000f0fff0ff0ff0ffff0fff00000f000ff000000f000f"); assert_eq!(None, value); } @@ -406,7 +406,7 @@ fn post_data_result() { test_info.reveal_result(&alice, &dr_id, alice_reveal.clone()).unwrap(); // owner posts a data result - let dr = test_info.get_data_request(&dr_id); + let dr = test_info.get_data_request(&dr_id).unwrap(); let result = test_helpers::construct_result(dr, alice_reveal, 0); test_info.post_data_result(dr_id.clone(), result, 0).unwrap(); @@ -453,9 +453,9 @@ fn post_data_results() { test_info.reveal_result(&alice, &dr_id2, alice_reveal2.clone()).unwrap(); // owner posts data results - let dr1 = test_info.get_data_request(&dr_id1); + let dr1 = test_info.get_data_request(&dr_id1).unwrap(); let result1 = test_helpers::construct_result(dr1, alice_reveal1, 0); - let dr2 = test_info.get_data_request(&dr_id2); + let dr2 = test_info.get_data_request(&dr_id2).unwrap(); let result2 = test_helpers::construct_result(dr2, alice_reveal2, 0); test_info .post_data_results(vec![(dr_id1.clone(), result1, 0), (dr_id2.clone(), result2, 0)]) @@ -503,7 +503,7 @@ fn cant_post_if_replication_factor_not_met() { test_info.reveal_result(&alice, &dr_id, alice_reveal.clone()).unwrap(); // post a data result - let dr = test_info.get_data_request(&dr_id); + let dr = test_info.get_data_request(&dr_id).unwrap(); let result = test_helpers::construct_result(dr, alice_reveal, 0); test_info.post_data_result(dr_id, result, 0).unwrap(); } @@ -790,7 +790,7 @@ fn get_data_requests_by_status_with_many_more_drs_in_pool() { .enumerate() { if i % 8 == 0 { - let dr_info = test_info.get_data_request(&request.id); + let dr_info = test_info.get_data_request(&request.id).unwrap(); let result = test_helpers::construct_result(dr_info.clone(), alice_reveal.clone(), 0); test_info.post_data_result(request.id.to_string(), result, 0).unwrap(); } From 51514a40a985a7b209bb984d1a5ddffc24c0eb99 Mon Sep 17 00:00:00 2001 From: gluax <16431709+gluax@users.noreply.github.com> Date: Mon, 22 Jul 2024 08:00:08 -0700 Subject: [PATCH 2/3] fix: spelling mistakes --- contract/src/msgs/data_requests/query.rs | 6 +++--- contract/src/msgs/data_requests/state.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/contract/src/msgs/data_requests/query.rs b/contract/src/msgs/data_requests/query.rs index 898984e4..637f25cd 100644 --- a/contract/src/msgs/data_requests/query.rs +++ b/contract/src/msgs/data_requests/query.rs @@ -12,8 +12,8 @@ impl QueryHandler for QueryMsg { } QueryMsg::GetDataRequestCommitments { dr_id } => { let dr = state::may_load_request(deps.storage, &Hash::from_hex_str(&dr_id)?)?; - let comittments = dr.map(|dr| dr.commits).unwrap_or_default(); - to_json_binary(&comittments)? + let commitments = dr.map(|dr| dr.commits).unwrap_or_default(); + to_json_binary(&commitments)? } QueryMsg::GetDataRequestReveal { dr_id, public_key } => { let dr = state::may_load_request(deps.storage, &Hash::from_hex_str(&dr_id)?)?; @@ -25,7 +25,7 @@ impl QueryHandler for QueryMsg { to_json_binary(&reveals)? } QueryMsg::GetDataResult { dr_id } => { - to_json_binary(&state::may_load_resuslt(deps.storage, &Hash::from_hex_str(&dr_id)?)?)? + to_json_binary(&state::may_load_result(deps.storage, &Hash::from_hex_str(&dr_id)?)?)? } QueryMsg::GetDataRequestsByStatus { status, offset, limit } => { to_json_binary(&state::requests_by_status(deps.storage, &status, offset, limit)?)? diff --git a/contract/src/msgs/data_requests/state.rs b/contract/src/msgs/data_requests/state.rs index 88d72ac6..e35f87c2 100644 --- a/contract/src/msgs/data_requests/state.rs +++ b/contract/src/msgs/data_requests/state.rs @@ -75,6 +75,6 @@ pub fn load_result(store: &dyn Storage, dr_id: &Hash) -> StdResult { DATA_RESULTS.load(store, dr_id) } -pub fn may_load_resuslt(store: &dyn Storage, dr_id: &Hash) -> StdResult> { +pub fn may_load_result(store: &dyn Storage, dr_id: &Hash) -> StdResult> { DATA_RESULTS.may_load(store, dr_id) } From e33ac2a2fdf55e239f37a8570946e72d76947415 Mon Sep 17 00:00:00 2001 From: gluax <16431709+gluax@users.noreply.github.com> Date: Mon, 22 Jul 2024 08:04:43 -0700 Subject: [PATCH 3/3] chore: update common to main --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ce0e1a62..e027ae70 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -687,7 +687,7 @@ dependencies = [ [[package]] name = "seda-common" version = "0.0.0" -source = "git+https://github.com/sedaprotocol/seda-common-rs.git?branch=refactor/consistent-optionals#f130c4d44c1c2be0b2a0734626247232566954c1" +source = "git+https://github.com/sedaprotocol/seda-common-rs.git?branch=main#f130c4d44c1c2be0b2a0734626247232566954c1" dependencies = [ "base64 0.22.1", "cosmwasm-schema", diff --git a/Cargo.toml b/Cargo.toml index dee6c00f..17da2fac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,7 +31,7 @@ k256 = { version = "0.13", default-features = false, features = ["ecdsa"] } libfuzzer-sys = "0.4" rand = "0.8" schemars = { version = "0.8", features = ["semver"] } -seda-common = { git = "https://github.com/sedaprotocol/seda-common-rs.git", branch = "refactor/consistent-optionals" } +seda-common = { git = "https://github.com/sedaprotocol/seda-common-rs.git", branch = "main" } # leaving this in to make local development easier # seda-common = { path = "../seda-common-rs" } semver = { version = "1.0", features = ["serde"] }