-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.go
145 lines (121 loc) · 4.07 KB
/
global.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
package stellar
import (
"context"
"sync"
"github.com/keybase/client/go/gregor"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/client/go/protocol/stellar1"
"github.com/keybase/client/go/stellar/remote"
"github.com/keybase/stellarnet"
"github.com/stellar/go/build"
"github.com/stellar/go/clients/federation"
"github.com/stellar/go/clients/horizon"
)
func ServiceInit(g *libkb.GlobalContext, remoter remote.Remoter) {
if g.Env.GetRunMode() != libkb.ProductionRunMode {
stellarnet.SetClientAndNetwork(horizon.DefaultTestNetClient, build.TestNetwork)
}
g.SetStellar(NewStellar(g, remoter))
}
type Stellar struct {
libkb.Contextified
remoter remote.Remoter
serverConfLock sync.Mutex
cachedServerConf stellar1.StellarServerDefinitions
autoClaimRunnerLock sync.Mutex
autoClaimRunner *AutoClaimRunner // often nil
hasWalletCacheLock sync.Mutex
hasWalletCache map[keybase1.UserVersion]bool
federationClient federation.ClientInterface
}
var _ libkb.Stellar = (*Stellar)(nil)
func NewStellar(g *libkb.GlobalContext, remoter remote.Remoter) *Stellar {
return &Stellar{
Contextified: libkb.NewContextified(g),
remoter: remoter,
hasWalletCache: make(map[keybase1.UserVersion]bool),
federationClient: getFederationClient(g),
}
}
func (s *Stellar) CreateWalletGated(ctx context.Context) (err error) {
_, _, err = CreateWalletGated(ctx, s.G())
return err
}
func (s *Stellar) CreateWalletSoft(ctx context.Context) {
CreateWalletSoft(ctx, s.G())
}
func (s *Stellar) Upkeep(ctx context.Context) error {
return Upkeep(ctx, s.G())
}
func (s *Stellar) OnLogout() {
s.autoClaimRunnerLock.Lock()
defer s.autoClaimRunnerLock.Unlock()
// Shutdown and delete the ACR.
if acr := s.autoClaimRunner; acr != nil {
acr.Shutdown(libkb.NewMetaContextBackground(s.G()))
}
s.autoClaimRunner = nil
}
func (s *Stellar) GetServerDefinitions(ctx context.Context) (ret stellar1.StellarServerDefinitions, err error) {
if s.cachedServerConf.Revision == 0 {
s.serverConfLock.Lock()
defer s.serverConfLock.Unlock()
if s.cachedServerConf.Revision == 0 {
// check if still 0, we might have waited for other thread
// to finish fetching.
if ret, err = remote.FetchServerConfig(ctx, s.G()); err != nil {
return ret, err
}
s.cachedServerConf = ret
}
}
return s.cachedServerConf, nil
}
func (s *Stellar) KickAutoClaimRunner(mctx libkb.MetaContext, trigger gregor.MsgID) {
// Create the ACR if one does not exist.
mctx.CDebugf("KickAutoClaimRunner(trigger:%v)", trigger)
s.autoClaimRunnerLock.Lock()
defer s.autoClaimRunnerLock.Unlock()
if s.autoClaimRunner == nil {
s.autoClaimRunner = NewAutoClaimRunner(s.remoter)
}
s.autoClaimRunner.Kick(mctx, trigger)
}
func (s *Stellar) InformHasWallet(ctx context.Context, uv keybase1.UserVersion) {
if uv.Uid.IsNil() {
s.G().Log.CErrorf(ctx, "Stellar.InformHasWallet called with nil UID")
return
}
if uv.EldestSeqno <= 0 {
// It is not possible for such a user to have a wallet.
s.G().Log.CErrorf(ctx, "Stellar.InformHasWallet called with %v EldestSeqno", uv.EldestSeqno)
return
}
s.hasWalletCacheLock.Lock()
defer s.hasWalletCacheLock.Unlock()
s.G().Log.CDebugf(ctx, "Stellar.InformHasWallet(%v)", uv)
s.hasWalletCache[uv] = true
}
func (s *Stellar) CachedHasWallet(ctx context.Context, uv keybase1.UserVersion) bool {
s.hasWalletCacheLock.Lock()
defer s.hasWalletCacheLock.Unlock()
has := s.hasWalletCache[uv]
s.G().Log.CDebugf(ctx, "Stellar.CachedHasWallet(%v) -> %v", uv, has)
return has
}
func (s *Stellar) SetFederationClientForTest(cli federation.ClientInterface) {
s.federationClient = cli
}
// getFederationClient is a helper function used during
// initialization.
func getFederationClient(g *libkb.GlobalContext) federation.ClientInterface {
if g.Env.GetRunMode() != libkb.ProductionRunMode {
return federation.DefaultTestNetClient
}
return federation.DefaultPublicNetClient
}
// getGlobal gets the libkb.Stellar off of G and asserts it into a stellar.Stellar
func getGlobal(g *libkb.GlobalContext) *Stellar {
return g.GetStellar().(*Stellar)
}