-
Notifications
You must be signed in to change notification settings - Fork 211
/
hare.go
45 lines (37 loc) · 1.24 KB
/
hare.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
// Package turbohare is a component returning block ids for layer as seen by this miner, without running any consensus process
package turbohare
import (
"bytes"
"context"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/log"
"sort"
)
type blockProvider interface {
LayerBlockIds(layerID types.LayerID) ([]types.BlockID, error)
}
// SuperHare is a method to provide fast hare results without consensus based on received blocks from gossip
type SuperHare struct {
blocks blockProvider
}
// New creates a new instance of SuperHare
func New(ctx context.Context, blocks blockProvider) *SuperHare {
return &SuperHare{blocks}
}
// Start is a stub to support service API
func (h *SuperHare) Start(ctx context.Context) error {
return nil
}
// Close is a stup to support service API
func (h *SuperHare) Close() {
}
// GetResult is the implementation for receiving consensus process result
func (h *SuperHare) GetResult(id types.LayerID) ([]types.BlockID, error) {
blks, err := h.blocks.LayerBlockIds(id)
if err != nil {
log.Error("WTF SUPERHARE?? %v err: %v", id, err)
return nil, err
}
sort.Slice(blks, func(i, j int) bool { return bytes.Compare(blks[i].Bytes(), blks[j].Bytes()) == -1 })
return blks, nil
}