-
Notifications
You must be signed in to change notification settings - Fork 2
/
wallet_test.go
380 lines (324 loc) · 11.8 KB
/
wallet_test.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package wallet_test
import (
"errors"
"fmt"
"testing"
"go.sia.tech/core/types"
"go.sia.tech/coreutils/chain"
"go.sia.tech/coreutils/testutil"
"go.sia.tech/coreutils/wallet"
"go.uber.org/zap/zaptest"
)
// check balance is a helper function that compares the wallet's balance to
// the expected values.
func checkBalance(w *wallet.SingleAddressWallet, spendable, confirmed, immature, unconfirmed types.Currency) error {
balance, err := w.Balance()
if err != nil {
return fmt.Errorf("failed to get balance: %w", err)
} else if !balance.Confirmed.Equals(confirmed) {
return fmt.Errorf("expected %v confirmed balance, got %v", confirmed, balance.Confirmed)
} else if !balance.Spendable.Equals(spendable) {
return fmt.Errorf("expected %v spendable balance, got %v", spendable, balance.Spendable)
} else if !balance.Unconfirmed.Equals(unconfirmed) {
return fmt.Errorf("expected %v unconfirmed balance, got %v", unconfirmed, balance.Unconfirmed)
} else if !balance.Immature.Equals(immature) {
return fmt.Errorf("expected %v immature balance, got %v", immature, balance.Immature)
}
return nil
}
func TestWallet(t *testing.T) {
log := zaptest.NewLogger(t)
network, genesis := testutil.Network()
cs, tipState, err := chain.NewDBStore(chain.NewMemDB(), network, genesis)
if err != nil {
t.Fatal(err)
}
cm := chain.NewManager(cs, tipState)
pk := types.GeneratePrivateKey()
ws := testutil.NewEphemeralWalletStore(pk)
if err := cm.AddSubscriber(ws, types.ChainIndex{}); err != nil {
t.Fatal(err)
}
w, err := wallet.NewSingleAddressWallet(pk, cm, ws, wallet.WithLogger(log.Named("wallet")))
if err != nil {
t.Fatal(err)
}
defer w.Close()
if err := checkBalance(w, types.ZeroCurrency, types.ZeroCurrency, types.ZeroCurrency, types.ZeroCurrency); err != nil {
t.Fatal(err)
}
initialReward := cm.TipState().BlockReward()
// mine a block to fund the wallet
b := testutil.MineBlock(cm, w.Address())
if err := cm.AddBlocks([]types.Block{b}); err != nil {
t.Fatal(err)
}
maturityHeight := cm.TipState().MaturityHeight()
// check that the wallet has a single event
if events, err := w.Events(0, 100); err != nil {
t.Fatal(err)
} else if len(events) != 1 {
t.Fatalf("expected 1 event, got %v", len(events))
} else if events[0].Source != wallet.EventSourceMinerPayout {
t.Fatalf("expected miner payout, got %v", events[0].Source)
} else if events[0].MaturityHeight != maturityHeight {
t.Fatalf("expected maturity height %v, got %v", maturityHeight, events[0].MaturityHeight)
}
// check that the wallet has an immature balance
if err := checkBalance(w, types.ZeroCurrency, types.ZeroCurrency, initialReward, types.ZeroCurrency); err != nil {
t.Fatal(err)
}
// create a transaction that splits the wallet's balance into 20 outputs
txn := types.Transaction{
SiacoinOutputs: make([]types.SiacoinOutput, 20),
}
for i := range txn.SiacoinOutputs {
txn.SiacoinOutputs[i] = types.SiacoinOutput{
Value: initialReward.Div64(20),
Address: w.Address(),
}
}
// try funding the transaction, expect it to fail since the outputs are immature
_, err = w.FundTransaction(&txn, initialReward, false)
if err != wallet.ErrNotEnoughFunds {
t.Fatal("expected ErrNotEnoughFunds, got", err)
}
// mine until the payout matures
tip := cm.TipState()
target := tip.MaturityHeight() + 1
for i := tip.Index.Height; i < target; i++ {
b := testutil.MineBlock(cm, types.VoidAddress)
if err := cm.AddBlocks([]types.Block{b}); err != nil {
t.Fatal(err)
}
}
// check that one payout has matured
if err := checkBalance(w, initialReward, initialReward, types.ZeroCurrency, types.ZeroCurrency); err != nil {
t.Fatal(err)
}
// check that the wallet has a single event
count, err := w.EventCount()
if err != nil {
t.Fatal(err)
} else if count != 1 {
t.Fatalf("expected 1 transaction, got %v", count)
}
// check that the payout transaction was created
events, err := w.Events(0, 100)
if err != nil {
t.Fatal(err)
} else if len(events) != 1 {
t.Fatalf("expected 1 transaction, got %v", len(events))
} else if events[0].Source != wallet.EventSourceMinerPayout {
t.Fatalf("expected miner payout, got %v", events[0].Source)
}
// fund and sign the transaction
toSign, err := w.FundTransaction(&txn, initialReward, false)
if err != nil {
t.Fatal(err)
}
w.SignTransaction(&txn, toSign, types.CoveredFields{WholeTransaction: true})
// check that wallet now has no spendable balance
if err := checkBalance(w, types.ZeroCurrency, initialReward, types.ZeroCurrency, types.ZeroCurrency); err != nil {
t.Fatal(err)
}
// check the wallet has no unconfirmed transactions
poolTxns, err := w.UnconfirmedTransactions()
if err != nil {
t.Fatal(err)
} else if len(poolTxns) != 0 {
t.Fatalf("expected 0 unconfirmed transaction, got %v", len(poolTxns))
}
// add the transaction to the pool
if _, err := cm.AddPoolTransactions([]types.Transaction{txn}); err != nil {
t.Fatal(err)
}
// check that the wallet has one unconfirmed transaction
poolTxns, err = w.UnconfirmedTransactions()
if err != nil {
t.Fatal(err)
} else if len(poolTxns) != 1 {
t.Fatalf("expected 1 unconfirmed transaction, got %v", len(poolTxns))
} else if poolTxns[0].Transaction.ID() != txn.ID() {
t.Fatalf("expected transaction %v, got %v", txn.ID(), poolTxns[0].Transaction.ID())
} else if poolTxns[0].Source != wallet.EventSourceTransaction {
t.Fatalf("expected wallet source, got %v", poolTxns[0].Source)
} else if !poolTxns[0].Inflow.Equals(initialReward) {
t.Fatalf("expected %v inflow, got %v", initialReward, poolTxns[0].Inflow)
} else if !poolTxns[0].Outflow.Equals(initialReward) {
t.Fatalf("expected %v outflow, got %v", types.ZeroCurrency, poolTxns[0].Outflow)
}
// check that the wallet now has an unconfirmed balance
// note: the wallet should still have a "confirmed" balance since the pool
// transaction is not yet confirmed.
if err := checkBalance(w, types.ZeroCurrency, initialReward, types.ZeroCurrency, initialReward); err != nil {
t.Fatal(err)
}
// mine a block to confirm the transaction
b = testutil.MineBlock(cm, types.VoidAddress)
if err := cm.AddBlocks([]types.Block{b}); err != nil {
t.Fatal(err)
}
// check that the balance was confirmed and the other values reset
if err := checkBalance(w, initialReward, initialReward, types.ZeroCurrency, types.ZeroCurrency); err != nil {
t.Fatal(err)
}
// check that the wallet has two events
count, err = w.EventCount()
if err != nil {
t.Fatal(err)
} else if count != 2 {
t.Fatalf("expected 2 transactions, got %v", count)
}
// check that the paginated transactions are in the proper order
events, err = w.Events(0, 100)
if err != nil {
t.Fatal(err)
} else if len(events) != 2 {
t.Fatalf("expected 2 transactions, got %v", len(events))
} else if events[0].ID != types.Hash256(txn.ID()) {
t.Fatalf("expected transaction %v, got %v", txn.ID(), events[1].ID)
} else if len(events[0].Transaction.SiacoinOutputs) != 20 {
t.Fatalf("expected 20 outputs, got %v", len(events[1].Transaction.SiacoinOutputs))
}
// send all the outputs to the burn address individually
sent := make([]types.Transaction, 20)
sendAmount := initialReward.Div64(20)
for i := range sent {
sent[i].SiacoinOutputs = []types.SiacoinOutput{
{Address: types.VoidAddress, Value: sendAmount},
}
toSign, err := w.FundTransaction(&sent[i], sendAmount, false)
if err != nil {
t.Fatal(err)
}
w.SignTransaction(&sent[i], toSign, types.CoveredFields{WholeTransaction: true})
}
// add the transactions to the pool
if _, err := cm.AddPoolTransactions(sent); err != nil {
t.Fatal(err)
}
b = testutil.MineBlock(cm, types.VoidAddress)
if err := cm.AddBlocks([]types.Block{b}); err != nil {
t.Fatal(err)
}
// check that the wallet now has 22 transactions, the initial payout
// transaction, the split transaction, and 20 void transactions
count, err = w.EventCount()
if err != nil {
t.Fatal(err)
} else if count != 22 {
t.Fatalf("expected 22 transactions, got %v", count)
}
// check that all the wallet balances have reset
if err := checkBalance(w, types.ZeroCurrency, types.ZeroCurrency, types.ZeroCurrency, types.ZeroCurrency); err != nil {
t.Fatal(err)
}
// check that the paginated transactions are in the proper order
events, err = w.Events(0, 20) // limit of 20 so the original two transactions are not included
if err != nil {
t.Fatal(err)
} else if len(events) != 20 {
t.Fatalf("expected 20 transactions, got %v", len(events))
}
for i := range sent {
// events should be chronologically ordered, reverse the order they
// were added to the transaction pool
j := len(events) - i - 1
if events[j].ID != types.Hash256(sent[i].ID()) {
t.Fatalf("expected transaction %v, got %v", sent[i].ID(), events[i].ID)
}
}
}
func TestWalletUnconfirmed(t *testing.T) {
log := zaptest.NewLogger(t)
network, genesis := testutil.Network()
cs, tipState, err := chain.NewDBStore(chain.NewMemDB(), network, genesis)
if err != nil {
t.Fatal(err)
}
cm := chain.NewManager(cs, tipState)
pk := types.GeneratePrivateKey()
ws := testutil.NewEphemeralWalletStore(pk)
if err := cm.AddSubscriber(ws, types.ChainIndex{}); err != nil {
t.Fatal(err)
}
w, err := wallet.NewSingleAddressWallet(pk, cm, ws, wallet.WithLogger(log.Named("wallet")))
if err != nil {
t.Fatal(err)
}
defer w.Close()
// check that the wallet has no balance
if err := checkBalance(w, types.ZeroCurrency, types.ZeroCurrency, types.ZeroCurrency, types.ZeroCurrency); err != nil {
t.Fatal(err)
}
initialReward := cm.TipState().BlockReward()
// mine a block to fund the wallet
b := testutil.MineBlock(cm, w.Address())
if err := cm.AddBlocks([]types.Block{b}); err != nil {
t.Fatal(err)
}
// check that the wallet has an immature balance
if err := checkBalance(w, types.ZeroCurrency, types.ZeroCurrency, initialReward, types.ZeroCurrency); err != nil {
t.Fatal(err)
}
// mine until the payout matures
tip := cm.TipState()
target := tip.MaturityHeight() + 1
for i := tip.Index.Height; i < target; i++ {
b := testutil.MineBlock(cm, types.VoidAddress)
if err := cm.AddBlocks([]types.Block{b}); err != nil {
t.Fatal(err)
}
}
// check that one payout has matured
if err := checkBalance(w, initialReward, initialReward, types.ZeroCurrency, types.ZeroCurrency); err != nil {
t.Fatal(err)
}
// fund and sign a transaction sending half the balance to the burn address
txn := types.Transaction{
SiacoinOutputs: []types.SiacoinOutput{
{Address: types.VoidAddress, Value: initialReward.Div64(2)},
{Address: w.Address(), Value: initialReward.Div64(2)},
},
}
toSign, err := w.FundTransaction(&txn, initialReward, false)
if err != nil {
t.Fatal(err)
}
w.SignTransaction(&txn, toSign, types.CoveredFields{WholeTransaction: true})
// check that wallet now has no spendable balance
if err := checkBalance(w, types.ZeroCurrency, initialReward, types.ZeroCurrency, types.ZeroCurrency); err != nil {
t.Fatal(err)
}
// add the transaction to the pool
if _, err := cm.AddPoolTransactions([]types.Transaction{txn}); err != nil {
t.Fatal(err)
}
// check that the wallet has one unconfirmed transaction
poolTxns, err := w.UnconfirmedTransactions()
if err != nil {
t.Fatal(err)
} else if len(poolTxns) != 1 {
t.Fatal("expected 1 unconfirmed transaction")
}
txn2 := types.Transaction{
SiacoinOutputs: []types.SiacoinOutput{
{Address: types.VoidAddress, Value: initialReward.Div64(2)},
},
}
// try to send a new transaction without using the unconfirmed output
_, err = w.FundTransaction(&txn2, initialReward.Div64(2), false)
if !errors.Is(err, wallet.ErrNotEnoughFunds) {
t.Fatalf("expected funding error with no usable utxos, got %v", err)
}
toSign, err = w.FundTransaction(&txn2, initialReward.Div64(2), true)
if err != nil {
t.Fatal(err)
}
w.SignTransaction(&txn2, toSign, types.CoveredFields{WholeTransaction: true})
// broadcast the transaction
if _, err := cm.AddPoolTransactions([]types.Transaction{txn, txn2}); err != nil {
t.Fatal(err)
}
}