Skip to content

Commit

Permalink
chore: Expose the full SessionSuppliers struct
Browse files Browse the repository at this point in the history
  • Loading branch information
red-0ne committed Dec 6, 2023
1 parent d462807 commit cb999a0
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 39 deletions.
4 changes: 2 additions & 2 deletions pkg/appgateserver/endpoint_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion pkg/appgateserver/synchronous.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/sdk/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
16 changes: 8 additions & 8 deletions pkg/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
8 changes: 3 additions & 5 deletions pkg/sdk/send_relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
42 changes: 21 additions & 21 deletions pkg/sdk/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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 {
Expand All @@ -93,7 +93,7 @@ func (sdk *poktrollSDK) GetSessionSupplierEndpoints(

currentSession.SuppliersEndpoints = append(
currentSession.SuppliersEndpoints,
&SupplierEndpoint{
&SingleSupplierEndpoint{
Url: url,
RpcType: endpoint.RpcType,
SupplierAddress: supplier.Address,
Expand All @@ -104,5 +104,5 @@ func (sdk *poktrollSDK) GetSessionSupplierEndpoints(
}
}

return currentSession.SuppliersEndpoints, nil
return currentSession, nil
}

0 comments on commit cb999a0

Please sign in to comment.