-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel.go
209 lines (174 loc) · 5.74 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
package postgres
import (
"context"
"database/sql"
"github.com/jmoiron/sqlx"
"github.com/code-payments/code-server/pkg/code/data/nonce"
pgutil "github.com/code-payments/code-server/pkg/database/postgres"
q "github.com/code-payments/code-server/pkg/database/query"
)
const (
nonceTableName = "codewallet__core_nonce"
)
type nonceModel struct {
Id sql.NullInt64 `db:"id"`
Address string `db:"address"`
Authority string `db:"authority"`
Blockhash string `db:"blockhash"`
Purpose uint `db:"purpose"`
State uint `db:"state"`
Signature string `db:"signature"`
}
func toNonceModel(obj *nonce.Record) (*nonceModel, error) {
if err := obj.Validate(); err != nil {
return nil, err
}
return &nonceModel{
Id: sql.NullInt64{Int64: int64(obj.Id), Valid: true},
Address: obj.Address,
Authority: obj.Authority,
Blockhash: obj.Blockhash,
Purpose: uint(obj.Purpose),
State: uint(obj.State),
Signature: obj.Signature,
}, nil
}
func fromNonceModel(obj *nonceModel) *nonce.Record {
return &nonce.Record{
Id: uint64(obj.Id.Int64),
Address: obj.Address,
Authority: obj.Authority,
Blockhash: obj.Blockhash,
Purpose: nonce.Purpose(obj.Purpose),
State: nonce.State(obj.State),
Signature: obj.Signature,
}
}
func (m *nonceModel) dbSave(ctx context.Context, db *sqlx.DB) error {
return pgutil.ExecuteInTx(ctx, db, sql.LevelDefault, func(tx *sqlx.Tx) error {
query := `INSERT INTO ` + nonceTableName + `
(address, authority, blockhash, purpose, state, signature)
VALUES ($1,$2,$3,$4,$5,$6)
ON CONFLICT (address)
DO UPDATE
SET blockhash = $3, state = $5, signature = $6
WHERE ` + nonceTableName + `.address = $1
RETURNING
id, address, authority, blockhash, purpose, state, signature`
err := tx.QueryRowxContext(
ctx,
query,
m.Address,
m.Authority,
m.Blockhash,
m.Purpose,
m.State,
m.Signature,
).StructScan(m)
return pgutil.CheckNoRows(err, nonce.ErrInvalidNonce)
})
}
func dbGetCount(ctx context.Context, db *sqlx.DB) (uint64, error) {
var res uint64
query := `SELECT COUNT(*) FROM ` + nonceTableName
err := db.GetContext(ctx, &res, query)
if err != nil {
return 0, err
}
return res, nil
}
func dbGetCountByState(ctx context.Context, db *sqlx.DB, state nonce.State) (uint64, error) {
var res uint64
query := `SELECT COUNT(*) FROM ` + nonceTableName + ` WHERE state = $1`
err := db.GetContext(ctx, &res, query, state)
if err != nil {
return 0, err
}
return res, nil
}
func dbGetCountByStateAndPurpose(ctx context.Context, db *sqlx.DB, state nonce.State, purpose nonce.Purpose) (uint64, error) {
var res uint64
query := `SELECT COUNT(*) FROM ` + nonceTableName + ` WHERE state = $1 and purpose = $2`
err := db.GetContext(ctx, &res, query, state, purpose)
if err != nil {
return 0, err
}
return res, nil
}
func dbGetNonce(ctx context.Context, db *sqlx.DB, address string) (*nonceModel, error) {
res := &nonceModel{}
query := `SELECT
id, address, authority, blockhash, purpose, state, signature
FROM ` + nonceTableName + `
WHERE address = $1
`
err := db.GetContext(ctx, res, query, address)
if err != nil {
return nil, pgutil.CheckNoRows(err, nonce.ErrNonceNotFound)
}
return res, nil
}
func dbGetAllByState(ctx context.Context, db *sqlx.DB, state nonce.State, cursor q.Cursor, limit uint64, direction q.Ordering) ([]*nonceModel, error) {
res := []*nonceModel{}
// Signature null check is required because some legacy records didn't have this
// set and causes this call to fail. This is a result of the field not being
// defined at the time of record creation.
//
// todo: Fix said nonce records
query := `SELECT
id, address, authority, blockhash, purpose, state, signature
FROM ` + nonceTableName + `
WHERE (state = $1 AND signature IS NOT NULL)
`
opts := []interface{}{state}
query, opts = q.PaginateQuery(query, opts, cursor, limit, direction)
err := db.SelectContext(ctx, &res, query, opts...)
if err != nil {
return nil, pgutil.CheckNoRows(err, nonce.ErrNonceNotFound)
}
if len(res) == 0 {
return nil, nonce.ErrNonceNotFound
}
return res, nil
}
// todo: Implementation still isn't perfect, but better than no randomness. It's
// sufficiently efficient, as long as our nonce pool is larger than the max offset.
// todo: We may need to tune the offset based on pool size and environment, but it
// should be sufficiently good enough for now.
func dbGetRandomAvailableByPurpose(ctx context.Context, db *sqlx.DB, purpose nonce.Purpose) (*nonceModel, error) {
res := &nonceModel{}
// Signature null check is required because some legacy records didn't have this
// set and causes this call to fail. This is a result of the field not being
// defined at the time of record creation.
//
// todo: Fix said nonce records
query := `SELECT
id, address, authority, blockhash, purpose, state, signature
FROM ` + nonceTableName + `
WHERE state = $1 AND purpose = $2 AND signature IS NOT NULL
OFFSET FLOOR(RANDOM() * 100)
LIMIT 1
`
fallbackQuery := `SELECT
id, address, authority, blockhash, purpose, state, signature
FROM ` + nonceTableName + `
WHERE state = $1 AND purpose = $2 AND signature IS NOT NULL
LIMIT 1
`
err := db.GetContext(ctx, res, query, nonce.StateAvailable, purpose)
if err != nil {
err = pgutil.CheckNoRows(err, nonce.ErrNonceNotFound)
// No nonces found. Because our query isn't perfect, fall back to a
// strategy that will guarantee to select something if an available
// nonce exists.
if err == nonce.ErrNonceNotFound {
err := db.GetContext(ctx, res, fallbackQuery, nonce.StateAvailable, purpose)
if err != nil {
return nil, pgutil.CheckNoRows(err, nonce.ErrNonceNotFound)
}
return res, nil
}
return nil, err
}
return res, nil
}