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 { pub struct Delegation {
owner: Addr, owner: Addr,
amount: Coin, amount: Coin,
block_height: u64,
} }
impl Delegation { impl Delegation {
pub fn new(owner: Addr, amount: Coin) -> Self { pub fn new(owner: Addr, amount: Coin, block_height: u64) -> Self {
Delegation { owner, amount } Delegation {
owner,
amount,
block_height,
}
} }
pub fn amount(&self) -> &Coin { pub fn amount(&self) -> &Coin {
@@ -46,8 +51,8 @@ impl Display for Delegation {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!( write!(
f, f,
"{} {} delegated by {}", "{} {} delegated by {} at block {}",
self.amount.amount, self.amount.denom, self.owner 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!", "Non-UTF8 address used as key in bucket. The storage is corrupted!",
)), )),
coin(entry.1.u128(), DENOM), 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!", "Non-UTF8 address used as key in bucket. The storage is corrupted!",
)), )),
coin(entry.1.u128(), DENOM), 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!", "Non-UTF8 address used as key in bucket. The storage is corrupted!",
)), )),
coin(entry.1.amount.u128(), DENOM), 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( Some(delegation_value) => Ok(Delegation::new(
address, address,
coin(delegation_value.amount.u128(), DENOM), coin(delegation_value.amount.u128(), DENOM),
delegation_value.block_height,
)), )),
None => Err(ContractError::NoMixnodeDelegationFound { None => Err(ContractError::NoMixnodeDelegationFound {
identity: mix_identity, 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!", "Non-UTF8 address used as key in bucket. The storage is corrupted!",
)), )),
coin(entry.1.amount.u128(), DENOM), 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( Some(delegation_value) => Ok(Delegation::new(
address, address,
coin(delegation_value.amount.u128(), DENOM), coin(delegation_value.amount.u128(), DENOM),
delegation_value.block_height,
)), )),
None => Err(ContractError::NoGatewayDelegationFound { None => Err(ContractError::NoGatewayDelegationFound {
identity: gateway_identity, identity: gateway_identity,
@@ -288,7 +292,7 @@ mod tests {
use crate::transactions; use crate::transactions;
use cosmwasm_std::testing::mock_info; use cosmwasm_std::testing::mock_info;
use cosmwasm_std::{Addr, Storage}; use cosmwasm_std::{Addr, Storage};
use mixnet_contract::{Gateway, MixNode}; use mixnet_contract::{Gateway, MixNode, RawDelegationData};
#[test] #[test]
fn mixnodes_empty_on_init() { fn mixnodes_empty_on_init() {
@@ -793,11 +797,18 @@ mod tests {
let delegation_owner = Addr::unchecked("bar"); let delegation_owner = Addr::unchecked("bar");
mix_delegations(&mut deps.storage, &node_identity) 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(); .unwrap();
assert_eq!( 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) query_mixnode_delegation(deps.as_ref(), node_identity, delegation_owner)
) )
} }
@@ -1178,11 +1189,18 @@ mod tests {
let delegation_owner = Addr::unchecked("bar"); let delegation_owner = Addr::unchecked("bar");
gateway_delegations(&mut deps.storage, &node_identity) 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(); .unwrap();
assert_eq!( 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) query_gateway_delegation(deps.as_ref(), node_identity, delegation_owner)
) )
} }