-
Notifications
You must be signed in to change notification settings - Fork 154
/
discovery_test.go
102 lines (90 loc) · 3.17 KB
/
discovery_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
// Copyright (c) 2020-2023 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wallet
import (
"context"
"testing"
"decred.org/dcrwallet/v5/wallet/walletdb"
)
// TestDiscoveryCursorPos tests that the account cursor index is not reset
// during address discovery such that an address could be reused.
func TestDiscoveryCursorPos(t *testing.T) {
ctx := context.Background()
cfg := basicWalletConfig
// normally would just do the upgrade, but the buffers record
// off-by-ones after the upgrade. will be fixed in a later commit.
cfg.DisableCoinTypeUpgrades = true
w, teardown := testWallet(ctx, t, &cfg, nil)
defer teardown()
/*
// Upgrade the cointype before proceeding. The test is invalid if a
// cointype upgrade occurs during discovery.
err := w.UpgradeToSLIP0044CoinType(ctx)
if err != nil {
t.Fatal(err)
}
*/
// Advance the cursor within the gap limit but without recording the
// returned addresses in the database (these may be persisted during a
// later update).
w.addressBuffersMu.Lock()
xpub := w.addressBuffers[0].albExternal.branchXpub
w.addressBuffers[0].albExternal.cursor = 9 // 0-9 have been returned
w.addressBuffersMu.Unlock()
// Perform address discovery
// All peer funcs may be left unimplemented; wallet only records the genesis block.
network := &mockNetwork{}
err := w.DiscoverActiveAddresses(ctx, network, &w.chainParams.GenesisHash, false, w.GapLimit())
if err != nil {
t.Fatal(err)
}
w.addressBuffersMu.Lock()
lastUsed := w.addressBuffers[0].albExternal.lastUsed
cursor := w.addressBuffers[0].albExternal.cursor
w.addressBuffersMu.Unlock()
wasLastUsed := ^uint32(0)
wasCursor := uint32(9)
if lastUsed != wasLastUsed || cursor != wasCursor {
t.Errorf("cursor was reset: lastUsed=%d (want %d) cursor=%d (want %d)",
lastUsed, wasLastUsed, cursor, wasCursor)
}
// Manually mark an address between the lastUsed and cursor as used, and
// addresses through the cursor as returned, then perform discovery
// again. The cursor should be reduced such that the next returned
// address would be the same as before, without introducing a backwards
// reset or wasted addresses.
addr4, err := deriveChildAddress(xpub, 4, w.chainParams)
if err != nil {
t.Fatal(err)
}
err = walletdb.Update(ctx, w.db, func(dbtx walletdb.ReadWriteTx) error {
ns := dbtx.ReadBucket(waddrmgrNamespaceKey)
err = w.manager.MarkReturnedChildIndex(dbtx, 0, 0, 9) // 0-9 have been returned
if err != nil {
return err
}
maddr4, err := w.manager.Address(ns, addr4)
if err != nil {
return err
}
return w.markUsedAddress("", dbtx, maddr4)
})
if err != nil {
t.Fatal(err)
}
err = w.DiscoverActiveAddresses(ctx, network, &w.chainParams.GenesisHash, false, w.GapLimit())
if err != nil {
t.Fatal(err)
}
w.addressBuffersMu.Lock()
lastUsed = w.addressBuffers[0].albExternal.lastUsed
cursor = w.addressBuffers[0].albExternal.cursor
w.addressBuffersMu.Unlock()
wasLastUsed = 5
wasCursor -= 5
if lastUsed != wasLastUsed || cursor != wasCursor {
t.Errorf("cursor was reset: lastUsed=%d (want %d) cursor=%d (want %d)",
lastUsed, wasLastUsed, cursor, wasCursor)
}
}