Skip to content

Commit

Permalink
added handling subscriptionIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
zkokelj committed Sep 27, 2023
1 parent fb1a12b commit e264045
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
1 change: 1 addition & 0 deletions go/rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
Attestation = "obscuroscan_attestation"
StopHost = "test_stopHost"
Subscribe = "eth_subscribe"
Unsubscribe = "eth_unsubscribe"
SubscribeNamespace = "eth"
SubscriptionTypeLogs = "logs"

Expand Down
7 changes: 7 additions & 0 deletions tools/walletextension/accountmanager/account_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ func (m *AccountManager) ProxyRequest(rpcReq *wecommon.RPCRequest, rpcResp *inte
}
}

// In some cases, we subscribe with each account current user added, and we also need to unsubscribe with all of them
if rpcReq.Method == rpc.Unsubscribe {
// TODO: The unsubscribe param is hash of concatenated subscriptions.
// We need to iterate over them and unsubscribe from all subscriptions
fmt.Println("we need to handle unsubscribe here")
}

return m.executeCall(rpcReq, rpcResp)
}

Expand Down
39 changes: 33 additions & 6 deletions tools/walletextension/subscriptions/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package subscriptions
import (
"context"
"fmt"
"strings"
"time"

"github.com/ethereum/go-ethereum/crypto"

gethlog "github.com/ethereum/go-ethereum/log"
gethrpc "github.com/ethereum/go-ethereum/rpc"
"github.com/obscuronet/go-obscuro/go/common"
Expand Down Expand Up @@ -34,11 +37,10 @@ func (s *SubscriptionManager) HandleNewSubscriptions(clients []rpc.Client, req *

// save subscriptions
subscriptions := make([]*gethrpc.ClientSubscription, 0, len(clients))
subscriptionIDS := make([]string, 0, len(clients))

// TODO: Create a buffered channel and perform deduplication of logs or implement additional logic to filter logs

// TODO: Create response...

// Do periodic checks if userConn is closed and unsubscribe from all subscriptions for this user
go func() {
for {
Expand Down Expand Up @@ -80,14 +82,39 @@ func (s *SubscriptionManager) HandleNewSubscriptions(clients []rpc.Client, req *
s.logger.Info(fmt.Sprintf("Subscribing to: %d clients", len(clients)))
for _, client := range clients {
s.logger.Info(fmt.Sprintf("Subscribing for an event with client: %s", client))
subscription, err := s.addSubscription(client, req, resp, commonChannel)
var subscriptionID interface{}
subscription, err := s.addSubscription(client, req, &subscriptionID, commonChannel)
if err != nil {
s.logger.Info(fmt.Sprintf("Error: %v", err))
} else {
// If there was no error, the subscription was successful. Store it for unsubscribing in the future
subscriptions = append(subscriptions, subscription)

// check if subscriptionID is of type string
if strSubscriptionID, ok := subscriptionID.(string); ok {
subscriptionIDS = append(subscriptionIDS, strSubscriptionID)
s.logger.Info(fmt.Sprintf("Subscribed with subscription ID: %s", subscriptionID))
} else {
s.logger.Info(fmt.Sprintf("Error: invalid type of subscriptionID. Expected string, got: %T", subscriptionID))
}
}
subscriptions = append(subscriptions, subscription)
// TODO: get subscription ID
// s.logger.Info(fmt.Sprintf("Subscribed with subscription ID: %s", subscriptionID))
}

// create a response with new subscriptionID by concatenating them and computing hash of the concatenated string
combinedSubscriptionIDS := strings.Join(subscriptionIDS, "")
// Compute Keccak-256 hash
subscriptionsIDHash := crypto.Keccak256([]byte(combinedSubscriptionIDS))
// Convert hash to hex string for better readability
*resp = fmt.Sprintf("%x", subscriptionsIDHash)

// TODO:
// We need to store subscriptionsIDHash and subscriptionIDS and have them available for unsubscribe -
// where is the best place to store them?
// - 1. option is database -> More complicated,
// can contain elements that are not relevant anymore in case of crashes, etd.
// - 2. option is in-memory storage, it si simpler, but will consume more RAM,
// easier to handle since on every crash/restart it is cleared (and also all the subscriptions are dropped by our logic)

return nil
}

Expand Down

0 comments on commit e264045

Please sign in to comment.