/
storage_breaks.go
68 lines (56 loc) · 1.83 KB
/
storage_breaks.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
package storage
import (
"context"
"fmt"
"github.com/keybase/client/go/chat/globals"
"github.com/keybase/client/go/chat/utils"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/chat1"
"github.com/keybase/client/go/protocol/keybase1"
)
type breakTracker struct {
globals.Contextified
utils.DebugLabeler
}
func newBreakTracker(g *globals.Context) *breakTracker {
return &breakTracker{
Contextified: globals.NewContextified(g),
DebugLabeler: utils.NewDebugLabeler(g.GetLog(), "BreakTracker", false),
}
}
func (b *breakTracker) makeDbKey(tlfID chat1.TLFID) libkb.DbKey {
return libkb.DbKey{
Typ: libkb.DBChatBlocks,
Key: fmt.Sprintf("breaks:%s", tlfID),
}
}
func (b *breakTracker) UpdateTLF(ctx context.Context, tlfID chat1.TLFID,
breaks []keybase1.TLFIdentifyFailure) (err error) {
defer b.Trace(ctx, func() error { return err }, "UpdateTLF(%s)", tlfID)()
key := b.makeDbKey(tlfID)
dat, err := encode(breaks)
if err != nil {
return NewInternalError(ctx, b.DebugLabeler, "encode error: %s", err.Error())
}
if err = b.G().LocalChatDb.PutRaw(key, dat); err != nil {
return NewInternalError(ctx, b.DebugLabeler, "PutRaw error: %s", err.Error())
}
return nil
}
func (b *breakTracker) IsTLFBroken(ctx context.Context, tlfID chat1.TLFID) (res bool, err error) {
defer b.Trace(ctx, func() error { return err }, "IsTLFBroken(%s)", tlfID)()
key := b.makeDbKey(tlfID)
raw, found, err := b.G().LocalChatDb.GetRaw(key)
if err != nil {
return true, NewInternalError(ctx, b.DebugLabeler, "GetRaw error: %s", err.Error())
}
// Assume to be broken if we have no record
if !found {
return true, nil
}
var breaks []keybase1.TLFIdentifyFailure
if err = decode(raw, &breaks); err != nil {
return true, NewInternalError(ctx, b.DebugLabeler, "decode error: %s", err.Error())
}
return len(breaks) != 0, nil
}