Skip to content

Commit

Permalink
ref(types): make U128 a string again
Browse files Browse the repository at this point in the history
  • Loading branch information
FranklinWaller committed Aug 19, 2024
1 parent fe04866 commit 0c1e461
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 18 deletions.
12 changes: 6 additions & 6 deletions src/msgs/data_requests/execute_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ fn json_post_request() {
let consensus_filter: Bytes = "consensus_filter".as_bytes().into();

#[cfg(not(feature = "cosmwasm"))]
let gas_price = 100;
let gas_price = "100".to_string();
#[cfg(feature = "cosmwasm")]
let gas_price: U128 = 100u128.into();

#[cfg(not(feature = "cosmwasm"))]
let gas_limit = 100;
let gas_limit = "100".to_string();
#[cfg(feature = "cosmwasm")]
let gas_limit: U128 = 100u128.into();

Expand All @@ -76,8 +76,8 @@ fn json_post_request() {
tally_inputs: tally_inputs.clone(),
replication_factor: 1,
consensus_filter: consensus_filter.clone(),
gas_price,
gas_limit,
gas_price: gas_price.clone(),
gas_limit: gas_limit.clone(),
memo: memo.clone(),
};
let expected_json = json!({
Expand Down Expand Up @@ -110,7 +110,7 @@ fn json_post_request() {
#[test]
fn json_reveal_result() {
#[cfg(not(feature = "cosmwasm"))]
let gas_used = 100;
let gas_used = "100".to_string();
#[cfg(feature = "cosmwasm")]
let gas_used: U128 = 100u128.into();

Expand All @@ -122,7 +122,7 @@ fn json_reveal_result() {
let reveal_body = RevealBody {
salt: "salt".to_string(),
exit_code: 0,
gas_used,
gas_used: gas_used.clone(),
reveal: reveal.clone(),
};
let expected_json = json!({
Expand Down
4 changes: 2 additions & 2 deletions src/msgs/data_requests/sudo_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn json_post_result() {
let result_bytes: Bytes = "result".as_bytes().into();

#[cfg(not(feature = "cosmwasm"))]
let gas_used = 100;
let gas_used = "100".to_string();
#[cfg(feature = "cosmwasm")]
let gas_used: U128 = 100u128.into();

Expand All @@ -32,7 +32,7 @@ fn json_post_result() {
dr_id: "dr_id".to_string(),
block_height: 100,
exit_code: 0,
gas_used,
gas_used: gas_used.clone(),
result: result_bytes.clone(),
payback_address: payback_address.clone(),
seda_payload: seda_payload.clone(),
Expand Down
27 changes: 23 additions & 4 deletions src/msgs/data_requests/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ impl TryHashSelf for DataResult {
#[cfg(feature = "cosmwasm")]
let gas_used = self.gas_used.to_be_bytes();
#[cfg(not(feature = "cosmwasm"))]
let gas_used = self.gas_used.to_be_bytes();
let gas_used = self
.gas_used
.parse::<u128>()
.expect("gas used should be parseable to u128")
.to_be_bytes();

let mut payback_hasher = Keccak256::new();
#[cfg(feature = "cosmwasm")]
Expand Down Expand Up @@ -217,7 +221,12 @@ impl TryHashSelf for RevealBody {
#[cfg(feature = "cosmwasm")]
hasher.update(self.gas_used.to_be_bytes());
#[cfg(not(feature = "cosmwasm"))]
hasher.update(self.gas_used.to_be_bytes());
hasher.update(
self.gas_used
.parse::<u128>()
.expect("`gas_used` should be parseable to u128")
.to_be_bytes(),
);
hasher.update(reveal_hash);

Ok(hasher.finalize().into())
Expand Down Expand Up @@ -286,11 +295,21 @@ impl TryHashSelf for PostDataRequestArgs {
#[cfg(feature = "cosmwasm")]
dr_hasher.update(self.gas_price.to_be_bytes());
#[cfg(not(feature = "cosmwasm"))]
dr_hasher.update(self.gas_price.to_be_bytes());
dr_hasher.update(
self.gas_price
.parse::<u128>()
.expect("`gas_price` should be parseable to u128")
.to_be_bytes(),
);
#[cfg(feature = "cosmwasm")]
dr_hasher.update(self.gas_limit.to_be_bytes());
#[cfg(not(feature = "cosmwasm"))]
dr_hasher.update(self.gas_limit.to_be_bytes());
dr_hasher.update(
self.gas_limit
.parse::<u128>()
.expect("`gas_limit` should be parseable to u128")
.to_be_bytes(),
);
dr_hasher.update(memo_hash);

Ok(dr_hasher.finalize().into())
Expand Down
9 changes: 7 additions & 2 deletions src/msgs/staking/execute/unstake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ impl Execute {
fn generate_hash(amount: U128, chain_id: &str, contract_addr: &str, sequence: u128) -> Hash {
hash([
"unstake".as_bytes(),
#[cfg(feature = "cosmwasm")]
&amount.to_be_bytes(),
#[cfg(not(feature = "cosmwasm"))]
&amount.parse::<u128>()
.expect("`amount` should be parseable to u128")
.to_be_bytes(),
chain_id.as_bytes(),
contract_addr.as_bytes(),
&sequence.to_be_bytes(),
Expand All @@ -30,7 +35,7 @@ impl VerifySelf for Execute {
}

fn msg_hash(&self, chain_id: &str, contract_addr: &str, sequence: Self::Extra) -> Result<Hash> {
Ok(Self::generate_hash(self.amount, chain_id, contract_addr, sequence))
Ok(Self::generate_hash(self.amount.clone(), chain_id, contract_addr, sequence))
}
}

Expand Down Expand Up @@ -62,7 +67,7 @@ impl Execute {
contract_addr: &str,
sequence: u128,
) -> ExecuteFactory {
let hash = Self::generate_hash(amount, chain_id, contract_addr, sequence);
let hash = Self::generate_hash(amount.clone(), chain_id, contract_addr, sequence);
ExecuteFactory {
public_key,
amount,
Expand Down
12 changes: 11 additions & 1 deletion src/msgs/staking/execute/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ impl Execute {
fn generate_hash(amount: U128, chain_id: &str, contract_addr: &str, sequence: u128) -> Hash {
hash([
"withdraw".as_bytes(),
#[cfg(feature = "cosmwasm")]
&amount.to_be_bytes(),
#[cfg(not(feature = "cosmwasm"))]
&amount.parse::<u128>()
.expect("`amount` should be parseable to u128")
.to_be_bytes(),
chain_id.as_bytes(),
contract_addr.as_bytes(),
&sequence.to_be_bytes(),
Expand All @@ -32,7 +37,12 @@ impl VerifySelf for Execute {
fn msg_hash(&self, chain_id: &str, contract_addr: &str, sequence: Self::Extra) -> Result<Hash> {
Ok(hash([
"withdraw".as_bytes(),
#[cfg(feature = "cosmwasm")]
&self.amount.to_be_bytes(),
#[cfg(not(feature = "cosmwasm"))]
&self.amount.parse::<u128>()
.expect("`amount` should be parseable to u128")
.to_be_bytes(),
chain_id.as_bytes(),
contract_addr.as_bytes(),
&sequence.to_be_bytes(),
Expand Down Expand Up @@ -68,7 +78,7 @@ impl Execute {
contract_addr: &str,
sequence: u128,
) -> ExecuteFactory {
let hash = Self::generate_hash(amount, chain_id, contract_addr, sequence);
let hash = Self::generate_hash(amount.clone(), chain_id, contract_addr, sequence);
ExecuteFactory {
public_key,
amount,
Expand Down
4 changes: 2 additions & 2 deletions src/msgs/staking/execute_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn json_stake() {
#[test]
fn json_unstake() {
#[cfg(not(feature = "cosmwasm"))]
let amount: U128 = 0;
let amount: U128 = "0".to_string();
#[cfg(feature = "cosmwasm")]
let amount: U128 = 0u128.into();
let serialized = json!({
Expand All @@ -66,7 +66,7 @@ fn json_unstake() {
#[test]
fn json_withdraw() {
#[cfg(not(feature = "cosmwasm"))]
let amount: U128 = 0;
let amount: U128 = "0".to_string();
#[cfg(feature = "cosmwasm")]
let amount: U128 = 0u128.into();
let serialized = json!({
Expand Down
4 changes: 3 additions & 1 deletion src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ pub(crate) use verify_self::VerifySelf;

#[cfg(feature = "cosmwasm")]
pub(crate) type U128 = cosmwasm_std::Uint128;

// Is required to be a String, JSON does not support u128 numbers
#[cfg(not(feature = "cosmwasm"))]
pub(crate) type U128 = u128;
pub(crate) type U128 = String;

pub fn serialize_as_str<S, V>(value: V, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down

0 comments on commit 0c1e461

Please sign in to comment.