code.vegaprotocol.io/vega@v0.79.0/datanode/networkhistory/block_commit_handler_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 networkhistory_test 17 18 import ( 19 "context" 20 "errors" 21 "testing" 22 "time" 23 24 "code.vegaprotocol.io/vega/datanode/networkhistory" 25 "code.vegaprotocol.io/vega/logging" 26 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func TestRetries(t *testing.T) { 31 log := logging.NewTestLogger() 32 33 callCount := 0 34 snapshotData := func(ctx context.Context, chainID string, toHeight int64) error { 35 callCount++ 36 if callCount < 3 { 37 return errors.New("not yet ready") 38 } 39 40 return nil 41 } 42 43 commitHandler := networkhistory.NewBlockCommitHandler(log, networkhistory.NewDefaultConfig(), snapshotData, true, time.Duration(0), 1*time.Millisecond, 6) 44 45 commitHandler.OnBlockCommitted(context.Background(), "", 1000, true) 46 47 assert.Equal(t, 3, callCount) 48 } 49 50 func TestAlteringSnapshotIntervalBelowMinIntervalWithFileSource(t *testing.T) { 51 log := logging.NewTestLogger() 52 53 var snapshots []int64 54 55 snapshotData := func(ctx context.Context, chainID string, toHeight int64) error { 56 snapshots = append(snapshots, toHeight) 57 return nil 58 } 59 60 commitHandler := networkhistory.NewBlockCommitHandler(log, networkhistory.NewDefaultConfig(), snapshotData, true, time.Duration(0), 1, 1) 61 62 ctx := context.Background() 63 for blockHeight := int64(0); blockHeight < 6100; blockHeight++ { 64 snapshotTaken := blockHeight%1000 == 0 65 if blockHeight >= 5000 { 66 snapshotTaken = blockHeight%300 == 0 67 } 68 commitHandler.OnBlockCommitted(ctx, "", blockHeight, snapshotTaken) 69 } 70 71 assert.Equal(t, 6, len(snapshots)) 72 assert.Equal(t, int64(1000), snapshots[0]) 73 assert.Equal(t, int64(2000), snapshots[1]) 74 assert.Equal(t, int64(3000), snapshots[2]) 75 assert.Equal(t, int64(4000), snapshots[3]) 76 assert.Equal(t, int64(5000), snapshots[4]) 77 assert.Equal(t, int64(6000), snapshots[5]) 78 } 79 80 func TestAlteringBlockCommitHandlerSnapshotInterval(t *testing.T) { 81 log := logging.NewTestLogger() 82 83 var snapshots []int64 84 85 snapshotData := func(ctx context.Context, chainID string, toHeight int64) error { 86 snapshots = append(snapshots, toHeight) 87 return nil 88 } 89 commitHandler := networkhistory.NewBlockCommitHandler(log, networkhistory.NewDefaultConfig(), snapshotData, false, time.Duration(0), 90 1, 1) 91 ctx := context.Background() 92 93 for blockHeight := int64(0); blockHeight < 6100; blockHeight++ { 94 snapshotTaken := blockHeight%1000 == 0 95 if blockHeight >= 5000 { 96 snapshotTaken = blockHeight%500 == 0 97 } 98 99 commitHandler.OnBlockCommitted(ctx, "", blockHeight, snapshotTaken) 100 } 101 102 assert.Equal(t, 7, len(snapshots)) 103 assert.Equal(t, int64(1000), snapshots[0]) 104 assert.Equal(t, int64(2000), snapshots[1]) 105 assert.Equal(t, int64(3000), snapshots[2]) 106 assert.Equal(t, int64(4000), snapshots[3]) 107 assert.Equal(t, int64(5000), snapshots[4]) 108 assert.Equal(t, int64(5500), snapshots[5]) 109 assert.Equal(t, int64(6000), snapshots[6]) 110 } 111 112 func TestPublishingOff(t *testing.T) { 113 log := logging.NewTestLogger() 114 115 snapshotInterval := &struct { 116 interval int 117 }{ 118 interval: 1000, 119 } 120 121 var snapshots []int64 122 123 snapshotData := func(ctx context.Context, chainID string, toHeight int64) error { 124 if toHeight >= 5000 { 125 snapshotInterval.interval = 500 126 } 127 128 snapshots = append(snapshots, toHeight) 129 return nil 130 } 131 132 cfg := networkhistory.NewDefaultConfig() 133 cfg.Publish = false 134 commitHandler := networkhistory.NewBlockCommitHandler(log, cfg, snapshotData, false, 0, 1, 1) 135 136 ctx := context.Background() 137 for blockHeight := int64(0); blockHeight < 6100; blockHeight++ { 138 commitHandler.OnBlockCommitted(ctx, "", blockHeight, true) // show that regardless of what the core says about snapshot taken, none is taken here as publish is false. 139 } 140 141 assert.Equal(t, 0, len(snapshots)) 142 }