@@ -650,21 +650,16 @@ func TestBlockchainInfo(t *testing.T) {
650
650
}
651
651
}
652
652
653
- func TestValidatorSetHandling (t * testing.T ) {
654
- assert := assert .New (t )
655
- require := require .New (t )
656
-
657
- waitCh := make (chan interface {})
658
-
659
- vKeys := make ([]tmcrypto.PrivKey , 2 )
660
- apps := make ([]* mocks.Application , 2 )
661
- nodes := make ([]* FullNode , 2 )
653
+ func createGenesisValidators (numNodes int , appCreator func (vKeyToRemove tmcrypto.PrivKey ) * mocks.Application , require * require.Assertions ) * FullClient {
654
+ vKeys := make ([]tmcrypto.PrivKey , numNodes )
655
+ apps := make ([]* mocks.Application , numNodes )
656
+ nodes := make ([]* FullNode , numNodes )
662
657
663
658
genesisValidators := make ([]tmtypes.GenesisValidator , len (vKeys ))
664
659
for i := 0 ; i < len (vKeys ); i ++ {
665
660
vKeys [i ] = ed25519 .GenPrivKey ()
666
661
genesisValidators [i ] = tmtypes.GenesisValidator {Address : vKeys [i ].PubKey ().Address (), PubKey : vKeys [i ].PubKey (), Power : int64 (i + 100 ), Name : fmt .Sprintf ("gen #%d" , i )}
667
- apps [i ] = createApp (vKeys [0 ], waitCh , require )
662
+ apps [i ] = appCreator (vKeys [0 ])
668
663
}
669
664
670
665
dalc := & mockda.DataAvailabilityLayerClient {}
@@ -712,6 +707,39 @@ func TestValidatorSetHandling(t *testing.T) {
712
707
err := nodes [i ].Start ()
713
708
require .NoError (err )
714
709
}
710
+ return rpc
711
+ }
712
+
713
+ // Tests moving from two validators to one validator and then back to two validators
714
+ func TestValidatorSetHandling (t * testing.T ) {
715
+ assert := assert .New (t )
716
+ require := require .New (t )
717
+
718
+ waitCh := make (chan interface {})
719
+
720
+ numNodes := 2
721
+ createApp := func (vKeyToRemove tmcrypto.PrivKey ) * mocks.Application {
722
+ app := & mocks.Application {}
723
+ app .On ("InitChain" , mock .Anything ).Return (abci.ResponseInitChain {})
724
+ app .On ("CheckTx" , mock .Anything ).Return (abci.ResponseCheckTx {})
725
+ app .On ("BeginBlock" , mock .Anything ).Return (abci.ResponseBeginBlock {})
726
+ app .On ("Commit" , mock .Anything ).Return (abci.ResponseCommit {})
727
+ app .On ("GetAppHash" , mock .Anything ).Return (abci.ResponseGetAppHash {})
728
+ app .On ("GenerateFraudProof" , mock .Anything ).Return (abci.ResponseGenerateFraudProof {})
729
+
730
+ pbValKey , err := encoding .PubKeyToProto (vKeyToRemove .PubKey ())
731
+ require .NoError (err )
732
+
733
+ app .On ("EndBlock" , mock .Anything ).Return (abci.ResponseEndBlock {}).Times (2 )
734
+ app .On ("EndBlock" , mock .Anything ).Return (abci.ResponseEndBlock {ValidatorUpdates : []abci.ValidatorUpdate {{PubKey : pbValKey , Power : 0 }}}).Once ()
735
+ app .On ("EndBlock" , mock .Anything ).Return (abci.ResponseEndBlock {}).Once ()
736
+ app .On ("EndBlock" , mock .Anything ).Return (abci.ResponseEndBlock {ValidatorUpdates : []abci.ValidatorUpdate {{PubKey : pbValKey , Power : 100 }}}).Once ()
737
+ app .On ("EndBlock" , mock .Anything ).Return (abci.ResponseEndBlock {}).Run (func (args mock.Arguments ) {
738
+ waitCh <- nil
739
+ })
740
+ return app
741
+ }
742
+ rpc := createGenesisValidators (numNodes , createApp , require )
715
743
716
744
<- waitCh
717
745
<- waitCh
@@ -721,8 +749,8 @@ func TestValidatorSetHandling(t *testing.T) {
721
749
vals , err := rpc .Validators (context .Background (), & h , nil , nil )
722
750
assert .NoError (err )
723
751
assert .NotNil (vals )
724
- assert .EqualValues (len ( genesisValidators ) , vals .Total )
725
- assert .Len (vals .Validators , len ( genesisValidators ) )
752
+ assert .EqualValues (numNodes , vals .Total )
753
+ assert .Len (vals .Validators , numNodes )
726
754
assert .EqualValues (vals .BlockHeight , h )
727
755
}
728
756
@@ -731,8 +759,8 @@ func TestValidatorSetHandling(t *testing.T) {
731
759
vals , err := rpc .Validators (context .Background (), & h , nil , nil )
732
760
assert .NoError (err )
733
761
assert .NotNil (vals )
734
- assert .EqualValues (len ( genesisValidators ) - 1 , vals .Total )
735
- assert .Len (vals .Validators , len ( genesisValidators ) - 1 )
762
+ assert .EqualValues (numNodes - 1 , vals .Total )
763
+ assert .Len (vals .Validators , numNodes - 1 )
736
764
assert .EqualValues (vals .BlockHeight , h )
737
765
}
738
766
@@ -743,40 +771,81 @@ func TestValidatorSetHandling(t *testing.T) {
743
771
vals , err := rpc .Validators (context .Background (), & h , nil , nil )
744
772
assert .NoError (err )
745
773
assert .NotNil (vals )
746
- assert .EqualValues (len ( genesisValidators ) , vals .Total )
747
- assert .Len (vals .Validators , len ( genesisValidators ) )
774
+ assert .EqualValues (numNodes , vals .Total )
775
+ assert .Len (vals .Validators , numNodes )
748
776
assert .EqualValues (vals .BlockHeight , h )
749
777
}
750
778
751
779
// check for "latest block"
752
780
vals , err := rpc .Validators (context .Background (), nil , nil , nil )
753
781
assert .NoError (err )
754
782
assert .NotNil (vals )
755
- assert .EqualValues (len ( genesisValidators ) , vals .Total )
756
- assert .Len (vals .Validators , len ( genesisValidators ) )
783
+ assert .EqualValues (numNodes , vals .Total )
784
+ assert .Len (vals .Validators , numNodes )
757
785
assert .GreaterOrEqual (vals .BlockHeight , int64 (9 ))
758
786
}
759
787
760
- func createApp (keyToRemove tmcrypto.PrivKey , waitCh chan interface {}, require * require.Assertions ) * mocks.Application {
761
- app := & mocks.Application {}
762
- app .On ("InitChain" , mock .Anything ).Return (abci.ResponseInitChain {})
763
- app .On ("CheckTx" , mock .Anything ).Return (abci.ResponseCheckTx {})
764
- app .On ("BeginBlock" , mock .Anything ).Return (abci.ResponseBeginBlock {})
765
- app .On ("Commit" , mock .Anything ).Return (abci.ResponseCommit {})
766
- app .On ("GetAppHash" , mock .Anything ).Return (abci.ResponseGetAppHash {})
767
- app .On ("GenerateFraudProof" , mock .Anything ).Return (abci.ResponseGenerateFraudProof {})
788
+ // Tests moving from a centralized validator to empty validator set
789
+ func TestValidatorSetHandlingBased (t * testing.T ) {
790
+ assert := assert .New (t )
791
+ require := require .New (t )
768
792
769
- pbValKey , err := encoding .PubKeyToProto (keyToRemove .PubKey ())
770
- require .NoError (err )
793
+ waitCh := make (chan interface {})
771
794
772
- app .On ("EndBlock" , mock .Anything ).Return (abci.ResponseEndBlock {}).Times (2 )
773
- app .On ("EndBlock" , mock .Anything ).Return (abci.ResponseEndBlock {ValidatorUpdates : []abci.ValidatorUpdate {{PubKey : pbValKey , Power : 0 }}}).Once ()
774
- app .On ("EndBlock" , mock .Anything ).Return (abci.ResponseEndBlock {}).Once ()
775
- app .On ("EndBlock" , mock .Anything ).Return (abci.ResponseEndBlock {ValidatorUpdates : []abci.ValidatorUpdate {{PubKey : pbValKey , Power : 100 }}}).Once ()
776
- app .On ("EndBlock" , mock .Anything ).Return (abci.ResponseEndBlock {}).Run (func (args mock.Arguments ) {
777
- waitCh <- nil
778
- })
779
- return app
795
+ numNodes := 1
796
+ createApp := func (vKeyToRemove tmcrypto.PrivKey ) * mocks.Application {
797
+ app := & mocks.Application {}
798
+ app .On ("InitChain" , mock .Anything ).Return (abci.ResponseInitChain {})
799
+ app .On ("CheckTx" , mock .Anything ).Return (abci.ResponseCheckTx {})
800
+ app .On ("BeginBlock" , mock .Anything ).Return (abci.ResponseBeginBlock {})
801
+ app .On ("Commit" , mock .Anything ).Return (abci.ResponseCommit {})
802
+ app .On ("GetAppHash" , mock .Anything ).Return (abci.ResponseGetAppHash {})
803
+ app .On ("GenerateFraudProof" , mock .Anything ).Return (abci.ResponseGenerateFraudProof {})
804
+
805
+ pbValKey , err := encoding .PubKeyToProto (vKeyToRemove .PubKey ())
806
+ require .NoError (err )
807
+
808
+ app .On ("EndBlock" , mock .Anything ).Return (abci.ResponseEndBlock {}).Times (2 )
809
+ app .On ("EndBlock" , mock .Anything ).Return (abci.ResponseEndBlock {ValidatorUpdates : []abci.ValidatorUpdate {{PubKey : pbValKey , Power : 0 }}}).Once ()
810
+ app .On ("EndBlock" , mock .Anything ).Return (abci.ResponseEndBlock {}).Run (func (args mock.Arguments ) {
811
+ waitCh <- nil
812
+ })
813
+ return app
814
+ }
815
+
816
+ rpc := createGenesisValidators (numNodes , createApp , require )
817
+
818
+ <- waitCh
819
+
820
+ // test first blocks
821
+ for h := int64 (1 ); h <= 3 ; h ++ {
822
+ vals , err := rpc .Validators (context .Background (), & h , nil , nil )
823
+ assert .NoError (err )
824
+ assert .NotNil (vals )
825
+ assert .EqualValues (numNodes , vals .Total )
826
+ assert .Len (vals .Validators , numNodes )
827
+ assert .EqualValues (vals .BlockHeight , h )
828
+ }
829
+
830
+ // 3rd EndBlock removes the first validator and makes the rollup based
831
+ for h := int64 (4 ); h <= 9 ; h ++ {
832
+ <- waitCh
833
+ vals , err := rpc .Validators (context .Background (), & h , nil , nil )
834
+ assert .NoError (err )
835
+ assert .NotNil (vals )
836
+ assert .EqualValues (numNodes - 1 , vals .Total )
837
+ assert .Len (vals .Validators , numNodes - 1 )
838
+ assert .EqualValues (vals .BlockHeight , h )
839
+ }
840
+
841
+ // check for "latest block"
842
+ <- waitCh
843
+ vals , err := rpc .Validators (context .Background (), nil , nil , nil )
844
+ assert .NoError (err )
845
+ assert .NotNil (vals )
846
+ assert .EqualValues (numNodes - 1 , vals .Total )
847
+ assert .Len (vals .Validators , numNodes - 1 )
848
+ assert .GreaterOrEqual (vals .BlockHeight , int64 (9 ))
780
849
}
781
850
782
851
// copy-pasted from store/store_test.go
0 commit comments