Add block_height in the Delegation structure as well (#757)

This commit is contained in:
Bogdan-Ștefan Neacşu
2021-09-02 18:26:40 +03:00
committed by GitHub
parent 219c45a352
commit 28be53eefb
3 changed files with 36 additions and 9 deletions
+9 -4
View File
@@ -26,11 +26,16 @@ impl RawDelegationData {
pub struct Delegation {
owner: Addr,
amount: Coin,
block_height: u64,
}
impl Delegation {
pub fn new(owner: Addr, amount: Coin) -> Self {
Delegation { owner, amount }
pub fn new(owner: Addr, amount: Coin, block_height: u64) -> Self {
Delegation {
owner,
amount,
block_height,
}
}
pub fn amount(&self) -> &Coin {
@@ -46,8 +51,8 @@ impl Display for Delegation {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"{} {} delegated by {}",
self.amount.amount, self.amount.denom, self.owner
"{} {} delegated by {} at block {}",
self.amount.amount, self.amount.denom, self.owner, self.block_height
)
}
}
+4
View File
@@ -245,6 +245,8 @@ pub fn migrate(deps: DepsMut, env: Env, _msg: MigrateMsg) -> Result<Response, Co
"Non-UTF8 address used as key in bucket. The storage is corrupted!",
)),
coin(entry.1.u128(), DENOM),
// dummy data, used as a placeholder in the context of migration
42,
)
})
})
@@ -280,6 +282,8 @@ pub fn migrate(deps: DepsMut, env: Env, _msg: MigrateMsg) -> Result<Response, Co
"Non-UTF8 address used as key in bucket. The storage is corrupted!",
)),
coin(entry.1.u128(), DENOM),
// dummy data, used as a placeholder in the context of migration
42,
)
})
})
+23 -5
View File
@@ -129,6 +129,7 @@ pub(crate) fn query_mixnode_delegations_paged(
"Non-UTF8 address used as key in bucket. The storage is corrupted!",
)),
coin(entry.1.amount.u128(), DENOM),
entry.1.block_height,
)
})
})
@@ -184,6 +185,7 @@ pub(crate) fn query_mixnode_delegation(
Some(delegation_value) => Ok(Delegation::new(
address,
coin(delegation_value.amount.u128(), DENOM),
delegation_value.block_height,
)),
None => Err(ContractError::NoMixnodeDelegationFound {
identity: mix_identity,
@@ -213,6 +215,7 @@ pub(crate) fn query_gateway_delegations_paged(
"Non-UTF8 address used as key in bucket. The storage is corrupted!",
)),
coin(entry.1.amount.u128(), DENOM),
entry.1.block_height,
)
})
})
@@ -268,6 +271,7 @@ pub(crate) fn query_gateway_delegation(
Some(delegation_value) => Ok(Delegation::new(
address,
coin(delegation_value.amount.u128(), DENOM),
delegation_value.block_height,
)),
None => Err(ContractError::NoGatewayDelegationFound {
identity: gateway_identity,
@@ -288,7 +292,7 @@ mod tests {
use crate::transactions;
use cosmwasm_std::testing::mock_info;
use cosmwasm_std::{Addr, Storage};
use mixnet_contract::{Gateway, MixNode};
use mixnet_contract::{Gateway, MixNode, RawDelegationData};
#[test]
fn mixnodes_empty_on_init() {
@@ -793,11 +797,18 @@ mod tests {
let delegation_owner = Addr::unchecked("bar");
mix_delegations(&mut deps.storage, &node_identity)
.save(delegation_owner.as_bytes(), &raw_delegation_fixture(42))
.save(
delegation_owner.as_bytes(),
&RawDelegationData::new(42u128.into(), 12_345),
)
.unwrap();
assert_eq!(
Ok(Delegation::new(delegation_owner.clone(), coin(42, DENOM))),
Ok(Delegation::new(
delegation_owner.clone(),
coin(42, DENOM),
12_345
)),
query_mixnode_delegation(deps.as_ref(), node_identity, delegation_owner)
)
}
@@ -1178,11 +1189,18 @@ mod tests {
let delegation_owner = Addr::unchecked("bar");
gateway_delegations(&mut deps.storage, &node_identity)
.save(delegation_owner.as_bytes(), &raw_delegation_fixture(42))
.save(
delegation_owner.as_bytes(),
&&RawDelegationData::new(42u128.into(), 12_345),
)
.unwrap();
assert_eq!(
Ok(Delegation::new(delegation_owner.clone(), coin(42, DENOM))),
Ok(Delegation::new(
delegation_owner.clone(),
coin(42, DENOM),
12_345
)),
query_gateway_delegation(deps.as_ref(), node_identity, delegation_owner)
)
}