Skip to content

Commit

Permalink
refactor: match changes in common
Browse files Browse the repository at this point in the history
  • Loading branch information
gluax committed Jul 30, 2024
1 parent 9c55c64 commit bc8cec4
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 116 deletions.
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.

3 changes: 1 addition & 2 deletions contract/src/msgs/data_requests/execute/commit_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ impl ExecuteHandler for execute::commit_result::Execute {

// verify the proof
let chain_id = CHAIN_ID.load(deps.storage)?;
let proof = Vec::<u8>::from_hex_str(&self.proof)?;
self.verify(&public_key, &proof, &chain_id, env.contract.address.as_str(), dr.height)?;
self.verify(&public_key, &chain_id, env.contract.address.as_str(), dr.height)?;

// add the commitment to the data request
let commitment = Hash::from_hex_str(&self.commitment)?;
Expand Down
5 changes: 2 additions & 3 deletions contract/src/msgs/data_requests/execute/reveal_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ impl ExecuteHandler for execute::reveal_result::Execute {

// verify the proof
let chain_id = CHAIN_ID.load(deps.storage)?;
let proof = Vec::<u8>::from_hex_str(&self.proof)?;
let public_key = PublicKey::from_hex_str(&self.public_key)?;
let reveal_body_hash = self.reveal_body.try_hash()?;
self.verify(
&public_key,
&proof,
&chain_id,
env.contract.address.as_str(),
(dr.height, reveal_body_hash),
dr.height,
reveal_body_hash,
)?;

// error if data request executor has not submitted a commitment
Expand Down
78 changes: 37 additions & 41 deletions contract/src/msgs/data_requests/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,19 @@ impl TestInfo {
let dr = self.get_data_request(dr_id).unwrap();
let commitment = commitment.to_hex();

let mut msg = execute::commit_result::Execute {
dr_id: dr_id.to_string(),
let msg = execute::commit_result::Execute::new(
dr_id.to_string(),
commitment,
public_key: sender.pub_key_hex(),
proof: Default::default(),
};
msg.proof = msg
.sign(
&sender.sign_key(),
&msg.msg_hash(self.chain_id(), self.contract_addr(), dr.height)?,
)?
.to_hex();

self.execute(sender, &msg.into())
sender.pub_key_hex(),
&sender.sign_key(),
self.chain_id(),
self.contract_addr(),
dr.height,
)?
.into();
// msg.sign(signing_key, msg_hash)

self.execute(sender, &msg)
}

#[track_caller]
Expand All @@ -153,20 +152,18 @@ impl TestInfo {
let dr = self.get_data_request(dr_id).unwrap();
let commitment = commitment.to_hex();

let mut msg = execute::commit_result::Execute {
dr_id: dr_id.to_string(),
let msg = execute::commit_result::Execute::new(
dr_id.to_string(),
commitment,
public_key: sender.pub_key_hex(),
proof: Default::default(),
};
msg.proof = msg
.sign(
&sender.sign_key(),
&msg.msg_hash(self.chain_id(), self.contract_addr(), dr.height.saturating_sub(3))?,
)?
.to_hex();

self.execute(sender, &msg.into())
sender.pub_key_hex(),
&sender.sign_key(),
self.chain_id(),
self.contract_addr(),
dr.height.saturating_sub(3),
)?
.into();

self.execute(sender, &msg)
}

#[track_caller]
Expand All @@ -179,22 +176,21 @@ impl TestInfo {
let dr = self.get_data_request(dr_id).unwrap();
let reveal_body_hash = reveal_body.try_hash()?;

let mut msg = execute::reveal_result::Execute {
let msg = execute::reveal_result::Execute::new(
dr_id.to_string(),
reveal_body,
dr_id: dr_id.to_string(),
public_key: sender.pub_key_hex(),
proof: Default::default(),
stderr: vec![],
stdout: vec![],
};
msg.proof = msg
.sign(
&sender.sign_key(),
&msg.msg_hash(self.chain_id(), self.contract_addr(), (dr.height, reveal_body_hash))?,
)?
.to_hex();

self.execute(sender, &msg.into())
sender.pub_key_hex(),
vec![],
vec![],
&sender.sign_key(),
self.chain_id(),
self.contract_addr(),
dr.height,
reveal_body_hash,
)?
.into();

self.execute(sender, &msg)
}

#[track_caller]
Expand Down
2 changes: 0 additions & 2 deletions contract/src/msgs/staking/execute/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ impl ExecuteHandler for execute::stake::Execute {
fn execute(self, deps: DepsMut, env: Env, info: MessageInfo) -> Result<Response, ContractError> {
// verify the proof
let chain_id = CHAIN_ID.load(deps.storage)?;
let proof = Vec::<u8>::from_hex_str(&self.proof)?;
let public_key = PublicKey::from_hex_str(&self.public_key)?;
self.verify(
&public_key,
&proof,
&chain_id,
env.contract.address.as_str(),
inc_get_seq(deps.storage, &public_key)?,
Expand Down
2 changes: 0 additions & 2 deletions contract/src/msgs/staking/execute/unstake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ impl ExecuteHandler for execute::unstake::Execute {
fn execute(self, deps: DepsMut, env: Env, _info: MessageInfo) -> Result<Response, ContractError> {
// verify the proof
let chain_id = CHAIN_ID.load(deps.storage)?;
let proof = Vec::<u8>::from_hex_str(&self.proof)?;
let public_key = PublicKey::from_hex_str(&self.public_key)?;
self.verify(
&public_key,
&proof,
&chain_id,
env.contract.address.as_str(),
inc_get_seq(deps.storage, &public_key)?,
Expand Down
2 changes: 0 additions & 2 deletions contract/src/msgs/staking/execute/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ impl ExecuteHandler for execute::withdraw::Execute {
fn execute(self, deps: DepsMut, env: Env, info: MessageInfo) -> Result<Response, ContractError> {
// verify the proof
let chain_id = CHAIN_ID.load(deps.storage)?;
let proof = Vec::<u8>::from_hex_str(&self.proof)?;
let public_key = PublicKey::from_hex_str(&self.public_key)?;
self.verify(
&public_key,
&proof,
&chain_id,
env.contract.address.as_str(),
inc_get_seq(deps.storage, &public_key)?,
Expand Down
116 changes: 53 additions & 63 deletions contract/src/msgs/staking/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,17 @@ impl TestInfo {
let memo = memo.map(|s| Binary::from(s.as_bytes()));
let seq = self.get_account_sequence(sender.pub_key());

let mut msg = execute::stake::Execute {
public_key: sender.pub_key_hex(),
proof: Default::default(),
let msg = execute::stake::Execute::new(
sender.pub_key_hex(),
memo,
};
msg.proof = msg
.sign(
&sender.sign_key(),
&msg.msg_hash(self.chain_id(), self.contract_addr(), seq.into())?,
)?
.to_hex();

self.execute_with_funds(sender, &msg.into(), amount)
&sender.sign_key(),
self.chain_id(),
self.contract_addr(),
seq.into(),
)?
.into();

self.execute_with_funds(sender, &msg, amount)
}

#[track_caller]
Expand All @@ -48,19 +46,17 @@ impl TestInfo {
pub fn increase_stake(&mut self, sender: &mut TestExecutor, amount: u128) -> Result<(), ContractError> {
let seq = self.get_account_sequence(sender.pub_key());

let mut msg = execute::stake::Execute {
public_key: sender.pub_key_hex(),
proof: Default::default(),
memo: None,
};
msg.proof = msg
.sign(
&sender.sign_key(),
&msg.msg_hash(self.chain_id(), self.contract_addr(), seq.into())?,
)?
.to_hex();

self.execute_with_funds(sender, &msg.into(), amount)
let msg = execute::stake::Execute::new(
sender.pub_key_hex(),
None,
&sender.sign_key(),
self.chain_id(),
self.contract_addr(),
seq.into(),
)?
.into();

self.execute_with_funds(sender, &msg, amount)
}

#[track_caller]
Expand All @@ -72,57 +68,51 @@ impl TestInfo {
let memo = memo.map(|s| Binary::from(s.as_bytes()));
let seq = self.get_account_sequence(sender.pub_key());

let mut msg = execute::stake::Execute {
public_key: sender.pub_key_hex(),
proof: Default::default(),
let msg = execute::stake::Execute::new(
sender.pub_key_hex(),
memo,
};
msg.proof = msg
.sign(
&sender.sign_key(),
&msg.msg_hash(self.chain_id(), self.contract_addr(), seq.into())?,
)?
.to_hex();

self.execute(sender, &msg.into())
&sender.sign_key(),
self.chain_id(),
self.contract_addr(),
seq.into(),
)?
.into();

self.execute(sender, &msg)
}

#[track_caller]
pub fn unstake(&mut self, sender: &TestExecutor, amount: u128) -> Result<(), ContractError> {
let seq = self.get_account_sequence(sender.pub_key());

let mut msg = execute::unstake::Execute {
public_key: sender.pub_key_hex(),
proof: Default::default(),
amount: amount.into(),
};
msg.proof = msg
.sign(
&sender.sign_key(),
&msg.msg_hash(self.chain_id(), self.contract_addr(), seq.into())?,
)?
.to_hex();

self.execute(sender, &msg.into())
let msg = execute::unstake::Execute::new(
sender.pub_key_hex(),
amount.into(),
&sender.sign_key(),
self.chain_id(),
self.contract_addr(),
seq.into(),
)?
.into();

self.execute(sender, &msg)
}

#[track_caller]
pub fn withdraw(&mut self, sender: &mut TestExecutor, amount: u128) -> Result<(), ContractError> {
let seq = self.get_account_sequence(sender.pub_key());

let mut msg = execute::withdraw::Execute {
public_key: sender.pub_key_hex(),
proof: Default::default(),
amount: amount.into(),
};
msg.proof = msg
.sign(
&sender.sign_key(),
&msg.msg_hash(self.chain_id(), self.contract_addr(), seq.into())?,
)?
.to_hex();

let res = self.execute(sender, &msg.into());
let msg = execute::withdraw::Execute::new(
sender.pub_key_hex(),
amount.into(),
&sender.sign_key(),
self.chain_id(),
self.contract_addr(),
seq.into(),
)?
.into();

let res = self.execute(sender, &msg);
sender.add_seda(10);
res
}
Expand Down

0 comments on commit bc8cec4

Please sign in to comment.