Skip to content

Commit d5537de

Browse files
add guard to avoid underflow and deleting needed nodesConfig
1 parent 4247666 commit d5537de

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

sharding/nodesCoordinator/indexHashedNodesCoordinatorLite.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,15 @@ func (ihnc *indexHashedNodesCoordinator) IsEpochInConfig(epoch uint32) bool {
6767
}
6868

6969
func (ihnc *indexHashedNodesCoordinator) removeOlderEpochs(epoch uint32, maxDelta uint32) {
70+
if epoch < maxDelta {
71+
return
72+
}
73+
epochToRemove := epoch - maxDelta
74+
7075
ihnc.mutNodesConfig.Lock()
7176
if len(ihnc.nodesConfig) >= int(maxDelta) {
7277
for currEpoch := range ihnc.nodesConfig {
73-
if currEpoch <= epoch-maxDelta {
78+
if currEpoch <= epochToRemove {
7479
delete(ihnc.nodesConfig, currEpoch)
7580
}
7681
}

sharding/nodesCoordinator/indexHashedNodesCoordinatorLite_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,46 @@ func TestIndexHashedNodesCoordinator_IsEpochInConfig(t *testing.T) {
187187
exists = ihnc.IsEpochInConfig(epoch + 1)
188188
assert.False(t, exists)
189189
}
190+
191+
func TestIndexHashedNodesCoordinator_RemoveOlderEpochsShouldNotRemoveWhenEpochIsLessThanMaxDelta(t *testing.T) {
192+
t.Parallel()
193+
194+
arguments := createArguments()
195+
ihnc, err := NewIndexHashedNodesCoordinator(arguments)
196+
require.Nil(t, err)
197+
198+
ihnc.nodesConfig[1] = &epochNodesConfig{}
199+
ihnc.nodesConfig[2] = &epochNodesConfig{}
200+
ihnc.nodesConfig[3] = &epochNodesConfig{}
201+
202+
ihnc.removeOlderEpochs(3, nodesCoordinatorStoredEpochs)
203+
204+
require.Len(t, ihnc.nodesConfig, 4)
205+
require.Contains(t, ihnc.nodesConfig, uint32(0))
206+
require.Contains(t, ihnc.nodesConfig, uint32(1))
207+
require.Contains(t, ihnc.nodesConfig, uint32(2))
208+
require.Contains(t, ihnc.nodesConfig, uint32(3))
209+
}
210+
211+
func TestIndexHashedNodesCoordinator_RemoveOlderEpochsShouldRemoveOnlyOlderEpochs(t *testing.T) {
212+
t.Parallel()
213+
214+
arguments := createArguments()
215+
ihnc, err := NewIndexHashedNodesCoordinator(arguments)
216+
require.Nil(t, err)
217+
218+
ihnc.nodesConfig[0] = &epochNodesConfig{}
219+
ihnc.nodesConfig[1] = &epochNodesConfig{}
220+
ihnc.nodesConfig[2] = &epochNodesConfig{}
221+
ihnc.nodesConfig[3] = &epochNodesConfig{}
222+
ihnc.nodesConfig[4] = &epochNodesConfig{}
223+
224+
ihnc.removeOlderEpochs(4, nodesCoordinatorStoredEpochs)
225+
226+
require.Len(t, ihnc.nodesConfig, 4)
227+
require.NotContains(t, ihnc.nodesConfig, uint32(0))
228+
require.Contains(t, ihnc.nodesConfig, uint32(1))
229+
require.Contains(t, ihnc.nodesConfig, uint32(2))
230+
require.Contains(t, ihnc.nodesConfig, uint32(3))
231+
require.Contains(t, ihnc.nodesConfig, uint32(4))
232+
}

0 commit comments

Comments
 (0)