-
Notifications
You must be signed in to change notification settings - Fork 402
/
referralmanager.go
95 lines (80 loc) · 2.81 KB
/
referralmanager.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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information
package testplanet
import (
"context"
"os"
"path/filepath"
"storj.io/common/pb"
"storj.io/common/peertls/extensions"
"storj.io/common/peertls/tlsopts"
"storj.io/common/testrand"
"storj.io/storj/pkg/server"
)
// DefaultReferralManagerServer implements the default behavior of a mock referral manager
type DefaultReferralManagerServer struct {
tokenCount int
}
// newReferralManager initializes a referral manager server
func (planet *Planet) newReferralManager() (*server.Server, error) {
prefix := "referralmanager"
log := planet.log.Named(prefix)
referralmanagerDir := filepath.Join(planet.directory, prefix)
if err := os.MkdirAll(referralmanagerDir, 0700); err != nil {
return nil, err
}
identity, err := planet.NewIdentity()
if err != nil {
return nil, err
}
config := server.Config{
Address: "127.0.0.1:0",
PrivateAddress: "127.0.0.1:0",
Config: tlsopts.Config{
RevocationDBURL: "bolt://" + filepath.Join(referralmanagerDir, "revocation.db"),
UsePeerCAWhitelist: true,
PeerIDVersions: "*",
Extensions: extensions.Config{
Revocation: false,
WhitelistSignedLeaf: false,
},
},
}
var endpoints pb.ReferralManagerServer
// only create a referral manager server if testplanet was reconfigured with a custom referral manager endpoint
if planet.config.Reconfigure.ReferralManagerServer != nil {
endpoints = planet.config.Reconfigure.ReferralManagerServer(log)
} else {
return nil, nil
}
tlsOptions, err := tlsopts.NewOptions(identity, config.Config, nil)
if err != nil {
return nil, err
}
referralmanager, err := server.New(log, tlsOptions, config.Address, config.PrivateAddress, nil)
if err != nil {
return nil, err
}
pb.DRPCRegisterReferralManager(referralmanager.DRPC(), endpoints)
log.Debug("id=" + identity.ID.String() + " addr=" + referralmanager.Addr().String())
return referralmanager, nil
}
// GetTokens implements a mock GetTokens endpoint that returns a number of referral tokens. By default, it returns 0 tokens.
func (server *DefaultReferralManagerServer) GetTokens(ctx context.Context, req *pb.GetTokensRequest) (*pb.GetTokensResponse, error) {
tokens := make([][]byte, server.tokenCount)
for i := 0; i < server.tokenCount; i++ {
uuid := testrand.UUID()
tokens[i] = uuid[:]
}
return &pb.GetTokensResponse{
TokenSecrets: tokens,
}, nil
}
// RedeemToken implements a mock RedeemToken endpoint.
func (server *DefaultReferralManagerServer) RedeemToken(ctx context.Context, req *pb.RedeemTokenRequest) (*pb.RedeemTokenResponse, error) {
return &pb.RedeemTokenResponse{}, nil
}
// SetTokenCount sets the number of tokens GetTokens endpoint should return.
func (server *DefaultReferralManagerServer) SetTokenCount(tokenCount int) {
server.tokenCount = tokenCount
}