code.vegaprotocol.io/vega@v0.79.0/core/validators/validators_checkpoint_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package validators
    17  
    18  import (
    19  	"bytes"
    20  	"context"
    21  	"testing"
    22  	"time"
    23  
    24  	bmocks "code.vegaprotocol.io/vega/core/broker/mocks"
    25  
    26  	"github.com/golang/mock/gomock"
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  func TestValidatorsCheckpoint(t *testing.T) {
    31  	topology := &Topology{}
    32  	topology.validators = map[string]*valState{}
    33  
    34  	topology.validators["node1"] = &valState{
    35  		data: ValidatorData{
    36  			ID:              "id1",
    37  			VegaPubKey:      "pubkey1",
    38  			VegaPubKeyIndex: 1,
    39  			TmPubKey:        "tmpubkey1",
    40  			EthereumAddress: "eth1",
    41  			FromEpoch:       1,
    42  		},
    43  		blockAdded:                   2,
    44  		status:                       ValidatorStatusTendermint,
    45  		statusChangeBlock:            1,
    46  		lastBlockWithPositiveRanking: 1,
    47  		heartbeatTracker: &validatorHeartbeatTracker{
    48  			expectedNextHash:      "",
    49  			expectedNexthashSince: time.Now(),
    50  			blockIndex:            0,
    51  			blockSigs:             [10]bool{true, false, false, false, false, false, false, false, false, false},
    52  		},
    53  		numberOfEthereumEventsForwarded: 4,
    54  		validatorPower:                  100,
    55  	}
    56  	topology.validators["node1"] = &valState{
    57  		data: ValidatorData{
    58  			ID:              "id2",
    59  			VegaPubKey:      "pubkey2",
    60  			VegaPubKeyIndex: 0,
    61  			TmPubKey:        "tmpubkey2",
    62  			EthereumAddress: "eth2",
    63  			FromEpoch:       2,
    64  		},
    65  		blockAdded:                   1,
    66  		status:                       ValidatorStatusTendermint,
    67  		statusChangeBlock:            2,
    68  		lastBlockWithPositiveRanking: 1,
    69  		heartbeatTracker: &validatorHeartbeatTracker{
    70  			expectedNextHash:      "abcde",
    71  			expectedNexthashSince: time.Now(),
    72  			blockIndex:            0,
    73  			blockSigs:             [10]bool{false, false, false, false, false, false, false, false, false, false},
    74  		},
    75  		numberOfEthereumEventsForwarded: 3,
    76  		validatorPower:                  120,
    77  	}
    78  
    79  	topology.pendingPubKeyRotations = make(pendingKeyRotationMapping)
    80  	hash1, err := topology.Checkpoint()
    81  	require.NoError(t, err)
    82  
    83  	topology2 := &Topology{}
    84  	ctrl := gomock.NewController(t)
    85  	defer ctrl.Finish()
    86  	broker := bmocks.NewMockBroker(ctrl)
    87  	topology2.broker = broker
    88  	broker.EXPECT().Send(gomock.Any()).AnyTimes()
    89  	topology2.Load(context.Background(), hash1)
    90  	hash2, err := topology2.Checkpoint()
    91  	require.NoError(t, err)
    92  
    93  	require.True(t, bytes.Equal(hash1, hash2))
    94  }