code.vegaprotocol.io/vega@v0.79.0/core/governance/node_validation_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 governance_test 17 18 import ( 19 "context" 20 "testing" 21 "time" 22 23 "code.vegaprotocol.io/vega/core/governance" 24 "code.vegaprotocol.io/vega/core/governance/mocks" 25 "code.vegaprotocol.io/vega/core/types" 26 "code.vegaprotocol.io/vega/logging" 27 28 "github.com/golang/mock/gomock" 29 "github.com/stretchr/testify/assert" 30 ) 31 32 type testNodeValidation struct { 33 *governance.NodeValidation 34 ctrl *gomock.Controller 35 assets *mocks.MockAssets 36 witness *mocks.MockWitness 37 } 38 39 func getTestNodeValidation(t *testing.T, tm time.Time) *testNodeValidation { 40 t.Helper() 41 ctrl := gomock.NewController(t) 42 assets := mocks.NewMockAssets(ctrl) 43 witness := mocks.NewMockWitness(ctrl) 44 45 nv := governance.NewNodeValidation( 46 logging.NewTestLogger(), assets, tm, witness) 47 assert.NotNil(t, nv) 48 49 return &testNodeValidation{ 50 NodeValidation: nv, 51 ctrl: ctrl, 52 assets: assets, 53 witness: witness, 54 } 55 } 56 57 func TestNodeValidation(t *testing.T) { 58 t.Run("test node validation required - true", testNodeValidationRequiredTrue) 59 t.Run("test node validation required - false", testNodeValidationRequiredFalse) 60 61 t.Run("start - error no node validation required", testStartErrorNoNodeValidationRequired) 62 t.Run("start - error check proposal failed", testStartErrorCheckProposalFailed) 63 } 64 65 func testNodeValidationRequiredTrue(t *testing.T) { 66 nv := getTestNodeValidation(t, time.Now()) 67 defer nv.ctrl.Finish() 68 69 p := &types.Proposal{ 70 Terms: &types.ProposalTerms{ 71 Change: &types.ProposalTermsNewAsset{}, 72 }, 73 } 74 75 assert.True(t, nv.IsNodeValidationRequired(p)) 76 } 77 78 func testNodeValidationRequiredFalse(t *testing.T) { 79 nv := getTestNodeValidation(t, time.Now()) 80 defer nv.ctrl.Finish() 81 82 p := &types.Proposal{ 83 Terms: &types.ProposalTerms{ 84 Change: &types.ProposalTermsNewMarket{}, 85 }, 86 } 87 88 assert.False(t, nv.IsNodeValidationRequired(p)) 89 } 90 91 func testStartErrorNoNodeValidationRequired(t *testing.T) { 92 nv := getTestNodeValidation(t, time.Now()) 93 defer nv.ctrl.Finish() 94 95 p := &types.Proposal{ 96 Terms: &types.ProposalTerms{ 97 Change: &types.ProposalTermsNewMarket{}, 98 }, 99 } 100 101 err := nv.Start(context.Background(), p) 102 assert.EqualError(t, err, governance.ErrNoNodeValidationRequired.Error()) 103 } 104 105 func testStartErrorCheckProposalFailed(t *testing.T) { 106 tm := time.Now() 107 nv := getTestNodeValidation(t, tm) 108 defer nv.ctrl.Finish() 109 110 // first closing time < validation time 111 p := &types.Proposal{ 112 Terms: &types.ProposalTerms{ 113 ClosingTimestamp: tm.Add(1 * time.Hour).Unix(), 114 ValidationTimestamp: tm.Add(2 * time.Hour).Unix(), 115 Change: &types.ProposalTermsNewAsset{}, 116 }, 117 } 118 119 err := nv.Start(context.Background(), p) 120 assert.EqualError(t, err, governance.ErrProposalValidationTimestampTooLate.Error()) 121 122 // validation timestamp after 2 days 123 p.Terms.ClosingTimestamp = tm.Add(3 * 24 * time.Hour).Unix() 124 p.Terms.ValidationTimestamp = tm.Add(2*24*time.Hour + 1*time.Second).Unix() 125 err = nv.Start(context.Background(), p) 126 assert.EqualError(t, err, governance.ErrProposalValidationTimestampOutsideRange.Error()) 127 128 // validation timestamp = submission time 129 p.Terms.ValidationTimestamp = tm.Unix() 130 err = nv.Start(context.Background(), p) 131 assert.EqualError(t, err, governance.ErrProposalValidationTimestampOutsideRange.Error()) 132 133 // all good 134 nv.assets.EXPECT().NewAsset(gomock.Any(), gomock.Any(), gomock.Any()) 135 nv.witness.EXPECT().StartCheck(gomock.Any(), gomock.Any(), gomock.Any()) 136 p.Terms.ValidationTimestamp = tm.Add(1 * 24 * time.Hour).Unix() 137 err = nv.Start(context.Background(), p) 138 assert.NoError(t, err) 139 }