code.vegaprotocol.io/vega@v0.79.0/core/notary/snapshot_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 notary_test 17 18 import ( 19 "bytes" 20 "context" 21 "testing" 22 "time" 23 24 "code.vegaprotocol.io/vega/core/types" 25 "code.vegaprotocol.io/vega/libs/proto" 26 commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1" 27 snapshot "code.vegaprotocol.io/vega/protos/vega/snapshot/v1" 28 29 "github.com/golang/mock/gomock" 30 "github.com/stretchr/testify/require" 31 ) 32 33 var allKey = (&types.PayloadNotary{}).Key() 34 35 func TestNotarySnapshotEmpty(t *testing.T) { 36 notr := getTestNotary(t) 37 s, _, err := notr.GetState(allKey) 38 require.Nil(t, err) 39 require.NotNil(t, s) 40 } 41 42 func TestNotarySnapshotVotesKinds(t *testing.T) { 43 notr := getTestNotary(t) 44 45 s1, _, err := notr.GetState(allKey) 46 require.Nil(t, err) 47 48 resID := "resid" 49 notr.top.EXPECT().Len().AnyTimes().Return(1) 50 notr.top.EXPECT().IsValidatorVegaPubKey(gomock.Any()).AnyTimes().Return(true) 51 notr.top.EXPECT().IsValidator().AnyTimes().Return(true) 52 53 notr.StartAggregate(resID, types.NodeSignatureKindAssetNew, []byte("123456")) 54 55 s2, _, err := notr.GetState(allKey) 56 require.Nil(t, err) 57 require.False(t, bytes.Equal(s1, s2)) 58 } 59 60 func populateNotary(t *testing.T, notr *testNotary) { 61 t.Helper() 62 notr.top.EXPECT().IsValidator().AnyTimes().Return(true) 63 // First ID/Kind 64 resID := "resid1" 65 notr.StartAggregate(resID, types.NodeSignatureKindAssetNew, []byte("123456")) 66 notr.StartAggregate( 67 resID, types.NodeSignatureKindAssetWithdrawal, []byte("56789")) 68 69 key := "123456" 70 ns := commandspb.NodeSignature{ 71 Sig: []byte("123456"), 72 Id: resID, 73 Kind: types.NodeSignatureKindAssetNew, 74 } 75 err := notr.RegisterSignature(context.Background(), key, ns) 76 require.Nil(t, err) 77 78 // same key different sig 79 ns = commandspb.NodeSignature{ 80 Sig: []byte("56789"), 81 Id: resID, 82 Kind: types.NodeSignatureKindAssetNew, 83 } 84 err = notr.RegisterSignature(context.Background(), key, ns) 85 require.Nil(t, err) 86 87 // Add another ID/Kind 88 resID = "resid2" 89 notr.StartAggregate(resID, types.NodeSignatureKindAssetNew, []byte("a123456")) 90 91 ns = commandspb.NodeSignature{ 92 Sig: []byte("a123456"), 93 Id: resID, 94 Kind: types.NodeSignatureKindAssetNew, 95 } 96 err = notr.RegisterSignature(context.Background(), "123456", ns) 97 require.Nil(t, err) 98 99 // same sig different key (unlikely) 100 ns = commandspb.NodeSignature{ 101 Sig: []byte("b123456"), 102 Id: resID, 103 Kind: types.NodeSignatureKindAssetNew, 104 } 105 106 err = notr.RegisterSignature(context.Background(), "56789", ns) 107 require.Nil(t, err) 108 } 109 110 func TestNotarySnapshotRoundTrip(t *testing.T) { 111 notr := getTestNotary(t) 112 defer notr.ctrl.Finish() 113 114 notr.top.EXPECT().Len().AnyTimes().Return(1) 115 notr.top.EXPECT().IsValidatorVegaPubKey(gomock.Any()).AnyTimes().Return(true) 116 notr.top.EXPECT().IsTendermintValidator(gomock.Any()).AnyTimes().Return(true) 117 notr.top.EXPECT().IsValidator().AnyTimes().Return(true) 118 notr.top.EXPECT().SelfVegaPubKey().AnyTimes().Return("123456") 119 120 populateNotary(t, notr) 121 122 state, _, err := notr.GetState(allKey) 123 require.Nil(t, err) 124 125 snap := &snapshot.Payload{} 126 err = proto.Unmarshal(state, snap) 127 require.Nil(t, err) 128 129 snapNotr := getTestNotary(t) 130 defer snapNotr.ctrl.Finish() 131 snapNotr.top.EXPECT().Len().AnyTimes().Return(1) 132 snapNotr.top.EXPECT().IsValidator().AnyTimes().Return(true) 133 snapNotr.top.EXPECT().SelfVegaPubKey().AnyTimes().Return("123456") 134 snapNotr.top.EXPECT().IsValidatorVegaPubKey(gomock.Any()).AnyTimes().Return(true) 135 snapNotr.top.EXPECT().IsTendermintValidator(gomock.Any()).AnyTimes().Return(true) 136 137 _, err = snapNotr.LoadState(context.Background(), types.PayloadFromProto(snap)) 138 require.Nil(t, err) 139 140 s1, _, err := notr.GetState(allKey) 141 require.Nil(t, err) 142 s2, _, err := notr.GetState(allKey) 143 require.Nil(t, err) 144 require.True(t, bytes.Equal(s1, s2)) 145 146 // Check the loaded in (and original) node signatures exist and are perceived to be ok 147 _, ok1 := snapNotr.IsSigned(context.Background(), "resid1", types.NodeSignatureKindAssetNew) 148 _, ok2 := notr.IsSigned(context.Background(), "resid1", types.NodeSignatureKindAssetNew) 149 require.True(t, ok1) 150 require.True(t, ok2) 151 } 152 153 func TestRetryPendingOnly(t *testing.T) { 154 notr := getTestNotary(t) 155 defer notr.ctrl.Finish() 156 157 notr.top.EXPECT().Len().AnyTimes().Return(1) 158 notr.top.EXPECT().IsValidatorVegaPubKey(gomock.Any()).AnyTimes().Return(true) 159 notr.top.EXPECT().IsTendermintValidator(gomock.Any()).AnyTimes().Return(true) 160 notr.top.EXPECT().IsValidator().AnyTimes().Return(true) 161 notr.top.EXPECT().SelfVegaPubKey().AnyTimes().Return("123456") 162 163 // we will start this signature aggregation but not send in a signature 164 notr.StartAggregate( 165 "resid1", types.NodeSignatureKindERC20MultiSigSignerAdded, []byte("123444")) 166 167 // we will start another one send in signatures from a pretend other validator 168 resID := "resid2" 169 notr.StartAggregate(resID, types.NodeSignatureKindAssetNew, []byte("123456")) 170 key := "other-validator" 171 ns := commandspb.NodeSignature{ 172 Sig: []byte("123456"), 173 Id: resID, 174 Kind: types.NodeSignatureKindAssetNew, 175 } 176 err := notr.RegisterSignature(context.Background(), key, ns) 177 require.Nil(t, err) 178 179 // get the snapshot state and load into a new engine 180 state, _, err := notr.GetState(allKey) 181 require.Nil(t, err) 182 183 snap := &snapshot.Payload{} 184 err = proto.Unmarshal(state, snap) 185 require.Nil(t, err) 186 187 snapNotr := getTestNotary(t) 188 defer snapNotr.ctrl.Finish() 189 snapNotr.top.EXPECT().Len().AnyTimes().Return(1) 190 snapNotr.top.EXPECT().IsValidator().AnyTimes().Return(true) 191 snapNotr.top.EXPECT().SelfVegaPubKey().AnyTimes().Return("this-validator") 192 snapNotr.top.EXPECT().IsValidatorVegaPubKey(gomock.Any()).AnyTimes().Return(true) 193 snapNotr.top.EXPECT().IsTendermintValidator(gomock.Any()).AnyTimes().Return(true) 194 195 _, err = snapNotr.LoadState(context.Background(), types.PayloadFromProto(snap)) 196 require.Nil(t, err) 197 198 s1, _, err := notr.GetState(allKey) 199 require.Nil(t, err) 200 s2, _, err := notr.GetState(allKey) 201 require.Nil(t, err) 202 require.True(t, bytes.Equal(s1, s2)) 203 204 snapNotr.cmd.EXPECT().Command(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(1) 205 snapNotr.OnTick(context.Background(), time.Now()) 206 }