-
Notifications
You must be signed in to change notification settings - Fork 211
/
util.go
46 lines (41 loc) · 1.41 KB
/
util.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
package miner
import (
"fmt"
"golang.org/x/exp/maps"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/activesets"
"github.com/spacemeshos/go-spacemesh/sql/ballots"
"github.com/spacemeshos/go-spacemesh/sql/blocks"
"github.com/spacemeshos/go-spacemesh/sql/layers"
)
func ActiveSetFromEpochFirstBlock(db sql.Executor, epoch types.EpochID) ([]types.ATXID, error) {
bid, err := layers.FirstAppliedInEpoch(db, epoch)
if err != nil {
return nil, err
}
return activeSetFromBlock(db, bid)
}
func activeSetFromBlock(db sql.Executor, bid types.BlockID) ([]types.ATXID, error) {
block, err := blocks.Get(db, bid)
if err != nil {
return nil, fmt.Errorf("actives get block: %w", err)
}
activeMap := make(map[types.ATXID]struct{})
// the active set is the union of all active sets recorded in rewarded miners' ref ballot
for _, r := range block.Rewards {
activeMap[r.AtxID] = struct{}{}
ballot, err := ballots.FirstInEpoch(db, r.AtxID, block.LayerIndex.GetEpoch())
if err != nil {
return nil, fmt.Errorf("actives get ballot: %w", err)
}
actives, err := activesets.Get(db, ballot.EpochData.ActiveSetHash)
if err != nil {
return nil, fmt.Errorf("actives get active hash for ballot %s: %w", ballot.ID().String(), err)
}
for _, id := range actives.Set {
activeMap[id] = struct{}{}
}
}
return maps.Keys(activeMap), nil
}