-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdeposit.go
75 lines (68 loc) · 1.66 KB
/
deposit.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
package mtg
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/MixinNetwork/mixin/logger"
)
type DepositEntry struct {
Destination string
Tag string
}
type SafeDepositView struct {
DepositHash string `json:"deposit_hash"`
DepositIndex int64 `json:"deposit_index"`
Sender string `json:"sender"`
Destination string `json:"destination"`
Tag string `json:"tag"`
}
func (e DepositEntry) UniqueKey() string {
return fmt.Sprintf("%s:%s", e.Destination, e.Tag)
}
func (grp *Group) readOutputDepositUntilSufficient(ctx context.Context, id string) (*SafeDepositView, error) {
key := fmt.Sprintf("readOutputDepositUntilSufficient(%s)", id)
val, err := grp.store.ReadCache(ctx, key)
if err != nil {
panic(err)
}
if val != "" {
var r SafeDepositView
err = json.Unmarshal([]byte(val), &r)
if err != nil {
panic(err)
}
return &r, nil
}
r, err := grp.readOutputDepositUntilSufficientImpl(ctx, id)
if err != nil || r == nil {
return r, err
}
b, err := json.Marshal(r)
if err != nil {
panic(err)
}
err = grp.store.WriteCache(ctx, key, string(b))
if err != nil {
panic(err)
}
return r, nil
}
func (grp *Group) readOutputDepositUntilSufficientImpl(ctx context.Context, id string) (*SafeDepositView, error) {
for {
var deposit *SafeDepositView
err := grp.mixin.Get(ctx, fmt.Sprintf("/safe/outputs/%s/deposit", id), nil, &deposit)
logger.Verbosef("Group.readOutputDeposit(%s) => %v %v\n", id, deposit, err)
if err != nil {
if CheckRetryableError(err) {
time.Sleep(3 * time.Second)
continue
}
if strings.Contains(err.Error(), "not found") {
return nil, nil
}
}
return deposit, err
}
}