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

feat: batch post results #187

Merged
merged 4 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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 = "feat/batch-post-results" }
# leaving this in to make local development easier
# seda-common = { path = "../seda-common-rs" }
semver = { version = "1.0", features = ["serde"] }
Expand Down
29 changes: 29 additions & 0 deletions contract/src/msgs/data_requests/sudo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,40 @@ use super::{
*,
};
pub(in crate::msgs::data_requests) mod post_result;
pub(in crate::msgs::data_requests) mod post_results;

fn post_result(result: sudo::PostResult, deps: &mut DepsMut, env: &Env) -> Result<Event, ContractError> {
// find the data request from the committed pool (if it exists, otherwise error)
let dr_id = Hash::from_hex_str(&result.dr_id)?;
let dr = state::load_request(deps.storage, &dr_id)?;

if !dr.is_tallying() {
return Err(ContractError::NotEnoughReveals);
}

let block_height: u64 = env.block.height;

let event = Event::new("seda-data-post-result").add_attributes([
gluax marked this conversation as resolved.
Show resolved Hide resolved
("version", CONTRACT_VERSION.to_string()),
("dr_id", result.dr_id),
("block_height", block_height.to_string()),
("exit_code", result.exit_code.to_string()),
("result", to_json_string(&result.result)?),
("payback_address", dr.payback_address.to_base64()),
("seda_payload", dr.seda_payload.to_base64()),
]);

state::post_result(deps.storage, &dr_id, &result.result)?;

let result_id = result.result.try_hash()?;
Ok(event.add_attribute("result_id", result_id.to_hex()))
}

impl SudoHandler for SudoMsg {
fn sudo(self, deps: DepsMut, env: Env) -> Result<Response, ContractError> {
match self {
SudoMsg::PostDataResult(sudo) => sudo.sudo(deps, env),
SudoMsg::PostDataResults(sudo) => sudo.sudo(deps, env),
}
}
}
29 changes: 3 additions & 26 deletions contract/src/msgs/data_requests/sudo/post_result.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,9 @@
use super::*;

impl SudoHandler for sudo::post_result::Sudo {
impl SudoHandler for sudo::PostResult {
/// Posts a data request to the pool
gluax marked this conversation as resolved.
Show resolved Hide resolved
fn sudo(self, deps: DepsMut, env: Env) -> Result<Response, ContractError> {
// find the data request from the committed pool (if it exists, otherwise error)
let dr_id = Hash::from_hex_str(&self.dr_id)?;
let dr = state::load_request(deps.storage, &dr_id)?;

if !dr.is_tallying() {
return Err(ContractError::NotEnoughReveals);
}

let block_height: u64 = env.block.height;

let event = Event::new("seda-data-post-result").add_attributes([
("version", CONTRACT_VERSION.to_string()),
("dr_id", self.dr_id.clone()),
("block_height", block_height.to_string()),
("exit_code", self.exit_code.to_string()),
("result", to_json_string(&self.result)?),
("payback_address", dr.payback_address.to_base64()),
("seda_payload", dr.seda_payload.to_base64()),
]);

state::post_result(deps.storage, &dr_id, &self.result)?;

let result_id = self.result.try_hash()?;
let event = event.add_attribute("result_id", result_id.to_hex());
fn sudo(self, mut deps: DepsMut, env: Env) -> Result<Response, ContractError> {
let event = post_result(self, &mut deps, &env)?;

Ok(Response::new().add_event(event))
}
Expand Down
16 changes: 16 additions & 0 deletions contract/src/msgs/data_requests/sudo/post_results.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use super::*;

impl SudoHandler for sudo::post_results::Sudo {
/// Posts a data request to the pool
gluax marked this conversation as resolved.
Show resolved Hide resolved
fn sudo(self, mut deps: DepsMut, env: Env) -> Result<Response, ContractError> {
let mut response = Response::new();
for event in self
.results
.into_iter()
.map(|result| post_result(result, &mut deps, &env))
{
response = response.add_event(event?);
}
Ok(response)
}
}
27 changes: 23 additions & 4 deletions contract/src/msgs/data_requests/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl TestInfo {

#[track_caller]
pub fn post_data_result(&mut self, dr_id: String, result: DataResult, exit_code: u8) -> Result<(), ContractError> {
let msg = sudo::post_result::Sudo {
let msg = sudo::PostResult {
dr_id,
result,
exit_code,
Expand All @@ -223,16 +223,35 @@ impl TestInfo {
}

#[track_caller]
pub fn get_data_request(&self, dr_id: &String) -> DataRequest {
pub fn post_data_results(&mut self, results: Vec<(String, DataResult, u8)>) -> Result<(), ContractError> {
let msg = sudo::post_results::Sudo {
results: results
.into_iter()
.map(|(dr_id, result, exit_code)| sudo::PostResult {
dr_id,
result,
exit_code,
})
.collect(),
}
.into();
self.sudo(&msg)
}

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

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

#[track_caller]
Expand Down
58 changes: 56 additions & 2 deletions contract/src/msgs/data_requests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,6 @@ fn reveal_must_match_commitment() {
fn post_data_result() {
let mut test_info = TestInfo::init();

// post a data request
// post a data request
let alice = test_info.new_executor("alice", Some(2));
let dr = test_helpers::calculate_dr_id_and_args(1, 1);
Expand All @@ -409,7 +408,62 @@ fn post_data_result() {
// owner posts a data result
let dr = test_info.get_data_request(&dr_id);
let result = test_helpers::construct_result(dr, alice_reveal, 0);
test_info.post_data_result(dr_id, result, 0).unwrap();
test_info.post_data_result(dr_id.clone(), result, 0).unwrap();

// check we can get the results
let _res1 = test_info.get_data_result(&dr_id);
}

#[test]
fn post_data_results() {
let mut test_info = TestInfo::init();

// post data request 1
let alice = test_info.new_executor("alice", Some(2));
let dr1 = test_helpers::calculate_dr_id_and_args(1, 1);
let dr_id1 = test_info.post_data_request(&alice, dr1, vec![], vec![], 1).unwrap();

// alice commits data result 1
let alice_reveal1 = RevealBody {
salt: alice.salt(),
reveal: "10".hash().into(),
gas_used: 0u128.into(),
exit_code: 0,
};
test_info
.commit_result(&alice, &dr_id1, alice_reveal1.try_hash().unwrap())
.unwrap();
test_info.reveal_result(&alice, &dr_id1, alice_reveal1.clone()).unwrap();

// post data request 2
let alice = test_info.new_executor("alice", Some(2));
let dr2 = test_helpers::calculate_dr_id_and_args(2, 1);
let dr_id2 = test_info.post_data_request(&alice, dr2, vec![], vec![], 2).unwrap();

// alice commits data result 2
let alice_reveal2 = RevealBody {
salt: alice.salt(),
reveal: "10".hash().into(),
gas_used: 0u128.into(),
exit_code: 0,
};
test_info
.commit_result(&alice, &dr_id2, alice_reveal2.try_hash().unwrap())
.unwrap();
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 result1 = test_helpers::construct_result(dr1, alice_reveal1, 0);
let dr2 = test_info.get_data_request(&dr_id2);
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)])
.unwrap();

// check we can get the results
let _res1 = test_info.get_data_result(&dr_id1);
let _res2 = test_info.get_data_result(&dr_id2);
}

#[test]
Expand Down