code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/key_rotations_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 sqlstore_test 17 18 import ( 19 "context" 20 "fmt" 21 "testing" 22 "time" 23 24 "code.vegaprotocol.io/vega/datanode/entities" 25 "code.vegaprotocol.io/vega/datanode/sqlstore" 26 27 "github.com/stretchr/testify/assert" 28 "github.com/stretchr/testify/require" 29 ) 30 31 func TestKeyRotationsGetByTx(t *testing.T) { 32 ctx := tempTransaction(t) 33 34 ks, keyRotations := setKeyRotationStoreTest(t, ctx) 35 36 got, err := ks.GetByTxHash(ctx, keyRotations[0].TxHash) 37 require.NoError(t, err) 38 want := []entities.KeyRotation{keyRotations[0]} 39 assert.Equal(t, want, got) 40 41 got, err = ks.GetByTxHash(ctx, keyRotations[1].TxHash) 42 require.NoError(t, err) 43 want = []entities.KeyRotation{keyRotations[1]} 44 assert.Equal(t, want, got) 45 } 46 47 func TestKeyRotationsCursorPagination(t *testing.T) { 48 t.Run("should return all key rotations if no pagination is specified", testKeyRotationPaginationNoPagination) 49 t.Run("should return first page of key rotations if first is provided", testKeyRotationPaginationFirstPage) 50 t.Run("should return last page of key rotations if last is provided", testKeyRotationPaginationLastPage) 51 t.Run("should return specified page of key rotations if first and after is provided", testKeyRotationPaginationFirstAndAfter) 52 t.Run("should return specified page of key rotations if last and before is provided", testKeyRotationPaginationLastAndBefore) 53 54 t.Run("should return all key rotations if no pagination is specified - newest first", testKeyRotationPaginationNoPaginationNewestFirst) 55 t.Run("should return first page of key rotations if first is provided - newest first", testKeyRotationPaginationFirstPageNewestFirst) 56 t.Run("should return last page of key rotations if last is provided - newest first", testKeyRotationPaginationLastPageNewestFirst) 57 t.Run("should return specified page of key rotations if first and after is provided - newest first", testKeyRotationPaginationFirstAndAfterNewestFirst) 58 t.Run("should return specified page of key rotations if last and before is provided - newest first", testKeyRotationPaginationLastAndBeforeNewestFirst) 59 60 t.Run("should return all key rotations for specific node if no pagination is specified", testKeyRotationPaginationForNodeNoPagination) 61 t.Run("should return first page of key rotations for specific node if first is provided", testKeyRotationPaginationForNodeFirstPage) 62 t.Run("should return last page of key rotations for specific node if last is provided", testKeyRotationPaginationForNodeLastPage) 63 t.Run("should return specified page of key rotations for specific node if first and after is provided", testKeyRotationPaginationForNodeFirstAndAfter) 64 t.Run("should return specified page of key rotations for specific node if last and before is provided", testKeyRotationPaginationForNodeLastAndBefore) 65 66 t.Run("should return all key rotations for specific node if no pagination is specified - newest first", testKeyRotationPaginationForNodeNoPaginationNewestFirst) 67 t.Run("should return first page of key rotations for specific node if first is provided - newest first", testKeyRotationPaginationForNodeFirstPageNewestFirst) 68 t.Run("should return last page of key rotations for specific node if last is provided - newest first", testKeyRotationPaginationForNodeLastPageNewestFirst) 69 t.Run("should return specified page of key rotations for specific node if first and after is provided - newest first", testKeyRotationPaginationForNodeFirstAndAfterNewestFirst) 70 t.Run("should return specified page of key rotations for specific node if last and before is provided - newest first", testKeyRotationPaginationForNodeLastAndBeforeNewestFirst) 71 } 72 73 func testKeyRotationPaginationNoPagination(t *testing.T) { 74 ctx := tempTransaction(t) 75 76 ks, keys := setKeyRotationStoreTest(t, ctx) 77 pagination, err := entities.NewCursorPagination(nil, nil, nil, nil, false) 78 require.NoError(t, err) 79 80 got, pageInfo, err := ks.GetAllPubKeyRotations(ctx, pagination) 81 require.NoError(t, err) 82 assert.Len(t, got, 20) 83 want := keys 84 assert.Equal(t, want, got) 85 assert.Equal(t, entities.PageInfo{ 86 HasNextPage: false, 87 HasPreviousPage: false, 88 StartCursor: want[0].Cursor().Encode(), 89 EndCursor: want[19].Cursor().Encode(), 90 }, pageInfo) 91 } 92 93 func testKeyRotationPaginationFirstPage(t *testing.T) { 94 ctx := tempTransaction(t) 95 96 ks, keys := setKeyRotationStoreTest(t, ctx) 97 first := int32(3) 98 pagination, err := entities.NewCursorPagination(&first, nil, nil, nil, false) 99 require.NoError(t, err) 100 101 got, pageInfo, err := ks.GetAllPubKeyRotations(ctx, pagination) 102 require.NoError(t, err) 103 assert.Len(t, got, 3) 104 want := keys[:3] 105 assert.Equal(t, want, got) 106 assert.Equal(t, entities.PageInfo{ 107 HasNextPage: true, 108 HasPreviousPage: false, 109 StartCursor: want[0].Cursor().Encode(), 110 EndCursor: want[2].Cursor().Encode(), 111 }, pageInfo) 112 } 113 114 func testKeyRotationPaginationLastPage(t *testing.T) { 115 ctx := tempTransaction(t) 116 117 ks, keys := setKeyRotationStoreTest(t, ctx) 118 last := int32(3) 119 pagination, err := entities.NewCursorPagination(nil, nil, &last, nil, false) 120 require.NoError(t, err) 121 122 got, pageInfo, err := ks.GetAllPubKeyRotations(ctx, pagination) 123 require.NoError(t, err) 124 assert.Len(t, got, 3) 125 want := keys[17:] 126 assert.Equal(t, want, got) 127 assert.Equal(t, entities.PageInfo{ 128 HasNextPage: false, 129 HasPreviousPage: true, 130 StartCursor: want[0].Cursor().Encode(), 131 EndCursor: want[2].Cursor().Encode(), 132 }, pageInfo) 133 } 134 135 func testKeyRotationPaginationFirstAndAfter(t *testing.T) { 136 ctx := tempTransaction(t) 137 138 ks, keys := setKeyRotationStoreTest(t, ctx) 139 first := int32(3) 140 after := keys[2].Cursor().Encode() 141 pagination, err := entities.NewCursorPagination(&first, &after, nil, nil, false) 142 require.NoError(t, err) 143 144 got, pageInfo, err := ks.GetAllPubKeyRotations(ctx, pagination) 145 require.NoError(t, err) 146 assert.Len(t, got, 3) 147 want := keys[3:6] 148 assert.Equal(t, want, got) 149 assert.Equal(t, entities.PageInfo{ 150 HasNextPage: true, 151 HasPreviousPage: true, 152 StartCursor: want[0].Cursor().Encode(), 153 EndCursor: want[2].Cursor().Encode(), 154 }, pageInfo) 155 } 156 157 func testKeyRotationPaginationLastAndBefore(t *testing.T) { 158 ctx := tempTransaction(t) 159 160 ks, keys := setKeyRotationStoreTest(t, ctx) 161 last := int32(3) 162 before := keys[17].Cursor().Encode() 163 164 pagination, err := entities.NewCursorPagination(nil, nil, &last, &before, false) 165 require.NoError(t, err) 166 167 got, pageInfo, err := ks.GetAllPubKeyRotations(ctx, pagination) 168 require.NoError(t, err) 169 assert.Len(t, got, 3) 170 want := keys[14:17] 171 assert.Equal(t, want, got) 172 assert.Equal(t, entities.PageInfo{ 173 HasNextPage: true, 174 HasPreviousPage: true, 175 StartCursor: want[0].Cursor().Encode(), 176 EndCursor: want[2].Cursor().Encode(), 177 }, pageInfo) 178 } 179 180 func testKeyRotationPaginationNoPaginationNewestFirst(t *testing.T) { 181 ctx := tempTransaction(t) 182 183 ks, keys := setKeyRotationStoreTest(t, ctx) 184 pagination, err := entities.NewCursorPagination(nil, nil, nil, nil, true) 185 require.NoError(t, err) 186 187 got, pageInfo, err := ks.GetAllPubKeyRotations(ctx, pagination) 188 require.NoError(t, err) 189 assert.Len(t, got, 20) 190 want := entities.ReverseSlice(keys) 191 assert.Equal(t, want, got) 192 assert.Equal(t, entities.PageInfo{ 193 HasNextPage: false, 194 HasPreviousPage: false, 195 StartCursor: want[0].Cursor().Encode(), 196 EndCursor: want[19].Cursor().Encode(), 197 }, pageInfo) 198 } 199 200 func testKeyRotationPaginationFirstPageNewestFirst(t *testing.T) { 201 ctx := tempTransaction(t) 202 203 ks, keys := setKeyRotationStoreTest(t, ctx) 204 first := int32(3) 205 pagination, err := entities.NewCursorPagination(&first, nil, nil, nil, true) 206 require.NoError(t, err) 207 208 got, pageInfo, err := ks.GetAllPubKeyRotations(ctx, pagination) 209 require.NoError(t, err) 210 assert.Len(t, got, 3) 211 want := entities.ReverseSlice(keys)[:3] 212 assert.Equal(t, want, got) 213 assert.Equal(t, entities.PageInfo{ 214 HasNextPage: true, 215 HasPreviousPage: false, 216 StartCursor: want[0].Cursor().Encode(), 217 EndCursor: want[2].Cursor().Encode(), 218 }, pageInfo) 219 } 220 221 func testKeyRotationPaginationLastPageNewestFirst(t *testing.T) { 222 ctx := tempTransaction(t) 223 224 ks, keys := setKeyRotationStoreTest(t, ctx) 225 last := int32(3) 226 pagination, err := entities.NewCursorPagination(nil, nil, &last, nil, true) 227 require.NoError(t, err) 228 229 got, pageInfo, err := ks.GetAllPubKeyRotations(ctx, pagination) 230 require.NoError(t, err) 231 assert.Len(t, got, 3) 232 want := entities.ReverseSlice(keys)[17:] 233 assert.Equal(t, want, got) 234 assert.Equal(t, entities.PageInfo{ 235 HasNextPage: false, 236 HasPreviousPage: true, 237 StartCursor: want[0].Cursor().Encode(), 238 EndCursor: want[2].Cursor().Encode(), 239 }, pageInfo) 240 } 241 242 func testKeyRotationPaginationFirstAndAfterNewestFirst(t *testing.T) { 243 ctx := tempTransaction(t) 244 245 ks, keys := setKeyRotationStoreTest(t, ctx) 246 first := int32(3) 247 after := keys[17].Cursor().Encode() 248 pagination, err := entities.NewCursorPagination(&first, &after, nil, nil, true) 249 require.NoError(t, err) 250 251 got, pageInfo, err := ks.GetAllPubKeyRotations(ctx, pagination) 252 require.NoError(t, err) 253 assert.Len(t, got, 3) 254 want := entities.ReverseSlice(keys)[3:6] 255 assert.Equal(t, want, got) 256 assert.Equal(t, entities.PageInfo{ 257 HasNextPage: true, 258 HasPreviousPage: true, 259 StartCursor: want[0].Cursor().Encode(), 260 EndCursor: want[2].Cursor().Encode(), 261 }, pageInfo) 262 } 263 264 func testKeyRotationPaginationLastAndBeforeNewestFirst(t *testing.T) { 265 ctx := tempTransaction(t) 266 267 ks, keys := setKeyRotationStoreTest(t, ctx) 268 last := int32(3) 269 before := keys[2].Cursor().Encode() 270 pagination, err := entities.NewCursorPagination(nil, nil, &last, &before, true) 271 require.NoError(t, err) 272 273 got, pageInfo, err := ks.GetAllPubKeyRotations(ctx, pagination) 274 require.NoError(t, err) 275 assert.Len(t, got, 3) 276 want := entities.ReverseSlice(keys)[14:17] 277 assert.Equal(t, want, got) 278 assert.Equal(t, entities.PageInfo{ 279 HasNextPage: true, 280 HasPreviousPage: true, 281 StartCursor: want[0].Cursor().Encode(), 282 EndCursor: want[2].Cursor().Encode(), 283 }, pageInfo) 284 } 285 286 func testKeyRotationPaginationForNodeNoPagination(t *testing.T) { 287 ctx := tempTransaction(t) 288 289 ks, keys := setKeyRotationStoreTest(t, ctx) 290 pagination, err := entities.NewCursorPagination(nil, nil, nil, nil, false) 291 require.NoError(t, err) 292 293 got, pageInfo, err := ks.GetPubKeyRotationsPerNode(ctx, "deadbeef01", pagination) 294 require.NoError(t, err) 295 assert.Len(t, got, 10) 296 want := keys[:10] 297 assert.Equal(t, want, got) 298 assert.Equal(t, entities.PageInfo{ 299 HasNextPage: false, 300 HasPreviousPage: false, 301 StartCursor: want[0].Cursor().Encode(), 302 EndCursor: want[9].Cursor().Encode(), 303 }, pageInfo) 304 } 305 306 func testKeyRotationPaginationForNodeFirstPage(t *testing.T) { 307 ctx := tempTransaction(t) 308 309 ks, keys := setKeyRotationStoreTest(t, ctx) 310 first := int32(3) 311 pagination, err := entities.NewCursorPagination(&first, nil, nil, nil, false) 312 require.NoError(t, err) 313 314 got, pageInfo, err := ks.GetPubKeyRotationsPerNode(ctx, "deadbeef01", pagination) 315 require.NoError(t, err) 316 assert.Len(t, got, 3) 317 want := keys[:3] 318 assert.Equal(t, want, got) 319 assert.Equal(t, entities.PageInfo{ 320 HasNextPage: true, 321 HasPreviousPage: false, 322 StartCursor: want[0].Cursor().Encode(), 323 EndCursor: want[2].Cursor().Encode(), 324 }, pageInfo) 325 } 326 327 func testKeyRotationPaginationForNodeLastPage(t *testing.T) { 328 ctx := tempTransaction(t) 329 330 ks, keys := setKeyRotationStoreTest(t, ctx) 331 last := int32(3) 332 pagination, err := entities.NewCursorPagination(nil, nil, &last, nil, false) 333 require.NoError(t, err) 334 335 got, pageInfo, err := ks.GetPubKeyRotationsPerNode(ctx, "deadbeef01", pagination) 336 require.NoError(t, err) 337 assert.Len(t, got, 3) 338 want := keys[7:10] 339 assert.Equal(t, want, got) 340 assert.Equal(t, entities.PageInfo{ 341 HasNextPage: false, 342 HasPreviousPage: true, 343 StartCursor: want[0].Cursor().Encode(), 344 EndCursor: want[2].Cursor().Encode(), 345 }, pageInfo) 346 } 347 348 func testKeyRotationPaginationForNodeFirstAndAfter(t *testing.T) { 349 ctx := tempTransaction(t) 350 351 ks, keys := setKeyRotationStoreTest(t, ctx) 352 first := int32(3) 353 after := keys[2].Cursor().Encode() 354 pagination, err := entities.NewCursorPagination(&first, &after, nil, nil, false) 355 require.NoError(t, err) 356 357 got, pageInfo, err := ks.GetPubKeyRotationsPerNode(ctx, "deadbeef01", pagination) 358 require.NoError(t, err) 359 assert.Len(t, got, 3) 360 want := keys[3:6] 361 assert.Equal(t, want, got) 362 assert.Equal(t, entities.PageInfo{ 363 HasNextPage: true, 364 HasPreviousPage: true, 365 StartCursor: want[0].Cursor().Encode(), 366 EndCursor: want[2].Cursor().Encode(), 367 }, pageInfo) 368 } 369 370 func testKeyRotationPaginationForNodeLastAndBefore(t *testing.T) { 371 ctx := tempTransaction(t) 372 373 ks, keys := setKeyRotationStoreTest(t, ctx) 374 last := int32(3) 375 before := keys[7].Cursor().Encode() 376 pagination, err := entities.NewCursorPagination(nil, nil, &last, &before, false) 377 require.NoError(t, err) 378 379 got, pageInfo, err := ks.GetPubKeyRotationsPerNode(ctx, "deadbeef01", pagination) 380 require.NoError(t, err) 381 assert.Len(t, got, 3) 382 want := keys[4:7] 383 assert.Equal(t, want, got) 384 assert.Equal(t, entities.PageInfo{ 385 HasNextPage: true, 386 HasPreviousPage: true, 387 StartCursor: want[0].Cursor().Encode(), 388 EndCursor: want[2].Cursor().Encode(), 389 }, pageInfo) 390 } 391 392 func testKeyRotationPaginationForNodeNoPaginationNewestFirst(t *testing.T) { 393 ctx := tempTransaction(t) 394 395 ks, keys := setKeyRotationStoreTest(t, ctx) 396 pagination, err := entities.NewCursorPagination(nil, nil, nil, nil, true) 397 require.NoError(t, err) 398 399 got, pageInfo, err := ks.GetPubKeyRotationsPerNode(ctx, "deadbeef01", pagination) 400 require.NoError(t, err) 401 assert.Len(t, got, 10) 402 want := entities.ReverseSlice(keys[0:10]) 403 assert.Equal(t, want, got) 404 assert.Equal(t, entities.PageInfo{ 405 HasNextPage: false, 406 HasPreviousPage: false, 407 StartCursor: want[0].Cursor().Encode(), 408 EndCursor: want[9].Cursor().Encode(), 409 }, pageInfo) 410 } 411 412 func testKeyRotationPaginationForNodeFirstPageNewestFirst(t *testing.T) { 413 ctx := tempTransaction(t) 414 415 ks, keys := setKeyRotationStoreTest(t, ctx) 416 first := int32(3) 417 pagination, err := entities.NewCursorPagination(&first, nil, nil, nil, true) 418 require.NoError(t, err) 419 420 got, pageInfo, err := ks.GetPubKeyRotationsPerNode(ctx, "deadbeef01", pagination) 421 require.NoError(t, err) 422 assert.Len(t, got, 3) 423 want := entities.ReverseSlice(keys[0:10])[:3] 424 assert.Equal(t, want, got) 425 assert.Equal(t, entities.PageInfo{ 426 HasNextPage: true, 427 HasPreviousPage: false, 428 StartCursor: want[0].Cursor().Encode(), 429 EndCursor: want[2].Cursor().Encode(), 430 }, pageInfo) 431 } 432 433 func testKeyRotationPaginationForNodeLastPageNewestFirst(t *testing.T) { 434 ctx := tempTransaction(t) 435 436 ks, keys := setKeyRotationStoreTest(t, ctx) 437 last := int32(3) 438 pagination, err := entities.NewCursorPagination(nil, nil, &last, nil, true) 439 require.NoError(t, err) 440 441 got, pageInfo, err := ks.GetPubKeyRotationsPerNode(ctx, "deadbeef01", pagination) 442 require.NoError(t, err) 443 assert.Len(t, got, 3) 444 want := entities.ReverseSlice(keys[0:10])[7:] 445 assert.Equal(t, want, got) 446 assert.Equal(t, entities.PageInfo{ 447 HasNextPage: false, 448 HasPreviousPage: true, 449 StartCursor: want[0].Cursor().Encode(), 450 EndCursor: want[2].Cursor().Encode(), 451 }, pageInfo) 452 } 453 454 func testKeyRotationPaginationForNodeFirstAndAfterNewestFirst(t *testing.T) { 455 ctx := tempTransaction(t) 456 457 ks, keys := setKeyRotationStoreTest(t, ctx) 458 first := int32(3) 459 after := keys[7].Cursor().Encode() 460 pagination, err := entities.NewCursorPagination(&first, &after, nil, nil, true) 461 require.NoError(t, err) 462 463 got, pageInfo, err := ks.GetPubKeyRotationsPerNode(ctx, "deadbeef01", pagination) 464 require.NoError(t, err) 465 assert.Len(t, got, 3) 466 want := entities.ReverseSlice(keys[0:10])[3:6] 467 assert.Equal(t, want, got) 468 assert.Equal(t, entities.PageInfo{ 469 HasNextPage: true, 470 HasPreviousPage: true, 471 StartCursor: want[0].Cursor().Encode(), 472 EndCursor: want[2].Cursor().Encode(), 473 }, pageInfo) 474 } 475 476 func testKeyRotationPaginationForNodeLastAndBeforeNewestFirst(t *testing.T) { 477 ctx := tempTransaction(t) 478 479 ks, keys := setKeyRotationStoreTest(t, ctx) 480 last := int32(3) 481 before := keys[2].Cursor().Encode() 482 pagination, err := entities.NewCursorPagination(nil, nil, &last, &before, true) 483 require.NoError(t, err) 484 485 got, pageInfo, err := ks.GetPubKeyRotationsPerNode(ctx, "deadbeef01", pagination) 486 require.NoError(t, err) 487 assert.Len(t, got, 3) 488 want := entities.ReverseSlice(keys[0:10])[4:7] 489 assert.Equal(t, want, got) 490 assert.Equal(t, entities.PageInfo{ 491 HasNextPage: true, 492 HasPreviousPage: true, 493 StartCursor: want[0].Cursor().Encode(), 494 EndCursor: want[2].Cursor().Encode(), 495 }, pageInfo) 496 } 497 498 func setKeyRotationStoreTest(t *testing.T, ctx context.Context) (*sqlstore.KeyRotations, []entities.KeyRotation) { 499 t.Helper() 500 bs := sqlstore.NewBlocks(connectionSource) 501 ns := sqlstore.NewNode(connectionSource) 502 ks := sqlstore.NewKeyRotations(connectionSource) 503 504 keyRotations := make([]entities.KeyRotation, 20) 505 blockTime := time.Date(2022, 8, 2, 9, 0, 0, 0, time.Local) 506 block := addTestBlockForTime(t, ctx, bs, blockTime) 507 508 addTestNode(t, ctx, ns, block, "deadbeef01") 509 addTestNode(t, ctx, ns, block, "deadbeef02") 510 511 for i := 0; i < 2; i++ { 512 for j := 0; j < 10; j++ { 513 blockTime = blockTime.Add(time.Minute) 514 block := addTestBlockForTime(t, ctx, bs, blockTime) 515 516 kr := entities.KeyRotation{ 517 NodeID: entities.NodeID(fmt.Sprintf("deadbeef%02d", i+1)), 518 OldPubKey: entities.VegaPublicKey(fmt.Sprintf("cafed00d%02d", j+1)), 519 NewPubKey: entities.VegaPublicKey(fmt.Sprintf("cafed00d%02d", j+2)), 520 BlockHeight: uint64((i * 10) + j + 1), 521 VegaTime: block.VegaTime, 522 TxHash: generateTxHash(), 523 } 524 if err := ks.Upsert(ctx, &kr); err != nil { 525 t.Fatalf("creating key rotation test data: %v", err) 526 } 527 528 keyRotations[(i*10 + j)] = kr 529 } 530 } 531 532 return ks, keyRotations 533 }