/
scheduler.go
163 lines (136 loc) · 4.31 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Package scheduler implements the tendermint backed scheduling backend.
package scheduler
import (
"bytes"
"context"
"fmt"
"github.com/eapache/channels"
tmabcitypes "github.com/tendermint/tendermint/abci/types"
tmpubsub "github.com/tendermint/tendermint/libs/pubsub"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-core/go/common/logging"
"github.com/oasisprotocol/oasis-core/go/common/pubsub"
consensus "github.com/oasisprotocol/oasis-core/go/consensus/api"
tmapi "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/api"
app "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/scheduler"
"github.com/oasisprotocol/oasis-core/go/scheduler/api"
)
// ServiceClient is the scheduler service client interface.
type ServiceClient interface {
api.Backend
tmapi.ServiceClient
}
type serviceClient struct {
tmapi.BaseServiceClient
logger *logging.Logger
querier *app.QueryFactory
notifier *pubsub.Broker
}
func (sc *serviceClient) StateToGenesis(ctx context.Context, height int64) (*api.Genesis, error) {
q, err := sc.querier.QueryAt(ctx, height)
if err != nil {
return nil, fmt.Errorf("scheduler: genesis query failed: %w", err)
}
return q.Genesis(ctx)
}
func (sc *serviceClient) Cleanup() {
}
func (sc *serviceClient) GetValidators(ctx context.Context, height int64) ([]*api.Validator, error) {
q, err := sc.querier.QueryAt(ctx, height)
if err != nil {
return nil, err
}
return q.Validators(ctx)
}
func (sc *serviceClient) GetCommittees(ctx context.Context, request *api.GetCommitteesRequest) ([]*api.Committee, error) {
q, err := sc.querier.QueryAt(ctx, request.Height)
if err != nil {
return nil, err
}
committees, err := q.AllCommittees(ctx)
if err != nil {
return nil, err
}
var runtimeCommittees []*api.Committee
for _, c := range committees {
if c.RuntimeID.Equal(&request.RuntimeID) {
runtimeCommittees = append(runtimeCommittees, c)
}
}
return runtimeCommittees, nil
}
func (sc *serviceClient) WatchCommittees(ctx context.Context) (<-chan *api.Committee, pubsub.ClosableSubscription, error) {
typedCh := make(chan *api.Committee)
sub := sc.notifier.Subscribe()
sub.Unwrap(typedCh)
return typedCh, sub, nil
}
func (sc *serviceClient) getCurrentCommittees() ([]*api.Committee, error) {
q, err := sc.querier.QueryAt(context.TODO(), consensus.HeightLatest)
if err != nil {
return nil, err
}
return q.AllCommittees(context.TODO())
}
// Implements api.ServiceClient.
func (sc *serviceClient) ServiceDescriptor() tmapi.ServiceDescriptor {
return tmapi.NewStaticServiceDescriptor(api.ModuleName, app.EventType, []tmpubsub.Query{app.QueryApp})
}
// Implements api.ServiceClient.
func (sc *serviceClient) DeliverEvent(ctx context.Context, height int64, tx tmtypes.Tx, ev *tmabcitypes.Event) error {
for _, pair := range ev.GetAttributes() {
if bytes.Equal(pair.GetKey(), app.KeyElected) {
var kinds []api.CommitteeKind
if err := cbor.Unmarshal(pair.GetValue(), &kinds); err != nil {
sc.logger.Error("worker: malformed elected committee types list",
"err", err,
)
continue
}
q, err := sc.querier.QueryAt(ctx, height)
if err != nil {
sc.logger.Error("worker: couldn't query elected committees",
"err", err,
)
continue
}
committees, err := q.KindsCommittees(ctx, kinds)
if err != nil {
sc.logger.Error("worker: couldn't query elected committees",
"err", err,
)
continue
}
for _, c := range committees {
sc.notifier.Broadcast(c)
}
}
}
return nil
}
// New constructs a new tendermint-based scheduler Backend instance.
func New(ctx context.Context, backend tmapi.Backend) (ServiceClient, error) {
// Initialze and register the tendermint service component.
a := app.New()
if err := backend.RegisterApplication(a); err != nil {
return nil, err
}
sc := &serviceClient{
logger: logging.GetLogger("scheduler/tendermint"),
querier: a.QueryFactory().(*app.QueryFactory),
}
sc.notifier = pubsub.NewBrokerEx(func(ch channels.Channel) {
currentCommittees, err := sc.getCurrentCommittees()
if err != nil {
sc.logger.Error("couldn't get current committees. won't send them. good luck to the subscriber",
"err", err,
)
return
}
for _, c := range currentCommittees {
ch.In() <- c
}
})
return sc, nil
}