|
| 1 | +package db |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/btcsuite/btcd/btcutil/hdkeychain" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +// fakeHorizonOps is a minimal ScanHorizonOps used by the ExtendScanHorizon |
| 12 | +// tests. It records the inserted child indices and the final advanced |
| 13 | +// next-index so each scenario can assert the exact derivation work performed. |
| 14 | +type fakeHorizonOps struct { |
| 15 | + account *HorizonAccount |
| 16 | + |
| 17 | + inserted []uint32 |
| 18 | + advancedTo uint32 |
| 19 | + advanceCall bool |
| 20 | +} |
| 21 | + |
| 22 | +// GetHorizonAccount returns the preconfigured account. |
| 23 | +func (o *fakeHorizonOps) GetHorizonAccount(_ context.Context, |
| 24 | + _ ScanHorizon) (*HorizonAccount, error) { |
| 25 | + |
| 26 | + return o.account, nil |
| 27 | +} |
| 28 | + |
| 29 | +// InsertDerivedAddress records the child index that was derived and persisted. |
| 30 | +func (o *fakeHorizonOps) InsertDerivedAddress(_ context.Context, _ int64, |
| 31 | + _ AddressType, _ uint32, index uint32, _ []byte, _ []byte) error { |
| 32 | + |
| 33 | + o.inserted = append(o.inserted, index) |
| 34 | + |
| 35 | + return nil |
| 36 | +} |
| 37 | + |
| 38 | +// AdvanceNextIndex records the advanced next-index value. |
| 39 | +func (o *fakeHorizonOps) AdvanceNextIndex(_ context.Context, _ int64, |
| 40 | + _ uint32, nextIndex uint32) error { |
| 41 | + |
| 42 | + o.advanceCall = true |
| 43 | + o.advancedTo = nextIndex |
| 44 | + |
| 45 | + return nil |
| 46 | +} |
| 47 | + |
| 48 | +// validChildDeriveFunc returns a derivation callback that always derives a |
| 49 | +// valid address, recording nothing of the input. |
| 50 | +func validChildDeriveFunc() AddressDerivationFunc { |
| 51 | + return func(_ context.Context, |
| 52 | + _ AddressDerivationParams) (*DerivedAddressData, error) { |
| 53 | + |
| 54 | + return &DerivedAddressData{ |
| 55 | + ScriptPubKey: []byte{0x00, 0x14}, |
| 56 | + PubKey: []byte{0x02}, |
| 57 | + }, nil |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// TestExtendScanHorizonNoOpBelowNextIndex verifies that ExtendScanHorizon does |
| 62 | +// nothing when the discovered index is below the branch's current next index, |
| 63 | +// so replaying an already-covered horizon never re-derives or advances. |
| 64 | +func TestExtendScanHorizonNoOpBelowNextIndex(t *testing.T) { |
| 65 | + t.Parallel() |
| 66 | + |
| 67 | + ops := &fakeHorizonOps{ |
| 68 | + account: &HorizonAccount{ |
| 69 | + AccountID: 7, |
| 70 | + NextExternalIndex: 5, |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + // The horizon index is below the persisted next-external index, so the |
| 75 | + // call must be a no-op. |
| 76 | + err := ExtendScanHorizon(t.Context(), ops, validChildDeriveFunc(), |
| 77 | + ScanHorizon{Branch: externalBranch, Index: 3}) |
| 78 | + require.NoError(t, err) |
| 79 | + |
| 80 | + require.Empty(t, ops.inserted) |
| 81 | + require.False(t, ops.advanceCall) |
| 82 | +} |
| 83 | + |
| 84 | +// TestExtendScanHorizonRejectsInvalidBranch verifies that a branch other than |
| 85 | +// external or internal is rejected with ErrInvalidParam before any account |
| 86 | +// load or derivation. |
| 87 | +func TestExtendScanHorizonRejectsInvalidBranch(t *testing.T) { |
| 88 | + t.Parallel() |
| 89 | + |
| 90 | + ops := &fakeHorizonOps{account: &HorizonAccount{AccountID: 7}} |
| 91 | + |
| 92 | + err := ExtendScanHorizon(t.Context(), ops, validChildDeriveFunc(), |
| 93 | + ScanHorizon{Branch: 2, Index: 1}) |
| 94 | + require.ErrorIs(t, err, ErrInvalidParam) |
| 95 | + |
| 96 | + require.Empty(t, ops.inserted) |
| 97 | + require.False(t, ops.advanceCall) |
| 98 | +} |
| 99 | + |
| 100 | +// TestExtendScanHorizonRejectsMaxIndex verifies that a horizon index above |
| 101 | +// MaxAddressIndex is rejected with ErrMaxAddressIndexReached, matching the |
| 102 | +// legacy address manager's per-account child bound. |
| 103 | +func TestExtendScanHorizonRejectsMaxIndex(t *testing.T) { |
| 104 | + t.Parallel() |
| 105 | + |
| 106 | + ops := &fakeHorizonOps{account: &HorizonAccount{AccountID: 7}} |
| 107 | + |
| 108 | + err := ExtendScanHorizon(t.Context(), ops, validChildDeriveFunc(), |
| 109 | + ScanHorizon{Branch: externalBranch, Index: MaxAddressIndex + 1}) |
| 110 | + require.ErrorIs(t, err, ErrMaxAddressIndexReached) |
| 111 | + |
| 112 | + require.Empty(t, ops.inserted) |
| 113 | + require.False(t, ops.advanceCall) |
| 114 | +} |
| 115 | + |
| 116 | +// TestExtendScanHorizonSkipsInvalidChild verifies that an HD-invalid child |
| 117 | +// index is skipped without persisting a row while derivation advances to the |
| 118 | +// next valid child, mirroring the legacy extendAddresses inner loop. |
| 119 | +func TestExtendScanHorizonSkipsInvalidChild(t *testing.T) { |
| 120 | + t.Parallel() |
| 121 | + |
| 122 | + ops := &fakeHorizonOps{ |
| 123 | + account: &HorizonAccount{ |
| 124 | + AccountID: 7, |
| 125 | + NextExternalIndex: 0, |
| 126 | + }, |
| 127 | + } |
| 128 | + |
| 129 | + // Index 1 derives an ErrInvalidChild, so it must be skipped: only indices |
| 130 | + // 0 and 2 are persisted while the loop still reaches horizon index 2. |
| 131 | + deriveFn := func(_ context.Context, |
| 132 | + params AddressDerivationParams) (*DerivedAddressData, error) { |
| 133 | + |
| 134 | + if params.Index == 1 { |
| 135 | + return nil, hdkeychain.ErrInvalidChild |
| 136 | + } |
| 137 | + |
| 138 | + return &DerivedAddressData{ |
| 139 | + ScriptPubKey: []byte{0x00, 0x14}, |
| 140 | + PubKey: []byte{0x02}, |
| 141 | + }, nil |
| 142 | + } |
| 143 | + |
| 144 | + err := ExtendScanHorizon(t.Context(), ops, deriveFn, |
| 145 | + ScanHorizon{Branch: externalBranch, Index: 2}) |
| 146 | + require.NoError(t, err) |
| 147 | + |
| 148 | + // Index 1 was skipped; indices 0 and 2 were persisted. |
| 149 | + require.Equal(t, []uint32{0, 2}, ops.inserted) |
| 150 | + |
| 151 | + // The next index advanced past the last derived child. |
| 152 | + require.True(t, ops.advanceCall) |
| 153 | + require.Equal(t, uint32(3), ops.advancedTo) |
| 154 | +} |
| 155 | + |
| 156 | +// TestExtendScanHorizonAdvancesAfterInserts verifies that, after deriving the |
| 157 | +// full range, ExtendScanHorizon persists every child and advances the branch |
| 158 | +// next-index to one past the last derived child. |
| 159 | +func TestExtendScanHorizonAdvancesAfterInserts(t *testing.T) { |
| 160 | + t.Parallel() |
| 161 | + |
| 162 | + ops := &fakeHorizonOps{ |
| 163 | + account: &HorizonAccount{ |
| 164 | + AccountID: 7, |
| 165 | + NextInternalIndex: 1, |
| 166 | + AddrSchema: ScopeAddrSchema{ |
| 167 | + InternalAddrType: WitnessPubKey, |
| 168 | + ExternalAddrType: WitnessPubKey, |
| 169 | + }, |
| 170 | + }, |
| 171 | + } |
| 172 | + |
| 173 | + // Extend the internal branch from its next index 1 through index 3. |
| 174 | + err := ExtendScanHorizon(t.Context(), ops, validChildDeriveFunc(), |
| 175 | + ScanHorizon{Branch: internalBranch, Index: 3}) |
| 176 | + require.NoError(t, err) |
| 177 | + |
| 178 | + require.Equal(t, []uint32{1, 2, 3}, ops.inserted) |
| 179 | + require.True(t, ops.advanceCall) |
| 180 | + require.Equal(t, uint32(4), ops.advancedTo) |
| 181 | +} |
0 commit comments