/
propagate_balance.go
196 lines (170 loc) · 4.73 KB
/
propagate_balance.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package endpoint
import (
"context"
"math/big"
"net/http"
"time"
"goji.io/pat"
"github.com/spolu/settle/lib/db"
"github.com/spolu/settle/lib/errors"
"github.com/spolu/settle/lib/format"
"github.com/spolu/settle/lib/ptr"
"github.com/spolu/settle/lib/svc"
"github.com/spolu/settle/mint"
"github.com/spolu/settle/mint/model"
)
const (
// EndPtPropagateBalance creates a new balance.
EndPtPropagateBalance EndPtName = "PropagateBalance"
)
func init() {
registrar[EndPtPropagateBalance] = NewPropagateBalance
}
// PropagateBalance fetches the balance propagated and creates a local
// propagated copy of it.
type PropagateBalance struct {
Client *mint.Client
ID string
Token string
Owner string
}
// NewPropagateBalance constructs and initialiezes the endpoint.
func NewPropagateBalance(
r *http.Request,
) (Endpoint, error) {
ctx := r.Context()
client := &mint.Client{}
err := client.Init(ctx)
if err != nil {
return nil, errors.Trace(err) // 500
}
return &PropagateBalance{
Client: client,
}, nil
}
// Validate validates the input parameters.
func (e *PropagateBalance) Validate(
r *http.Request,
) error {
ctx := r.Context()
// Validate id.
id, owner, token, err := ValidateID(ctx, pat.Param(r, "balance"))
if err != nil {
return errors.Trace(err)
}
e.ID = *id
e.Owner = *owner
e.Token = *token
return nil
}
// Execute executes the endpoint.
func (e *PropagateBalance) Execute(
ctx context.Context,
) (*int, *svc.Resp, error) {
balance, err := e.Client.RetrieveBalance(ctx, e.ID)
if err != nil {
return nil, nil, errors.Trace(errors.NewUserErrorf(err,
402, "propagation_failed",
"Failed to retrieve canonical balance: %s", e.ID,
))
}
owner, token, err := mint.NormalizedOwnerAndTokenFromID(ctx, balance.ID)
if err != nil {
return nil, nil, errors.Trace(errors.NewUserErrorf(nil,
402, "propagation_failed",
"Received invalid balance id: %s", balance.ID,
))
}
if e.ID != balance.ID {
return nil, nil, errors.Trace(errors.NewUserErrorf(nil,
402, "propagation_failed",
"Unexpected balance id: %s expected %s", balance.ID, e.ID,
))
}
if e.Owner != owner || balance.Owner != owner {
return nil, nil, errors.Trace(errors.NewUserErrorf(nil,
402, "propagation_failed",
"Unexpected balance owner: %s expected %s", owner, e.Owner,
))
}
if e.Token != token {
return nil, nil, errors.Trace(errors.NewUserErrorf(nil,
402, "propagation_failed",
"Unexpected balance token: %s expected %s", token, e.Token,
))
}
asset, err := mint.AssetResourceFromName(ctx, balance.Asset)
if err != nil {
return nil, nil, errors.Trace(errors.NewUserErrorf(nil,
402, "propagation_failed",
"Received invalid balance asset: %s", balance.Asset,
))
}
value, err := ValidateAmount(ctx, balance.Value.String())
if err != nil {
return nil, nil, errors.Trace(errors.NewUserErrorf(nil,
402, "propagation_failed",
"Received invalid balance value: %s", balance.Value.String(),
))
}
user, host, err := mint.UsernameAndMintHostFromAddress(ctx, balance.Holder)
if err != nil {
return nil, nil, errors.Trace(errors.NewUserErrorf(nil,
402, "propagation_failed",
"Received invalid balance holder: %s", balance.Holder,
))
}
if host != mint.GetHost(ctx) {
return nil, nil, errors.Trace(errors.NewUserErrorf(nil,
402, "propagation_failed",
"Received balance whose holder is not one of this mint users.",
))
}
u, err := model.LoadUserByUsername(ctx, user)
if err != nil {
return nil, nil, errors.Trace(err) // 500
} else if u == nil {
return nil, nil, errors.Trace(errors.NewUserErrorf(nil,
402, "propagation_failed",
"User impacted by balance does not exist: %s@%s",
user, mint.GetHost(ctx),
))
}
ctx = db.Begin(ctx, "mint")
defer db.LoggedRollback(ctx)
code := http.StatusCreated
bal, err := model.LoadPropagatedBalanceByOwnerToken(ctx, owner, token)
if err != nil {
return nil, nil, errors.Trace(err) // 500
} else if bal != nil {
// Only the balance value is mutable.
bal.Value = model.Amount(*value)
err := bal.Save(ctx)
if err != nil {
return nil, nil, errors.Trace(err) // 500
}
code = http.StatusOK
} else {
// Create propagated balance locally.
bal, err = model.CreatePropagatedBalance(ctx,
owner,
token,
time.Unix(0, balance.Created*mint.TimeResolutionNs),
asset.Name,
balance.Holder,
model.Amount(*value),
)
if err != nil {
return nil, nil, errors.Trace(err) // 500
}
mint.Logf(ctx,
"Propagated balance: id=%s[%s] created=%q propagation=%s "+
"asset=%s holder=%s value=%s",
bal.Owner, bal.Token, bal.Created, bal.Propagation, bal.Asset,
bal.Holder, (*big.Int)(&bal.Value).String())
}
db.Commit(ctx)
return ptr.Int(code), &svc.Resp{
"balance": format.JSONPtr(model.NewBalanceResource(ctx, bal)),
}, nil
}