stake_unstake.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. //! Integration test of consensus staking and unstaking for Alice.
  19. //!
  20. //! We first airdrop Alice native tokes, and then she can stake,
  21. //! propose and unstake them a couple of times.
  22. //!
  23. //! With this test, we want to confirm the consensus contract state
  24. //! transitions work for a single party and are able to be verified.
  25. //!
  26. //! TODO: Malicious cases
  27. use darkfi::Result;
  28. use log::info;
  29. use darkfi_consensus_contract::model::{calculate_grace_period, EPOCH_LENGTH, REWARD};
  30. use darkfi_contract_test_harness::{init_logger, Holder, TestHarness};
  31. #[async_std::test]
  32. async fn consensus_contract_stake_unstake() -> Result<()> {
  33. init_logger();
  34. // Holders this test will use
  35. const HOLDERS: [Holder; 2] = [Holder::Faucet, Holder::Alice];
  36. // Some numbers we want to assert
  37. const ALICE_AIRDROP: u64 = 1000;
  38. // Slot to verify against
  39. let mut current_slot = 1;
  40. // Initialize harness
  41. let mut th = TestHarness::new(&["money".to_string(), "consensus".to_string()]).await?;
  42. // Now Alice can airdrop some native tokens to herself
  43. info!(target: "consensus", "[Faucet] =========================");
  44. info!(target: "consensus", "[Faucet] Building Alice airdrop tx");
  45. info!(target: "consensus", "[Faucet] =========================");
  46. let (airdrop_tx, airdrop_params) = th.airdrop_native(ALICE_AIRDROP, Holder::Alice)?;
  47. info!(target: "consensus", "[Faucet] ==========================");
  48. info!(target: "consensus", "[Faucet] Executing Alice airdrop tx");
  49. info!(target: "consensus", "[Faucet] ==========================");
  50. th.execute_airdrop_native_tx(Holder::Faucet, &airdrop_tx, &airdrop_params, current_slot)
  51. .await?;
  52. info!(target: "consensus", "[Alice] ==========================");
  53. info!(target: "consensus", "[Alice] Executing Alice airdrop tx");
  54. info!(target: "consensus", "[Alice] ==========================");
  55. th.execute_airdrop_native_tx(Holder::Alice, &airdrop_tx, &airdrop_params, current_slot).await?;
  56. th.assert_trees(&HOLDERS);
  57. // Gather new owncoin
  58. let alice_oc = th.gather_owncoin(Holder::Alice, airdrop_params.outputs[0].clone(), None)?;
  59. // Now Alice can stake her owncoin
  60. info!(target: "consensus", "[Alice] =================");
  61. info!(target: "consensus", "[Alice] Building stake tx");
  62. info!(target: "consensus", "[Alice] =================");
  63. let (stake_tx, stake_params, stake_secret_key) =
  64. th.stake(Holder::Alice, current_slot, alice_oc.clone()).await?;
  65. info!(target: "consensus", "[Faucet] ========================");
  66. info!(target: "consensus", "[Faucet] Executing Alice stake tx");
  67. info!(target: "consensus", "[Faucet] ========================");
  68. th.execute_stake_tx(Holder::Faucet, &stake_tx, &stake_params, current_slot).await?;
  69. info!(target: "consensus", "[Alice] ========================");
  70. info!(target: "consensus", "[Alice] Executing Alice stake tx");
  71. info!(target: "consensus", "[Alice] ========================");
  72. th.execute_stake_tx(Holder::Alice, &stake_tx, &stake_params, current_slot).await?;
  73. th.assert_trees(&HOLDERS);
  74. // Gather new staked owncoin
  75. let alice_staked_oc = th.gather_consensus_staked_owncoin(
  76. Holder::Alice,
  77. stake_params.output,
  78. Some(stake_secret_key),
  79. )?;
  80. // Verify values match
  81. assert!(alice_oc.note.value == alice_staked_oc.note.value);
  82. // We progress one slot
  83. current_slot += 1;
  84. let slot_checkpoint = th.generate_slot_checkpoint(current_slot).await?;
  85. // Since alice didn't wait for the grace period to pass, her proposal should fail
  86. info!(target: "consensus", "[Malicious] =====================================");
  87. info!(target: "consensus", "[Malicious] Checking proposal before grace period");
  88. info!(target: "consensus", "[Malicious] =====================================");
  89. let (proposal_tx, _, _, _) =
  90. th.proposal(Holder::Alice, slot_checkpoint, alice_staked_oc.clone()).await?;
  91. th.execute_erroneous_proposal_txs(Holder::Alice, &vec![proposal_tx], current_slot, 1).await?;
  92. // We progress after grace period
  93. current_slot += (calculate_grace_period() * EPOCH_LENGTH) + EPOCH_LENGTH;
  94. let slot_checkpoint = th.generate_slot_checkpoint(current_slot).await?;
  95. // With alice's current coin value she can become the slot proposer,
  96. // so she creates a proposal transaction to burn her staked coin,
  97. // reward herself and mint the new coin.
  98. info!(target: "consensus", "[Alice] ====================");
  99. info!(target: "consensus", "[Alice] Building proposal tx");
  100. info!(target: "consensus", "[Alice] ====================");
  101. let (
  102. proposal_tx,
  103. proposal_params,
  104. _proposal_signing_secret_key,
  105. proposal_decryption_secret_key,
  106. ) = th.proposal(Holder::Alice, slot_checkpoint, alice_staked_oc.clone()).await?;
  107. info!(target: "consensus", "[Faucet] ===========================");
  108. info!(target: "consensus", "[Faucet] Executing Alice proposal tx");
  109. info!(target: "consensus", "[Faucet] ===========================");
  110. th.execute_proposal_tx(Holder::Faucet, &proposal_tx, &proposal_params, current_slot).await?;
  111. info!(target: "consensus", "[Alice] ===========================");
  112. info!(target: "consensus", "[Alice] Executing Alice proposal tx");
  113. info!(target: "consensus", "[Alice] ===========================");
  114. th.execute_proposal_tx(Holder::Alice, &proposal_tx, &proposal_params, current_slot).await?;
  115. th.assert_trees(&HOLDERS);
  116. // Gather new staked owncoin which includes the reward
  117. let alice_rewarded_staked_oc = th.gather_consensus_staked_owncoin(
  118. Holder::Alice,
  119. proposal_params.output,
  120. Some(proposal_decryption_secret_key),
  121. )?;
  122. // Verify values match
  123. assert!((alice_staked_oc.note.value + REWARD) == alice_rewarded_staked_oc.note.value);
  124. // We progress one slot
  125. current_slot += 1;
  126. th.generate_slot_checkpoint(current_slot).await?;
  127. // Alice can request for her owncoin to get unstaked
  128. info!(target: "consensus", "[Alice] ===========================");
  129. info!(target: "consensus", "[Alice] Building unstake request tx");
  130. info!(target: "consensus", "[Alice] ===========================");
  131. let (
  132. unstake_request_tx,
  133. unstake_request_params,
  134. unstake_request_output_secret_key,
  135. _unstake_request_signature_secret_key,
  136. ) = th.unstake_request(Holder::Alice, current_slot, alice_rewarded_staked_oc.clone()).await?;
  137. info!(target: "consensus", "[Faucet] ==================================");
  138. info!(target: "consensus", "[Faucet] Executing Alice unstake request tx");
  139. info!(target: "consensus", "[Faucet] ==================================");
  140. th.execute_unstake_request_tx(
  141. Holder::Faucet,
  142. &unstake_request_tx,
  143. &unstake_request_params,
  144. current_slot,
  145. )
  146. .await?;
  147. info!(target: "consensus", "[Alice] ==================================");
  148. info!(target: "consensus", "[Alice] Executing Alice unstake request tx");
  149. info!(target: "consensus", "[Alice] ==================================");
  150. th.execute_unstake_request_tx(
  151. Holder::Alice,
  152. &unstake_request_tx,
  153. &unstake_request_params,
  154. current_slot,
  155. )
  156. .await?;
  157. th.assert_trees(&HOLDERS);
  158. // Gather new unstake request owncoin
  159. let alice_unstake_request_oc = th.gather_consensus_unstaked_owncoin(
  160. Holder::Alice,
  161. unstake_request_params.output,
  162. Some(unstake_request_output_secret_key),
  163. )?;
  164. // Verify values match
  165. assert!(alice_rewarded_staked_oc.note.value == alice_unstake_request_oc.note.value);
  166. // Now we will test if we can reuse token in proposal or unstake it again
  167. current_slot += 1;
  168. let slot_checkpoint = th.generate_slot_checkpoint(current_slot).await?;
  169. info!(target: "consensus", "[Malicious] ========================================");
  170. info!(target: "consensus", "[Malicious] Checking using unstaked coin in proposal");
  171. info!(target: "consensus", "[Malicious] ========================================");
  172. let (proposal_tx, _, _, _) =
  173. th.proposal(Holder::Alice, slot_checkpoint, alice_unstake_request_oc.clone()).await?;
  174. th.execute_erroneous_proposal_txs(Holder::Alice, &vec![proposal_tx], current_slot, 1).await?;
  175. info!(target: "consensus", "[Malicious] =============================");
  176. info!(target: "consensus", "[Malicious] Checking unstaking coin again");
  177. info!(target: "consensus", "[Malicious] =============================");
  178. let (unstake_request_tx, _, _, _) =
  179. th.unstake_request(Holder::Alice, current_slot, alice_unstake_request_oc.clone()).await?;
  180. th.execute_erroneous_unstake_request_txs(
  181. Holder::Alice,
  182. &vec![unstake_request_tx],
  183. current_slot,
  184. 1,
  185. )
  186. .await?;
  187. // We progress after grace period
  188. current_slot += (calculate_grace_period() * EPOCH_LENGTH) + EPOCH_LENGTH;
  189. // Now Alice can unstake her owncoin
  190. info!(target: "consensus", "[Alice] ===================");
  191. info!(target: "consensus", "[Alice] Building unstake tx");
  192. info!(target: "consensus", "[Alice] ===================");
  193. let (unstake_tx, unstake_params, _) =
  194. th.unstake(Holder::Alice, alice_unstake_request_oc.clone())?;
  195. info!(target: "consensus", "[Faucet] ==========================");
  196. info!(target: "consensus", "[Faucet] Executing Alice unstake tx");
  197. info!(target: "consensus", "[Faucet] ==========================");
  198. th.execute_unstake_tx(Holder::Faucet, &unstake_tx, &unstake_params, current_slot).await?;
  199. info!(target: "consensus", "[Alice] ==========================");
  200. info!(target: "consensus", "[Alice] Executing Alice unstake tx");
  201. info!(target: "consensus", "[Alice] ==========================");
  202. th.execute_unstake_tx(Holder::Alice, &unstake_tx, &unstake_params, current_slot).await?;
  203. th.assert_trees(&HOLDERS);
  204. // Gather new unstaked owncoin
  205. let alice_unstaked_oc = th.gather_owncoin(Holder::Alice, unstake_params.output, None)?;
  206. // Verify values match
  207. assert!(alice_unstake_request_oc.note.value == alice_unstaked_oc.note.value);
  208. // Statistics
  209. th.statistics();
  210. // Thanks for reading
  211. Ok(())
  212. }