Skip to content

Commit

Permalink
Support watching custom scripts (#71)
Browse files Browse the repository at this point in the history
* Add external script repo

* Add external script repo impls

* Remove leftover

* Support (un)watching external scripts

* Prevent send notifications for unsubscribed scripts/acounts

* Fixes
  • Loading branch information
altafan authored Jan 18, 2024
1 parent 2f60093 commit 0167fed
Show file tree
Hide file tree
Showing 30 changed files with 1,601 additions and 236 deletions.
622 changes: 447 additions & 175 deletions api-spec/protobuf/gen/go/ocean/v1/notification.pb.go

Large diffs are not rendered by default.

84 changes: 84 additions & 0 deletions api-spec/protobuf/gen/go/ocean/v1/notification_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api-spec/protobuf/ocean/v1/account.proto
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,4 @@ message DeleteAccountRequest{
// Account namespace or label.
string account_name = 1;
}
message DeleteAccountResponse{}
message DeleteAccountResponse{}
30 changes: 27 additions & 3 deletions api-spec/protobuf/ocean/v1/notification.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ import "ocean/v1/types.proto";
// server-side stream or by subscribing webhooks invoked whenever an event
// occurs.
service NotificationService {
// WatchExternalScript allows to get notified about utxos/txs related to the given
// external script, ie. not derived from a wallet account.
// The service answers with the label assigned to the given script.
// The label is used as identifier of the utxos/txs received from the streams.
rpc WatchExternalScript(WatchExternalScriptRequest) returns(WatchExternalScriptResponse);
// UnwatchExternalScript allows to stop watching for the script identified with
// the given label.
rpc UnwatchExternalScript(UnwatchExternalScriptRequest) returns(UnwatchExternalScriptResponse);

//**************//
// STREAMS //
//**************//
Expand All @@ -24,14 +33,29 @@ service NotificationService {
//***************//

// Adds a webhook registered for some kind of event.
rpc AddWebhook(AddWebhookRequest) returns(AddWebhookResponse){}
rpc AddWebhook(AddWebhookRequest) returns(AddWebhookResponse);

// Removes some previously added webhook.
rpc RemoveWebhook(RemoveWebhookRequest) returns(RemoveWebhookResponse){}
rpc RemoveWebhook(RemoveWebhookRequest) returns(RemoveWebhookResponse);

// Returns registered webhooks.
rpc ListWebhooks(ListWebhooksRequest) returns(ListWebhooksResponse){}
rpc ListWebhooks(ListWebhooksRequest) returns(ListWebhooksResponse);
}

message WatchExternalScriptRequest {
// The script to watch.
string script = 1;
// Optional: the private blinding key in case the script locks confidential utxos to unblind.
string blinding_key = 2;
}
message WatchExternalScriptResponse {
string label = 1;
}

message UnwatchExternalScriptRequest {
string label = 1;
}
message UnwatchExternalScriptResponse {}

message TransactionNotificationsRequest{}
message TransactionNotificationsResponse{
Expand Down
3 changes: 2 additions & 1 deletion internal/app-config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ func (c *AppConfig) notificationService() *application.NotificationService {
}

rm, _ := c.repoManager()
c.notifySvc = application.NewNotificationService(rm)
bcs, _ := c.bcScanner()
c.notifySvc = application.NewNotificationService(rm, bcs)
return c.notifySvc
}

Expand Down
23 changes: 0 additions & 23 deletions internal/core/application/account_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,29 +269,6 @@ func (as *AccountService) registerHandlerForWalletEvents() {
as.bcScanner.StopWatchForAccount(event.AccountName)
},
)
// Start watching for when utxos are spent as soon as they are added to the storage.
as.repoManager.RegisterHandlerForUtxoEvent(
domain.UtxoAdded, func(event domain.UtxoEvent) {
accountName := event.Utxos[0].AccountName
as.bcScanner.WatchForUtxos(accountName, event.Utxos)
},
)

// In background, make sure to watch for all utxos to get notified when they are spent.
go func() {
utxos, err := as.repoManager.UtxoRepository().GetAllUtxos(
context.Background(),
)
if err != nil {
as.warn(err, "account service: error while getting all utxos")
return
}
for _, u := range utxos {
if !u.IsSpent() {
as.bcScanner.WatchForUtxos(u.AccountName, []domain.UtxoInfo{u.Info()})
}
}
}()
}

func (as *AccountService) listenToUtxoChannel(
Expand Down
Loading

0 comments on commit 0167fed

Please sign in to comment.