mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-05-21 14:26:45 +00:00

* if more candidates then max, choose half with highest blueWork and half with lowest * Add a Test GhostDAG sorter * Add a test for pick virtual parents * Fix review nits
44 lines
1.3 KiB
Go
44 lines
1.3 KiB
Go
package consensus
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/testapi"
|
|
"sort"
|
|
"testing"
|
|
)
|
|
|
|
type testGhostDAGSorter struct {
|
|
slice []*externalapi.DomainHash
|
|
tc testapi.TestConsensus
|
|
test testing.TB
|
|
}
|
|
|
|
// NewTestGhostDAGSorter returns a sort.Interface over the slice, so you can sort it via GhostDAG ordering
|
|
func NewTestGhostDAGSorter(slice []*externalapi.DomainHash, tc testapi.TestConsensus, t testing.TB) sort.Interface {
|
|
return testGhostDAGSorter{
|
|
slice: slice,
|
|
tc: tc,
|
|
test: t,
|
|
}
|
|
}
|
|
|
|
func (sorter testGhostDAGSorter) Len() int {
|
|
return len(sorter.slice)
|
|
}
|
|
|
|
func (sorter testGhostDAGSorter) Less(i, j int) bool {
|
|
ghostdagDataI, err := sorter.tc.GHOSTDAGDataStore().Get(sorter.tc.DatabaseContext(), sorter.slice[i])
|
|
if err != nil {
|
|
sorter.test.Fatalf("TestGhostDAGSorter: Failed getting ghostdag data for %s", err)
|
|
}
|
|
ghostdagDataJ, err := sorter.tc.GHOSTDAGDataStore().Get(sorter.tc.DatabaseContext(), sorter.slice[j])
|
|
if err != nil {
|
|
sorter.test.Fatalf("TestGhostDAGSorter: Failed getting ghostdag data for %s", err)
|
|
}
|
|
return !sorter.tc.GHOSTDAGManager().Less(sorter.slice[i], ghostdagDataI, sorter.slice[j], ghostdagDataJ)
|
|
}
|
|
|
|
func (sorter testGhostDAGSorter) Swap(i, j int) {
|
|
sorter.slice[i], sorter.slice[j] = sorter.slice[j], sorter.slice[i]
|
|
}
|