Skip to content

Commit

Permalink
feat: tests for balance_and_delegations reconstruct
Browse files Browse the repository at this point in the history
  • Loading branch information
ratik committed Feb 22, 2024
1 parent cce6719 commit 3e0bee6
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/puppeteer-base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ pub mod query;
pub mod reply;
pub mod state;
pub mod sudo;
#[cfg(test)]
mod tests;
10 changes: 5 additions & 5 deletions packages/puppeteer-base/src/sudo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ impl KVReconstruct for BalancesAndDelegations {
fn reconstruct(
storage_values: &[neutron_sdk::bindings::types::StorageValue],
) -> NeutronResult<Self> {
if storage_values.is_empty() {
return Err(NeutronError::InvalidQueryResultFormat(
"storage_values length is 0".into(),
));
}
let mut coins: Vec<cosmwasm_std::Coin> = Vec::with_capacity(storage_values.len());
let kv = &storage_values[0];
if kv.value.len() > 0 {
Expand All @@ -189,11 +194,6 @@ impl KVReconstruct for BalancesAndDelegations {
let mut delegations: Vec<cosmwasm_std::Delegation> =
Vec::with_capacity((storage_values.len() - 2) / 2);

if storage_values.is_empty() {
return Err(NeutronError::InvalidQueryResultFormat(
"storage_values length is 0".into(),
));
}
// first StorageValue is denom
if storage_values[1].value.is_empty() {
// Incoming denom cannot be empty, it should always be configured on chain.
Expand Down
132 changes: 132 additions & 0 deletions packages/puppeteer-base/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
use super::sudo::BalancesAndDelegations;
use cosmwasm_std::{to_json_binary, Addr, Binary, Coin, Delegation, Uint128};
use neutron_sdk::bindings::types::StorageValue;
use neutron_sdk::interchain_queries::types::KVReconstruct;
use neutron_sdk::NeutronResult;
use prost::Message;

#[test]
fn test_reconstruct_balance_and_delegations_no_delegations() {
let coin = cosmos_sdk_proto::cosmos::base::v1beta1::Coin {
denom: "uatom".to_string(),
amount: "1000".to_string(),
};
let mut buf_coin = Vec::new();
coin.encode(&mut buf_coin).unwrap();
let storage_values: Vec<StorageValue> = vec![
StorageValue {
storage_prefix: "prefix".to_string(),
key: Binary::from("balances".as_bytes()),
value: buf_coin.into(),
},
StorageValue {
storage_prefix: "prefix".to_string(),
key: Binary::from("denom".as_bytes()),
value: to_json_binary(&"uatom".to_string()).unwrap(),
},
];
let result: NeutronResult<BalancesAndDelegations> =
BalancesAndDelegations::reconstruct(&storage_values);
match result {
Ok(balances_and_delegations) => {
let expected_coins = vec![cosmwasm_std::Coin {
denom: "uatom".to_string(),
amount: Uint128::from(1000u128),
}];
assert_eq!(balances_and_delegations.balances.coins, expected_coins);

let expected_delegations: Vec<Delegation> = vec![];
assert_eq!(
balances_and_delegations.delegations.delegations,
expected_delegations
);
}
Err(e) => {
panic!("reconstruct method returned an error: {:?}", e);
}
}
}

#[test]
fn test_reconstruct_balance_and_delegations_with_delegations() {
let coin = cosmos_sdk_proto::cosmos::base::v1beta1::Coin {
denom: "uatom".to_string(),
amount: "1000".to_string(),
};
let mut buf_coin = Vec::new();
coin.encode(&mut buf_coin).unwrap();
let mut storage_values: Vec<StorageValue> = vec![
StorageValue {
storage_prefix: "prefix".to_string(),
key: Binary::from("balances".as_bytes()),
value: buf_coin.into(),
},
StorageValue {
storage_prefix: "prefix".to_string(),
key: Binary::from("denom".as_bytes()),
value: to_json_binary(&"uatom".to_string()).unwrap(),
},
];

let delegation = cosmos_sdk_proto::cosmos::staking::v1beta1::Delegation {
delegator_address: "delegator".to_string(),
validator_address: "validator".to_string(),
shares: "1000".to_string(),
};
let mut buf = Vec::new();
delegation.encode(&mut buf).unwrap();
storage_values.push(StorageValue {
storage_prefix: "prefix".to_string(),
key: Binary::from("delegation".as_bytes()),
value: buf.into(),
});

let validator = cosmos_sdk_proto::cosmos::staking::v1beta1::Validator {
operator_address: "operator".to_string(),
consensus_pubkey: None,
jailed: false,
status: 1,
tokens: "1000".to_string(),
delegator_shares: "1000".to_string(),
description: None,
unbonding_height: 0,
unbonding_time: None,
commission: None,
min_self_delegation: "1000".to_string(),
};
let mut buf = Vec::new();
validator.encode(&mut buf).unwrap();
storage_values.push(StorageValue {
storage_prefix: "prefix".to_string(),
key: Binary::from("validator".as_bytes()),
value: buf.into(),
});

let result: NeutronResult<BalancesAndDelegations> =
BalancesAndDelegations::reconstruct(&storage_values);
match result {
Ok(balances_and_delegations) => {
let expected_coins = vec![cosmwasm_std::Coin {
denom: "uatom".to_string(),
amount: Uint128::from(1000u128),
}];
assert_eq!(balances_and_delegations.balances.coins, expected_coins);

let expected_delegations: Vec<Delegation> = vec![Delegation {
delegator: Addr::unchecked("delegator"),
validator: "validator".to_string(),
amount: Coin {
denom: "uatom".to_string(),
amount: Uint128::from(1000u128),
},
}];
assert_eq!(
balances_and_delegations.delegations.delegations,
expected_delegations
);
}
Err(e) => {
panic!("reconstruct method returned an error: {:?}", e);
}
}
}

0 comments on commit 3e0bee6

Please sign in to comment.