diff --git a/Cargo.lock b/Cargo.lock index 164d12dd..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=main#3d1c3ee3e8dc286f0ddafe0fec3c8a9ef91de4cc" +source = "git+https://github.com/sedaprotocol/seda-common-rs.git?branch=main#f130c4d44c1c2be0b2a0734626247232566954c1" dependencies = [ "base64 0.22.1", "cosmwasm-schema", diff --git a/contract/src/msgs/data_requests/query.rs b/contract/src/msgs/data_requests/query.rs index d49d56bc..637f25cd 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 commitments = dr.map(|dr| dr.commits).unwrap_or_default(); + to_json_binary(&commitments)? } 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_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 12a4c23f..e35f87c2 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) } @@ -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) } 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(); }