-
Notifications
You must be signed in to change notification settings - Fork 10
/
group.go
283 lines (251 loc) 路 7.07 KB
/
group.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package mtg
import (
"context"
"encoding/hex"
"fmt"
"sort"
"strings"
"time"
"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/crypto"
"github.com/MixinNetwork/mixin/logger"
"github.com/fox-one/mixin-sdk-go"
)
const (
groupGenesisId = "group-genesis-id"
)
type Group struct {
mixin *mixin.Client
store Store
workers []Worker
grouper func(*Output) string
groupSize int
clock *Clock
id string
members []string
epoch time.Time
threshold int
pin string
}
func BuildGroup(ctx context.Context, store Store, conf *Configuration) (*Group, error) {
if cg := conf.Genesis; len(cg.Members) < cg.Threshold || cg.Threshold < 1 {
return nil, fmt.Errorf("invalid group threshold %d %d", len(cg.Members), cg.Threshold)
}
if !strings.Contains(strings.Join(conf.Genesis.Members, ","), conf.App.ClientId) {
return nil, fmt.Errorf("app %s not belongs to the group", conf.App.ClientId)
}
s := &mixin.Keystore{
ClientID: conf.App.ClientId,
SessionID: conf.App.SessionId,
PrivateKey: conf.App.PrivateKey,
PinToken: conf.App.PinToken,
}
client, err := mixin.NewFromKeystore(s)
if err != nil {
return nil, err
}
err = client.VerifyPin(ctx, conf.App.PIN)
if err != nil {
return nil, err
}
grp := &Group{
mixin: client,
store: store,
pin: conf.App.PIN,
id: generateGenesisId(conf),
groupSize: conf.GroupSize,
}
if grp.groupSize <= 0 {
grp.groupSize = OutputsBatchSize
}
clock, err := NewClock(store)
if err != nil {
return nil, err
}
grp.clock = clock
oid, err := store.ReadProperty([]byte(groupGenesisId))
if err != nil {
return nil, err
}
if len(oid) > 0 && string(oid) != grp.id {
return nil, fmt.Errorf("malformed group genesis id %s %s", string(oid), grp.id)
}
err = store.WriteProperty([]byte(groupGenesisId), []byte(grp.id))
if err != nil {
return nil, err
}
for _, id := range conf.Genesis.Members {
ts := time.Unix(0, conf.Genesis.Timestamp)
err = grp.AddNode(id, conf.Genesis.Threshold, ts)
if err != nil {
return nil, err
}
}
members, threshold, epoch, err := grp.ListActiveNodes()
if err != nil {
return nil, err
}
grp.members = members
grp.threshold = threshold
grp.epoch = epoch
return grp, nil
}
func (grp *Group) SetOutputGrouper(per func(out *Output) string) {
grp.grouper = per
}
func (grp *Group) GenesisId() string {
return grp.id
}
func (grp *Group) GetMembers() []string {
return grp.members
}
func (grp *Group) GetThreshold() int {
return grp.threshold
}
func (grp *Group) AddWorker(wkr Worker) {
grp.workers = append(grp.workers, wkr)
}
func (grp *Group) Run(ctx context.Context) {
logger.Printf("Group(%s, %d, %s).Run(v0.2.2)\n", mixin.HashMembers(grp.members), grp.threshold, grp.GenesisId())
filter := make(map[string]bool)
for {
// drain all the utxos in the order of created time
grp.drainOutputsFromNetwork(ctx, filter, 500, "created")
grp.drainOutputsFromNetwork(ctx, filter, 500, "updated")
// handle the utxos queue by created time
grp.handleActionsQueue(ctx)
// sing any possible transactions from BuildTransaction
grp.signTransactions(ctx)
// publish all signed transactions to the mainnet
grp.publishTransactions(ctx)
grp.signCollectibleTransactions(ctx)
grp.publishCollectibleTransactions(ctx)
}
}
func (grp *Group) ListOutputsForAsset(groupId, assetId, state string, limit int) ([]*Output, error) {
return grp.store.ListOutputsForAsset(groupId, state, assetId, limit)
}
// FIXME sign one transaction per loop, slow
func (grp *Group) signTransactions(ctx context.Context) error {
txs, err := grp.store.ListTransactions(TransactionStateInitial, 0)
if err != nil || len(txs) == 0 {
return err
}
tx := txs[0]
for _, ct := range txs {
// because we rely on the updated time of outputs, then build
// transaction can result in different order, so sign the first
// signed transaction by others at first
outs, err := grp.store.ListOutputsForTransaction(ct.TraceId)
if err != nil {
return err
}
if len(outs) > 0 {
tx = ct
break
}
}
raw, err := grp.signTransaction(ctx, tx)
logger.Verbosef("Group.signTransaction(%v) => %s %v", *tx, hex.EncodeToString(raw), err)
if err != nil {
return err
}
ver, _ := common.UnmarshalVersionedTransaction(raw)
tx.Raw = raw
tx.Hash = ver.PayloadHash()
tx.UpdatedAt = grp.clock.Now()
tx.State = TransactionStateSigning
p := DecodeMixinExtra(string(ver.Extra))
if p.T.String() != tx.TraceId {
panic(hex.EncodeToString(raw))
}
if p.G != tx.GroupId {
panic(hex.EncodeToString(raw))
}
return grp.store.WriteTransaction(tx)
}
func (grp *Group) publishTransactions(ctx context.Context) error {
txs, err := grp.store.ListTransactions(TransactionStateSigned, 0)
if err != nil || len(txs) == 0 {
return err
}
for _, tx := range txs {
snapshot, err := grp.snapshotTransaction(ctx, tx.Raw)
if err != nil {
return err
} else if !snapshot {
continue
}
tx.State = TransactionStateSnapshot
err = grp.store.WriteTransaction(tx)
if err != nil {
return err
}
}
return nil
}
func (grp *Group) signCollectibleTransactions(ctx context.Context) error {
txs, err := grp.store.ListCollectibleTransactions(TransactionStateInitial, 1)
if err != nil || len(txs) != 1 {
return err
}
tx := txs[0]
raw, err := grp.signCollectibleTransaction(ctx, tx)
logger.Verbosef("Group.signCollectibleTransaction(%v) => %s %v", *tx, hex.EncodeToString(raw), err)
if err != nil {
return err
}
ver, _ := common.UnmarshalVersionedTransaction(raw)
tx.Raw = raw
tx.Hash = ver.PayloadHash()
tx.UpdatedAt = grp.clock.Now()
tx.State = TransactionStateSigning
nfm, err := DecodeNFOMemo(ver.Extra)
if err != nil {
panic(hex.EncodeToString(raw))
} else if nfm.WillMint() && nfoTraceId(ver.Extra) != tx.TraceId {
panic(hex.EncodeToString(raw))
}
return grp.store.WriteCollectibleTransaction(tx.TraceId, tx)
}
func (grp *Group) publishCollectibleTransactions(ctx context.Context) error {
txs, err := grp.store.ListCollectibleTransactions(TransactionStateSigned, 0)
if err != nil || len(txs) == 0 {
return err
}
for _, tx := range txs {
snapshot, err := grp.snapshotTransaction(ctx, tx.Raw)
if err != nil {
return err
} else if !snapshot {
continue
}
tx.State = TransactionStateSnapshot
err = grp.store.WriteCollectibleTransaction(tx.TraceId, tx)
if err != nil {
return err
}
}
return nil
}
func (grp *Group) snapshotTransaction(ctx context.Context, b []byte) (bool, error) {
raw := hex.EncodeToString(b)
h, err := grp.mixin.SendRawTransaction(ctx, raw)
logger.Verbosef("Group.snapshotTransaction(%s) => %s, %v", raw, h, err)
if err != nil {
return false, err
}
s, err := grp.mixin.GetRawTransaction(ctx, *h)
if err != nil {
return false, err
}
return s.Snapshot != nil && s.Snapshot.HasValue(), nil
}
func generateGenesisId(conf *Configuration) string {
sort.Slice(conf.Genesis.Members, func(i, j int) bool {
return conf.Genesis.Members[i] < conf.Genesis.Members[j]
})
id := strings.Join(conf.Genesis.Members, "")
id = fmt.Sprintf("%s:%d:%d", id, conf.Genesis.Threshold, conf.Genesis.Timestamp)
return crypto.NewHash([]byte(id)).String()
}