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

fix: update hashing function to latest spec #64

Merged
merged 1 commit into from
Sep 25, 2023
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
175 changes: 107 additions & 68 deletions packages/data-requests/src/data_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,13 @@ mod dr_tests {
use crate::msg::InstantiateMsg;
use crate::state::DataRequestInputs;
use crate::utils::hash_data_request;
use crate::utils::hash_update;
use common::msg::DataRequestsExecuteMsg as ExecuteMsg;
use common::msg::DataRequestsQueryMsg as QueryMsg;
use common::msg::GetDataRequestResponse;
use common::msg::PostDataRequestArgs;
use common::state::Reveal;
use common::types::Bytes;
use common::types::Commitment;
use common::types::Memo;
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cosmwasm_std::{coins, from_binary};
use sha3::Digest;
Expand Down Expand Up @@ -205,17 +203,15 @@ mod dr_tests {

// set by relayer and SEDA protocol
let seda_payload: Bytes = Vec::new();

let chain_id = 31337;
let nonce = 1;
let value = "hello world".to_string();
let payback_address: Bytes = Vec::new();

// memo
let chain_id: u128 = 31337;
let nonce: u128 = 1;
let mut hasher = Keccak256::new();
hash_update(&mut hasher, &chain_id);
hash_update(&mut hasher, &nonce);
hasher.update(value);
let binary_hash = format!("0x{}", hex::encode(hasher.finalize()));
let memo1: Memo = binary_hash.clone().into_bytes();
hasher.update(chain_id.to_be_bytes());
hasher.update(nonce.to_be_bytes());
let memo1 = hasher.finalize().to_vec();

let dr_inputs1 = DataRequestInputs {
dr_binary_id: dr_binary_id.clone(),
Expand Down Expand Up @@ -275,15 +271,15 @@ mod dr_tests {
let seda_payload: Bytes = Vec::new();
let commits: HashMap<String, Commitment> = HashMap::new();
let reveals: HashMap<String, Reveal> = HashMap::new();
let chain_id = 31337;
let nonce = 1;
let value = "hello world".to_string();

// memo
let chain_id: u128 = 31337;
let nonce: u128 = 1;
let mut hasher = Keccak256::new();
hash_update(&mut hasher, &chain_id);
hash_update(&mut hasher, &nonce);
hasher.update(value);
let binary_hash = format!("0x{}", hex::encode(hasher.finalize()));
let memo1: Memo = binary_hash.clone().into_bytes();
hasher.update(chain_id.to_be_bytes());
hasher.update(nonce.to_be_bytes());
let memo1 = hasher.finalize().to_vec();

let dr_inputs1 = DataRequestInputs {
dr_binary_id: dr_binary_id.clone(),
tally_binary_id: tally_binary_id.clone(),
Expand Down Expand Up @@ -358,17 +354,16 @@ mod dr_tests {

// set by relayer and SEDA protocol
let seda_payload: Bytes = Vec::new();

let chain_id = 31337;
let nonce = 1;
let value = 1;
let payback_address: Bytes = Vec::new();

// memo
let chain_id: u128 = 31337;
let nonce: u128 = 1;
let mut hasher = Keccak256::new();
hash_update(&mut hasher, &chain_id);
hash_update(&mut hasher, &nonce);
hash_update(&mut hasher, &value);
let binary_hash1 = format!("0x{}", hex::encode(hasher.finalize()));
let memo1: Memo = binary_hash1.clone().into_bytes();
hasher.update(chain_id.to_be_bytes());
hasher.update(nonce.to_be_bytes());
let memo1 = hasher.finalize().to_vec();

let dr_inputs1 = DataRequestInputs {
dr_binary_id: dr_binary_id.clone(),
tally_binary_id: tally_binary_id.clone(),
Expand All @@ -385,15 +380,13 @@ mod dr_tests {
};
let constructed_dr_id1 = hash_data_request(dr_inputs1);

let chain_id = 31337;
let nonce = 1;
let value = 2;
// memo
let chain_id: u128 = 31337;
let nonce: u128 = 2;
let mut hasher = Keccak256::new();
hash_update(&mut hasher, &chain_id);
hash_update(&mut hasher, &nonce);
hash_update(&mut hasher, &value);
let binary_hash2 = format!("0x{}", hex::encode(hasher.finalize()));
let memo2: Memo = binary_hash2.clone().into_bytes();
hasher.update(chain_id.to_be_bytes());
hasher.update(nonce.to_be_bytes());
let memo2 = hasher.finalize().to_vec();

let dr_inputs2 = DataRequestInputs {
dr_binary_id: dr_binary_id.clone(),
Expand All @@ -411,15 +404,13 @@ mod dr_tests {
};
let constructed_dr_id2 = hash_data_request(dr_inputs2);

let chain_id = 31337;
let nonce = 1;
let value = 3;
// memo
let chain_id: u128 = 31337;
let nonce: u128 = 3;
let mut hasher = Keccak256::new();
hash_update(&mut hasher, &chain_id);
hash_update(&mut hasher, &nonce);
hash_update(&mut hasher, &value);
let binary_hash3 = format!("0x{}", hex::encode(hasher.finalize()));
let memo3: Memo = binary_hash3.clone().into_bytes();
hasher.update(chain_id.to_be_bytes());
hasher.update(nonce.to_be_bytes());
let memo3 = hasher.finalize().to_vec();

let dr_inputs3 = DataRequestInputs {
dr_binary_id: dr_binary_id.clone(),
Expand Down Expand Up @@ -522,15 +513,13 @@ mod dr_tests {
let commits: HashMap<String, Commitment> = HashMap::new();
let reveals: HashMap<String, Reveal> = HashMap::new();

let chain_id = 31337;
let nonce = 1;
let value = 1;
// memo
let chain_id: u128 = 31337;
let nonce: u128 = 1;
let mut hasher = Keccak256::new();
hash_update(&mut hasher, &chain_id);
hash_update(&mut hasher, &nonce);
hash_update(&mut hasher, &value);
let binary_hash1 = format!("0x{}", hex::encode(hasher.finalize()));
let memo1: Memo = binary_hash1.clone().into_bytes();
hasher.update(chain_id.to_be_bytes());
hasher.update(nonce.to_be_bytes());
let memo1 = hasher.finalize().to_vec();

let dr_inputs1 = DataRequestInputs {
dr_binary_id: dr_binary_id.clone(),
Expand All @@ -548,15 +537,13 @@ mod dr_tests {
};
let constructed_dr_id1 = hash_data_request(dr_inputs1);

let chain_id = 31337;
let nonce = 1;
let value = 2;
// memo
let chain_id: u128 = 31337;
let nonce: u128 = 2;
let mut hasher = Keccak256::new();
hash_update(&mut hasher, &chain_id);
hash_update(&mut hasher, &nonce);
hash_update(&mut hasher, &value);
let binary_hash2 = format!("0x{}", hex::encode(hasher.finalize()));
let memo2: Memo = binary_hash2.clone().into_bytes();
hasher.update(chain_id.to_be_bytes());
hasher.update(nonce.to_be_bytes());
let memo2 = hasher.finalize().to_vec();

let dr_inputs2 = DataRequestInputs {
dr_binary_id: dr_binary_id.clone(),
Expand All @@ -574,15 +561,13 @@ mod dr_tests {
};
let constructed_dr_id2 = hash_data_request(dr_inputs2);

let chain_id = 31337;
let nonce = 1;
let value = 3;
// memo
let chain_id: u128 = 31337;
let nonce: u128 = 3;
let mut hasher = Keccak256::new();
hash_update(&mut hasher, &chain_id);
hash_update(&mut hasher, &nonce);
hash_update(&mut hasher, &value);
let binary_hash3 = format!("0x{}", hex::encode(hasher.finalize()));
let memo3: Memo = binary_hash3.clone().into_bytes();
hasher.update(chain_id.to_be_bytes());
hasher.update(nonce.to_be_bytes());
let memo3 = hasher.finalize().to_vec();

let dr_inputs3 = DataRequestInputs {
dr_binary_id: dr_binary_id.clone(),
Expand Down Expand Up @@ -797,4 +782,58 @@ mod dr_tests {
response
);
}

#[test]
fn test_hash_data_request() {
let mut deps = mock_dependencies();

// instantiate contract
let msg = InstantiateMsg {
token: "token".to_string(),
proxy: "proxy".to_string(),
};
let info = mock_info("creator", &coins(2, "token"));
instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();

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

let replication_factor: u16 = 123;

// set by dr creator
let gas_price: u128 = 456;
let gas_limit: u128 = 789;

// set by relayer and SEDA protocol
let seda_payload: Bytes = "seda_payload".to_string().into_bytes();
let payback_address: Bytes = "payback_address".to_string().into_bytes();

// memo
let chain_id: u128 = 31337;
let nonce: u128 = 1;
let mut hasher = Keccak256::new();
hasher.update(chain_id.to_be_bytes());
hasher.update(nonce.to_be_bytes());
let memo = hasher.finalize().to_vec();

// format inputs
let dr_inputs1 = DataRequestInputs {
dr_binary_id,
tally_binary_id,
dr_inputs,
tally_inputs,
memo,
replication_factor,
gas_price,
gas_limit,
seda_payload,
payback_address,
};

// reconstruct dr_id
let constructed_dr_id1 = hash_data_request(dr_inputs1);
println!("constructed_dr_id1: {}", constructed_dr_id1);
}
}
14 changes: 0 additions & 14 deletions packages/data-requests/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,14 @@ pub fn check_eligibility(deps: &DepsMut, dr_executor: Addr) -> Result<bool, Cont
Ok(query_response.value)
}

pub fn pad_to_32_bytes(value: &u128) -> [u8; 32] {
let mut bytes = [0u8; 32];
let small_bytes = &value.to_be_bytes();
bytes[(32 - small_bytes.len())..].copy_from_slice(small_bytes);
bytes
}

pub fn hash_update(hasher: &mut Keccak256, value: &u128) {
let bytes = pad_to_32_bytes(value);
hasher.update(bytes);
}

pub fn hash_data_request(posted_dr: DataRequestInputs) -> String {
let mut hasher = Keccak256::new();
hasher.update(posted_dr.dr_binary_id);
hasher.update(posted_dr.dr_inputs);
hasher.update(posted_dr.gas_limit.to_be_bytes());
hasher.update(posted_dr.gas_price.to_be_bytes());
hasher.update(posted_dr.memo);
hasher.update(posted_dr.payback_address);
hasher.update(posted_dr.replication_factor.to_be_bytes());
hasher.update(posted_dr.seda_payload);
hasher.update(posted_dr.tally_binary_id);
hasher.update(posted_dr.tally_inputs);

Expand Down
17 changes: 7 additions & 10 deletions packages/integration-tests/src/data_request.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::tests::utils::{proper_instantiate, USER};
use common::msg::{GetDataRequestResponse, GetDataRequestsFromPoolResponse, PostDataRequestArgs};
use common::types::{Bytes, Hash, Memo};
use common::types::{Bytes, Hash};
use cosmwasm_std::Addr;
use cw_multi_test::Executor;
use data_requests::state::DataRequestInputs;
use data_requests::utils::{hash_data_request, hash_update};
use data_requests::utils::hash_data_request;
use proxy_contract::msg::{ProxyExecuteMsg, ProxyQueryMsg};
use sha3::{Digest, Keccak256};

Expand All @@ -21,15 +21,12 @@ fn post_data_request() {
let gas_price: u128 = 10;
let gas_limit: u128 = 10;
let seda_payload: Bytes = Vec::new();
let chain_id = 31337;
let nonce = 1;
let value = "test".to_string();
let chain_id: u128 = 31337;
let nonce: u128 = 1;
let mut hasher = Keccak256::new();
hash_update(&mut hasher, &chain_id);
hash_update(&mut hasher, &nonce);
hasher.update(value);
let binary_hash = format!("0x{}", hex::encode(hasher.finalize()));
let memo1: Memo = binary_hash.clone().into_bytes();
hasher.update(chain_id.to_be_bytes());
hasher.update(nonce.to_be_bytes());
let memo1 = hasher.finalize().to_vec();
let payback_address: Bytes = Vec::new();
let dr_inputs1 = DataRequestInputs {
dr_binary_id: dr_binary_id.clone(),
Expand Down
30 changes: 12 additions & 18 deletions packages/integration-tests/src/data_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use common::msg::{
PostDataRequestArgs,
};
use common::state::Reveal;
use common::types::{Bytes, Hash, Memo};
use common::types::{Bytes, Hash};
use cosmwasm_std::Addr;
use cw_multi_test::Executor;
use data_requests::state::DataRequestInputs;
use data_requests::utils::{hash_data_request, hash_update};
use data_requests::utils::hash_data_request;
use proxy_contract::msg::{ProxyExecuteMsg, ProxyQueryMsg};
use sha3::{Digest, Keccak256};
use staking::consts::MINIMUM_STAKE_TO_REGISTER;
Expand Down Expand Up @@ -49,15 +49,12 @@ fn commit_reveal_result() {
let gas_price: u128 = 10;
let gas_limit: u128 = 10;
let seda_payload: Bytes = Vec::new();
let chain_id = 31337;
let nonce = 1;
let value = "test".to_string();
let chain_id: u128 = 31337;
let nonce: u128 = 1;
let mut hasher = Keccak256::new();
hash_update(&mut hasher, &chain_id);
hash_update(&mut hasher, &nonce);
hasher.update(value);
let binary_hash = format!("0x{}", hex::encode(hasher.finalize()));
let memo1: Memo = binary_hash.clone().into_bytes();
hasher.update(chain_id.to_be_bytes());
hasher.update(nonce.to_be_bytes());
let memo1 = hasher.finalize().to_vec();
let payback_address: Bytes = Vec::new();
let dr_inputs1 = DataRequestInputs {
dr_binary_id: dr_binary_id.clone(),
Expand Down Expand Up @@ -203,15 +200,12 @@ fn ineligible_post_data_result() {
let gas_price: u128 = 10;
let gas_limit: u128 = 10;
let seda_payload: Bytes = Vec::new();
let chain_id = 31337;
let nonce = 1;
let value = "test".to_string();
let chain_id: u128 = 31337;
let nonce: u128 = 1;
let mut hasher = Keccak256::new();
hash_update(&mut hasher, &chain_id);
hash_update(&mut hasher, &nonce);
hasher.update(value);
let binary_hash = format!("0x{}", hex::encode(hasher.finalize()));
let memo1: Memo = binary_hash.clone().into_bytes();
hasher.update(chain_id.to_be_bytes());
hasher.update(nonce.to_be_bytes());
let memo1 = hasher.finalize().to_vec();
let payback_address: Bytes = Vec::new();
let dr_inputs1 = DataRequestInputs {
dr_binary_id: dr_binary_id.clone(),
Expand Down