Skip to content

Commit

Permalink
refactor(drs): acc seq for drs
Browse files Browse the repository at this point in the history
  • Loading branch information
gluax committed Jun 5, 2024
1 parent 997799d commit 0c74455
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 33 deletions.
5 changes: 5 additions & 0 deletions contract/src/msgs/data_requests/execute/commit_result.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use crate::state::{inc_get_seq, CHAIN_ID};

#[cw_serde]
pub struct Execute {
Expand All @@ -11,12 +12,16 @@ pub struct Execute {
impl 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> {
let chain_id = CHAIN_ID.load(deps.storage)?;
// compute message hash
let message_hash = hash([
"commit_data_result".as_bytes(),
&self.dr_id,
&env.block.height.to_be_bytes(),
&self.commitment,
chain_id.as_bytes(),
env.contract.address.as_str().as_bytes(),
&inc_get_seq(deps.storage, &self.public_key)?.to_be_bytes(),
]);

// verify the proof
Expand Down
6 changes: 1 addition & 5 deletions contract/src/msgs/data_requests/execute/post_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ impl Execute {
return Err(ContractError::DataRequestAlreadyExists);
}

// TODO: verify the payback address... it's not a seda addr right?
// let Ok(addr) = Addr::from_slice(&self.payback_address) else {
// return Err(ContractError::InvalidPaybackAddr);
// };

// TODO: verify the payback non seda address...
// TODO: review this event
let res = Response::new()
.add_attribute("action", "post_data_request")
Expand Down
6 changes: 6 additions & 0 deletions contract/src/msgs/data_requests/execute/reveal_result.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use crate::state::{inc_get_seq, CHAIN_ID};

#[cw_serde]
pub struct Execute {
Expand All @@ -12,6 +13,8 @@ impl 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> {
let chain_id = CHAIN_ID.load(deps.storage)?;

// compute hash of reveal body
let reveal_body_hash = self.reveal_body.hash();

Expand All @@ -21,6 +24,9 @@ impl Execute {
&self.dr_id,
&env.block.height.to_be_bytes(),
&reveal_body_hash,
chain_id.as_bytes(),
env.contract.address.as_str().as_bytes(),
&inc_get_seq(deps.storage, &self.public_key)?.to_be_bytes(),
]);

// verify the proof
Expand Down
4 changes: 2 additions & 2 deletions contract/src/msgs/staking/execute/increase_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use self::staking::owner::utils::is_staker_allowed;
use super::*;
use crate::{
crypto::{hash, verify_proof},
state::{CHAIN_ID, TOKEN},
state::{inc_get_seq, CHAIN_ID, TOKEN},
utils::get_attached_funds,
};

Expand All @@ -24,7 +24,7 @@ impl Execute {
"increase_stake".as_bytes(),
chain_id.as_bytes(),
env.contract.address.as_str().as_bytes(),
&state::inc_get_seq(deps.storage, &self.public_key)?.to_be_bytes(),
&inc_get_seq(deps.storage, &self.public_key)?.to_be_bytes(),
]);

// verify the proof
Expand Down
4 changes: 2 additions & 2 deletions contract/src/msgs/staking/execute/register_and_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::*;
use crate::{
crypto::{hash, verify_proof},
msgs::staking::Staker,
state::{CHAIN_ID, TOKEN},
state::{inc_get_seq, CHAIN_ID, TOKEN},
types::{Hasher, PublicKey},
utils::get_attached_funds,
};
Expand All @@ -25,7 +25,7 @@ impl Execute {
&self.memo.hash(),
chain_id.as_bytes(),
env.contract.address.as_str().as_bytes(),
&state::inc_get_seq(deps.storage, &self.public_key)?.to_be_bytes(),
&inc_get_seq(deps.storage, &self.public_key)?.to_be_bytes(),
]);

// verify the proof
Expand Down
4 changes: 2 additions & 2 deletions contract/src/msgs/staking/execute/unregister.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;
use crate::{
crypto::{hash, verify_proof},
state::CHAIN_ID,
state::{inc_get_seq, CHAIN_ID},
};

#[cw_serde]
Expand All @@ -19,7 +19,7 @@ impl Execute {
"unregister".as_bytes(),
chain_id.as_bytes(),
env.contract.address.as_str().as_bytes(),
&state::inc_get_seq(deps.storage, &self.public_key)?.to_be_bytes(),
&inc_get_seq(deps.storage, &self.public_key)?.to_be_bytes(),
]);

// verify the proof
Expand Down
4 changes: 2 additions & 2 deletions contract/src/msgs/staking/execute/unstake.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;
use crate::{
crypto::{hash, verify_proof},
state::CHAIN_ID,
state::{inc_get_seq, CHAIN_ID},
};

#[cw_serde]
Expand All @@ -21,7 +21,7 @@ impl Execute {
&self.amount.to_be_bytes(),
chain_id.as_bytes(),
env.contract.address.as_str().as_bytes(),
&state::inc_get_seq(deps.storage, &self.public_key)?.to_be_bytes(),
&inc_get_seq(deps.storage, &self.public_key)?.to_be_bytes(),
]);

// verify the proof
Expand Down
4 changes: 2 additions & 2 deletions contract/src/msgs/staking/execute/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use cosmwasm_std::{coins, BankMsg};
use super::*;
use crate::{
crypto::{hash, verify_proof},
state::{CHAIN_ID, TOKEN},
state::{inc_get_seq, CHAIN_ID, TOKEN},
};

#[cw_serde]
Expand All @@ -23,7 +23,7 @@ impl Execute {
&self.amount.to_be_bytes(),
chain_id.as_bytes(),
env.contract.address.as_str().as_bytes(),
&state::inc_get_seq(deps.storage, &self.public_key)?.to_be_bytes(),
&inc_get_seq(deps.storage, &self.public_key)?.to_be_bytes(),
]);

// verify the proof
Expand Down
9 changes: 5 additions & 4 deletions contract/src/msgs/staking/query.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{state::CONFIG, *};
use super::*;
use crate::state::get_seq;

#[cw_serde]
pub struct StakerAndSeq {
Expand Down Expand Up @@ -27,18 +28,18 @@ impl QueryMsg {
match self {
QueryMsg::GetStaker { public_key: executor } => to_json_binary(&utils::get_staker(deps, &executor)?),
QueryMsg::GetAccountSeq { public_key } => {
let seq: Uint128 = state::get_seq(deps.storage, &public_key)?.into();
let seq: Uint128 = get_seq(deps.storage, &public_key)?.into();
to_json_binary(&seq)
}
QueryMsg::GetStakerAndSeq { public_key } => {
let staker = utils::get_staker(deps, &public_key)?;
let seq: Uint128 = state::get_seq(deps.storage, &public_key)?.into();
let seq: Uint128 = get_seq(deps.storage, &public_key)?.into();
to_json_binary(&StakerAndSeq { staker, seq })
}
QueryMsg::IsExecutorEligible { public_key: executor } => {
to_json_binary(&utils::is_executor_eligible(deps, executor)?)
}
QueryMsg::GetStakingConfig {} => to_json_binary(&CONFIG.load(deps.storage)?),
QueryMsg::GetStakingConfig {} => to_json_binary(&state::CONFIG.load(deps.storage)?),
}
}
}
Expand Down
13 changes: 0 additions & 13 deletions contract/src/msgs/staking/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,3 @@ pub const CONFIG: Item<StakingConfig> = Item::new("config");

/// A map of stakers (of address to info).
pub const STAKERS: Map<&PublicKey, Staker> = Map::new("data_request_executors");

/// A map of public key to account sequence number.
const ACCOUNT_SEQ: Map<&PublicKey, u128> = Map::new("account_seq");

pub fn get_seq(store: &dyn Storage, public_key: &PublicKey) -> StdResult<u128> {
ACCOUNT_SEQ.may_load(store, public_key).map(|x| x.unwrap_or_default())
}

pub fn inc_get_seq(store: &mut dyn Storage, public_key: &PublicKey) -> StdResult<u128> {
let seq = ACCOUNT_SEQ.may_load(store, public_key)?.unwrap_or_default();
ACCOUNT_SEQ.save(store, public_key, &(seq + 1))?;
Ok(seq)
}
18 changes: 17 additions & 1 deletion contract/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
use cw_storage_plus::Item;
use cosmwasm_std::{StdResult, Storage};
use cw_storage_plus::{Item, Map};

use crate::types::PublicKey;

/// Token denom used for staking (e.g., `aseda`).
pub const TOKEN: Item<String> = Item::new("token");

/// Chain ID of the network (e.g., `seda-1`).
/// Used as a "magic number"
pub const CHAIN_ID: Item<String> = Item::new("chain_id");

/// A map of public key to account sequence number.
const ACCOUNT_SEQ: Map<&PublicKey, u128> = Map::new("account_seq");

pub fn get_seq(store: &dyn Storage, public_key: &PublicKey) -> StdResult<u128> {
ACCOUNT_SEQ.may_load(store, public_key).map(|x| x.unwrap_or_default())
}

pub fn inc_get_seq(store: &mut dyn Storage, public_key: &PublicKey) -> StdResult<u128> {
let seq = ACCOUNT_SEQ.may_load(store, public_key)?.unwrap_or_default();
ACCOUNT_SEQ.save(store, public_key, &(seq + 1))?;
Ok(seq)
}

0 comments on commit 0c74455

Please sign in to comment.