Elichai Turkel 3d0a2a47b2
Move testGHOSTDagSorter to testutils, and build a boilerplate for overriding specific managers (#1486)
* Move testGHOSTDagSorter to testutils

* Allow overriding managers in consensus, starting with ghostdag

* Add test prefix to SetDataDir and SetGHOSTDAGManager

Co-authored-by: stasatdaglabs <39559713+stasatdaglabs@users.noreply.github.com>
2021-02-08 15:24:26 +02:00

44 lines
1.3 KiB
Go

package testutils
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]
}