Skip to content

Commit 41b5a04

Browse files
committed
go/consensus/cometbft: Create service descriptor once
1 parent 9621e09 commit 41b5a04

8 files changed

Lines changed: 98 additions & 65 deletions

File tree

go/consensus/cometbft/beacon/beacon.go

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ import (
1313
cmttypes "github.com/cometbft/cometbft/types"
1414
"github.com/eapache/channels"
1515

16-
beaconAPI "github.com/oasisprotocol/oasis-core/go/beacon/api"
16+
"github.com/oasisprotocol/oasis-core/go/beacon/api"
1717
"github.com/oasisprotocol/oasis-core/go/common/cache/lru"
1818
"github.com/oasisprotocol/oasis-core/go/common/crypto/hash"
1919
"github.com/oasisprotocol/oasis-core/go/common/logging"
2020
"github.com/oasisprotocol/oasis-core/go/common/pubsub"
2121
consensus "github.com/oasisprotocol/oasis-core/go/consensus/api"
2222
"github.com/oasisprotocol/oasis-core/go/consensus/api/events"
23-
tmapi "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/api"
23+
cmtapi "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/api"
2424
app "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/apps/beacon"
2525
)
2626

@@ -30,45 +30,49 @@ const epochCacheCapacity = 128
3030
// ServiceClient is the beacon service client.
3131
type ServiceClient struct {
3232
sync.RWMutex
33-
tmapi.BaseServiceClient
33+
cmtapi.BaseServiceClient
3434

3535
logger *logging.Logger
3636

37-
consensus consensus.Backend
38-
querier *app.QueryFactory
37+
consensus consensus.Backend
38+
querier *app.QueryFactory
39+
descriptor *cmtapi.ServiceDescriptor
3940

4041
epochNotifier *pubsub.Broker
41-
epochLastNotified beaconAPI.EpochTime
42-
epoch beaconAPI.EpochTime
42+
epochLastNotified api.EpochTime
43+
epoch api.EpochTime
4344
epochCurrentBlock int64
4445
epochCache *lru.Cache
4546

4647
vrfNotifier *pubsub.Broker
4748
vrfLastNotified hash.Hash
48-
vrfEvent *beaconAPI.VRFEvent
49+
vrfEvent *api.VRFEvent
4950

5051
initialNotify bool
5152

52-
baseEpoch beaconAPI.EpochTime
53+
baseEpoch api.EpochTime
5354
baseBlock int64
5455
}
5556

5657
// New constructs a new CometBFT backed beacon service client.
57-
func New(baseEpoch beaconAPI.EpochTime, baseBlock int64, consensus consensus.Backend, querier *app.QueryFactory) *ServiceClient {
58+
func New(baseEpoch api.EpochTime, baseBlock int64, consensus consensus.Backend, querier *app.QueryFactory) *ServiceClient {
59+
descriptor := cmtapi.NewStaticServiceDescriptor(api.ModuleName, app.EventType, []cmtpubsub.Query{app.QueryApp})
60+
5861
return &ServiceClient{
5962
logger: logging.GetLogger("cometbft/beacon"),
6063
consensus: consensus,
6164
querier: querier,
65+
descriptor: descriptor,
6266
epochNotifier: pubsub.NewBroker(false),
63-
epochLastNotified: beaconAPI.EpochInvalid,
67+
epochLastNotified: api.EpochInvalid,
6468
epochCache: lru.New(lru.Capacity(epochCacheCapacity, false)),
6569
vrfNotifier: pubsub.NewBroker(false),
6670
baseEpoch: baseEpoch,
6771
baseBlock: baseBlock,
6872
}
6973
}
7074

71-
func (sc *ServiceClient) StateToGenesis(ctx context.Context, height int64) (*beaconAPI.Genesis, error) {
75+
func (sc *ServiceClient) StateToGenesis(ctx context.Context, height int64) (*api.Genesis, error) {
7276
q, err := sc.querier.QueryAt(ctx, height)
7377
if err != nil {
7478
return nil, err
@@ -77,7 +81,7 @@ func (sc *ServiceClient) StateToGenesis(ctx context.Context, height int64) (*bea
7781
return q.Genesis(ctx)
7882
}
7983

80-
func (sc *ServiceClient) ConsensusParameters(ctx context.Context, height int64) (*beaconAPI.ConsensusParameters, error) {
84+
func (sc *ServiceClient) ConsensusParameters(ctx context.Context, height int64) (*api.ConsensusParameters, error) {
8185
q, err := sc.querier.QueryAt(ctx, height)
8286
if err != nil {
8387
return nil, fmt.Errorf("beacon: genesis query failed: %w", err)
@@ -86,21 +90,21 @@ func (sc *ServiceClient) ConsensusParameters(ctx context.Context, height int64)
8690
return q.ConsensusParameters(ctx)
8791
}
8892

89-
func (sc *ServiceClient) GetBaseEpoch(context.Context) (beaconAPI.EpochTime, error) {
93+
func (sc *ServiceClient) GetBaseEpoch(context.Context) (api.EpochTime, error) {
9094
return sc.baseEpoch, nil
9195
}
9296

93-
func (sc *ServiceClient) GetEpoch(ctx context.Context, height int64) (beaconAPI.EpochTime, error) {
97+
func (sc *ServiceClient) GetEpoch(ctx context.Context, height int64) (api.EpochTime, error) {
9498
q, err := sc.querier.QueryAt(ctx, height)
9599
if err != nil {
96-
return beaconAPI.EpochInvalid, err
100+
return api.EpochInvalid, err
97101
}
98102

99103
epoch, _, err := q.Epoch(ctx)
100104
return epoch, err
101105
}
102106

103-
func (sc *ServiceClient) GetFutureEpoch(ctx context.Context, height int64) (*beaconAPI.EpochTimeState, error) {
107+
func (sc *ServiceClient) GetFutureEpoch(ctx context.Context, height int64) (*api.EpochTimeState, error) {
104108
q, err := sc.querier.QueryAt(ctx, height)
105109
if err != nil {
106110
return nil, err
@@ -109,7 +113,7 @@ func (sc *ServiceClient) GetFutureEpoch(ctx context.Context, height int64) (*bea
109113
return q.FutureEpoch(ctx)
110114
}
111115

112-
func (sc *ServiceClient) GetEpochBlock(ctx context.Context, epoch beaconAPI.EpochTime) (int64, error) {
116+
func (sc *ServiceClient) GetEpochBlock(ctx context.Context, epoch api.EpochTime) (int64, error) {
113117
now, currentBlk := sc.currentEpochBlock()
114118
switch {
115119
case epoch == now:
@@ -140,15 +144,15 @@ func (sc *ServiceClient) GetEpochBlock(ctx context.Context, epoch beaconAPI.Epoc
140144

141145
// Find historic epoch with bounded bisection.
142146
const maxIterations = 20 // Should be good enough for most use cases.
143-
var prevEpoch beaconAPI.EpochTime
147+
var prevEpoch api.EpochTime
144148
for range maxIterations {
145149
q, err := sc.querier.QueryAt(ctx, height)
146150
if err != nil {
147151
return 0, fmt.Errorf("failed to query epoch: %w", err)
148152
}
149153

150154
var (
151-
curEpoch beaconAPI.EpochTime
155+
curEpoch api.EpochTime
152156
epochHeight int64
153157
)
154158
curEpoch, epochHeight, err = q.Epoch(ctx)
@@ -176,7 +180,7 @@ func (sc *ServiceClient) GetEpochBlock(ctx context.Context, epoch beaconAPI.Epoc
176180
return 0, fmt.Errorf("failed to find historic epoch")
177181
}
178182

179-
func (sc *ServiceClient) WaitEpoch(ctx context.Context, epoch beaconAPI.EpochTime) error {
183+
func (sc *ServiceClient) WaitEpoch(ctx context.Context, epoch api.EpochTime) error {
180184
ch, sub, err := sc.WatchEpochs(ctx)
181185
if err != nil {
182186
return err
@@ -198,18 +202,18 @@ func (sc *ServiceClient) WaitEpoch(ctx context.Context, epoch beaconAPI.EpochTim
198202
}
199203
}
200204

201-
func (sc *ServiceClient) WatchEpochs(context.Context) (<-chan beaconAPI.EpochTime, pubsub.ClosableSubscription, error) {
205+
func (sc *ServiceClient) WatchEpochs(context.Context) (<-chan api.EpochTime, pubsub.ClosableSubscription, error) {
202206
hook := sc.epochNotifierHook()
203-
ch := make(chan beaconAPI.EpochTime)
207+
ch := make(chan api.EpochTime)
204208
sub := sc.epochNotifier.SubscribeEx(hook)
205209
sub.Unwrap(ch)
206210

207211
return ch, sub, nil
208212
}
209213

210-
func (sc *ServiceClient) WatchLatestEpoch(context.Context) (<-chan beaconAPI.EpochTime, pubsub.ClosableSubscription, error) {
214+
func (sc *ServiceClient) WatchLatestEpoch(context.Context) (<-chan api.EpochTime, pubsub.ClosableSubscription, error) {
211215
hook := sc.epochNotifierHook()
212-
ch := make(chan beaconAPI.EpochTime)
216+
ch := make(chan api.EpochTime)
213217
sub := sc.epochNotifier.SubscribeBufferedEx(1, hook)
214218
sub.Unwrap(ch)
215219

@@ -225,7 +229,7 @@ func (sc *ServiceClient) GetBeacon(ctx context.Context, height int64) ([]byte, e
225229
return q.Beacon(ctx)
226230
}
227231

228-
func (sc *ServiceClient) GetVRFState(ctx context.Context, height int64) (*beaconAPI.VRFState, error) {
232+
func (sc *ServiceClient) GetVRFState(ctx context.Context, height int64) (*api.VRFState, error) {
229233
q, err := sc.querier.QueryAt(ctx, height)
230234
if err != nil {
231235
return nil, err
@@ -234,17 +238,17 @@ func (sc *ServiceClient) GetVRFState(ctx context.Context, height int64) (*beacon
234238
return q.VRFState(ctx)
235239
}
236240

237-
func (sc *ServiceClient) WatchLatestVRFEvent(context.Context) (<-chan *beaconAPI.VRFEvent, *pubsub.Subscription, error) {
241+
func (sc *ServiceClient) WatchLatestVRFEvent(context.Context) (<-chan *api.VRFEvent, *pubsub.Subscription, error) {
238242
hook := sc.vrfNotifierHook()
239-
ch := make(chan *beaconAPI.VRFEvent)
243+
ch := make(chan *api.VRFEvent)
240244
sub := sc.vrfNotifier.SubscribeEx(hook)
241245
sub.Unwrap(ch)
242246

243247
return ch, sub, nil
244248
}
245249

246-
func (sc *ServiceClient) ServiceDescriptor() *tmapi.ServiceDescriptor {
247-
return tmapi.NewStaticServiceDescriptor("beacon", app.EventType, []cmtpubsub.Query{app.QueryApp})
250+
func (sc *ServiceClient) ServiceDescriptor() *cmtapi.ServiceDescriptor {
251+
return sc.descriptor
248252
}
249253

250254
func (sc *ServiceClient) DeliverHeight(ctx context.Context, height int64) error {
@@ -266,13 +270,13 @@ func (sc *ServiceClient) DeliverHeight(ctx context.Context, height int64) error
266270
sc.epochNotifier.Broadcast(epoch)
267271
}
268272

269-
var vrfState *beaconAPI.VRFState
273+
var vrfState *api.VRFState
270274
vrfState, err = q.VRFState(ctx)
271275
if err != nil {
272276
return fmt.Errorf("beacon: failed to query VRF state: %w", err)
273277
}
274278
if vrfState != nil {
275-
var event beaconAPI.VRFEvent
279+
var event api.VRFEvent
276280
event.FromState(vrfState)
277281

278282
if sc.updateCachedVRFEvent(&event) {
@@ -289,8 +293,8 @@ func (sc *ServiceClient) DeliverEvent(_ context.Context, height int64, _ cmttype
289293
key := pair.GetKey()
290294
val := pair.GetValue()
291295

292-
if events.IsAttributeKind(key, &beaconAPI.EpochEvent{}) {
293-
var event beaconAPI.EpochEvent
296+
if events.IsAttributeKind(key, &api.EpochEvent{}) {
297+
var event api.EpochEvent
294298
if err := events.DecodeValue(val, &event); err != nil {
295299
sc.logger.Error("epochtime: malformed epoch event value",
296300
"err", err,
@@ -302,8 +306,8 @@ func (sc *ServiceClient) DeliverEvent(_ context.Context, height int64, _ cmttype
302306
sc.epochNotifier.Broadcast(event.Epoch)
303307
}
304308
}
305-
if events.IsAttributeKind(key, &beaconAPI.VRFEvent{}) {
306-
var event beaconAPI.VRFEvent
309+
if events.IsAttributeKind(key, &api.VRFEvent{}) {
310+
var event api.VRFEvent
307311
if err := events.DecodeValue(val, &event); err != nil {
308312
sc.logger.Error("beacon: malformed VRF event",
309313
"err", err,
@@ -318,7 +322,7 @@ func (sc *ServiceClient) DeliverEvent(_ context.Context, height int64, _ cmttype
318322
return nil
319323
}
320324

321-
func (sc *ServiceClient) updateCachedEpoch(height int64, epoch beaconAPI.EpochTime) bool {
325+
func (sc *ServiceClient) updateCachedEpoch(height int64, epoch api.EpochTime) bool {
322326
sc.Lock()
323327
defer sc.Unlock()
324328

@@ -338,7 +342,7 @@ func (sc *ServiceClient) updateCachedEpoch(height int64, epoch beaconAPI.EpochTi
338342
return false
339343
}
340344

341-
func (sc *ServiceClient) updateCachedVRFEvent(event *beaconAPI.VRFEvent) bool {
345+
func (sc *ServiceClient) updateCachedVRFEvent(event *api.VRFEvent) bool {
342346
sc.Lock()
343347
defer sc.Unlock()
344348

@@ -358,7 +362,7 @@ func (sc *ServiceClient) updateCachedVRFEvent(event *beaconAPI.VRFEvent) bool {
358362
return false
359363
}
360364

361-
func (sc *ServiceClient) currentEpochBlock() (beaconAPI.EpochTime, int64) {
365+
func (sc *ServiceClient) currentEpochBlock() (api.EpochTime, int64) {
362366
sc.RLock()
363367
defer sc.RUnlock()
364368

go/consensus/cometbft/governance/governance.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,22 @@ type ServiceClient struct {
2424

2525
logger *logging.Logger
2626

27-
consensus consensus.Backend
28-
querier *app.QueryFactory
27+
consensus consensus.Backend
28+
querier *app.QueryFactory
29+
descriptor *tmapi.ServiceDescriptor
2930

3031
eventNotifier *pubsub.Broker
3132
}
3233

3334
// New constructs a new CometBFT backed governance service client.
3435
func New(consensus consensus.Backend, querier *app.QueryFactory) *ServiceClient {
36+
descriptor := tmapi.NewStaticServiceDescriptor(api.ModuleName, app.EventType, []cmtpubsub.Query{app.QueryApp})
37+
3538
return &ServiceClient{
3639
logger: logging.GetLogger("cometbft/staking"),
3740
consensus: consensus,
3841
querier: querier,
42+
descriptor: descriptor,
3943
eventNotifier: pubsub.NewBroker(false),
4044
}
4145
}
@@ -164,7 +168,7 @@ func (sc *ServiceClient) ConsensusParameters(ctx context.Context, height int64)
164168

165169
// ServiceDescriptor implements api.ServiceClient.
166170
func (sc *ServiceClient) ServiceDescriptor() *tmapi.ServiceDescriptor {
167-
return tmapi.NewStaticServiceDescriptor(api.ModuleName, app.EventType, []cmtpubsub.Query{app.QueryApp})
171+
return sc.descriptor
168172
}
169173

170174
// DeliverEvent implements api.ServiceClient.

go/consensus/cometbft/keymanager/keymanager.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,18 @@ import (
2222
type ServiceClient struct {
2323
tmapi.BaseServiceClient
2424

25+
descriptor *tmapi.ServiceDescriptor
26+
2527
secretsClient *secrets.ServiceClient
2628
churpClient *churp.ServiceClient
2729
}
2830

2931
// New constructs a new CometBFT backed key manager service client.
3032
func New(querier *app.QueryFactory) *ServiceClient {
33+
descriptor := tmapi.NewStaticServiceDescriptor(api.ModuleName, app.EventType, []cmtpubsub.Query{app.QueryApp})
34+
3135
return &ServiceClient{
36+
descriptor: descriptor,
3237
secretsClient: secrets.New(querier),
3338
churpClient: churp.New(querier),
3439
}
@@ -64,7 +69,7 @@ func (sc *ServiceClient) Churp() churpAPI.Backend {
6469

6570
// ServiceDescriptor implements api.ServiceClient.
6671
func (sc *ServiceClient) ServiceDescriptor() *tmapi.ServiceDescriptor {
67-
return tmapi.NewStaticServiceDescriptor(api.ModuleName, app.EventType, []cmtpubsub.Query{app.QueryApp})
72+
return sc.descriptor
6873
}
6974

7075
// DeliverEvent implements api.ServiceClient.

go/consensus/cometbft/registry/registry.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ type ServiceClient struct {
2626

2727
logger *logging.Logger
2828

29-
consensus consensus.Backend
30-
querier *app.QueryFactory
29+
consensus consensus.Backend
30+
querier *app.QueryFactory
31+
descriptor *tmapi.ServiceDescriptor
3132

3233
entityNotifier *pubsub.Broker
3334
nodeNotifier *pubsub.Broker
@@ -38,10 +39,13 @@ type ServiceClient struct {
3839

3940
// New constructs a new CometBFT backed registry service client.
4041
func New(consensus consensus.Backend, querier *app.QueryFactory) *ServiceClient {
42+
descriptor := tmapi.NewStaticServiceDescriptor(api.ModuleName, app.EventType, []cmtpubsub.Query{app.QueryApp})
43+
4144
return &ServiceClient{
4245
logger: logging.GetLogger("cometbft/registry"),
4346
consensus: consensus,
4447
querier: querier,
48+
descriptor: descriptor,
4549
entityNotifier: pubsub.NewBroker(false),
4650
nodeNotifier: pubsub.NewBroker(false),
4751
nodeListNotifier: pubsub.NewBroker(false),
@@ -239,7 +243,7 @@ func (sc *ServiceClient) ConsensusParameters(ctx context.Context, height int64)
239243

240244
// ServiceDescriptor implements api.ServiceClient.
241245
func (sc *ServiceClient) ServiceDescriptor() *tmapi.ServiceDescriptor {
242-
return tmapi.NewStaticServiceDescriptor(api.ModuleName, app.EventType, []cmtpubsub.Query{app.QueryApp})
246+
return sc.descriptor
243247
}
244248

245249
// DeliverEvent implements api.ServiceClient.

0 commit comments

Comments
 (0)