-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathoptions.go
More file actions
254 lines (227 loc) · 6.93 KB
/
Copy pathoptions.go
File metadata and controls
254 lines (227 loc) · 6.93 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package blockchain
import (
"sync"
"time"
ipfsCluster "github.com/ipfs-cluster/ipfs-cluster/api/rest/client"
"github.com/ipfs/kubo/client/rpc"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
)
type (
Option func(*options) error
options struct {
authorizer peer.ID
authorizedPeers []peer.ID
allowTransientConnection bool
blockchainEndPoint string
secretsPath string
timeout int
wg *sync.WaitGroup
minPingSuccessCount int
maxPingTime int
topicName string
chainName string
relays []string
updatePoolName func(string) error
getPoolName func() string
updateChainName func(string) error
getChainName func() string
fetchFrequency time.Duration //Hours that it should update the list of pool users and pool requests if not called through pubsub
rpc *rpc.HttpApi
ipfsClusterApi ipfsCluster.Client
selfPeerID peer.ID // Peer ID derived from private key, used for authorization checks
clusterPeerID peer.ID // IPFS cluster peer ID (original identity), used for on-chain pool membership
signingKey crypto.PrivKey // Private key for signing outgoing requests (mobile client)
clientProtocolID string // Protocol ID for kubo p2p forwarding (e.g. "/x/fula-blockchain")
}
)
func defaultUpdatePoolName(newPoolName string) error {
return nil
}
func defaultGetPoolName() string {
return "0"
}
func defaultUpdateChainName(newChainName string) error {
return nil
}
func defaultGetChainName() string {
return ""
}
func newOptions(o ...Option) (*options, error) {
opts := options{
authorizer: "", // replace with an appropriate default peer.ID
authorizedPeers: []peer.ID{}, // default to an empty slice
allowTransientConnection: true, // or false, as per your default
blockchainEndPoint: "api.node3.functionyard.fula.network", // default endpoint
secretsPath: "", //path to secrets dir
timeout: 30, // default timeout in seconds
wg: nil, // initialized WaitGroup
minPingSuccessCount: 7, // default minimum success count
maxPingTime: 900, // default maximum ping time in miliseconds
topicName: "0", // default topic name
chainName: "", // default chain name (empty means auto-detect)
relays: []string{}, // default to an empty slice
updatePoolName: defaultUpdatePoolName, // set a default function or leave nil
getPoolName: defaultGetPoolName,
updateChainName: defaultUpdateChainName, // set a default function or leave nil
getChainName: defaultGetChainName,
fetchFrequency: time.Hour * 1, // default frequency, e.g., 1 hour
rpc: nil,
ipfsClusterApi: nil,
}
for _, apply := range o {
if err := apply(&opts); err != nil {
return nil, err
}
}
return &opts, nil
}
func WithAuthorizer(a peer.ID) Option {
return func(o *options) error {
o.authorizer = a
return nil
}
}
func WithAuthorizedPeers(l []peer.ID) Option {
return func(o *options) error {
o.authorizedPeers = l
return nil
}
}
func WithAllowTransientConnection(t bool) Option {
return func(o *options) error {
o.allowTransientConnection = t
return nil
}
}
func WithBlockchainEndPoint(b string) Option {
return func(o *options) error {
if b == "" {
b = "api.node3.functionyard.fula.network"
}
o.blockchainEndPoint = b
return nil
}
}
func WithSecretsPath(b string) Option {
return func(o *options) error {
o.secretsPath = b
return nil
}
}
func WithTimeout(to int) Option {
return func(o *options) error {
o.timeout = to
return nil
}
}
func WithWg(wg *sync.WaitGroup) Option {
return func(o *options) error {
o.wg = wg
return nil
}
}
func WithMinSuccessPingCount(sr int) Option {
return func(o *options) error {
o.minPingSuccessCount = sr
return nil
}
}
func WithIpfsClusterAPI(n ipfsCluster.Client) Option {
return func(o *options) error {
o.ipfsClusterApi = n
return nil
}
}
func WithMaxPingTime(t int) Option {
return func(o *options) error {
o.maxPingTime = t
return nil
}
}
func WithIpfsClient(n *rpc.HttpApi) Option {
return func(o *options) error {
o.rpc = n
return nil
}
}
func WithFetchFrequency(t time.Duration) Option {
return func(o *options) error {
o.fetchFrequency = t
return nil
}
}
func WithTopicName(n string) Option {
return func(o *options) error {
o.topicName = n
return nil
}
}
func WithUpdatePoolName(updatePoolName func(string) error) Option {
return func(o *options) error {
o.updatePoolName = updatePoolName
return nil
}
}
func WithGetPoolName(getPoolName func() string) Option {
return func(o *options) error {
o.getPoolName = getPoolName
return nil
}
}
func WithChainName(n string) Option {
return func(o *options) error {
o.chainName = n
return nil
}
}
func WithUpdateChainName(updateChainName func(string) error) Option {
return func(o *options) error {
o.updateChainName = updateChainName
return nil
}
}
func WithGetChainName(getChainName func() string) Option {
return func(o *options) error {
o.getChainName = getChainName
return nil
}
}
// WithRelays sets the relay addresses.
func WithRelays(r []string) Option {
return func(o *options) error {
o.relays = r
return nil
}
}
// WithSelfPeerID sets the peer ID for the local node (derived from private key).
// Used for authorization checks (replaces h.ID() when no libp2p host is present).
func WithSelfPeerID(id peer.ID) Option {
return func(o *options) error {
o.selfPeerID = id
return nil
}
}
// WithClusterPeerID sets the IPFS cluster peer ID (original identity, not HMAC-derived).
// This is the peer ID registered on-chain for pool membership checks.
func WithClusterPeerID(id peer.ID) Option {
return func(o *options) error {
o.clusterPeerID = id
return nil
}
}
// WithRequestSigning enables signed request headers on outgoing HTTP requests.
// The private key is used to sign requests so the receiving go-fula can verify the caller.
func WithRequestSigning(key crypto.PrivKey) Option {
return func(o *options) error {
o.signingKey = key
return nil
}
}
// WithClientProtocolID sets the libp2p protocol ID used when dialing through kubo p2p.
func WithClientProtocolID(pid string) Option {
return func(o *options) error {
o.clientProtocolID = pid
return nil
}
}