Skip to content

Commit

Permalink
refactor: use custom events
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesondh committed Sep 26, 2023
1 parent 28e495c commit 5891d54
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 129 deletions.
1 change: 0 additions & 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 packages/data-requests/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use cosmwasm_std::StdResult;

// version info for migration info
const CONTRACT_NAME: &str = "data-requests";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
pub const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
Expand Down
63 changes: 50 additions & 13 deletions packages/data-requests/src/data_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use common::state::DataRequest;
use common::types::Hash;

pub mod data_requests {
use crate::contract::CONTRACT_VERSION;
use common::msg::PostDataRequestArgs;
use cosmwasm_std::Event;
use std::collections::HashMap;

use crate::{
Expand Down Expand Up @@ -44,6 +46,15 @@ pub mod data_requests {
if data_request_or_result_exists(deps.as_ref(), posted_dr.dr_id.clone()) {
return Err(ContractError::DataRequestAlreadyExists);
}

// require dr_binary_id and tally_binary_id to be non-empty
if posted_dr.dr_binary_id == *"" {
return Err(ContractError::EmptyArg("dr_binary_id".to_string()));
}
if posted_dr.tally_binary_id == *"" {
return Err(ContractError::EmptyArg("tally_binary_id".to_string()));
}

let dr_inputs = DataRequestInputs {
dr_binary_id: posted_dr.dr_binary_id.clone(),
tally_binary_id: posted_dr.tally_binary_id.clone(),
Expand Down Expand Up @@ -99,13 +110,39 @@ pub mod data_requests {
})?;

Ok(Response::new()
.add_attribute("action", "post_data_request")
.set_data(to_binary(&PostDataRequestResponse {
dr_id: posted_dr.dr_id,
dr_id: posted_dr.dr_id.clone(),
})?)
.add_attributes(vec![
("action", "post_data_request"),
("seda_data_request", &serde_json::to_string(&dr).unwrap()),
]))
.add_event(Event::new("seda-data-request").add_attributes(vec![
("version", CONTRACT_VERSION),
("dr_id", &posted_dr.dr_id),
("dr_binary_id", &posted_dr.dr_binary_id),
("tally_binary_id", &posted_dr.tally_binary_id),
(
"dr_inputs",
&serde_json::to_string(&posted_dr.dr_inputs).unwrap(),
),
(
"tally_inputs",
&serde_json::to_string(&posted_dr.tally_inputs).unwrap(),
),
("memo", &serde_json::to_string(&posted_dr.memo).unwrap()),
(
"replication_factor",
&posted_dr.replication_factor.to_string(),
),
("gas_price", &posted_dr.gas_price.to_string()),
("gas_limit", &posted_dr.gas_limit.to_string()),
(
"seda_payload",
&serde_json::to_string(&posted_dr.seda_payload).unwrap(),
),
(
"payback_address",
&serde_json::to_string(&posted_dr.payback_address).unwrap(),
),
])))
}

/// Returns a data request from the pool with the given id, if it exists.
Expand Down Expand Up @@ -193,8 +230,8 @@ mod dr_tests {
.unwrap();
let value: GetDataRequestResponse = from_binary(&res).unwrap();
assert_eq!(None, value.value);
let dr_binary_id: Hash = "".to_string();
let tally_binary_id: Hash = "".to_string();
let dr_binary_id: Hash = "dr_binary_id".to_string();
let tally_binary_id: Hash = "tally_binary_id".to_string();
let dr_inputs: Bytes = Vec::new();
let tally_inputs: Bytes = Vec::new();

Expand Down Expand Up @@ -261,8 +298,8 @@ mod dr_tests {
.unwrap();
let received_value: GetDataRequestResponse = from_binary(&res).unwrap();

let dr_binary_id: Hash = "".to_string();
let tally_binary_id: Hash = "".to_string();
let dr_binary_id: Hash = "dr_binary_id".to_string();
let tally_binary_id: Hash = "tally_binary_id".to_string();
let dr_inputs: Bytes = Vec::new();
let replication_factor: u16 = 3;

Expand Down Expand Up @@ -344,8 +381,8 @@ mod dr_tests {
};
let info = mock_info("creator", &coins(2, "token"));
let _res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();
let dr_binary_id: Hash = "".to_string();
let tally_binary_id: Hash = "".to_string();
let dr_binary_id: Hash = "dr_binary_id".to_string();
let tally_binary_id: Hash = "tally_binary_id".to_string();
let dr_inputs: Bytes = Vec::new();
let tally_inputs: Bytes = Vec::new();

Expand Down Expand Up @@ -499,8 +536,8 @@ mod dr_tests {

let payback_address: Bytes = Vec::new();

let dr_binary_id: Hash = "".to_string();
let tally_binary_id: Hash = "".to_string();
let dr_binary_id: Hash = "dr_binary_id".to_string();
let tally_binary_id: Hash = "tally_binary_id".to_string();
let dr_inputs: Bytes = Vec::new();
let tally_inputs: Bytes = Vec::new();

Expand Down
77 changes: 41 additions & 36 deletions packages/data-requests/src/data_request_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::error::ContractError;

pub mod data_request_results {

use cosmwasm_std::{Addr, Env};
use cosmwasm_std::{Addr, Env, Event};
use sha3::{Digest, Keccak256};

use common::msg::{
Expand All @@ -19,9 +19,9 @@ pub mod data_request_results {
use common::state::{DataResult, Reveal};
use common::types::Bytes;

use crate::contract::CONTRACT_VERSION;
use crate::{
state::DATA_RESULTS,
types::{CommitmentEntity, DataResultEntity, RevealEntity},
utils::{check_eligibility, hash_data_result, validate_sender},
ContractError::{
AlreadyCommitted, AlreadyRevealed, IneligibleExecutor, NotCommitted, RevealMismatch,
Expand Down Expand Up @@ -54,18 +54,14 @@ pub mod data_request_results {

DATA_REQUESTS.save(deps.storage, dr_id.clone(), &dr)?;

Ok(Response::new().add_attributes(vec![
("action", "commit_data_result"),
(
"seda_commitment",
&serde_json::to_string(&CommitmentEntity {
dr_id,
executor: sender.to_string(),
commitment,
})
.unwrap(),
),
]))
Ok(Response::new()
.add_attribute("action", "commit_data_result")
.add_event(Event::new("seda-commitment").add_attributes(vec![
("version", CONTRACT_VERSION),
("dr_id", dr_id.as_str()),
("executor", sender.as_str()),
("commitment", commitment.as_str()),
])))
}

/// Posts a data result of a data request with an attached result.
Expand Down Expand Up @@ -111,7 +107,13 @@ pub mod data_request_results {

DATA_REQUESTS.save(deps.storage, dr_id.clone(), &dr)?;

let mut dr_result_entity: DataResultEntity = None;
let mut events = vec![Event::new("seda-reveal").add_attributes(vec![
("version", CONTRACT_VERSION),
("dr_id", dr_id.as_str()),
("executor", sender.as_str()),
("reveal", serde_json::to_string(&reveal).unwrap().as_str()),
])];

if u16::try_from(dr.reveals.len()).unwrap() == dr.replication_factor {
let block_height: u64 = env.block.height;
let exit_code: u8 = 0;
Expand All @@ -123,35 +125,38 @@ pub mod data_request_results {
let result_id = hash_data_result(&dr, block_height, exit_code, &result);

let dr_result = DataResult {
result_id,
result_id: result_id.clone(),
dr_id: dr_id.clone(),
block_height,
exit_code,
result,
payback_address,
seda_payload,
result: result.clone(),
payback_address: payback_address.clone(),
seda_payload: seda_payload.clone(),
};
dr_result_entity = Some(dr_result.clone());
DATA_RESULTS.save(deps.storage, dr_id.clone(), &dr_result)?;
DATA_REQUESTS.remove(deps.storage, dr_id.clone());

events.push(Event::new("seda-data-result").add_attributes(vec![
("version", CONTRACT_VERSION),
("result_id", &result_id),
("dr_id", &dr_id),
("block_height", &block_height.to_string()),
("exit_code", &exit_code.to_string()),
("result", &serde_json::to_string(&result).unwrap()),
(
"payback_address",
&serde_json::to_string(&payback_address).unwrap(),
),
(
"seda_payload",
&serde_json::to_string(&seda_payload).unwrap(),
),
]));
}

Ok(Response::new().add_attributes(vec![
("action", "reveal_data_result"),
(
"seda_reveal",
&serde_json::to_string(&RevealEntity {
dr_id,
executor: sender.to_string(),
reveal,
})
.unwrap(),
),
(
"seda_data_request_result",
&serde_json::to_string(&dr_result_entity).unwrap(),
),
]))
Ok(Response::new()
.add_attribute("action", "reveal_data_result")
.add_events(events))
}

/// Returns a data result from the results with the given id, if it exists.
Expand Down
2 changes: 2 additions & 0 deletions packages/data-requests/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ pub enum ContractError {
RevealMismatch,
#[error("Only proxy can pass a sender")]
NotProxy,
#[error("Arg cannot be empty: {0}")]
EmptyArg(String),
}
18 changes: 0 additions & 18 deletions packages/data-requests/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,2 @@
use common::state::{DataResult, Reveal};
use common::types::Hash;
use serde::{Deserialize, Serialize};

pub type Input = Vec<u8>;
pub type PayloadItem = Vec<u8>;

#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
pub struct CommitmentEntity {
pub dr_id: Hash,
pub executor: String,
pub commitment: Hash,
}
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
pub struct RevealEntity {
pub dr_id: Hash,
pub executor: String,
pub reveal: Reveal,
}
pub type DataResultEntity = Option<DataResult>;
9 changes: 3 additions & 6 deletions packages/integration-tests/src/data_request.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::tests::utils::{proper_instantiate, USER};
use common::msg::{GetDataRequestResponse, GetDataRequestsFromPoolResponse, PostDataRequestArgs};
use common::state::DataRequest;
use common::types::{Bytes, Hash};
use cosmwasm_std::Addr;
use cw_multi_test::Executor;
Expand All @@ -14,8 +13,8 @@ fn post_data_request() {
let (mut app, proxy_contract) = proper_instantiate();

// format inputs to post data request
let dr_binary_id: Hash = "".to_string();
let tally_binary_id: Hash = "".to_string();
let dr_binary_id: Hash = "dr_binary_id".to_string();
let tally_binary_id: Hash = "tally_binary_id".to_string();
let dr_inputs: Bytes = Vec::new();
let tally_inputs: Bytes = Vec::new();
let replication_factor: u16 = 3;
Expand Down Expand Up @@ -71,9 +70,7 @@ fn post_data_request() {
// TODO: this is an ugly way to get the dr_id.
// although PostDataRequest on the DataRequest contract returns it in `data`, the Proxy contract does not yet.
// https://github.com/sedaprotocol/seda-chain-contracts/issues/68
let dr: DataRequest =
serde_json::from_str(&res.events.last().unwrap().attributes.last().unwrap().value).unwrap();
let dr_id = dr.dr_id.clone();
let dr_id = &res.events.last().unwrap().attributes[2].value;

let msg = ProxyQueryMsg::GetDataRequest {
dr_id: dr_id.clone(),
Expand Down
18 changes: 7 additions & 11 deletions packages/integration-tests/src/data_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use common::msg::{
GetCommittedDataResultResponse, GetResolvedDataResultResponse, GetRevealedDataResultResponse,
PostDataRequestArgs,
};
use common::state::{DataRequest, Reveal};
use common::state::Reveal;
use common::types::{Bytes, Hash};
use cosmwasm_std::Addr;
use cw_multi_test::Executor;
Expand Down Expand Up @@ -41,8 +41,8 @@ fn commit_reveal_result() {
assert!(res.is_err());

// format inputs to post data request with replication factor of 2
let dr_binary_id: Hash = "".to_string();
let tally_binary_id: Hash = "".to_string();
let dr_binary_id: Hash = "dr_binary_id".to_string();
let tally_binary_id: Hash = "tally_binary_id".to_string();
let dr_inputs: Bytes = Vec::new();
let tally_inputs: Bytes = Vec::new();
let replication_factor: u16 = 2;
Expand Down Expand Up @@ -95,9 +95,7 @@ fn commit_reveal_result() {
// TODO: this is an ugly way to get the dr_id.
// although PostDataRequest on the DataRequest contract returns it in `data`, the Proxy contract does not yet.
// https://github.com/sedaprotocol/seda-chain-contracts/issues/68
let dr: DataRequest =
serde_json::from_str(&res.events.last().unwrap().attributes.last().unwrap().value).unwrap();
let dr_id = dr.dr_id.clone();
let dr_id = &res.events.last().unwrap().attributes[2].value;

// executor1 commits on the data request
let reveal = "2000";
Expand Down Expand Up @@ -196,8 +194,8 @@ fn ineligible_post_data_result() {
let (mut app, proxy_contract) = proper_instantiate();

// post a data request
let dr_binary_id: Hash = "".to_string();
let tally_binary_id: Hash = "".to_string();
let dr_binary_id: Hash = "dr_binary_id".to_string();
let tally_binary_id: Hash = "tally_binary_id".to_string();
let dr_inputs: Bytes = Vec::new();
let tally_inputs: Bytes = Vec::new();
let replication_factor: u16 = 2;
Expand Down Expand Up @@ -248,9 +246,7 @@ fn ineligible_post_data_result() {
// TODO: this is an ugly way to get the dr_id.
// although PostDataRequest on the DataRequest contract returns it in `data`, the Proxy contract does not yet.
// https://github.com/sedaprotocol/seda-chain-contracts/issues/68
let dr: DataRequest =
serde_json::from_str(&res.events.last().unwrap().attributes.last().unwrap().value).unwrap();
let dr_id = dr.dr_id.clone();
let dr_id = &res.events.last().unwrap().attributes[2].value;

// ineligible shouldn't be able to post a data result
let reveal = "2000";
Expand Down
1 change: 0 additions & 1 deletion packages/staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,5 @@ common = { workspace = true }
cosmwasm-schema = { workspace = true }
cosmwasm-std = { workspace = true }
cw-storage-plus = { workspace = true }
serde_json = { workspace = true }
thiserror = { workspace = true }
cw2 = { workspace = true }
2 changes: 1 addition & 1 deletion packages/staking/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use cosmwasm_std::StdResult;

// version info for migration info
const CONTRACT_NAME: &str = "staking";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
pub const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
Expand Down
Loading

0 comments on commit 5891d54

Please sign in to comment.