forked from kaspanet/kaspad
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_ghostdag.go
49 lines (41 loc) · 1.58 KB
/
test_ghostdag.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package testutils
import (
"sort"
"testing"
"github.com/Kash-Protocol/kashd/domain/consensus/model"
"github.com/Kash-Protocol/kashd/domain/consensus/model/externalapi"
"github.com/Kash-Protocol/kashd/domain/consensus/model/testapi"
)
type testGhostDAGSorter struct {
slice []*externalapi.DomainHash
tc testapi.TestConsensus
test testing.TB
stagingArea *model.StagingArea
}
// NewTestGhostDAGSorter returns a sort.Interface over the slice, so you can sort it via GhostDAG ordering
func NewTestGhostDAGSorter(stagingArea *model.StagingArea, slice []*externalapi.DomainHash, tc testapi.TestConsensus,
t testing.TB) sort.Interface {
return testGhostDAGSorter{
slice: slice,
tc: tc,
test: t,
stagingArea: stagingArea,
}
}
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.stagingArea, sorter.slice[i], false)
if err != nil {
sorter.test.Fatalf("TestGhostDAGSorter: Failed getting ghostdag data for %s", err)
}
ghostdagDataJ, err := sorter.tc.GHOSTDAGDataStore().Get(sorter.tc.DatabaseContext(), sorter.stagingArea, sorter.slice[j], false)
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]
}