forked from breez/server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
215 lines (198 loc) · 6.1 KB
/
db.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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"github.com/breez/server/breez"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/google/uuid"
"github.com/jackc/pgtype"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
const (
TypeUnknown = iota
TypeBoltzReverseSwapLockup
)
const (
StatusUnknown = iota
StatusUnconfirmed
StatusNotified
)
type BoltzReverseSwapInfo struct {
ID string `json:"id"`
TimeoutBlockHeight uint32 `json:"timeout_block_height"`
}
var (
pgxPool *pgxpool.Pool
)
func pgConnect() error {
var err error
pgxPool, err = pgxpool.Connect(context.Background(), os.Getenv("DATABASE_URL"))
if err != nil {
return fmt.Errorf("pgxpool.Connect(%v): %w", os.Getenv("DATABASE_URL"), err)
}
return nil
}
func insertSubswapPayment(paymentHash, paymentRequest string) error {
commandTag, err := pgxPool.Exec(context.Background(),
`INSERT INTO swap_payments
(payment_hash, payment_request)
VALUES ($1, $2)
ON CONFLICT DO NOTHING`, paymentHash, paymentRequest)
if err != nil {
log.Printf("pgxPool.Exec('INSERT INTO swap_payments(%v, %v): %v",
paymentHash, paymentRequest, err)
return fmt.Errorf("pgxPool.Exec(): %w", err)
}
log.Printf("pgxPool.Exec('INSERT INTO swap_payments(%v, %v)'; RowsAffected(): %v'",
paymentHash, paymentRequest, commandTag.RowsAffected())
return nil
}
func updateSubswapPayment(paymentHash, paymentPreimage, TxID string) error {
commandTag, err := pgxPool.Exec(context.Background(),
`UPDATE swap_payments
SET
payment_preimage=$2,
txid=txid||$3
WHERE payment_hash=$1`, paymentHash, paymentPreimage, []string{TxID})
if err != nil {
log.Printf("pgxPool.Exec('UPDATE swap_payments(%v, %v, %v): %v",
paymentHash, paymentPreimage, TxID, err)
return fmt.Errorf("pgxPool.Exec(): %w", err)
}
log.Printf("pgxPool.Exec('UPDATE INTO swap_payments(%v, %v, %v)'; RowsAffected(): %v'",
paymentHash, paymentPreimage, TxID, commandTag.RowsAffected())
return nil
}
func insertTxNotification(in *breez.PushTxNotificationRequest) (*uuid.UUID, error) {
u, err := uuid.NewRandom()
if err != nil {
return nil, fmt.Errorf("uuid.NewRandom(): %w", err)
}
var additionalInfo []byte
var txType int32
switch x := in.Info.(type) {
case *breez.PushTxNotificationRequest_BoltzReverseSwapLockupTxInfo:
additionalInfo, _ = json.Marshal(BoltzReverseSwapInfo{
ID: x.BoltzReverseSwapLockupTxInfo.BoltzId,
TimeoutBlockHeight: x.BoltzReverseSwapLockupTxInfo.TimeoutBlockHeight,
})
txType = TypeBoltzReverseSwapLockup
default:
txType = TypeUnknown
}
commandTag, err := pgxPool.Exec(context.Background(),
`INSERT INTO tx_notifications
(id, tx_type, status, additional_info, title, body, device_id, tx_hash, script, block_height_hint)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
ON CONFLICT (device_id, script) DO NOTHING`,
pgtype.UUID{Bytes: u, Status: pgtype.Present},
txType,
StatusUnconfirmed,
additionalInfo,
in.Title,
in.Body,
in.DeviceId,
in.TxHash,
in.Script,
in.BlockHeightHint,
)
if err != nil {
log.Printf("pgxPool.Exec(): %v", err)
return nil, fmt.Errorf("pgxPool.Exec(): %w", err)
}
log.Printf("pgxPool.Exec('INSERT INTO tx_notification()'; RowsAffected(): %v'", commandTag.RowsAffected())
if commandTag.RowsAffected() == 0 {
return nil, nil
}
return &u, nil
}
func txNotified(u uuid.UUID, txHash chainhash.Hash, tx []byte, blockHeigh uint32, blockHash []byte, txIndex uint32) error {
commandTag, err := pgxPool.Exec(context.Background(),
`UPDATE tx_notifications
SET status = $2, tx_hash=$3, tx=$4, block_height=$5, block_hash=$6, tx_index=$7
WHERE id=$1`,
u, StatusNotified, txHash.CloneBytes(), tx, blockHeigh, blockHash, txIndex,
)
if err != nil {
log.Printf("pgxPool.Exec(): %v", err)
return fmt.Errorf("pgxPool.Exec(): %w", err)
}
log.Printf("pgxPool.Exec('UPDATE tx_notifications'; RowsAffected(): %v'", commandTag.RowsAffected())
return nil
}
func boltzReverseSwapToNotify(currentHeight uint32) (pgx.Rows, error) {
return pgxPool.Query(context.Background(),
`SELECT id, additional_info, title, body, device_id, tx_hash, script, block_height_hint
FROM tx_notifications tn
WHERE tn.tx_type=$1 AND tn.status=$2 AND cast(tn.additional_info->>'timeout_block_height' as int)>$3`,
TypeBoltzReverseSwapLockup, StatusUnconfirmed, currentHeight,
)
}
func breezAppVersion() (pgx.Rows, error) {
return pgxPool.Query(context.Background(),
`SELECT version FROM breez_app_versions`,
)
}
func deviceNode(nodeID []byte, deviceID string) error {
commandTag, err := pgxPool.Exec(context.Background(),
`INSERT INTO deviceid_nodeid
(nodeid, deviceid, first_registration)
VALUES ($1, $2, NOW())
ON CONFLICT (nodeid) DO UPDATE SET deviceid=$2`,
nodeID, deviceID)
if err != nil {
log.Printf("pgxPool.Exec(): %v", err)
return fmt.Errorf("pgxPool.Exec(): %w", err)
}
log.Printf("pgxPool.Exec('INSERT INTO deviceid_nodeid(%x, %v)'; RowsAffected(): %v'", nodeID, deviceID, commandTag.RowsAffected())
return nil
}
func getDeviceToken(nodeID []byte) (string, error) {
var token string
err := pgxPool.QueryRow(context.Background(),
`SELECT deviceid
FROM deviceid_nodeid
WHERE nodeid=$1`, nodeID).Scan(&token)
if err == pgx.ErrNoRows {
return "", nil
}
if err != nil {
return "", err
}
return token, nil
}
func lspList(apiKeys []string) ([]string, error) {
type void struct{}
var member void
rows, err := pgxPool.Query(context.Background(),
`SELECT lsp_ids, api_user FROM api_keys
WHERE api_key = ANY($1)`, apiKeys)
if err != nil {
log.Printf("error in pgxPool.Query: %v", err)
return nil, fmt.Errorf("error in pgxPool.Query: %w", err)
}
defer rows.Close()
lspSet := make(map[string]void)
for rows.Next() {
var lspIds []string
var apiUser string
err = rows.Scan(&lspIds, &apiUser)
if err != nil {
log.Printf("error in rows.Scan: %v", err)
continue
}
log.Printf("ids: %#v, user: %#v", lspIds, apiUser)
for _, l := range lspIds {
lspSet[l] = member
}
}
var lspList []string
for l := range lspSet {
lspList = append(lspList, l)
}
return lspList, nil
}