/
scheduler.go
85 lines (74 loc) · 2.49 KB
/
scheduler.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package byzantine
import (
"context"
"fmt"
beacon "github.com/oasisprotocol/oasis-core/go/beacon/api"
"github.com/oasisprotocol/oasis-core/go/common"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
consensus "github.com/oasisprotocol/oasis-core/go/consensus/api"
"github.com/oasisprotocol/oasis-core/go/roothash/api/commitment"
scheduler "github.com/oasisprotocol/oasis-core/go/scheduler/api"
)
func schedulerNextElectionHeight(svc consensus.Backend, epoch beacon.EpochTime) (int64, beacon.EpochTime, error) {
ch, sub, err := svc.Scheduler().WatchCommittees(context.Background())
if err != nil {
return -1, beacon.EpochInvalid, fmt.Errorf("failed to watch committees: %w", err)
}
defer sub.Close()
for {
if committee := <-ch; committee.ValidFor >= epoch {
height, err := svc.Beacon().GetEpochBlock(context.Background(), committee.ValidFor)
if err != nil {
return -1, beacon.EpochInvalid, fmt.Errorf("failed to get epoch block: %w", err)
}
return height, committee.ValidFor, nil
}
}
}
func schedulerGetCommittee(ht *honestTendermint, height int64, kind scheduler.CommitteeKind, runtimeID common.Namespace) (*scheduler.Committee, error) {
committees, err := ht.service.Scheduler().GetCommittees(context.Background(), &scheduler.GetCommitteesRequest{
RuntimeID: runtimeID,
Height: height,
})
if err != nil {
return nil, fmt.Errorf("GetCommittees() error: %w", err)
}
for _, committee := range committees {
if committee.Kind != kind {
continue
}
if !committee.RuntimeID.Equal(&runtimeID) {
continue
}
return committee, nil
}
return nil, fmt.Errorf("query didn't return a committee for our runtime")
}
func schedulerCheckScheduled(committee *scheduler.Committee, nodeID signature.PublicKey, role scheduler.Role) error {
var roles []scheduler.Role
for _, member := range committee.Members {
if !member.PublicKey.Equal(nodeID) {
continue
}
if member.Role == role {
// All good.
return nil
}
roles = append(roles, role)
}
if len(roles) > 0 {
return fmt.Errorf("we're scheduled as %s, expected %s", fmt.Sprintf("%+v", roles), role)
}
if role == scheduler.RoleInvalid {
// It's expected that we're not scheduled.
return nil
}
return fmt.Errorf("we're not scheduled")
}
func schedulerCheckTxScheduler(committee *scheduler.Committee, nodeID signature.PublicKey, round uint64) bool {
scheduler, err := commitment.GetTransactionScheduler(committee, round)
if err != nil {
panic(err)
}
return scheduler.PublicKey.Equal(nodeID)
}