-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel.go
336 lines (280 loc) · 8.64 KB
/
model.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package postgres
import (
"context"
"database/sql"
"fmt"
"time"
"github.com/jmoiron/sqlx"
pg "github.com/code-payments/code-server/pkg/database/postgres"
"github.com/code-payments/code-server/pkg/code/data/merkletree"
)
const (
metadataTableName = "codewallet__core_merkletreemetadata"
nodeTableName = "codewallet__core_merkletreenodes"
)
type metadataModel struct {
Id sql.NullInt64 `db:"id"`
Name string `db:"name"`
Levels int `db:"levels"`
NextIndex uint64 `db:"next_index"`
Seed []byte `db:"seed"`
CreatedAt time.Time `db:"created_at"`
}
type nodeModel struct {
Id sql.NullInt64 `db:"id"`
TreeId int64 `db:"tree_id"`
Level int `db:"level"`
Index uint64 `db:"index"`
Hash []byte `db:"hash"`
LeafValue []byte `db:"leaf_value"`
Version uint64 `db:"version"`
CreatedAt time.Time `db:"created_at"`
}
func toMetadataModel(data *merkletree.Metadata) (*metadataModel, error) {
if err := data.Validate(); err != nil {
return nil, err
}
seedCopy := make([]byte, len(data.Seed))
copy(seedCopy, data.Seed)
return &metadataModel{
Name: data.Name,
Levels: int(data.Levels),
NextIndex: data.NextIndex,
Seed: seedCopy,
CreatedAt: data.CreatedAt,
}, nil
}
func fromMetadataModel(model *metadataModel) *merkletree.Metadata {
seedCopy := make([]byte, len(model.Seed))
copy(seedCopy, model.Seed)
return &merkletree.Metadata{
Id: uint64(model.Id.Int64),
Name: model.Name,
Levels: uint8(model.Levels),
NextIndex: model.NextIndex,
Seed: seedCopy,
CreatedAt: model.CreatedAt,
}
}
func toNodeModel(data *merkletree.Node) (*nodeModel, error) {
if err := data.Validate(); err != nil {
return nil, err
}
return &nodeModel{
TreeId: int64(data.TreeId),
Level: int(data.Level),
Index: data.Index,
Hash: data.Hash,
LeafValue: data.LeafValue,
Version: data.Version,
CreatedAt: data.CreatedAt,
}, nil
}
func fromNodeModel(model *nodeModel) *merkletree.Node {
return &merkletree.Node{
Id: uint64(model.Id.Int64),
TreeId: uint64(model.TreeId),
Level: uint8(model.Level),
Index: model.Index,
Hash: model.Hash,
LeafValue: model.LeafValue,
Version: model.Version,
CreatedAt: model.CreatedAt,
}
}
func (m *metadataModel) dbCreate(ctx context.Context, db *sqlx.DB) error {
query := `INSERT INTO ` + metadataTableName + `
(name, levels, next_index, seed, created_at)
VALUES ($1, $2, $3, $4, $5)
RETURNING id, name, levels, next_index, seed, created_at
`
if m.CreatedAt.IsZero() {
m.CreatedAt = time.Now()
}
err := db.QueryRowxContext(
ctx,
query,
m.Name,
m.Levels,
m.NextIndex,
m.Seed,
m.CreatedAt,
).StructScan(m)
if err != nil {
return pg.CheckUniqueViolation(err, merkletree.ErrMetadataExists)
}
return nil
}
func (m *metadataModel) dbUpdateInTx(ctx context.Context, tx *sqlx.Tx) error {
query := `UPDATE ` + metadataTableName + `
SET next_index = $2
WHERE name = $1 AND next_index = $2 - 1
RETURNING id, name, levels, next_index, seed, created_at
`
err := tx.QueryRowxContext(ctx, query, m.Name, m.NextIndex).StructScan(m)
if err != nil {
return pg.CheckNoRows(err, merkletree.ErrInvalidMetadata)
}
return nil
}
func (m *nodeModel) dbCreateInTx(ctx context.Context, tx *sqlx.Tx) error {
query := `INSERT INTO ` + nodeTableName + `
(tree_id, level, index, hash, leaf_value, version, created_at)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id, tree_id, level, index, hash, leaf_value, version, created_at
`
if m.CreatedAt.IsZero() {
m.CreatedAt = time.Now()
}
err := tx.QueryRowxContext(
ctx,
query,
m.TreeId,
m.Level,
m.Index,
m.Hash,
m.LeafValue,
m.Version,
m.CreatedAt,
).StructScan(m)
if err != nil {
return pg.CheckUniqueViolation(err, merkletree.ErrNodeExists)
}
return nil
}
func dbGetMetadata(ctx context.Context, db *sqlx.DB, name string) (*metadataModel, error) {
var res metadataModel
query := `SELECT id, name, levels, next_index, seed, created_at FROM ` + metadataTableName + `
WHERE name = $1
`
err := db.GetContext(ctx, &res, query, name)
if err != nil {
return nil, pg.CheckNoRows(err, merkletree.ErrMetadataNotFound)
}
return &res, nil
}
func dbAddLeaf(ctx context.Context, db *sqlx.DB, mtdt *metadataModel, leaf *nodeModel, toRoot []*nodeModel) error {
return pg.ExecuteInTx(ctx, db, sql.LevelDefault, func(tx *sqlx.Tx) error {
err := mtdt.dbUpdateInTx(ctx, tx)
if err != nil {
return err
}
err = leaf.dbCreateInTx(ctx, tx)
if err != nil {
return err
}
for _, node := range toRoot {
err = node.dbCreateInTx(ctx, tx)
if err != nil {
return err
}
}
return nil
})
}
func dbGetNode(ctx context.Context, db *sqlx.DB, tree uint64, level uint8, index uint64, version uint64) (*nodeModel, error) {
var res nodeModel
query := `SELECT id, tree_id, level, index, hash, leaf_value, version, created_at FROM ` + nodeTableName + `
WHERE tree_id = $1 AND level = $2 AND index = $3 AND version = $4
`
err := db.GetContext(ctx, &res, query, tree, level, index, version)
if err != nil {
return nil, pg.CheckNoRows(err, merkletree.ErrNodeNotFound)
}
return &res, nil
}
func dbGetLatestNode(ctx context.Context, db *sqlx.DB, tree uint64, level uint8, index uint64, untilVersion uint64) (*nodeModel, error) {
var res nodeModel
query := `SELECT id, tree_id, level, index, hash, leaf_value, version, created_at FROM ` + nodeTableName + `
WHERE tree_id = $1 AND level = $2 AND index = $3 AND version <= $4
ORDER BY version DESC
LIMIT 1
`
err := db.GetContext(ctx, &res, query, tree, level, index, untilVersion)
if err != nil {
return nil, pg.CheckNoRows(err, merkletree.ErrNodeNotFound)
}
return &res, nil
}
func dbGetLatestNodesForProof(ctx context.Context, db *sqlx.DB, tree uint64, levels uint8, forLeaf, untilVersion uint64) ([]*nodeModel, error) {
var query string
var res []*nodeModel
currentLevelIndex := forLeaf
for level := uint8(0); level < levels; level++ {
var siblingIndex uint64
if currentLevelIndex%2 == 0 {
siblingIndex = currentLevelIndex + 1
} else {
siblingIndex = currentLevelIndex - 1
}
// This is the most naive approach to a query, but it saves us from making
// 63 DB calls over the network serially.
if len(query) != 0 {
query += "UNION ALL\n"
}
query += fmt.Sprintf(`(SELECT id, tree_id, level, index, hash, leaf_value, version, created_at FROM `+nodeTableName+`
WHERE tree_id = %d AND level = %d AND index = %d AND version <= %d
ORDER BY version DESC
LIMIT 1)
`, tree, level, siblingIndex, untilVersion)
currentLevelIndex = currentLevelIndex / 2
}
err := db.SelectContext(ctx, &res, query)
if err != nil {
return nil, pg.CheckNoRows(err, nil)
}
return res, nil
}
func dbGetLatestNodesForFilledSubtrees(ctx context.Context, db *sqlx.DB, tree uint64, levels uint8, untilVersion uint64) ([]*nodeModel, error) {
var query string
var res []*nodeModel
currentLevelIndex := untilVersion - 1
for level := uint8(0); level < levels; level++ {
nodeIndex := currentLevelIndex
if nodeIndex%2 != 0 {
nodeIndex = nodeIndex - 1
}
// This is the most naive approach to a query, but it saves us from making
// 63 DB calls over the network serially.
if len(query) != 0 {
query += "UNION\n"
}
query += fmt.Sprintf(`(SELECT id, tree_id, level, index, hash, leaf_value, version, created_at FROM `+nodeTableName+`
WHERE tree_id = %d AND level = %d AND index = %d AND version <= %d
ORDER BY version DESC
LIMIT 1)
`, tree, level, nodeIndex, untilVersion)
currentLevelIndex = currentLevelIndex / 2
}
err := db.SelectContext(ctx, &res, query)
if err != nil {
return nil, pg.CheckNoRows(err, nil)
}
return res, nil
}
func dbGetLeafByHash(ctx context.Context, db *sqlx.DB, tree uint64, hash merkletree.Hash) (*nodeModel, error) {
var res nodeModel
query := `SELECT id, tree_id, level, index, hash, leaf_value, version, created_at FROM ` + nodeTableName + `
WHERE tree_id = $1 AND hash = $2 AND level = 0
ORDER BY version DESC
LIMIT 1
`
err := db.GetContext(ctx, &res, query, tree, hash)
if err != nil {
return nil, pg.CheckNoRows(err, merkletree.ErrNodeNotFound)
}
return &res, nil
}
func dbGetNodeByHash(ctx context.Context, db *sqlx.DB, tree uint64, hash merkletree.Hash) (*nodeModel, error) {
var res nodeModel
query := `SELECT id, tree_id, level, index, hash, leaf_value, version, created_at FROM ` + nodeTableName + `
WHERE tree_id = $1 AND hash = $2
ORDER BY version DESC
LIMIT 1
`
err := db.GetContext(ctx, &res, query, tree, hash)
if err != nil {
return nil, pg.CheckNoRows(err, merkletree.ErrNodeNotFound)
}
return &res, nil
}