add CappedList for ids.Set and use it in fetchANeededVtx

This commit is contained in:
Dan Laine 2020-06-23 15:01:55 -04:00
parent 67d9281501
commit 55079aa893
3 changed files with 66 additions and 2 deletions

View File

@ -78,7 +78,7 @@ func (ids *Set) Clear() { *ids = nil }
// List converts this set into a list
func (ids Set) List() []ID {
idList := make([]ID, ids.Len(), ids.Len())
idList := make([]ID, ids.Len())
i := 0
for id := range ids {
idList[i] = NewID(id)
@ -87,6 +87,27 @@ func (ids Set) List() []ID {
return idList
}
// CappedList returns a list of length at most [size].
// Size should be >= 0. If size < 0, returns nil.
func (ids Set) CappedList(size int) []ID {
if size < 0 {
return nil
}
if l := ids.Len(); l < size {
size = l
}
i := 0
idList := make([]ID, size)
for id := range ids {
if i >= size {
break
}
idList[i] = NewID(id)
i++
}
return idList
}
// Equals returns true if the sets contain the same elements
func (ids Set) Equals(oIDs Set) bool {
if ids.Len() != oIDs.Len() {

View File

@ -55,3 +55,46 @@ func TestSet(t *testing.T) {
t.Fatalf("Sets overlap")
}
}
func TestSetCappedList(t *testing.T) {
set := Set{}
id := Empty
if list := set.CappedList(0); len(list) != 0 {
t.Fatalf("List should have been empty but was %v", list)
}
set.Add(id)
if list := set.CappedList(0); len(list) != 0 {
t.Fatalf("List should have been empty but was %v", list)
} else if list := set.CappedList(1); len(list) != 1 {
t.Fatalf("List should have had length %d but had %d", 1, len(list))
} else if returnedID := list[0]; !id.Equals(returnedID) {
t.Fatalf("List should have been %s but was %s", id, returnedID)
} else if list := set.CappedList(2); len(list) != 1 {
t.Fatalf("List should have had length %d but had %d", 1, len(list))
} else if returnedID := list[0]; !id.Equals(returnedID) {
t.Fatalf("List should have been %s but was %s", id, returnedID)
}
id2 := NewID([32]byte{1})
set.Add(id2)
if list := set.CappedList(0); len(list) != 0 {
t.Fatalf("List should have been empty but was %v", list)
} else if list := set.CappedList(1); len(list) != 1 {
t.Fatalf("List should have had length %d but had %d", 1, len(list))
} else if returnedID := list[0]; !id.Equals(returnedID) && !id2.Equals(returnedID) {
t.Fatalf("List should have been %s but was %s", id, returnedID)
} else if list := set.CappedList(2); len(list) != 2 {
t.Fatalf("List should have had length %d but had %d", 2, len(list))
} else if list := set.CappedList(3); len(list) != 2 {
t.Fatalf("List should have had length %d but had %d", 2, len(list))
} else if returnedID := list[0]; !id.Equals(returnedID) && !id2.Equals(returnedID) {
t.Fatalf("list contains unexpected element %s", returnedID)
} else if returnedID := list[1]; !id.Equals(returnedID) && !id2.Equals(returnedID) {
t.Fatalf("list contains unexpected element %s", returnedID)
}
}

View File

@ -111,7 +111,7 @@ func (b *bootstrapper) FilterAccepted(containerIDs ids.Set) ids.Set {
// Calls fetch for a pending vertex if there are any
func (b *bootstrapper) fetchANeededVtx() error {
if b.needToFetch.Len() > 0 {
return b.fetch(b.needToFetch.List()[0])
return b.fetch(b.needToFetch.CappedList(1)[0])
}
return nil
}