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: common lib #169

Merged
merged 14 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,20 @@ cosmwasm-schema = "1.5.5"
cosmwasm-std = { version = "1.5.5" }
cw-multi-test = "1.1.0"
cw-storage-plus = "1.2.0"
cw2 = "1.1.2"
cw-utils = "1.0.3"
cw2 = "1.1.2"
hex = "0.4.3"
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", version = "0.0.0" }
semver = { version = "1.0", features = ["serde"] }
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde-big-array = { version = "0.5.1" }
serde_json = "1.0.117"
sha3 = "0.10"
thiserror = { version = "1.0" }
semver = { version = "1.0", features = ["serde"] }
vrf-rs = "0.0.0"
xshell = "0.2"

Expand Down
12 changes: 7 additions & 5 deletions contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ cw-utils = { workspace = true }
cw2 = { workspace = true }
hex = { workspace = true }
schemars = { workspace = true }
seda-common = { workspace = true, features = ["cosmwasm"] }
semver = { workspace = true }
serde = { workspace = true }
serde-big-array.workspace = true
sha3.workspace = true
serde-big-array = { workspace = true }
sha3 = { workspace = true }
thiserror = { workspace = true }
vrf-rs.workspace = true
vrf-rs = { workspace = true }

[dev-dependencies]
cw-multi-test = { workspace = true }
k256 = { workspace = true }
cw-multi-test.workspace = true
serde_json.workspace = true
seda-common = { workspace = true, features = ["cosmwasm", "test-utils"] }
serde_json = { workspace = true }
2 changes: 1 addition & 1 deletion contract/src/bin/schema.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cosmwasm_schema::write_api;
use seda_contract::msgs::{ExecuteMsg, InstantiateMsg, QueryMsg};
use seda_common::msgs::*;

fn main() {
write_api! {
Expand Down
9 changes: 5 additions & 4 deletions contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
use cosmwasm_std::{entry_point, Binary, Deps, DepsMut, Env, MessageInfo, Response};
use cosmwasm_std::{StdResult, Uint128};
use cw2::set_contract_version;
use seda_common::msgs::*;
use staking::StakingConfig;

use crate::{
consts::{INITIAL_MINIMUM_STAKE_FOR_COMMITTEE_ELIGIBILITY, INITIAL_MINIMUM_STAKE_TO_REGISTER},
error::ContractError,
msgs::{
owner::state::{OWNER, PENDING_OWNER},
staking::{state::CONFIG, StakingConfig},
ExecuteMsg,
InstantiateMsg,
QueryMsg,
staking::state::CONFIG,
ExecuteHandler,
QueryHandler,
},
state::{CHAIN_ID, TOKEN},
};
Expand Down
23 changes: 0 additions & 23 deletions contract/src/crypto.rs

This file was deleted.

15 changes: 1 addition & 14 deletions contract/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use cosmwasm_std::{StdError, Uint128};
use hex::FromHexError;
use thiserror::Error;
use vrf_rs::error::VrfError;

#[derive(Error, Debug, PartialEq)]
#[cfg_attr(test, derive(Clone))]
Expand Down Expand Up @@ -67,13 +66,8 @@ pub enum ContractError {
#[error("FromHex: Invalid hexadecimal input: {0}")]
FromHex(#[from] FromHexError),

#[cfg(not(test))]
#[error(transparent)]
Prove(#[from] VrfError),

#[cfg(test)]
#[error("{0}")]
Prove(String),
Common(#[from] seda_common::error::Error),
}

#[cfg(test)]
Expand All @@ -82,10 +76,3 @@ impl From<StdError> for ContractError {
ContractError::Std(err.to_string())
}
}

#[cfg(test)]
impl From<VrfError> for ContractError {
fn from(err: VrfError) -> Self {
ContractError::Prove(err.to_string())
}
}
12 changes: 2 additions & 10 deletions contract/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
pub mod consts;
pub mod contract;
mod crypto;
mod error;
pub mod msgs;
pub mod state;
mod types;
mod utils;

use seda_common::types;

#[cfg(test)]
mod test_utils;
#[cfg(test)]
pub use test_utils::*;
// pub mod test_helpers;
#[path = ""]
#[cfg(test)]
pub(crate) mod test {

// mod config_test;
// mod staking_test;
}
19 changes: 2 additions & 17 deletions contract/src/msgs/data_requests/execute/commit_result.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
use super::*;
use crate::state::{inc_get_seq, CHAIN_ID};

#[cw_serde]
pub struct Execute {
pub(in crate::msgs::data_requests) dr_id: Hash,
pub(in crate::msgs::data_requests) commitment: Hash,
pub(in crate::msgs::data_requests) public_key: PublicKey,
pub(in crate::msgs::data_requests) proof: Vec<u8>,
}

impl Execute {
impl ExecuteHandler for execute::commit_result::Execute {
/// Posts a data result of a data request with an attached hash of the answer and salt.
pub fn execute(self, deps: DepsMut, env: Env, info: MessageInfo) -> Result<Response, ContractError> {
fn execute(self, deps: DepsMut, env: Env, info: MessageInfo) -> Result<Response, ContractError> {
let chain_id = CHAIN_ID.load(deps.storage)?;
// compute message hash
let message_hash = hash([
Expand Down Expand Up @@ -55,10 +47,3 @@ impl Execute {
))
}
}

#[cfg(test)]
impl From<Execute> for crate::msgs::ExecuteMsg {
fn from(value: Execute) -> Self {
super::ExecuteMsg::CommitDataResult(value).into()
}
}
25 changes: 7 additions & 18 deletions contract/src/msgs/data_requests/execute/mod.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
use super::*;
use super::{
msgs::data_requests::execute::{self, ExecuteMsg},
*,
};

pub(in crate::msgs::data_requests) mod commit_result;
pub(in crate::msgs::data_requests) mod post_request;
pub(in crate::msgs::data_requests) mod reveal_result;

#[cw_serde]
pub enum ExecuteMsg {
CommitDataResult(commit_result::Execute),
PostDataRequest(post_request::Execute),
RevealDataResult(reveal_result::Execute),
}

impl ExecuteMsg {
pub fn execute(self, deps: DepsMut, env: Env, info: MessageInfo) -> Result<Response, ContractError> {
impl ExecuteHandler for ExecuteMsg {
fn execute(self, deps: DepsMut, env: Env, info: MessageInfo) -> Result<Response, ContractError> {
match self {
ExecuteMsg::CommitDataResult(msg) => msg.execute(deps, env, info),
ExecuteMsg::PostDataRequest(msg) => msg.execute(deps, info),
ExecuteMsg::PostDataRequest(msg) => msg.execute(deps, env, info),
ExecuteMsg::RevealDataResult(msg) => msg.execute(deps, env, info),
}
}
}

#[cfg(test)]
impl From<ExecuteMsg> for super::ExecuteMsg {
fn from(value: ExecuteMsg) -> Self {
Self::DataRequest(Box::new(value))
}
}
18 changes: 2 additions & 16 deletions contract/src/msgs/data_requests/execute/post_request.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
use super::*;

#[cw_serde]
pub struct Execute {
pub(in crate::msgs::data_requests) posted_dr: PostDataRequestArgs,
pub(in crate::msgs::data_requests) seda_payload: Bytes,
pub(in crate::msgs::data_requests) payback_address: Bytes,
}

impl Execute {
impl ExecuteHandler for execute::post_request::Execute {
/// Posts a data request to the pool
pub fn execute(self, deps: DepsMut, _info: MessageInfo) -> Result<Response, ContractError> {
fn execute(self, deps: DepsMut, _env: Env, _info: MessageInfo) -> Result<Response, ContractError> {
// hash the inputs to get the data request id
let dr_id = self.posted_dr.hash();

Expand Down Expand Up @@ -61,10 +54,3 @@ impl Execute {
Ok(res)
}
}

#[cfg(test)]
impl From<Execute> for crate::msgs::ExecuteMsg {
fn from(value: Execute) -> Self {
super::ExecuteMsg::PostDataRequest(value).into()
}
}
19 changes: 2 additions & 17 deletions contract/src/msgs/data_requests/execute/reveal_result.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
use super::*;
use crate::state::{inc_get_seq, CHAIN_ID};

#[cw_serde]
pub struct Execute {
pub(in crate::msgs::data_requests) dr_id: Hash,
pub(in crate::msgs::data_requests) reveal_body: RevealBody,
pub(in crate::msgs::data_requests) public_key: PublicKey,
pub(in crate::msgs::data_requests) proof: Vec<u8>,
}

impl Execute {
impl ExecuteHandler for execute::reveal_result::Execute {
/// Posts a data result of a data request with an attached result.
/// This removes the data request from the pool and creates a new entry in the data results.
pub fn execute(self, deps: DepsMut, env: Env, info: MessageInfo) -> Result<Response, ContractError> {
fn execute(self, deps: DepsMut, env: Env, info: MessageInfo) -> Result<Response, ContractError> {
let chain_id = CHAIN_ID.load(deps.storage)?;

// compute hash of reveal body
Expand Down Expand Up @@ -111,10 +103,3 @@ impl Execute {
Ok(response)
}
}

#[cfg(test)]
impl From<Execute> for crate::msgs::ExecuteMsg {
fn from(value: Execute) -> Self {
super::ExecuteMsg::RevealDataResult(value).into()
}
}
Loading