Skip to content

Commit

Permalink
sort clients on add
Browse files Browse the repository at this point in the history
  • Loading branch information
sideninja committed May 24, 2024
1 parent 2e7dea4 commit 05245f2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
21 changes: 7 additions & 14 deletions services/requester/cross-spork_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ func (s *sporkClients) add(client access.Client) error {
client: client,
})

// make sure clients are always sorted
slices.SortFunc(*s, func(a, b *sporkClient) int {
return int(a.firstHeight) - int(b.firstHeight)
})

return nil
}

Expand All @@ -62,20 +67,8 @@ func (s *sporkClients) get(height uint64) access.Client {
// continuous checks if all the past spork clients create a continuous
// range of heights.
func (s *sporkClients) continuous() bool {
firsts := make([]uint64, len(*s))
lasts := make([]uint64, len(*s))

for i, c := range *s {
firsts[i] = c.firstHeight
lasts[i] = c.lastHeight
}

slices.Sort(firsts)
slices.Sort(lasts)

// make sure each last height is one smaller than next range first height
for i := 0; i < len(lasts)-1; i++ {
if lasts[i]+1 != firsts[i+1] {
for i := 0; i < len(*s)-1; i++ {
if (*s)[i].lastHeight+1 != (*s)[i+1].firstHeight {
return false
}
}
Expand Down
4 changes: 3 additions & 1 deletion services/requester/cross-spork_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ func Test_CrossSporkClients(t *testing.T) {

client1 := testutils.SetupClientForRange(10, 100)
client2 := testutils.SetupClientForRange(101, 200)
client3 := testutils.SetupClientForRange(201, 300)

require.NoError(t, clients.add(client2))
require.NoError(t, clients.add(client3))
require.NoError(t, clients.add(client1))

require.True(t, clients.continuous())
Expand All @@ -51,7 +53,7 @@ func Test_CrossSporkClients(t *testing.T) {
require.Equal(t, client2, clients.get(200))

require.Equal(t, nil, clients.get(5))
require.Equal(t, nil, clients.get(300))
require.Equal(t, nil, clients.get(310))
})

t.Run("add and validate not-continues", func(t *testing.T) {
Expand Down

0 comments on commit 05245f2

Please sign in to comment.