code.vegaprotocol.io/vega@v0.79.0/commands/announce_node_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  	"encoding/hex"
    20  	"errors"
    21  	"testing"
    22  
    23  	"code.vegaprotocol.io/vega/commands"
    24  	commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestCheckAnnounceNode(t *testing.T) {
    30  	t.Run("Submitting a nil command fails", testNilAnnounceNodeFails)
    31  	t.Run("Submitting a node registration without vega pub key fails", testAnnounceNodeWithoutVegaPubKeyFails)
    32  	t.Run("Submitting a node registration with valid vega pub key succeeds", testAnnounceNodeWithValidVegaPubKeySucceeds)
    33  	t.Run("Submitting a node registration with invalid encoding of vega pub key succeeds", testAnnounceNodeWithInvalidEncodingOfVegaPubKeyFails)
    34  	t.Run("Submitting a node registration without ethereum pub key fails", testAnnounceNodeWithoutEthereumAddressFails)
    35  	t.Run("Submitting a node registration with ethereum address succeeds", testAnnounceNodeWithEthereumAddressSucceeds)
    36  	t.Run("Submitting a node registration without chain address fails", testAnnounceNodeWithoutChainPubKeyFails)
    37  	t.Run("Submitting a node registration with chain pub key succeeds", testAnnounceNodeWithChainPubKeySucceeds)
    38  	t.Run("Submitting a node registration with empty signatures fails", testAnnounceNodeWithEmptySignaturesFails)
    39  	t.Run("Submitting a node registration with nonhex signatures fails", testAnnounceNodeWithNonhexSignaturesFails)
    40  }
    41  
    42  func testNilAnnounceNodeFails(t *testing.T) {
    43  	err := checkAnnounceNode(nil)
    44  
    45  	assert.Error(t, err)
    46  }
    47  
    48  func testAnnounceNodeWithoutVegaPubKeyFails(t *testing.T) {
    49  	err := checkAnnounceNode(&commandspb.AnnounceNode{})
    50  	assert.Contains(t, err.Get("announce_node.vega_pub_key"), commands.ErrIsRequired)
    51  }
    52  
    53  func testAnnounceNodeWithValidVegaPubKeySucceeds(t *testing.T) {
    54  	err := checkAnnounceNode(&commandspb.AnnounceNode{
    55  		VegaPubKey: hex.EncodeToString([]byte("0xDEADBEEF")),
    56  	})
    57  	assert.NotContains(t, err.Get("announce_node.vega_pub_key"), commands.ErrIsRequired)
    58  	assert.NotContains(t, err.Get("announce_node.vega_pub_key"), commands.ErrShouldBeHexEncoded)
    59  }
    60  
    61  func testAnnounceNodeWithInvalidEncodingOfVegaPubKeyFails(t *testing.T) {
    62  	err := checkAnnounceNode(&commandspb.AnnounceNode{
    63  		VegaPubKey: "invalid-hex-encoding",
    64  	})
    65  	assert.Contains(t, err.Get("announce_node.vega_pub_key"), commands.ErrShouldBeAValidVegaPublicKey)
    66  }
    67  
    68  func testAnnounceNodeWithoutEthereumAddressFails(t *testing.T) {
    69  	err := checkAnnounceNode(&commandspb.AnnounceNode{})
    70  	assert.Contains(t, err.Get("announce_node.ethereum_address"), commands.ErrIsRequired)
    71  }
    72  
    73  func testAnnounceNodeWithEthereumAddressSucceeds(t *testing.T) {
    74  	err := checkAnnounceNode(&commandspb.AnnounceNode{
    75  		EthereumAddress: "0xDEADBEEF",
    76  	})
    77  	assert.NotContains(t, err.Get("announce_node.ethereum_address"), commands.ErrIsRequired)
    78  }
    79  
    80  func testAnnounceNodeWithoutChainPubKeyFails(t *testing.T) {
    81  	err := checkAnnounceNode(&commandspb.AnnounceNode{})
    82  	assert.Contains(t, err.Get("announce_node.chain_pub_key"), commands.ErrIsRequired)
    83  }
    84  
    85  func testAnnounceNodeWithChainPubKeySucceeds(t *testing.T) {
    86  	err := checkAnnounceNode(&commandspb.AnnounceNode{
    87  		ChainPubKey: "0xDEADBEEF",
    88  	})
    89  	assert.NotContains(t, err.Get("announce_node.chain_pub_key"), commands.ErrIsRequired)
    90  }
    91  
    92  func testAnnounceNodeWithEmptySignaturesFails(t *testing.T) {
    93  	err := checkAnnounceNode(&commandspb.AnnounceNode{})
    94  	assert.Contains(t, err.Get("announce_node.ethereum_signature"), commands.ErrIsRequired)
    95  	assert.Contains(t, err.Get("announce_node.vega_signature"), commands.ErrIsRequired)
    96  }
    97  
    98  func testAnnounceNodeWithNonhexSignaturesFails(t *testing.T) {
    99  	err := checkAnnounceNode(&commandspb.AnnounceNode{
   100  		VegaSignature: &commandspb.Signature{
   101  			Value: "hello",
   102  		},
   103  		EthereumSignature: &commandspb.Signature{
   104  			Value: "helloagain",
   105  		},
   106  	})
   107  	assert.Contains(t, err.Get("announce_node.ethereum_signature.value"), commands.ErrShouldBeHexEncoded)
   108  	assert.Contains(t, err.Get("announce_node.vega_signature.value"), commands.ErrShouldBeHexEncoded)
   109  }
   110  
   111  func checkAnnounceNode(cmd *commandspb.AnnounceNode) commands.Errors {
   112  	err := commands.CheckAnnounceNode(cmd)
   113  
   114  	var e commands.Errors
   115  	if ok := errors.As(err, &e); !ok {
   116  		return commands.NewErrors()
   117  	}
   118  
   119  	return e
   120  }