/
wallet_upkeep_background.go
104 lines (84 loc) · 2.91 KB
/
wallet_upkeep_background.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
// Copyright 2018 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
// WalletUpkeepBackground keeps the wallet bundle encrypted for the latest puk.
package engine
import (
"sync"
"time"
"github.com/keybase/client/go/libkb"
)
var WalletUpkeepBackgroundSettings = BackgroundTaskSettings{
Start: 40 * time.Second, // Wait after starting the app.
StartStagger: 20 * time.Second, // Wait an additional random amount.
WakeUp: 20 * time.Second, // Additional delay after waking from sleep.
Interval: 24 * time.Hour, // Wait between checks
Limit: 10 * time.Minute, // Time limit on each round
}
// WalletUpkeepBackground is an engine.
type WalletUpkeepBackground struct {
libkb.Contextified
sync.Mutex
args *WalletUpkeepBackgroundArgs
task *BackgroundTask
}
type WalletUpkeepBackgroundArgs struct {
// Channels used for testing. Normally nil.
testingMetaCh chan<- string
testingRoundResCh chan<- error
}
// NewWalletUpkeepBackground creates a WalletUpkeepBackground engine.
func NewWalletUpkeepBackground(g *libkb.GlobalContext, args *WalletUpkeepBackgroundArgs) *WalletUpkeepBackground {
task := NewBackgroundTask(g, &BackgroundTaskArgs{
Name: "WalletUpkeepBackground",
F: WalletUpkeepBackgroundRound,
Settings: WalletUpkeepBackgroundSettings,
testingMetaCh: args.testingMetaCh,
testingRoundResCh: args.testingRoundResCh,
})
return &WalletUpkeepBackground{
Contextified: libkb.NewContextified(g),
args: args,
// Install the task early so that Shutdown can be called before RunEngine.
task: task,
}
}
// Name is the unique engine name.
func (e *WalletUpkeepBackground) Name() string {
return "WalletUpkeepBackground"
}
// GetPrereqs returns the engine prereqs.
func (e *WalletUpkeepBackground) Prereqs() Prereqs {
return Prereqs{}
}
// RequiredUIs returns the required UIs.
func (e *WalletUpkeepBackground) RequiredUIs() []libkb.UIKind {
return []libkb.UIKind{}
}
// SubConsumers returns the other UI consumers for this engine.
func (e *WalletUpkeepBackground) SubConsumers() []libkb.UIConsumer {
return []libkb.UIConsumer{}
}
// Run starts the engine.
// Returns immediately, kicks off a background goroutine.
func (e *WalletUpkeepBackground) Run(m libkb.MetaContext) (err error) {
return RunEngine2(m, e.task)
}
func (e *WalletUpkeepBackground) Shutdown() {
e.task.Shutdown()
}
func WalletUpkeepBackgroundRound(m libkb.MetaContext) error {
g := m.G()
if g.ConnectivityMonitor.IsConnected(m.Ctx()) == libkb.ConnectivityMonitorNo {
m.CDebugf("WalletUpkeepBackgroundRound giving up offline")
return nil
}
if !g.ActiveDevice.Valid() {
m.CDebugf("WalletUpkeepBackgroundRound not logged in")
return nil
}
if !g.LocalSigchainGuard().IsAvailable(m.Ctx(), "WalletUpkeepBackgroundRound") {
m.CDebugf("WalletUpkeepBackgroundRound yielding to guard")
return nil
}
return g.GetStellar().Upkeep(m.Ctx())
}