code.vegaprotocol.io/vega@v0.79.0/commands/validator_heartbeat_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 commands_test
    17  
    18  import (
    19  	"errors"
    20  	"testing"
    21  
    22  	"code.vegaprotocol.io/vega/commands"
    23  	commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestNilValidatorHeartbeatFails(t *testing.T) {
    29  	err := checkValidatorHeartbeat(nil)
    30  
    31  	assert.Contains(t, err.Get("validator_heartbeat"), commands.ErrIsRequired)
    32  }
    33  
    34  func TestValidatorHeartbeat(t *testing.T) {
    35  	cases := []struct {
    36  		vh        commandspb.ValidatorHeartbeat
    37  		errString string
    38  	}{
    39  		{
    40  			vh: commandspb.ValidatorHeartbeat{
    41  				NodeId: "84e2b15102a8d6c1c6b4bdf40af8a0dc21b040eaaa1c94cd10d17604b75fdc35",
    42  				EthereumSignature: &commandspb.Signature{
    43  					Value: "84e2b15102a8d6c1c6b4bdf40af8a0dc21b040eaaa1c94cd10d17604b75fdc35",
    44  				},
    45  				VegaSignature: &commandspb.Signature{
    46  					Value: "84e2b15102a8d6c1c6b4bdf40af8a0dc21b040eaaa1c94cd10d17604b75fdc35",
    47  					Algo:  "some/algo",
    48  				},
    49  			},
    50  		},
    51  		{
    52  			vh: commandspb.ValidatorHeartbeat{
    53  				NodeId: "84e2b15102a8d6c1c6b4bdf40af8a0dc21b040eaaa1c94cd10d17604b75fdc35",
    54  				EthereumSignature: &commandspb.Signature{
    55  					Value: "84e2b15102a8d6c1c6b4bdf40af8a0dc21b040eaaa1c94cd10d17604b75fdc35",
    56  				},
    57  				VegaSignature: &commandspb.Signature{
    58  					Value: "84e2b15102a8d6c1c6b4bdf40af8a0dc21b040eaaa1c94cd10d17604b75fdc35",
    59  				},
    60  			},
    61  			errString: "validator_heartbeat.vega_signature.algo (is required)",
    62  		},
    63  		{
    64  			vh: commandspb.ValidatorHeartbeat{
    65  				NodeId: "84e2b15102a8d6c1c6b4bdf40af8a0dc21b040eaaa1c94cd10d17604b75fdc35",
    66  				EthereumSignature: &commandspb.Signature{
    67  					Value: "84e2b15102a8d6c1c6b4bdf40af8a0dc21b040eaaa1c94cd10d17604b75fdc35",
    68  				},
    69  				VegaSignature: &commandspb.Signature{
    70  					Algo: "some/algo",
    71  				},
    72  			},
    73  			errString: "validator_heartbeat.vega_signature.value (is required)",
    74  		},
    75  		{
    76  			vh: commandspb.ValidatorHeartbeat{
    77  				NodeId:            "84e2b15102a8d6c1c6b4bdf40af8a0dc21b040eaaa1c94cd10d17604b75fdc35",
    78  				EthereumSignature: &commandspb.Signature{},
    79  				VegaSignature: &commandspb.Signature{
    80  					Value: "84e2b15102a8d6c1c6b4bdf40af8a0dc21b040eaaa1c94cd10d17604b75fdc35",
    81  					Algo:  "some/algo",
    82  				},
    83  			},
    84  			errString: "validator_heartbeat.ethereum_signature.value (is required)",
    85  		},
    86  		{
    87  			vh: commandspb.ValidatorHeartbeat{
    88  				EthereumSignature: &commandspb.Signature{
    89  					Value: "84e2b15102a8d6c1c6b4bdf40af8a0dc21b040eaaa1c94cd10d17604b75fdc35",
    90  				},
    91  				VegaSignature: &commandspb.Signature{
    92  					Value: "84e2b15102a8d6c1c6b4bdf40af8a0dc21b040eaaa1c94cd10d17604b75fdc35",
    93  					Algo:  "some/algo",
    94  				},
    95  			},
    96  			errString: "validator_heartbeat.node_id (is required)",
    97  		},
    98  		{
    99  			vh: commandspb.ValidatorHeartbeat{
   100  				NodeId: "84e2b15102a8d6c1c6b4bdf40af8a0dc21b040eaaa1c94cd10d17604b75fdc35",
   101  				VegaSignature: &commandspb.Signature{
   102  					Value: "84e2b15102a8d6c1c6b4bdf40af8a0dc21b040eaaa1c94cd10d17604b75fdc35",
   103  					Algo:  "some/algo",
   104  				},
   105  			},
   106  			errString: "validator_heartbeat.ethereum_signature.value (is required)",
   107  		},
   108  	}
   109  
   110  	for _, c := range cases {
   111  		err := commands.CheckValidatorHeartbeat(&c.vh)
   112  		if len(c.errString) <= 0 {
   113  			assert.NoError(t, err)
   114  			continue
   115  		}
   116  		assert.EqualError(t, err, c.errString)
   117  	}
   118  }
   119  
   120  func checkValidatorHeartbeat(cmd *commandspb.ValidatorHeartbeat) commands.Errors {
   121  	err := commands.CheckValidatorHeartbeat(cmd)
   122  
   123  	var e commands.Errors
   124  	if ok := errors.As(err, &e); !ok {
   125  		return commands.NewErrors()
   126  	}
   127  
   128  	return e
   129  }