Skip to content

Commit dc69972

Browse files
authored
Desktop-only: only commit to coinflips when awake+unlocked (keybase#16646)
* rpc for desktop power status * rename MobileAppState * DealersHelper.ShouldCommit * desktop only commits to flips when awake+unlocked * log * compile protocol * fix
1 parent 82f80fc commit dc69972

37 files changed

Lines changed: 228 additions & 155 deletions

‎go/avatars/fullcaching.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ func (c *FullCachingSource) isStale(m libkb.MetaContext, item lru.DiskLRUEntry)
109109

110110
func (c *FullCachingSource) monitorAppState(m libkb.MetaContext) {
111111
c.debug(m, "monitorAppState: starting up")
112-
state := keybase1.AppState_FOREGROUND
112+
state := keybase1.MobileAppState_FOREGROUND
113113
for {
114-
state = <-m.G().AppState.NextUpdate(&state)
114+
state = <-m.G().MobileAppState.NextUpdate(&state)
115115
switch state {
116-
case keybase1.AppState_BACKGROUND:
116+
case keybase1.MobileAppState_BACKGROUND:
117117
c.debug(m, "monitorAppState: backgrounded")
118118
c.diskLRU.Flush(m.Ctx(), m.G())
119119
}

‎go/avatars/urlcaching.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ func (c *URLCachingSource) isStale(m libkb.MetaContext, item lru.DiskLRUEntry) b
5050

5151
func (c *URLCachingSource) monitorAppState(m libkb.MetaContext) {
5252
c.debug(m, "monitorAppState: starting up")
53-
state := keybase1.AppState_FOREGROUND
53+
state := keybase1.MobileAppState_FOREGROUND
5454
for {
55-
state = <-m.G().AppState.NextUpdate(&state)
55+
state = <-m.G().MobileAppState.NextUpdate(&state)
5656
switch state {
57-
case keybase1.AppState_BACKGROUND:
57+
case keybase1.MobileAppState_BACKGROUND:
5858
c.debug(m, "monitorAppState: backgrounded")
5959
c.diskLRU.Flush(m.Ctx(), m.G())
6060
}

‎go/bind/keybase.go‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -365,28 +365,28 @@ func SetAppStateForeground() {
365365
return
366366
}
367367
defer kbCtx.Trace("SetAppStateForeground", func() error { return nil })()
368-
kbCtx.AppState.Update(keybase1.AppState_FOREGROUND)
368+
kbCtx.MobileAppState.Update(keybase1.MobileAppState_FOREGROUND)
369369
}
370370
func SetAppStateBackground() {
371371
if !isInited() {
372372
return
373373
}
374374
defer kbCtx.Trace("SetAppStateBackground", func() error { return nil })()
375-
kbCtx.AppState.Update(keybase1.AppState_BACKGROUND)
375+
kbCtx.MobileAppState.Update(keybase1.MobileAppState_BACKGROUND)
376376
}
377377
func SetAppStateInactive() {
378378
if !isInited() {
379379
return
380380
}
381381
defer kbCtx.Trace("SetAppStateInactive", func() error { return nil })()
382-
kbCtx.AppState.Update(keybase1.AppState_INACTIVE)
382+
kbCtx.MobileAppState.Update(keybase1.MobileAppState_INACTIVE)
383383
}
384384
func SetAppStateBackgroundActive() {
385385
if !isInited() {
386386
return
387387
}
388388
defer kbCtx.Trace("SetAppStateBackgroundActive", func() error { return nil })()
389-
kbCtx.AppState.Update(keybase1.AppState_BACKGROUNDACTIVE)
389+
kbCtx.MobileAppState.Update(keybase1.MobileAppState_BACKGROUNDACTIVE)
390390
}
391391

392392
func waitForInit(maxDur time.Duration) error {
@@ -415,23 +415,23 @@ func BackgroundSync() {
415415
defer kbCtx.Trace("BackgroundSync", func() error { return nil })()
416416

417417
// Skip the sync if we aren't in the background
418-
if state := kbCtx.AppState.State(); state != keybase1.AppState_BACKGROUND {
418+
if state := kbCtx.MobileAppState.State(); state != keybase1.MobileAppState_BACKGROUND {
419419
kbCtx.Log.Debug("BackgroundSync: skipping, app not in background state: %v", state)
420420
return
421421
}
422422

423-
nextState := keybase1.AppState_BACKGROUNDACTIVE
424-
kbCtx.AppState.Update(nextState)
423+
nextState := keybase1.MobileAppState_BACKGROUNDACTIVE
424+
kbCtx.MobileAppState.Update(nextState)
425425
doneCh := make(chan struct{})
426426
go func() {
427427
defer func() { close(doneCh) }()
428428
select {
429-
case state := <-kbCtx.AppState.NextUpdate(&nextState):
429+
case state := <-kbCtx.MobileAppState.NextUpdate(&nextState):
430430
// if literally anything happens, let's get out of here
431431
kbCtx.Log.Debug("BackgroundSync: bailing out early, appstate change: %v", state)
432432
return
433433
case <-time.After(10 * time.Second):
434-
kbCtx.AppState.Update(keybase1.AppState_BACKGROUND)
434+
kbCtx.MobileAppState.Update(keybase1.MobileAppState_BACKGROUND)
435435
return
436436
}
437437
}()
@@ -513,7 +513,7 @@ func AppWillExit(pusher PushNotifier) {
513513
// stuck
514514
pushPendingMessageFailure(convs[0], pusher)
515515
}
516-
kbCtx.AppState.Update(keybase1.AppState_BACKGROUND)
516+
kbCtx.MobileAppState.Update(keybase1.MobileAppState_BACKGROUND)
517517
}
518518

519519
// AppDidEnterBackground notifies the service that the app is in the background
@@ -540,7 +540,7 @@ func AppDidEnterBackground() bool {
540540
}
541541
if stayRunning {
542542
kbCtx.Log.Debug("AppDidEnterBackground: setting background active")
543-
kbCtx.AppState.Update(keybase1.AppState_BACKGROUNDACTIVE)
543+
kbCtx.MobileAppState.Update(keybase1.MobileAppState_BACKGROUNDACTIVE)
544544
return true
545545
}
546546
SetAppStateBackground()
@@ -566,16 +566,16 @@ func AppBeginBackgroundTask(pusher PushNotifier) {
566566
// Poll active deliveries in case we can shutdown early
567567
beginTime := libkb.ForceWallClock(time.Now())
568568
ticker := time.NewTicker(5 * time.Second)
569-
appState := kbCtx.AppState.State()
570-
if appState != keybase1.AppState_BACKGROUNDACTIVE {
569+
appState := kbCtx.MobileAppState.State()
570+
if appState != keybase1.MobileAppState_BACKGROUNDACTIVE {
571571
kbCtx.Log.Debug("AppBeginBackgroundTask: not in background mode, early out")
572572
return
573573
}
574574
var g *errgroup.Group
575575
g, ctx = errgroup.WithContext(ctx)
576576
g.Go(func() error {
577577
select {
578-
case appState = <-kbCtx.AppState.NextUpdate(&appState):
578+
case appState = <-kbCtx.MobileAppState.NextUpdate(&appState):
579579
kbCtx.Log.Debug(
580580
"AppBeginBackgroundTask: app state change, aborting with no task shutdown: %v", appState)
581581
return errors.New("app state change")

‎go/chat/attachment_httpsrv.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ func (r *AttachmentHTTPSrv) OnCacheCleared(mctx libkb.MetaContext) {
9292
func (r *AttachmentHTTPSrv) monitorAppState() {
9393
ctx := context.Background()
9494
r.Debug(ctx, "monitorAppState: starting up")
95-
state := keybase1.AppState_FOREGROUND
95+
state := keybase1.MobileAppState_FOREGROUND
9696
for {
97-
state = <-r.G().AppState.NextUpdate(&state)
97+
state = <-r.G().MobileAppState.NextUpdate(&state)
9898
switch state {
99-
case keybase1.AppState_FOREGROUND:
99+
case keybase1.MobileAppState_FOREGROUND:
100100
r.startHTTPSrv()
101-
case keybase1.AppState_BACKGROUND, keybase1.AppState_INACTIVE:
101+
case keybase1.MobileAppState_BACKGROUND, keybase1.MobileAppState_INACTIVE:
102102
r.httpSrv.Stop()
103103
}
104104
}

‎go/chat/convloader.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,19 +160,19 @@ func (b *BackgroundConvLoader) monitorAppState() {
160160
ctx := context.Background()
161161
suspended := false
162162
b.Debug(ctx, "monitorAppState: starting up")
163-
state := keybase1.AppState_FOREGROUND
163+
state := keybase1.MobileAppState_FOREGROUND
164164
for {
165-
state = <-b.G().AppState.NextUpdate(&state)
165+
state = <-b.G().MobileAppState.NextUpdate(&state)
166166
switch state {
167-
case keybase1.AppState_FOREGROUND, keybase1.AppState_BACKGROUNDACTIVE:
167+
case keybase1.MobileAppState_FOREGROUND, keybase1.MobileAppState_BACKGROUNDACTIVE:
168168
b.Debug(ctx, "monitorAppState: active state: %v", state)
169169
// Only resume if we had suspended earlier (frontend can spam us with these)
170170
if suspended {
171171
b.Debug(ctx, "monitorAppState: resuming load thread")
172172
b.Resume(ctx)
173173
suspended = false
174174
}
175-
case keybase1.AppState_BACKGROUND:
175+
case keybase1.MobileAppState_BACKGROUND:
176176
b.Debug(ctx, "monitorAppState: backgrounded, suspending load thread")
177177
if !suspended {
178178
b.Suspend(ctx)

‎go/chat/convloader_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func TestConvLoaderAppState(t *testing.T) {
155155
require.Fail(t, "no remote call")
156156
}
157157
require.True(t, tc.Context().ConvLoader.Suspend(context.TODO()))
158-
tc.G.AppState.Update(keybase1.AppState_FOREGROUND)
158+
tc.G.MobileAppState.Update(keybase1.MobileAppState_FOREGROUND)
159159
select {
160160
case <-appStateCh:
161161
require.Fail(t, "no app state")
@@ -195,14 +195,14 @@ func TestConvLoaderAppState(t *testing.T) {
195195
case <-time.After(failDuration):
196196
require.Fail(t, "no remote call")
197197
}
198-
tc.G.AppState.Update(keybase1.AppState_BACKGROUND)
198+
tc.G.MobileAppState.Update(keybase1.MobileAppState_BACKGROUND)
199199
select {
200200
case <-appStateCh:
201201
case <-time.After(failDuration):
202202
require.Fail(t, "no app state")
203203
}
204204
tc.ChatG.ConvSource.(*HybridConversationSource).ri = ri
205-
tc.G.AppState.Update(keybase1.AppState_FOREGROUND)
205+
tc.G.MobileAppState.Update(keybase1.MobileAppState_FOREGROUND)
206206
select {
207207
case <-appStateCh:
208208
case <-time.After(failDuration):

‎go/chat/flip/chat_test.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ func (c *chatClient) SendChat(ctx context.Context, conversationID chat1.Conversa
6767
return nil
6868
}
6969

70+
func (c *chatClient) ShouldCommit(ctx context.Context) bool {
71+
return true
72+
}
73+
7074
func (s *chatServer) archive(msg GameMessageWrappedEncoded) {
7175
v := s.gameHistories[GameIDToKey(msg.GameID)]
7276
cl := s.clock

‎go/chat/flip/dealer.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ func (d *Dealer) handleMessageStart(ctx context.Context, msg *GameMessageWrapped
690690

691691
isLeader := true
692692
me := msg.Me
693-
// Make a new follower player controller if one didn't already exit (since we were
693+
// Make a new follower player controller if one didn't already exist (since we were
694694
// the Leader)
695695
if me == nil {
696696
me, err = d.newPlayerControl(d.dh.Me(), md, start)
@@ -730,7 +730,9 @@ func (d *Dealer) handleMessageStart(ctx context.Context, msg *GameMessageWrapped
730730
// with our commitment. We are now in the inner loop of the Dealer, so we
731731
// have to do this send in a Go-routine, so as not to deadlock the Dealer.
732732
if !isLeader {
733-
go d.sendCommitment(ctx, md, me)
733+
if d.dh.ShouldCommit(ctx) {
734+
go d.sendCommitment(ctx, md, me)
735+
}
734736
}
735737
return nil
736738
}

‎go/chat/flip/dealer_test.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ func (t *testDealersHelper) SendChat(ctx context.Context, conversationID chat1.C
4848
return nil
4949
}
5050

51+
func (t *testDealersHelper) ShouldCommit(ctx context.Context) bool {
52+
return true
53+
}
54+
5155
func randBytes(i int) []byte {
5256
ret := make([]byte, i)
5357
rand.Read(ret[:])

‎go/chat/flip/flip.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ type DealersHelper interface {
7272
ServerTime(context.Context) (time.Time, error)
7373
SendChat(ctx context.Context, ch chat1.ConversationID, gameID chat1.FlipGameID, msg GameMessageEncoded) error
7474
Me() UserDevice
75+
ShouldCommit(ctx context.Context) bool // Whether to send new commitments for games.
7576
}
7677

7778
// NewDealer makes a new Dealer with a given DealersHelper

0 commit comments

Comments
 (0)