code.vegaprotocol.io/vega@v0.79.0/core/nodewallets/commander_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 nodewallets_test 17 18 import ( 19 "context" 20 "testing" 21 22 "code.vegaprotocol.io/vega/core/nodewallets" 23 "code.vegaprotocol.io/vega/core/nodewallets/mocks" 24 vgnw "code.vegaprotocol.io/vega/core/nodewallets/vega" 25 "code.vegaprotocol.io/vega/core/txn" 26 vgrand "code.vegaprotocol.io/vega/libs/rand" 27 vgtesting "code.vegaprotocol.io/vega/libs/testing" 28 "code.vegaprotocol.io/vega/logging" 29 commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1" 30 31 tmctypes "github.com/cometbft/cometbft/rpc/core/types" 32 "github.com/golang/mock/gomock" 33 "github.com/stretchr/testify/assert" 34 "github.com/stretchr/testify/require" 35 ) 36 37 type testCommander struct { 38 *nodewallets.Commander 39 ctx context.Context 40 cfunc context.CancelFunc 41 ctrl *gomock.Controller 42 chain *mocks.MockChain 43 bstats *mocks.MockBlockchainStats 44 wallet *vgnw.Wallet 45 } 46 47 func getTestCommander(t *testing.T) *testCommander { 48 t.Helper() 49 ctx, cfunc := context.WithCancel(context.Background()) 50 ctrl := gomock.NewController(t) 51 chain := mocks.NewMockChain(ctrl) 52 bstats := mocks.NewMockBlockchainStats(ctrl) 53 vegaPaths, _ := vgtesting.NewVegaPaths() 54 registryPass := vgrand.RandomStr(10) 55 walletPass := vgrand.RandomStr(10) 56 57 _, err := nodewallets.GenerateVegaWallet(vegaPaths, registryPass, walletPass, false) 58 require.NoError(t, err) 59 wallet, err := nodewallets.GetVegaWallet(vegaPaths, registryPass) 60 require.NoError(t, err) 61 require.NotNil(t, wallet) 62 63 cmd, err := nodewallets.NewCommander(nodewallets.NewDefaultConfig(), logging.NewTestLogger(), chain, wallet, bstats) 64 require.NoError(t, err) 65 66 return &testCommander{ 67 Commander: cmd, 68 ctx: ctx, 69 cfunc: cfunc, 70 ctrl: ctrl, 71 chain: chain, 72 bstats: bstats, 73 wallet: wallet, 74 } 75 } 76 77 func TestCommand(t *testing.T) { 78 t.Run("Signed command - success", testSignedCommandSuccess) 79 t.Run("Signed command - failure", testSignedCommandFailure) 80 } 81 82 func testSignedCommandSuccess(t *testing.T) { 83 commander := getTestCommander(t) 84 defer commander.Finish() 85 86 cmd := txn.NodeVoteCommand 87 payload := &commandspb.NodeVote{ 88 Reference: "test", 89 } 90 expectedChainID := vgrand.RandomStr(5) 91 92 commander.bstats.EXPECT().Height().AnyTimes().Return(uint64(42)) 93 commander.chain.EXPECT().GetChainID(gomock.Any()).Times(1).Return(expectedChainID, nil) 94 commander.chain.EXPECT().SubmitTransactionSync(gomock.Any(), gomock.Any()).Times(1).Return(&tmctypes.ResultBroadcastTx{}, nil) 95 96 ok := make(chan error) 97 commander.Command(context.Background(), cmd, payload, func(_ string, err error) { 98 ok <- err 99 }, nil) 100 assert.NoError(t, <-ok) 101 } 102 103 func testSignedCommandFailure(t *testing.T) { 104 commander := getTestCommander(t) 105 defer commander.Finish() 106 107 cmd := txn.NodeVoteCommand 108 payload := &commandspb.NodeVote{ 109 Reference: "test", 110 } 111 commander.bstats.EXPECT().Height().AnyTimes().Return(uint64(42)) 112 commander.chain.EXPECT().GetChainID(gomock.Any()).Times(1).Return(vgrand.RandomStr(5), nil) 113 commander.chain.EXPECT().SubmitTransactionSync(gomock.Any(), gomock.Any()).Times(1).Return(&tmctypes.ResultBroadcastTx{}, assert.AnError) 114 115 ok := make(chan error) 116 commander.Command(context.Background(), cmd, payload, func(_ string, err error) { 117 ok <- err 118 }, nil) 119 assert.Error(t, <-ok) 120 } 121 122 func (t *testCommander) Finish() { 123 t.cfunc() 124 t.ctrl.Finish() 125 }