code.vegaprotocol.io/vega@v0.79.0/core/validators/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 validators_test
    17  
    18  import (
    19  	"context"
    20  	"encoding/hex"
    21  	"fmt"
    22  	"testing"
    23  
    24  	"code.vegaprotocol.io/vega/core/nodewallets"
    25  	"code.vegaprotocol.io/vega/core/validators"
    26  	vgrand "code.vegaprotocol.io/vega/libs/rand"
    27  	vgtesting "code.vegaprotocol.io/vega/libs/testing"
    28  	commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
    29  
    30  	"github.com/ethereum/go-ethereum/crypto"
    31  	"github.com/stretchr/testify/require"
    32  )
    33  
    34  func TestTendermintKey(t *testing.T) {
    35  	t.Parallel()
    36  	notBase64 := "170ffakjde"
    37  	require.Error(t, validators.VerifyTendermintKey(notBase64))
    38  
    39  	validKey := "794AFpbqJvHF711mhAK3fvSLnoXuuiig2ecrdeSJ/bk="
    40  	require.NoError(t, validators.VerifyTendermintKey(validKey))
    41  }
    42  
    43  func TestAnnounceNode(t *testing.T) {
    44  	t.Parallel()
    45  	ctx := context.Background()
    46  	tt := getTestTopology(t)
    47  	cmd := createSignedAnnounceCommand(t)
    48  
    49  	// Now announce it and check the signature verify
    50  	require.NoError(t, tt.Topology.ProcessAnnounceNode(ctx, cmd))
    51  
    52  	// Announce it again
    53  	require.ErrorIs(t, tt.Topology.ProcessAnnounceNode(ctx, cmd), validators.ErrVegaNodeAlreadyRegisterForChain)
    54  }
    55  
    56  func createSignedAnnounceCommand(t *testing.T) *commandspb.AnnounceNode {
    57  	t.Helper()
    58  	nodeWallets := createTestNodeWallets(t)
    59  	cmd := commandspb.AnnounceNode{
    60  		Id:              nodeWallets.Vega.ID().Hex(),
    61  		VegaPubKey:      nodeWallets.Vega.PubKey().Hex(),
    62  		VegaPubKeyIndex: nodeWallets.Vega.Index(),
    63  		ChainPubKey:     "794AFpbqJvHF711mhAK3fvSLnoXuuiig2ecrdeSJ/bk=",
    64  		EthereumAddress: nodeWallets.Ethereum.PubKey().Hex(),
    65  		FromEpoch:       1,
    66  		InfoUrl:         "www.some.com",
    67  		Name:            "that is not my name",
    68  		AvatarUrl:       "www.avatar.com",
    69  		Country:         "some country",
    70  	}
    71  	err := validators.SignAnnounceNode(&cmd, nodeWallets.Vega, nodeWallets.Ethereum)
    72  	require.NoError(t, err)
    73  
    74  	// verify that the expected signature for vega key is there
    75  	messageToSign := cmd.Id + cmd.VegaPubKey + fmt.Sprintf("%d", cmd.VegaPubKeyIndex) + cmd.ChainPubKey + cmd.EthereumAddress + fmt.Sprintf("%d", cmd.FromEpoch) + cmd.InfoUrl + cmd.Name + cmd.AvatarUrl + cmd.Country
    76  	sig, err := nodeWallets.Vega.Sign([]byte(messageToSign))
    77  	sigHex := hex.EncodeToString(sig)
    78  	require.NoError(t, err)
    79  	require.Equal(t, sigHex, cmd.VegaSignature.Value)
    80  
    81  	// verify that the expected signature for eth key is there
    82  	ethSig, err := nodeWallets.Ethereum.Sign(crypto.Keccak256([]byte(messageToSign)))
    83  	ethSigHex := hex.EncodeToString(ethSig)
    84  	require.NoError(t, err)
    85  	require.Equal(t, ethSigHex, cmd.EthereumSignature.Value)
    86  
    87  	return &cmd
    88  }
    89  
    90  func createTestNodeWallets(t *testing.T) *nodewallets.NodeWallets {
    91  	t.Helper()
    92  	config := nodewallets.NewDefaultConfig()
    93  	vegaPaths, cleanupFn := vgtesting.NewVegaPaths()
    94  	defer cleanupFn()
    95  	registryPass := vgrand.RandomStr(10)
    96  	walletsPass := vgrand.RandomStr(10)
    97  
    98  	if _, err := nodewallets.GenerateEthereumWallet(vegaPaths, registryPass, walletsPass, "", false); err != nil {
    99  		t.Fatal("couldn't generate Ethereum node wallet for tests")
   100  	}
   101  
   102  	if _, err := nodewallets.GenerateVegaWallet(vegaPaths, registryPass, walletsPass, false); err != nil {
   103  		t.Fatal("couldn't generate Vega node wallet for tests")
   104  	}
   105  	nw, err := nodewallets.GetNodeWallets(config, vegaPaths, registryPass)
   106  	require.NoError(t, err)
   107  	return nw
   108  }