@@ -12,23 +12,52 @@ use fleek_crypto::{
12
12
use hp_fixed:: unsigned:: HpUfixed ;
13
13
use lightning_committee_beacon:: { CommitteeBeaconConfig , CommitteeBeaconTimerConfig } ;
14
14
use lightning_interfaces:: types:: {
15
+ CommitteeSelectionBeaconCommit ,
15
16
ExecutionData ,
16
17
ExecutionError ,
17
18
HandshakePorts ,
18
19
NodePorts ,
20
+ Participation ,
19
21
UpdateMethod ,
20
22
} ;
21
- use lightning_interfaces:: SyncQueryRunnerInterface ;
23
+ use lightning_interfaces:: { KeystoreInterface , SyncQueryRunnerInterface } ;
22
24
use lightning_test_utils:: consensus:: MockConsensusConfig ;
23
25
use lightning_test_utils:: e2e:: {
24
26
DowncastToTestFullNode ,
25
27
TestFullNodeComponentsWithMockConsensus ,
26
28
TestNetwork ,
27
29
TestNetworkNode ,
28
30
} ;
31
+ use lightning_utils:: application:: QueryRunnerExt ;
29
32
use tempfile:: tempdir;
33
+ use utils:: {
34
+ create_genesis_committee,
35
+ deposit,
36
+ deposit_and_stake,
37
+ expect_tx_revert,
38
+ expect_tx_success,
39
+ get_flk_balance,
40
+ get_locked,
41
+ get_locked_time,
42
+ get_node_info,
43
+ get_stake_locked_until,
44
+ get_staked,
45
+ init_app,
46
+ prepare_deposit_update,
47
+ prepare_initial_stake_update,
48
+ prepare_regular_stake_update,
49
+ prepare_stake_lock_update,
50
+ prepare_unstake_update,
51
+ prepare_update_request_consensus,
52
+ prepare_update_request_node,
53
+ prepare_withdraw_unstaked_update,
54
+ run_update,
55
+ run_updates,
56
+ test_genesis,
57
+ test_init_app,
58
+ } ;
30
59
31
- use super :: utils :: * ;
60
+ use super :: * ;
32
61
33
62
#[ tokio:: test]
34
63
async fn test_stake ( ) {
@@ -793,3 +822,174 @@ async fn test_withdraw_unstaked_works_properly() {
793
822
// Shutdown the network.
794
823
network. shutdown ( ) . await ;
795
824
}
825
+
826
+ #[ tokio:: test]
827
+ async fn test_unstake_as_non_committee_node_opts_out_node_and_removes_after_epoch_change ( ) {
828
+ let network = utils:: TestNetwork :: builder ( )
829
+ . with_committee_nodes ( 4 )
830
+ . with_non_committee_nodes ( 1 )
831
+ . build ( )
832
+ . await
833
+ . unwrap ( ) ;
834
+ let query = network. query ( ) ;
835
+ let epoch = query. get_current_epoch ( ) ;
836
+
837
+ // Check the initial stake.
838
+ let stake = query. get_node_info ( & 4 , |n| n. stake ) . unwrap ( ) ;
839
+ assert_eq ! ( stake. staked, 1000u64 . into( ) ) ;
840
+ assert_eq ! ( stake. locked, 0u64 . into( ) ) ;
841
+
842
+ // Execute unstake transaction from the first node.
843
+ let resp = network
844
+ . execute ( vec ! [ network. node( 4 ) . build_transasction_as_owner(
845
+ UpdateMethod :: Unstake {
846
+ amount: 1000u64 . into( ) ,
847
+ node: network. node( 4 ) . keystore. get_ed25519_pk( ) ,
848
+ } ,
849
+ 1 ,
850
+ ) ] )
851
+ . await
852
+ . unwrap ( ) ;
853
+ assert_eq ! ( resp. block_number, 1 ) ;
854
+
855
+ // Check that the stake is now locked.
856
+ let stake = query. get_node_info ( & 4 , |n| n. stake ) . unwrap ( ) ;
857
+ assert_eq ! ( stake. staked, 0u64 . into( ) ) ;
858
+ assert_eq ! ( stake. locked, 1000u64 . into( ) ) ;
859
+
860
+ // Execute epoch change transactions.
861
+ let resp = network. execute_change_epoch ( epoch) . await . unwrap ( ) ;
862
+ assert_eq ! ( resp. block_number, 2 ) ;
863
+
864
+ // Execute commit-reveal transactions to complete the epoch change process.
865
+ let resp = network
866
+ . execute (
867
+ network
868
+ . nodes
869
+ . iter ( )
870
+ . enumerate ( )
871
+ . map ( |( i, n) | {
872
+ n. build_transaction ( UpdateMethod :: CommitteeSelectionBeaconCommit {
873
+ commit : CommitteeSelectionBeaconCommit :: build ( epoch, 0 , [ i as u8 ; 32 ] ) ,
874
+ } )
875
+ } )
876
+ . collect ( ) ,
877
+ )
878
+ . await
879
+ . unwrap ( ) ;
880
+ assert_eq ! ( resp. block_number, 3 ) ;
881
+ let resp = network
882
+ . execute (
883
+ network
884
+ . nodes
885
+ . iter ( )
886
+ . enumerate ( )
887
+ . map ( |( i, n) | {
888
+ n. build_transaction ( UpdateMethod :: CommitteeSelectionBeaconReveal {
889
+ reveal : [ i as u8 ; 32 ] ,
890
+ } )
891
+ } )
892
+ . collect ( ) ,
893
+ )
894
+ . await
895
+ . unwrap ( ) ;
896
+ assert_eq ! ( resp. block_number, 4 ) ;
897
+
898
+ // Check that the epoch has changed.
899
+ assert_eq ! ( query. get_current_epoch( ) , epoch + 1 ) ;
900
+
901
+ // Check that the node is no longer participating.
902
+ assert_eq ! (
903
+ query. get_node_info( & 4 , |n| n. participation) . unwrap( ) ,
904
+ Participation :: False
905
+ ) ;
906
+ }
907
+
908
+ #[ tokio:: test]
909
+ async fn test_unstake_as_committee_node_opts_out_node_and_removes_after_epoch_change ( ) {
910
+ let network = utils:: TestNetwork :: builder ( )
911
+ . with_committee_nodes ( 5 )
912
+ . build ( )
913
+ . await
914
+ . unwrap ( ) ;
915
+ let query = network. query ( ) ;
916
+ let epoch = query. get_current_epoch ( ) ;
917
+
918
+ // Check the initial stake.
919
+ let stake = query. get_node_info ( & 4 , |n| n. stake ) . unwrap ( ) ;
920
+ assert_eq ! ( stake. staked, 1000u64 . into( ) ) ;
921
+ assert_eq ! ( stake. locked, 0u64 . into( ) ) ;
922
+
923
+ // Execute unstake transaction from the first node.
924
+ let resp = network
925
+ . execute ( vec ! [ network. node( 4 ) . build_transasction_as_owner(
926
+ UpdateMethod :: Unstake {
927
+ amount: 1000u64 . into( ) ,
928
+ node: network. node( 4 ) . keystore. get_ed25519_pk( ) ,
929
+ } ,
930
+ 1 ,
931
+ ) ] )
932
+ . await
933
+ . unwrap ( ) ;
934
+ assert_eq ! ( resp. block_number, 1 ) ;
935
+
936
+ // Check that the stake is now locked.
937
+ let stake = query. get_node_info ( & 4 , |n| n. stake ) . unwrap ( ) ;
938
+ assert_eq ! ( stake. staked, 0u64 . into( ) ) ;
939
+ assert_eq ! ( stake. locked, 1000u64 . into( ) ) ;
940
+
941
+ // Execute epoch change transactions from participating nodes.
942
+ let resp = network
943
+ . execute (
944
+ network. nodes [ 0 ..4 ]
945
+ . iter ( )
946
+ . map ( |node| node. build_transaction ( UpdateMethod :: ChangeEpoch { epoch } ) )
947
+ . collect ( ) ,
948
+ )
949
+ . await
950
+ . unwrap ( ) ;
951
+ assert_eq ! ( resp. block_number, 2 ) ;
952
+
953
+ // Execute commit-reveal transactions to complete the epoch change process.
954
+ let resp = network
955
+ . execute (
956
+ network
957
+ . nodes
958
+ . iter ( )
959
+ . enumerate ( )
960
+ . map ( |( i, n) | {
961
+ n. build_transaction ( UpdateMethod :: CommitteeSelectionBeaconCommit {
962
+ commit : CommitteeSelectionBeaconCommit :: build ( epoch, 0 , [ i as u8 ; 32 ] ) ,
963
+ } )
964
+ } )
965
+ . collect ( ) ,
966
+ )
967
+ . await
968
+ . unwrap ( ) ;
969
+ assert_eq ! ( resp. block_number, 3 ) ;
970
+ let resp = network
971
+ . execute (
972
+ network
973
+ . nodes
974
+ . iter ( )
975
+ . enumerate ( )
976
+ . map ( |( i, n) | {
977
+ n. build_transaction ( UpdateMethod :: CommitteeSelectionBeaconReveal {
978
+ reveal : [ i as u8 ; 32 ] ,
979
+ } )
980
+ } )
981
+ . collect ( ) ,
982
+ )
983
+ . await
984
+ . unwrap ( ) ;
985
+ assert_eq ! ( resp. block_number, 4 ) ;
986
+
987
+ // Check that the epoch has changed.
988
+ assert_eq ! ( query. get_current_epoch( ) , epoch + 1 ) ;
989
+
990
+ // Check that the node is no longer participating.
991
+ assert_eq ! (
992
+ query. get_node_info( & 4 , |n| n. participation) . unwrap( ) ,
993
+ Participation :: False
994
+ ) ;
995
+ }
0 commit comments