-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go/keymanager/churp: Implement backend
- Loading branch information
Showing
10 changed files
with
468 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package churp | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/oasisprotocol/oasis-core/go/common" | ||
churpState "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/apps/keymanager/churp/state" | ||
"github.com/oasisprotocol/oasis-core/go/keymanager/churp" | ||
) | ||
|
||
// Query is the key manager query interface. | ||
type Query interface { | ||
Status(context.Context, common.Namespace, uint8) (*churp.Status, error) | ||
Statuses(context.Context, common.Namespace) ([]*churp.Status, error) | ||
AllStatuses(context.Context) ([]*churp.Status, error) | ||
} | ||
|
||
type querier struct { | ||
state *churpState.ImmutableState | ||
} | ||
|
||
func (kq *querier) Status(ctx context.Context, runtimeID common.Namespace, churpID uint8) (*churp.Status, error) { | ||
return kq.state.Status(ctx, runtimeID, churpID) | ||
} | ||
|
||
func (kq *querier) Statuses(ctx context.Context, runtimeID common.Namespace) ([]*churp.Status, error) { | ||
return kq.state.Statuses(ctx, runtimeID) | ||
} | ||
|
||
func (kq *querier) AllStatuses(ctx context.Context) ([]*churp.Status, error) { | ||
return kq.state.AllStatuses(ctx) | ||
} | ||
|
||
func NewQuery(state *churpState.ImmutableState) Query { | ||
return &querier{state} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package churp | ||
|
||
import ( | ||
"context" | ||
|
||
cmtabcitypes "github.com/cometbft/cometbft/abci/types" | ||
"github.com/eapache/channels" | ||
|
||
"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" | ||
"github.com/oasisprotocol/oasis-core/go/consensus/api/events" | ||
app "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/apps/keymanager" | ||
"github.com/oasisprotocol/oasis-core/go/keymanager/churp" | ||
"github.com/oasisprotocol/oasis-core/go/registry/api" | ||
) | ||
|
||
type ServiceClient struct { | ||
logger *logging.Logger | ||
|
||
querier *app.QueryFactory | ||
statusNotifier *pubsub.Broker | ||
} | ||
|
||
// Status implements churp.Backend. | ||
func (sc *ServiceClient) Status(ctx context.Context, query *churp.StatusQuery) (*churp.Status, error) { | ||
q, err := sc.querier.QueryAt(ctx, query.Height) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return q.Churp().Status(ctx, query.RuntimeID, query.ChurpID) | ||
} | ||
|
||
// Statuses implements churp.Backend. | ||
func (sc *ServiceClient) Statuses(ctx context.Context, query *api.NamespaceQuery) ([]*churp.Status, error) { | ||
q, err := sc.querier.QueryAt(ctx, query.Height) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return q.Churp().Statuses(ctx, query.ID) | ||
} | ||
|
||
// AllStatuses implements churp.Backend. | ||
func (sc *ServiceClient) AllStatuses(ctx context.Context, height int64) ([]*churp.Status, error) { | ||
q, err := sc.querier.QueryAt(ctx, height) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return q.Churp().AllStatuses(ctx) | ||
} | ||
|
||
// WatchStatuses implements churp.Backend. | ||
func (sc *ServiceClient) WatchStatuses() (<-chan *churp.Status, *pubsub.Subscription) { | ||
sub := sc.statusNotifier.Subscribe() | ||
ch := make(chan *churp.Status) | ||
sub.Unwrap(ch) | ||
|
||
return ch, sub | ||
} | ||
|
||
func (sc *ServiceClient) DeliverEvent(ev *cmtabcitypes.Event) error { | ||
for _, pair := range ev.GetAttributes() { | ||
key := pair.GetKey() | ||
val := pair.GetValue() | ||
|
||
if events.IsAttributeKind(key, &churp.CreateEvent{}) { | ||
var event churp.CreateEvent | ||
if err := events.DecodeValue(val, &event); err != nil { | ||
sc.logger.Error("worker: failed to get status from tag", | ||
"err", err, | ||
) | ||
continue | ||
} | ||
|
||
sc.statusNotifier.Broadcast(event.Status) | ||
} | ||
if events.IsAttributeKind(key, &churp.UpdateEvent{}) { | ||
var event churp.UpdateEvent | ||
if err := events.DecodeValue(val, &event); err != nil { | ||
sc.logger.Error("worker: failed to get status from tag", | ||
"err", err, | ||
) | ||
continue | ||
} | ||
|
||
sc.statusNotifier.Broadcast(event.Status) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// New constructs a new CometBFT backed key manager secrets management Backend | ||
// instance. | ||
func New(ctx context.Context, querier *app.QueryFactory) (*ServiceClient, error) { | ||
sc := ServiceClient{ | ||
logger: logging.GetLogger("cometbft/keymanager/churp"), | ||
querier: querier, | ||
} | ||
sc.statusNotifier = pubsub.NewBrokerEx(func(ch channels.Channel) { | ||
statuses, err := sc.AllStatuses(ctx, consensus.HeightLatest) | ||
if err != nil { | ||
sc.logger.Error("status notifier: unable to get a list of statuses", | ||
"err", err, | ||
) | ||
return | ||
} | ||
|
||
wr := ch.In() | ||
for _, v := range statuses { | ||
wr <- v | ||
} | ||
}) | ||
|
||
return &sc, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.