From cb999a08cb23da380a91afcc37e14d4f4d6b43a5 Mon Sep 17 00:00:00 2001 From: Redouane Lakrache Date: Wed, 6 Dec 2023 21:23:32 +0100 Subject: [PATCH] chore: Expose the full SessionSuppliers struct --- pkg/appgateserver/endpoint_selector.go | 4 +-- pkg/appgateserver/synchronous.go | 7 ++++- pkg/sdk/interface.go | 4 +-- pkg/sdk/sdk.go | 16 +++++----- pkg/sdk/send_relay.go | 8 ++--- pkg/sdk/session.go | 42 +++++++++++++------------- 6 files changed, 42 insertions(+), 39 deletions(-) diff --git a/pkg/appgateserver/endpoint_selector.go b/pkg/appgateserver/endpoint_selector.go index db9aa3442..821b18018 100644 --- a/pkg/appgateserver/endpoint_selector.go +++ b/pkg/appgateserver/endpoint_selector.go @@ -16,8 +16,8 @@ func (app *appGateServer) getRelayerUrl( ctx context.Context, serviceId string, rpcType sharedtypes.RPCType, - supplierEndpoints []*sdk.SupplierEndpoint, -) (supplierEndpoint *sdk.SupplierEndpoint, err error) { + supplierEndpoints []*sdk.SingleSupplierEndpoint, +) (supplierEndpoint *sdk.SingleSupplierEndpoint, err error) { for _, supplierEndpoint := range supplierEndpoints { // Skip services that don't match the requested serviceId. if supplierEndpoint.Header.Service.Id != serviceId { diff --git a/pkg/appgateserver/synchronous.go b/pkg/appgateserver/synchronous.go index b079a14a8..5d0b46a5a 100644 --- a/pkg/appgateserver/synchronous.go +++ b/pkg/appgateserver/synchronous.go @@ -38,7 +38,12 @@ func (app *appGateServer) handleSynchronousRelay( } // Get a supplier URL and address for the given service and session. - supplierEndpoint, err := app.getRelayerUrl(ctx, serviceId, requestType, sessionSuppliers) + supplierEndpoint, err := app.getRelayerUrl( + ctx, + serviceId, + requestType, + sessionSuppliers.SuppliersEndpoints, + ) if err != nil { return ErrAppGateHandleRelay.Wrapf("getting supplier URL: %s", err) } diff --git a/pkg/sdk/interface.go b/pkg/sdk/interface.go index a9c8b8e1f..8fcb22586 100644 --- a/pkg/sdk/interface.go +++ b/pkg/sdk/interface.go @@ -16,12 +16,12 @@ type POKTRollSDK interface { ctx context.Context, appAddress string, serviceId string, - ) (session []*SupplierEndpoint, err error) + ) (session *SessionSuppliers, err error) // SendRelay sends a relay request to the given supplier's endpoint. SendRelay( ctx context.Context, - sessionSupplierEndpoint *SupplierEndpoint, + sessionSupplierEndpoint *SingleSupplierEndpoint, request *http.Request, ) (response *servicetypes.RelayResponse, err error) } diff --git a/pkg/sdk/sdk.go b/pkg/sdk/sdk.go index 6cf676c62..233807cec 100644 --- a/pkg/sdk/sdk.go +++ b/pkg/sdk/sdk.go @@ -45,12 +45,12 @@ type poktrollSDK struct { // It used to get the current session for the application given a requested service. sessionQuerier client.SessionQueryClient - // sessionMu is a mutex to protect latestSessions map reads and updates. - sessionMu sync.RWMutex + // serviceSessionSuppliersMu is a mutex to protect latestSessions map reads and updates. + serviceSessionSuppliersMu sync.RWMutex - // latestSessions is a latest sessions map of serviceId -> {appAddress -> SessionSuppliers} - // based on the latest block data available. - latestSessions map[string]map[string]*sessionSuppliers + // serviceSessionSuppliers is a map of serviceId -> {appAddress -> SessionSuppliers} + // for a specific session + serviceSessionSuppliers map[string]map[string]*SessionSuppliers // accountQuerier is the querier for the account module. // It is used to get the the supplier's public key to verify the relay response signature. @@ -67,9 +67,9 @@ type poktrollSDK struct { func NewPOKTRollSDK(ctx context.Context, config *POKTRollSDKConfig) (POKTRollSDK, error) { sdk := &poktrollSDK{ - config: config, - latestSessions: make(map[string]map[string]*sessionSuppliers), - supplierAccountCache: make(map[string]cryptotypes.PubKey), + config: config, + serviceSessionSuppliers: make(map[string]map[string]*SessionSuppliers), + supplierAccountCache: make(map[string]cryptotypes.PubKey), } var err error diff --git a/pkg/sdk/send_relay.go b/pkg/sdk/send_relay.go index d24d17838..f1583aba8 100644 --- a/pkg/sdk/send_relay.go +++ b/pkg/sdk/send_relay.go @@ -6,7 +6,6 @@ import ( "io" "net/http" - cryptocomet "github.com/cometbft/cometbft/crypto" "github.com/pokt-network/poktroll/pkg/signer" "github.com/pokt-network/poktroll/x/service/types" ) @@ -17,7 +16,7 @@ import ( // the relay request. func (sdk *poktrollSDK) SendRelay( ctx context.Context, - supplierEndpoint *SupplierEndpoint, + supplierEndpoint *SingleSupplierEndpoint, request *http.Request, ) (response *types.RelayResponse, err error) { payloadBz, err := io.ReadAll(request.Body) @@ -48,12 +47,11 @@ func (sdk *poktrollSDK) SendRelay( return nil, ErrSDKHandleRelay.Wrapf("getting signable bytes: %s", err) } - hash := cryptocomet.Sha256(signableBz) - signature, err := signer.Sign(hash) + requestSig, err := signer.Sign(signableBz) if err != nil { return nil, ErrSDKHandleRelay.Wrapf("signing relay: %s", err) } - relayRequest.Meta.Signature = signature + relayRequest.Meta.Signature = requestSig // Marshal the relay request to bytes and create a reader to be used as an HTTP request body. cdc := types.ModuleCdc diff --git a/pkg/sdk/session.go b/pkg/sdk/session.go index 26e6db4fe..c99ebb2e6 100644 --- a/pkg/sdk/session.go +++ b/pkg/sdk/session.go @@ -8,19 +8,19 @@ import ( sharedtypes "github.com/pokt-network/poktroll/x/shared/types" ) -// sessionSuppliers is the structure that represents a session's end block height +// SessionSuppliers is the structure that represents a session's end block height // and its matching suppliers. -type sessionSuppliers struct { +type SessionSuppliers struct { // SessionEndBlockHeight is the session's end block height that helps // determine if the session is still valid without looking into SupplierEndpoints slice. - SessionEndBlockHeight int64 - SuppliersEndpoints []*SupplierEndpoint + Session *sessiontypes.Session + SuppliersEndpoints []*SingleSupplierEndpoint } -// SupplierEndpoint is the structure that represents a supplier's endpoint +// SingleSupplierEndpoint is the structure that represents a supplier's endpoint // augmented with the session's header and the supplier's address for easy // access to the needed information when sending a relay request. -type SupplierEndpoint struct { +type SingleSupplierEndpoint struct { Url *url.URL RpcType sharedtypes.RPCType SupplierAddress string @@ -33,28 +33,28 @@ type SupplierEndpoint struct { func (sdk *poktrollSDK) GetSessionSupplierEndpoints( ctx context.Context, appAddress, serviceId string, -) ([]*SupplierEndpoint, error) { - sdk.sessionMu.RLock() - defer sdk.sessionMu.RUnlock() +) (*SessionSuppliers, error) { + sdk.serviceSessionSuppliersMu.RLock() + defer sdk.serviceSessionSuppliersMu.RUnlock() latestBlockHeight := sdk.blockClient.LatestBlock(ctx).Height() // Create the latestSessions map entry for the serviceId if it doesn't exist. - if _, ok := sdk.latestSessions[serviceId]; !ok { - sdk.latestSessions[serviceId] = map[string]*sessionSuppliers{} + if _, ok := sdk.serviceSessionSuppliers[serviceId]; !ok { + sdk.serviceSessionSuppliers[serviceId] = map[string]*SessionSuppliers{} } // Create the latestSessions[serviceId] map entry for the appAddress if it doesn't exist. - if _, ok := sdk.latestSessions[serviceId][appAddress]; !ok { - sdk.latestSessions[serviceId][appAddress] = &sessionSuppliers{} + if _, ok := sdk.serviceSessionSuppliers[serviceId][appAddress]; !ok { + sdk.serviceSessionSuppliers[serviceId][appAddress] = &SessionSuppliers{} } // currentSession is guaranteed to exist after the checks above. - currentSession := sdk.latestSessions[serviceId][appAddress] + currentSession := sdk.serviceSessionSuppliers[serviceId][appAddress] // Return the current session's SuppliersEndpoints if the session is still valid. - if latestBlockHeight < currentSession.SessionEndBlockHeight { - return currentSession.SuppliersEndpoints, nil + if latestBlockHeight < currentSession.Session.Header.SessionEndBlockHeight { + return currentSession, nil } // Query for the current session. @@ -68,9 +68,9 @@ func (sdk *poktrollSDK) GetSessionSupplierEndpoints( return nil, err } - // Override the old SessionSuppliers and constructs the new one. - currentSession.SessionEndBlockHeight = session.Header.SessionEndBlockHeight - currentSession.SuppliersEndpoints = []*SupplierEndpoint{} + // Override the old Session and SessionSuppliers and construct the new one. + currentSession.Session = session + currentSession.SuppliersEndpoints = []*SingleSupplierEndpoint{} for _, supplier := range session.Suppliers { for _, service := range supplier.Services { @@ -93,7 +93,7 @@ func (sdk *poktrollSDK) GetSessionSupplierEndpoints( currentSession.SuppliersEndpoints = append( currentSession.SuppliersEndpoints, - &SupplierEndpoint{ + &SingleSupplierEndpoint{ Url: url, RpcType: endpoint.RpcType, SupplierAddress: supplier.Address, @@ -104,5 +104,5 @@ func (sdk *poktrollSDK) GetSessionSupplierEndpoints( } } - return currentSession.SuppliersEndpoints, nil + return currentSession, nil }