use std::sync::Arc;
use casper_types::{
execution::TransformKindV2,
system::{auction::BidAddr, AUCTION},
Deploy, Key, PublicKey, StoredValue, TimeDiff, Timestamp, Transaction, U512,
};
use crate::reactor::main_reactor::tests::{
configs_override::ConfigsOverride, fixture::TestFixture, initial_stakes::InitialStakes,
ERA_ONE, ERA_TWO, ONE_MIN, TEN_SECS,
};
#[tokio::test]
async fn run_withdraw_bid_network() {
let alice_stake = 200_000_000_000_u64;
let initial_stakes = InitialStakes::FromVec(vec![alice_stake.into(), 10_000_000_000]);
let unbonding_delay = 2;
let mut fixture = TestFixture::new(
initial_stakes,
Some(ConfigsOverride {
unbonding_delay,
..Default::default()
}),
)
.await;
let alice_secret_key = Arc::clone(&fixture.node_contexts[0].secret_key);
let alice_public_key = PublicKey::from(&*alice_secret_key);
fixture.run_until_block_height(0, ONE_MIN).await;
fixture.check_bid_existence_at_tip(&alice_public_key, None, true);
let mut deploy = Deploy::withdraw_bid(
fixture.chainspec.network_config.name.clone(),
fixture.system_contract_hash(AUCTION),
alice_public_key.clone(),
alice_stake.into(),
Timestamp::now(),
TimeDiff::from_seconds(60),
);
deploy.sign(&alice_secret_key);
let txn = Transaction::Deploy(deploy);
let txn_hash = txn.hash();
fixture.inject_transaction(txn).await;
fixture
.run_until_executed_transaction(&txn_hash, TEN_SECS)
.await;
let bid_key = Key::BidAddr(BidAddr::from(alice_public_key.clone()));
fixture
.successful_execution_transforms(&txn_hash)
.iter()
.find(|transform| match transform.kind() {
TransformKindV2::Prune(prune_key) => prune_key == &bid_key,
_ => false,
})
.expect("should have a prune record for bid");
fixture
.run_until_stored_switch_block_header(ERA_ONE, ONE_MIN)
.await;
fixture.check_bid_existence_at_tip(&alice_public_key, None, false);
fixture
.run_until_stored_switch_block_header(
ERA_ONE.saturating_add(unbonding_delay + 1),
ONE_MIN * 2,
)
.await;
}
#[tokio::test]
async fn run_undelegate_bid_network() {
let alice_stake = 200_000_000_000_u64;
let bob_stake = 300_000_000_000_u64;
let initial_stakes = InitialStakes::FromVec(vec![alice_stake.into(), bob_stake.into()]);
let unbonding_delay = 2;
let mut fixture = TestFixture::new(
initial_stakes,
Some(ConfigsOverride {
unbonding_delay,
..Default::default()
}),
)
.await;
let alice_secret_key = Arc::clone(&fixture.node_contexts[0].secret_key);
let alice_public_key = PublicKey::from(&*alice_secret_key);
let bob_public_key = PublicKey::from(&*fixture.node_contexts[1].secret_key);
fixture.run_until_block_height(0, ONE_MIN).await;
fixture.check_bid_existence_at_tip(&alice_public_key, None, true);
fixture.check_bid_existence_at_tip(&bob_public_key, None, true);
fixture.check_bid_existence_at_tip(&bob_public_key, Some(&alice_public_key), false);
let alice_delegation_amount =
U512::from(fixture.chainspec.core_config.minimum_delegation_amount);
let mut deploy = Deploy::delegate(
fixture.chainspec.network_config.name.clone(),
fixture.system_contract_hash(AUCTION),
bob_public_key.clone(),
alice_public_key.clone(),
alice_delegation_amount,
Timestamp::now(),
TimeDiff::from_seconds(60),
);
deploy.sign(&alice_secret_key);
let txn = Transaction::Deploy(deploy);
let txn_hash = txn.hash();
fixture.inject_transaction(txn).await;
fixture
.run_until_executed_transaction(&txn_hash, TEN_SECS)
.await;
let bid_key = Key::BidAddr(BidAddr::new_from_public_keys(
&bob_public_key,
Some(&alice_public_key),
));
fixture
.successful_execution_transforms(&txn_hash)
.iter()
.find(|transform| match transform.kind() {
TransformKindV2::Write(StoredValue::BidKind(bid_kind)) => {
Key::from(bid_kind.bid_addr()) == bid_key
}
_ => false,
})
.expect("should have a write record for delegate bid");
fixture.check_bid_existence_at_tip(&bob_public_key, Some(&alice_public_key), true);
let mut deploy = Deploy::undelegate(
fixture.chainspec.network_config.name.clone(),
fixture.system_contract_hash(AUCTION),
bob_public_key.clone(),
alice_public_key.clone(),
alice_delegation_amount,
Timestamp::now(),
TimeDiff::from_seconds(60),
);
deploy.sign(&alice_secret_key);
let txn = Transaction::Deploy(deploy);
let txn_hash = txn.hash();
fixture.inject_transaction(txn).await;
fixture
.run_until_executed_transaction(&txn_hash, TEN_SECS)
.await;
fixture
.successful_execution_transforms(&txn_hash)
.iter()
.find(|transform| match transform.kind() {
TransformKindV2::Prune(prune_key) => prune_key == &bid_key,
_ => false,
})
.expect("should have a prune record for undelegated bid");
fixture
.run_until_stored_switch_block_header(ERA_ONE, ONE_MIN)
.await;
fixture.check_bid_existence_at_tip(&alice_public_key, None, true);
fixture.check_bid_existence_at_tip(&bob_public_key, None, true);
fixture.check_bid_existence_at_tip(&bob_public_key, Some(&alice_public_key), false);
fixture
.run_until_stored_switch_block_header(
ERA_ONE.saturating_add(unbonding_delay + 1),
ONE_MIN * 2,
)
.await;
}
#[tokio::test]
async fn run_redelegate_bid_network() {
let alice_stake = 200_000_000_000_u64;
let bob_stake = 300_000_000_000_u64;
let charlie_stake = 300_000_000_000_u64;
let initial_stakes = InitialStakes::FromVec(vec![
alice_stake.into(),
bob_stake.into(),
charlie_stake.into(),
]);
let spec_override = ConfigsOverride {
unbonding_delay: 1,
minimum_era_height: 5,
..Default::default()
};
let mut fixture = TestFixture::new(initial_stakes, Some(spec_override)).await;
let alice_secret_key = Arc::clone(&fixture.node_contexts[0].secret_key);
let alice_public_key = PublicKey::from(&*alice_secret_key);
let bob_public_key = PublicKey::from(&*fixture.node_contexts[1].secret_key);
let charlie_public_key = PublicKey::from(&*fixture.node_contexts[2].secret_key);
fixture.run_until_block_height(0, ONE_MIN).await;
fixture.check_bid_existence_at_tip(&alice_public_key, None, true);
fixture.check_bid_existence_at_tip(&bob_public_key, None, true);
fixture.check_bid_existence_at_tip(&charlie_public_key, None, true);
fixture.check_bid_existence_at_tip(&bob_public_key, Some(&alice_public_key), false);
fixture.check_bid_existence_at_tip(&charlie_public_key, Some(&alice_public_key), false);
let alice_delegation_amount =
U512::from(fixture.chainspec.core_config.minimum_delegation_amount);
let mut deploy = Deploy::delegate(
fixture.chainspec.network_config.name.clone(),
fixture.system_contract_hash(AUCTION),
bob_public_key.clone(),
alice_public_key.clone(),
alice_delegation_amount,
Timestamp::now(),
TimeDiff::from_seconds(60),
);
deploy.sign(&alice_secret_key);
let txn = Transaction::Deploy(deploy);
let txn_hash = txn.hash();
fixture.inject_transaction(txn).await;
fixture
.run_until_executed_transaction(&txn_hash, ONE_MIN)
.await;
let bid_key = Key::BidAddr(BidAddr::new_from_public_keys(
&bob_public_key,
Some(&alice_public_key),
));
fixture
.successful_execution_transforms(&txn_hash)
.iter()
.find(|transform| match transform.kind() {
TransformKindV2::Write(StoredValue::BidKind(bid_kind)) => {
Key::from(bid_kind.bid_addr()) == bid_key
}
_ => false,
})
.expect("should have a write record for delegate bid");
fixture.check_bid_existence_at_tip(&bob_public_key, Some(&alice_public_key), true);
let mut deploy = Deploy::redelegate(
fixture.chainspec.network_config.name.clone(),
fixture.system_contract_hash(AUCTION),
bob_public_key.clone(),
alice_public_key.clone(),
charlie_public_key.clone(),
alice_delegation_amount,
Timestamp::now(),
TimeDiff::from_seconds(60),
);
deploy.sign(&alice_secret_key);
let transaction = Transaction::Deploy(deploy);
let transaction_hash = transaction.hash();
fixture.inject_transaction(transaction).await;
fixture
.run_until_executed_transaction(&transaction_hash, TEN_SECS)
.await;
fixture
.successful_execution_transforms(&transaction_hash)
.iter()
.find(|transform| match transform.kind() {
TransformKindV2::Prune(prune_key) => prune_key == &bid_key,
_ => false,
})
.expect("should have a prune record for undelegated bid");
fixture.check_bid_existence_at_tip(&bob_public_key, Some(&alice_public_key), false);
fixture.check_bid_existence_at_tip(&charlie_public_key, Some(&alice_public_key), false);
fixture
.run_until_stored_switch_block_header(ERA_ONE, ONE_MIN)
.await;
fixture.check_bid_existence_at_tip(&charlie_public_key, Some(&alice_public_key), false);
fixture
.run_until_stored_switch_block_header(ERA_TWO, ONE_MIN)
.await;
fixture.check_bid_existence_at_tip(&alice_public_key, None, true);
fixture.check_bid_existence_at_tip(&bob_public_key, None, true);
fixture.check_bid_existence_at_tip(&charlie_public_key, Some(&alice_public_key), true);
}