-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
193 lines (168 loc) · 6.49 KB
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package sdk
import (
"context"
"errors"
"fmt"
"github.com/cosmos/gogoproto/grpc"
sessiontypes "github.com/pokt-network/poktroll/x/session/types"
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
grpcoptions "google.golang.org/grpc"
)
// SessionClient is the interface to interact with the on-chain session module.
//
// For example, it can be used to get the current session for a given application
// and service id at a given height.
type SessionClient struct {
PoktNodeSessionFetcher
}
// GetSession returns the session with the given application address, service id and height.
func (s *SessionClient) GetSession(
ctx context.Context,
appAddress string,
serviceId string,
height int64,
) (session *sessiontypes.Session, err error) {
if s.PoktNodeSessionFetcher == nil {
return nil, errors.New("PoktNodeSessionFetcher not set")
}
req := &sessiontypes.QueryGetSessionRequest{
ApplicationAddress: appAddress,
ServiceId: serviceId,
BlockHeight: height,
}
// TODO_DISCUSS: Would it be feasible to add a GetCurrentSession, supported by the underlying protocol?
// It seems likely that GetSession will almost always be used to get the session
// matching the latest height.
// In addition, the current session that is being returned could include the
// latest block height, reducing the number of SDK calls needed for sending relays
// and removes the need for the BlockClient.
res, err := s.PoktNodeSessionFetcher.GetSession(ctx, req)
if err != nil {
return nil, err
}
return res.Session, nil
}
// NewPoktNodeSessionFetcher returns the default implementation of the
// PoktNodeSessionFetcher interface.
// It connects to a POKT full node through the session module's query client
// to get session data.
func NewPoktNodeSessionFetcher(grpcConn grpc.ClientConn) PoktNodeSessionFetcher {
return sessiontypes.NewQueryClient(grpcConn)
}
// PoktNodeSessionFetcher is an interface used by the SessionClient to fetch
// sessions using poktroll session type.
//
// Most users can rely on the default implementation provided by NewPoktNodeSessionFetcher function.
// A custom implementation of this interface can be used to gain more granular
// control over the interactions of the SessionClient with the POKT full node.
type PoktNodeSessionFetcher interface {
GetSession(
context.Context,
*sessiontypes.QueryGetSessionRequest,
...grpcoptions.CallOption,
) (*sessiontypes.QueryGetSessionResponse, error)
}
// SupplierAddress captures the address for a supplier.
// This is defined to help enforce type safety by requiring explicit type casting
// of a string before it can be used as a Supplier's address.
type SupplierAddress string
// EndpointFilter is a function type used by SessionFilter to return a boolean
// indicating whether the input endpoint should be filtered out.
type EndpointFilter func(Endpoint) bool
// SessionFilter wraps a Session, allowing node selection by filtering out endpoints
// based on the filters set on the struct.
// This is needed so functions that enable sending relays can be provided with a
// struct that contains both session data and the endpoint(s) selected for receiving relays.
type SessionFilter struct {
*sessiontypes.Session
EndpointFilters []EndpointFilter
// TODO_IMPROVE: Add a slice of endpoint ordering functions
}
// AllEndpoints returns all the endpoints corresponding to a session for the
// service id specified by the session header.
// The endpoints are not filtered.
func (f *SessionFilter) AllEndpoints() (map[SupplierAddress][]Endpoint, error) {
if f.Session == nil {
return nil, fmt.Errorf("AllEndpoints: Session not set on FilteredSession struct")
}
header := f.Session.Header
supplierEndpoints := make(map[SupplierAddress][]Endpoint)
for _, supplier := range f.Session.Suppliers {
// The endpoints slice is intentionally defined here to prevent any overwrites
// in the unlikely case that there are duplicate service IDs under a supplier.
var endpoints []Endpoint
for _, service := range supplier.Services {
// TODO_TECHDEBT: Remove this check once the session module ensures that
// oly the services corresponding to the session header is returned.
if service.ServiceId != f.Session.Header.ServiceId {
continue
}
var newEndpoints []Endpoint
for _, e := range service.Endpoints {
newEndpoints = append(newEndpoints, endpoint{
// TODO_TECHDEBT: Need deep copying here.
header: *header,
supplierEndpoint: *e,
supplier: SupplierAddress(supplier.OperatorAddress),
})
}
endpoints = append(endpoints, newEndpoints...)
}
supplierEndpoints[SupplierAddress(supplier.OperatorAddress)] = endpoints
}
return supplierEndpoints, nil
}
// TODO_TECHDEBT: add a unit test to cover this method.
// FilteredEndpoints returns the endpoints that pass all the filters set of
// the FilteredSession.
func (f *SessionFilter) FilteredEndpoints() ([]Endpoint, error) {
allEndpoints, err := f.AllEndpoints()
if err != nil {
return nil, fmt.Errorf("FilteredEndpoints: error getting all endpoints: %w", err)
}
var filteredEndpoints []Endpoint
for _, endpoints := range allEndpoints {
for _, endpoint := range endpoints {
includePoint := true
for _, filter := range f.EndpointFilters {
if filter(endpoint) {
includePoint = false
break
}
}
if includePoint {
filteredEndpoints = append(filteredEndpoints, endpoint)
}
}
}
return filteredEndpoints, nil
}
// Endpoint is a struct that represents an endpoint with its corresponding
// supplier and session that contains the endpoint.
// It implements the Endpoint interface.
type endpoint struct {
header sessiontypes.SessionHeader
supplierEndpoint sharedtypes.SupplierEndpoint
supplier SupplierAddress
}
// Endpoint returns the supplier endpoint for the endpoint.
func (e endpoint) Endpoint() sharedtypes.SupplierEndpoint {
return e.supplierEndpoint
}
// Supplier returns the supplier address for the endpoint.
func (e endpoint) Supplier() SupplierAddress {
return e.supplier
}
// Header returns the session header on which the supplier's endpoint was retrieved.
func (e endpoint) Header() sessiontypes.SessionHeader {
return e.header
}
// TODO_CONSIDERATION: Prefix the Endpoint methods with `Get` to make it clear
// that they are getters.
// Endpoint is an interface that represents an endpoint with its corresponding
// supplier and session that contains the endpoint.
type Endpoint interface {
Header() sessiontypes.SessionHeader
Supplier() SupplierAddress
Endpoint() sharedtypes.SupplierEndpoint
}