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

refactor: dr queries return optionals #193

Merged
merged 3 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
22 changes: 12 additions & 10 deletions contract/src/msgs/data_requests/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,28 @@ impl QueryHandler for QueryMsg {
fn query(self, deps: Deps, _env: Env) -> Result<Binary, ContractError> {
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();
Thomasvdam marked this conversation as resolved.
Show resolved Hide resolved
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)?)?)?
Thomasvdam marked this conversation as resolved.
Show resolved Hide resolved
}
QueryMsg::GetDataRequestsByStatus { status, offset, limit } => {
to_json_binary(&state::requests_by_status(deps.storage, &status, offset, limit)?)?
Expand Down
2 changes: 1 addition & 1 deletion contract/src/msgs/data_requests/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<DataRequest>> {
pub fn may_load_request(store: &dyn Storage, dr_id: &Hash) -> StdResult<Option<DataRequest>> {
DATA_REQUESTS.may_get(store, dr_id)
}

Expand Down
26 changes: 9 additions & 17 deletions contract/src/msgs/data_requests/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DataRequest> {
self.query(query::QueryMsg::GetDataRequest {
dr_id: dr_id.to_string(),
})
.unwrap()
}

#[track_caller]
pub fn post_data_request(
&mut self,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -241,23 +233,23 @@ impl TestInfo {
}

#[track_caller]
pub fn get_data_request(&self, dr_id: &str) -> DataRequest {
pub fn get_data_request(&self, dr_id: &str) -> Option<DataRequest> {
self.query(query::QueryMsg::GetDataRequest {
dr_id: dr_id.to_string(),
})
.unwrap()
}

#[track_caller]
pub fn get_data_result(&self, dr_id: &str) -> DataResult {
pub fn get_data_result(&self, dr_id: &str) -> Option<DataResult> {
self.query(query::QueryMsg::GetDataResult {
dr_id: dr_id.to_string(),
})
.unwrap()
}

#[track_caller]
pub fn get_data_result_commit(&self, dr_id: Hash, public_key: PublicKey) -> Option<Hash> {
pub fn get_data_request_commit(&self, dr_id: Hash, public_key: PublicKey) -> Option<Hash> {
self.query(query::QueryMsg::GetDataRequestCommitment {
dr_id: dr_id.to_hex(),
public_key: public_key.to_hex(),
Expand All @@ -266,12 +258,12 @@ impl TestInfo {
}

#[track_caller]
pub fn get_data_result_commits(&self, dr_id: Hash) -> HashMap<String, Hash> {
pub fn get_data_request_commits(&self, dr_id: Hash) -> HashMap<String, Hash> {
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<RevealBody> {
pub fn get_data_request_reveal(&self, dr_id: Hash, public_key: PublicKey) -> Option<RevealBody> {
self.query(query::QueryMsg::GetDataRequestReveal {
dr_id: dr_id.to_hex(),
public_key: public_key.to_hex(),
Expand All @@ -280,7 +272,7 @@ impl TestInfo {
}

#[track_caller]
pub fn get_data_result_reveals(&self, dr_id: Hash) -> HashMap<String, RevealBody> {
pub fn get_data_request_reveals(&self, dr_id: Hash) -> HashMap<String, RevealBody> {
self.query(query::QueryMsg::GetDataRequestCommitments { dr_id: dr_id.to_hex() })
.unwrap()
}
Expand Down
16 changes: 8 additions & 8 deletions contract/src/msgs/data_requests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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)])
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand Down