This repository has been archived by the owner on Jun 25, 2024. It is now read-only.
forked from lightninglabs/pool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
246 lines (213 loc) · 5.64 KB
/
debug.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
package main
import (
"bytes"
"fmt"
"path"
"path/filepath"
"github.com/getvoltage/pool"
"github.com/getvoltage/pool/auctioneer"
"github.com/getvoltage/pool/clientdb"
"github.com/getvoltage/pool/order"
"github.com/getvoltage/pool/poolrpc"
"github.com/getvoltage/pool/terms"
"github.com/lightningnetwork/lnd/lncfg"
"github.com/urfave/cli"
)
var debugCommands = []cli.Command{
{
Name: "debug",
ShortName: "d",
Usage: "Debug the local trader state.",
Hidden: true,
Subcommands: []cli.Command{
dumpAccountsCommand,
dumpOrdersCommand,
dumpPendingBatcheCommand,
removePendingBatchCommand,
},
},
}
var dumpAccountsCommand = cli.Command{
Name: "dumpaccounts",
ShortName: "da",
Usage: "dump all accounts contained in the local database",
Flags: []cli.Flag{
cli.StringFlag{
Name: "db",
Usage: "the specific pool database to use instead " +
"of the default one on ~/.pool/<network>/" +
"pool.db",
},
},
Action: dumpAccounts,
}
func dumpAccounts(ctx *cli.Context) error {
db, err := getPoolDB(ctx)
if err != nil {
return fmt.Errorf("error loading DB: %v", err)
}
accounts, err := db.Accounts()
if err != nil {
return fmt.Errorf("error loading accounts: %v", err)
}
rpcAccounts := make([]*poolrpc.Account, len(accounts))
for idx, acct := range accounts {
rpcAcct, err := pool.MarshallAccount(acct)
if err != nil {
return fmt.Errorf("error marshalling account: %v", err)
}
rpcAccounts[idx] = rpcAcct
}
printRespJSON(&poolrpc.ListAccountsResponse{Accounts: rpcAccounts})
return nil
}
var dumpOrdersCommand = cli.Command{
Name: "dumporders",
ShortName: "do",
Usage: "dump all orders contained in the local database",
Flags: []cli.Flag{
cli.StringFlag{
Name: "db",
Usage: "the specific pool database to use instead " +
"of the default one on ~/.pool/<network>/" +
"pool.db",
},
},
Action: dumpOrders,
}
func dumpOrders(ctx *cli.Context) error {
db, err := getPoolDB(ctx)
if err != nil {
return fmt.Errorf("error loading DB: %v", err)
}
orders, err := db.GetOrders()
if err != nil {
return fmt.Errorf("error loading orders: %v", err)
}
asks := make([]*poolrpc.Ask, 0, len(orders))
bids := make([]*poolrpc.Bid, 0, len(orders))
for _, dbOrder := range orders {
nonce := dbOrder.Nonce()
dbDetails := dbOrder.Details()
orderState, err := pool.DBOrderStateToRPCState(dbDetails.State)
if err != nil {
return err
}
details := &poolrpc.Order{
TraderKey: dbDetails.AcctKey[:],
RateFixed: dbDetails.FixedRate,
Amt: uint64(dbDetails.Amt),
MaxBatchFeeRateSatPerKw: uint64(
dbDetails.MaxBatchFeeRate,
),
OrderNonce: nonce[:],
State: orderState,
Units: uint32(dbDetails.Units),
UnitsUnfulfilled: uint32(dbDetails.UnitsUnfulfilled),
ReservedValueSat: uint64(dbOrder.ReservedValue(
terms.NewLinearFeeSchedule(0, 0),
)),
CreationTimestampNs: uint64(0),
MinUnitsMatch: uint32(
dbOrder.Details().MinUnitsMatch,
),
}
switch o := dbOrder.(type) {
case *order.Ask:
rpcAsk := &poolrpc.Ask{
Details: details,
LeaseDurationBlocks: dbDetails.LeaseDuration,
Version: uint32(o.Version),
}
asks = append(asks, rpcAsk)
case *order.Bid:
nodeTier, err := auctioneer.MarshallNodeTier(
o.MinNodeTier,
)
if err != nil {
return err
}
rpcBid := &poolrpc.Bid{
Details: details,
LeaseDurationBlocks: dbDetails.LeaseDuration,
Version: uint32(o.Version),
MinNodeTier: nodeTier,
}
bids = append(bids, rpcBid)
default:
return fmt.Errorf("unknown order type: %v", o)
}
}
printRespJSON(&poolrpc.ListOrdersResponse{Asks: asks, Bids: bids})
return nil
}
var dumpPendingBatcheCommand = cli.Command{
Name: "dumppendingbatch",
ShortName: "dpb",
Usage: "dump the current pending batch contained in the local " +
"database",
Flags: []cli.Flag{
cli.StringFlag{
Name: "db",
Usage: "the specific pool database to use instead " +
"of the default one on ~/.pool/<network>/" +
"pool.db",
},
},
Action: dumpPendingBatch,
}
func dumpPendingBatch(ctx *cli.Context) error {
db, err := getPoolDB(ctx)
if err != nil {
return fmt.Errorf("error loading DB: %v", err)
}
snapshot, err := db.PendingBatchSnapshot()
if err != nil {
return fmt.Errorf("error getting pending batch: %v", err)
}
fmt.Printf("Batch ID:\t%x\n", snapshot.BatchID[:])
fmt.Printf("TXID:\t\t%s\n", snapshot.BatchTX.TxHash())
var buf bytes.Buffer
err = snapshot.BatchTX.Serialize(&buf)
if err != nil {
return fmt.Errorf("error serializing TX: %v", err)
}
fmt.Printf("Raw TX:\t\t%x\n", buf.Bytes())
return nil
}
var removePendingBatchCommand = cli.Command{
Name: "removependingbatch",
ShortName: "rpb",
Usage: "remove the current pending batch from the local database",
Flags: []cli.Flag{
cli.StringFlag{
Name: "db",
Usage: "the specific pool database to use instead " +
"of the default one on ~/.pool/<network>/" +
"pool.db",
},
},
Action: removePendingBatch,
}
func removePendingBatch(ctx *cli.Context) error {
db, err := getPoolDB(ctx)
if err != nil {
return fmt.Errorf("error loading DB: %v", err)
}
return db.DeletePendingBatch()
}
func getPoolDB(ctx *cli.Context) (*clientdb.DB, error) {
fullDbPath := filepath.Join(
pool.DefaultBaseDir, ctx.GlobalString("network"),
clientdb.DBFilename,
)
if ctx.IsSet("db") {
fullDbPath = lncfg.CleanAndExpandPath(ctx.String("db"))
}
db, err := clientdb.New(path.Dir(fullDbPath), path.Base(fullDbPath))
if err != nil {
return nil, fmt.Errorf("error opening DB at %v: %v", fullDbPath,
err)
}
return db, nil
}