Skip to content

Commit fbcf005

Browse files
committed
test: non-zero delegator start index
1 parent 882dc46 commit fbcf005

File tree

1 file changed

+118
-12
lines changed

1 file changed

+118
-12
lines changed

staking-cli/tests/demo.rs

Lines changed: 118 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ trait DemoTestExt {
1818
async fn get_delegation(&self, validator: Address, delegator: Address) -> Result<U256>;
1919

2020
async fn setup_validators(&self, count: usize) -> Result<Vec<Address>>;
21+
22+
async fn assert_delegations(
23+
&self,
24+
validator: Address,
25+
start_index: u64,
26+
num_delegators: u64,
27+
expected: U256,
28+
) -> Result<()>;
2129
}
2230

2331
impl DemoTestExt for TestSystem {
@@ -71,6 +79,24 @@ impl DemoTestExt for TestSystem {
7179

7280
Ok(validators)
7381
}
82+
83+
async fn assert_delegations(
84+
&self,
85+
validator: Address,
86+
start_index: u64,
87+
num_delegators: u64,
88+
expected: U256,
89+
) -> Result<()> {
90+
for i in start_index..start_index + num_delegators {
91+
let delegator = generate_delegator_signer(i);
92+
let delegation = self.get_delegation(validator, delegator.address()).await?;
93+
assert_eq!(
94+
delegation, expected,
95+
"delegator {i} should have {expected} delegation"
96+
);
97+
}
98+
Ok(())
99+
}
74100
}
75101

76102
#[rstest]
@@ -864,6 +890,10 @@ async fn test_demo_batching_with_slow_blockchain() -> Result<()> {
864890
.assert()
865891
.success();
866892

893+
system
894+
.assert_delegations(validators[0], 0, num_delegators, parse_ether("100")?)
895+
.await?;
896+
867897
system
868898
.cmd(Signer::Mnemonic)
869899
.timeout(Duration::from_secs(5))
@@ -880,21 +910,97 @@ async fn test_demo_batching_with_slow_blockchain() -> Result<()> {
880910
.assert()
881911
.success();
882912

883-
for i in 0..num_delegators {
884-
let delegator = generate_delegator_signer(i);
885-
let delegation = system
886-
.get_delegation(validators[0], delegator.address())
887-
.await?;
888-
assert_eq!(
889-
delegation,
890-
U256::ZERO,
891-
"delegator {} should have 0 delegation after undelegate",
892-
i
893-
);
894-
}
913+
system
914+
.assert_delegations(validators[0], 0, num_delegators, U256::ZERO)
915+
.await?;
895916

896917
Ok(())
897918
})
898919
.await
899920
.map_err(|_| anyhow::anyhow!("test timed out - batching may be broken"))?
900921
}
922+
923+
#[test_log::test(tokio::test)]
924+
async fn test_demo_multiple_delegator_batches() -> Result<()> {
925+
let system = TestSystem::deploy().await?;
926+
let validators = system.setup_validators(1).await?;
927+
let validator_addrs = validators[0].to_string();
928+
929+
let num_delegators = 3u64;
930+
let amount = "100";
931+
932+
// First batch: delegators 0, 1, 2
933+
system
934+
.cmd(Signer::Mnemonic)
935+
.arg("demo")
936+
.arg("delegate")
937+
.arg("--validators")
938+
.arg(&validator_addrs)
939+
.arg("--delegator-start-index")
940+
.arg("0")
941+
.arg("--num-delegators")
942+
.arg(num_delegators.to_string())
943+
.arg("--min-amount")
944+
.arg(amount)
945+
.arg("--max-amount")
946+
.arg(amount)
947+
.assert()
948+
.success();
949+
950+
system
951+
.assert_delegations(validators[0], 0, num_delegators, parse_ether(amount)?)
952+
.await?;
953+
954+
// Second batch: delegators 3, 4, 5
955+
system
956+
.cmd(Signer::Mnemonic)
957+
.arg("demo")
958+
.arg("delegate")
959+
.arg("--validators")
960+
.arg(&validator_addrs)
961+
.arg("--delegator-start-index")
962+
.arg("3")
963+
.arg("--num-delegators")
964+
.arg(num_delegators.to_string())
965+
.arg("--min-amount")
966+
.arg(amount)
967+
.arg("--max-amount")
968+
.arg(amount)
969+
.assert()
970+
.success();
971+
972+
system
973+
.assert_delegations(validators[0], 3, num_delegators, parse_ether(amount)?)
974+
.await?;
975+
976+
// Verify first batch still has delegations
977+
system
978+
.assert_delegations(validators[0], 0, num_delegators, parse_ether(amount)?)
979+
.await?;
980+
981+
// Undelegate only second batch
982+
system
983+
.cmd(Signer::Mnemonic)
984+
.arg("demo")
985+
.arg("undelegate")
986+
.arg("--validators")
987+
.arg(&validator_addrs)
988+
.arg("--delegator-start-index")
989+
.arg("3")
990+
.arg("--num-delegators")
991+
.arg(num_delegators.to_string())
992+
.assert()
993+
.success();
994+
995+
// Second batch should be zero
996+
system
997+
.assert_delegations(validators[0], 3, num_delegators, U256::ZERO)
998+
.await?;
999+
1000+
// First batch should still have delegations
1001+
system
1002+
.assert_delegations(validators[0], 0, num_delegators, parse_ether(amount)?)
1003+
.await?;
1004+
1005+
Ok(())
1006+
}

0 commit comments

Comments
 (0)