Skip to content

Commit bab9a94

Browse files
committed
universe/supplycommit: add unit tests
1 parent 8d507b2 commit bab9a94

File tree

2 files changed

+1869
-0
lines changed

2 files changed

+1869
-0
lines changed

universe/supplycommit/mock.go

Lines changed: 381 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,381 @@
1+
package supplycommit
2+
3+
import (
4+
"context"
5+
6+
"github.com/btcsuite/btcd/btcec/v2"
7+
"github.com/btcsuite/btcd/btcutil"
8+
"github.com/btcsuite/btcd/btcutil/psbt"
9+
"github.com/btcsuite/btcd/chaincfg/chainhash"
10+
"github.com/btcsuite/btcd/wire"
11+
"github.com/lightninglabs/taproot-assets/asset"
12+
"github.com/lightninglabs/taproot-assets/mssmt"
13+
"github.com/lightninglabs/taproot-assets/proof"
14+
"github.com/lightninglabs/taproot-assets/tapsend"
15+
"github.com/lightningnetwork/lnd/chainntnfs"
16+
lfn "github.com/lightningnetwork/lnd/fn/v2"
17+
"github.com/lightningnetwork/lnd/keychain"
18+
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
19+
"github.com/lightningnetwork/lnd/lnwire"
20+
"github.com/stretchr/testify/mock"
21+
)
22+
23+
// mockSupplyTreeView is a mock implementation of the SupplyTreeView interface.
24+
type mockSupplyTreeView struct {
25+
mock.Mock
26+
}
27+
28+
func (m *mockSupplyTreeView) FetchSubTree(assetSpec asset.Specifier,
29+
treeType SupplySubTree) lfn.Result[mssmt.Tree] {
30+
31+
args := m.Called(assetSpec, treeType)
32+
return args.Get(0).(lfn.Result[mssmt.Tree])
33+
}
34+
35+
func (m *mockSupplyTreeView) FetchSubTrees(
36+
assetSpec asset.Specifier) lfn.Result[SupplyTrees] {
37+
38+
args := m.Called(assetSpec)
39+
return args.Get(0).(lfn.Result[SupplyTrees])
40+
}
41+
42+
func (m *mockSupplyTreeView) FetchRootSupplyTree(
43+
assetSpec asset.Specifier) lfn.Result[mssmt.Tree] {
44+
45+
args := m.Called(assetSpec)
46+
return args.Get(0).(lfn.Result[mssmt.Tree])
47+
}
48+
49+
// mockCommitmentTracker is a mock implementation of the CommitmentTracker
50+
// interface.
51+
type mockCommitmentTracker struct {
52+
mock.Mock
53+
}
54+
55+
func (m *mockCommitmentTracker) UnspentPrecommits(ctx context.Context,
56+
assetSpec asset.Specifier) lfn.Result[PreCommits] {
57+
58+
args := m.Called(ctx, assetSpec)
59+
return args.Get(0).(lfn.Result[PreCommits])
60+
}
61+
62+
func (m *mockCommitmentTracker) SupplyCommit(ctx context.Context,
63+
assetSpec asset.Specifier) RootCommitResp {
64+
65+
args := m.Called(ctx, assetSpec)
66+
return args.Get(0).(RootCommitResp)
67+
}
68+
69+
// fundPsbtMockFn defines a type for the mock function used in FundPsbt,
70+
// to simplify a long type assertion.
71+
type fundPsbtMockFn func(
72+
context.Context, *psbt.Packet, uint32,
73+
chainfee.SatPerKWeight, int32,
74+
) (*tapsend.FundedPsbt, error)
75+
76+
// signAndFinalizePsbtMockFn defines a type for the mock function used in
77+
// SignAndFinalizePsbt, to simplify a long type assertion.
78+
type signAndFinalizePsbtMockFn func(
79+
context.Context, *psbt.Packet,
80+
) (*psbt.Packet, error)
81+
82+
// mockWallet is a mock implementation of the Wallet interface.
83+
type mockWallet struct {
84+
mock.Mock
85+
}
86+
87+
func (m *mockWallet) FundPsbt(
88+
ctx context.Context, packet *psbt.Packet, minConfs uint32,
89+
feeRate chainfee.SatPerKWeight, changeIdx int32,
90+
) (*tapsend.FundedPsbt, error) {
91+
92+
args := m.Called(ctx, packet, minConfs, feeRate, changeIdx)
93+
94+
// Check if the first argument returned by the mock is a function.
95+
// If so, this indicates a custom mock implementation that should be
96+
// executed to get the actual return values.
97+
arg0 := args.Get(0)
98+
if fn, ok := arg0.(fundPsbtMockFn); ok {
99+
return fn(ctx, packet, minConfs, feeRate, changeIdx)
100+
}
101+
102+
if args.Get(0) == nil {
103+
return nil, args.Error(1)
104+
}
105+
return args.Get(0).(*tapsend.FundedPsbt), args.Error(1)
106+
}
107+
108+
func (m *mockWallet) SignAndFinalizePsbt(ctx context.Context,
109+
packet *psbt.Packet) (*psbt.Packet, error) {
110+
111+
args := m.Called(ctx, packet)
112+
113+
// Check if the first argument returned by the mock is a function.
114+
// If so, this indicates a custom mock implementation that should be
115+
// executed to get the actual return values.
116+
arg0 := args.Get(0)
117+
if fn, ok := arg0.(signAndFinalizePsbtMockFn); ok {
118+
return fn(ctx, packet)
119+
}
120+
121+
if args.Get(0) == nil {
122+
return nil, args.Error(1)
123+
}
124+
return args.Get(0).(*psbt.Packet), args.Error(1)
125+
}
126+
127+
func (m *mockWallet) ImportTaprootOutput(ctx context.Context,
128+
pubKey *btcec.PublicKey) (btcutil.Address, error) {
129+
130+
args := m.Called(ctx, pubKey)
131+
if args.Get(0) == nil {
132+
return nil, args.Error(1)
133+
}
134+
return args.Get(0).(btcutil.Address), args.Error(1)
135+
}
136+
137+
func (m *mockWallet) UnlockInput(ctx context.Context, op wire.OutPoint) error {
138+
args := m.Called(ctx, op)
139+
return args.Error(0)
140+
}
141+
142+
func (m *mockWallet) DeriveNextKey(
143+
ctx context.Context) (keychain.KeyDescriptor, error) {
144+
145+
args := m.Called(ctx)
146+
if args.Get(0) == nil {
147+
return keychain.KeyDescriptor{}, args.Error(1)
148+
}
149+
return args.Get(0).(keychain.KeyDescriptor), args.Error(1)
150+
}
151+
152+
// mockChainBridge is a mock implementation of the tapgarden.ChainBridge
153+
// interface.
154+
type mockChainBridge struct {
155+
mock.Mock
156+
}
157+
158+
func (m *mockChainBridge) RegisterConfirmationsNtfn(
159+
ctx context.Context, txid *chainhash.Hash, pkScript []byte,
160+
numConfs, heightHint uint32, includeBlock bool,
161+
reOrgChan chan struct{},
162+
) (*chainntnfs.ConfirmationEvent, chan error, error) {
163+
164+
args := m.Called(
165+
ctx, txid, pkScript, numConfs, heightHint, includeBlock,
166+
reOrgChan,
167+
)
168+
if args.Get(0) == nil {
169+
return nil, nil, args.Error(2)
170+
}
171+
return args.Get(0).(*chainntnfs.ConfirmationEvent),
172+
args.Get(1).(chan error), args.Error(2)
173+
}
174+
175+
func (m *mockChainBridge) RegisterSpendNtfn(ctx context.Context,
176+
outpoint *wire.OutPoint, pkScript []byte,
177+
heightHint uint32) (*chainntnfs.SpendEvent, error) {
178+
179+
args := m.Called(ctx, outpoint, pkScript, heightHint)
180+
if args.Get(0) == nil {
181+
return nil, args.Error(1)
182+
}
183+
return args.Get(0).(*chainntnfs.SpendEvent), args.Error(1)
184+
}
185+
186+
func (m *mockChainBridge) PublishTransaction(ctx context.Context,
187+
tx *wire.MsgTx, label string) error {
188+
189+
args := m.Called(ctx, tx, label)
190+
return args.Error(0)
191+
}
192+
193+
func (m *mockChainBridge) EstimateFee(ctx context.Context,
194+
confTarget uint32) (chainfee.SatPerKWeight, error) {
195+
196+
args := m.Called(ctx, confTarget)
197+
if args.Get(0) == nil {
198+
return chainfee.SatPerKWeight(0), args.Error(1)
199+
}
200+
return args.Get(0).(chainfee.SatPerKWeight), args.Error(1)
201+
}
202+
203+
func (m *mockChainBridge) CurrentHeight(ctx context.Context) (uint32, error) {
204+
args := m.Called(ctx)
205+
return args.Get(0).(uint32), args.Error(1)
206+
}
207+
208+
func (m *mockChainBridge) RegisterBlockEpochNtfn(
209+
ctx context.Context) (chan int32, chan error, error) {
210+
211+
args := m.Called(ctx)
212+
if args.Get(0) == nil {
213+
return nil, nil, args.Error(2)
214+
}
215+
return args.Get(0).(chan int32), args.Get(1).(chan error), args.Error(2)
216+
}
217+
218+
func (m *mockChainBridge) GetBlock(ctx context.Context,
219+
hash chainhash.Hash) (*wire.MsgBlock, error) {
220+
221+
args := m.Called(ctx, hash)
222+
if args.Get(0) == nil {
223+
return nil, args.Error(1)
224+
}
225+
return args.Get(0).(*wire.MsgBlock), args.Error(1)
226+
}
227+
228+
func (m *mockChainBridge) GetBlockHash(ctx context.Context,
229+
height int64) (chainhash.Hash, error) {
230+
231+
args := m.Called(ctx, height)
232+
return args.Get(0).(chainhash.Hash), args.Error(1)
233+
}
234+
235+
func (m *mockChainBridge) VerifyBlock(ctx context.Context,
236+
header wire.BlockHeader, height uint32) error {
237+
238+
args := m.Called(ctx, header, height)
239+
return args.Error(0)
240+
}
241+
242+
func (m *mockChainBridge) GetBlockTimestamp(ctx context.Context,
243+
height uint32) int64 {
244+
245+
args := m.Called(ctx, height)
246+
return args.Get(0).(int64)
247+
}
248+
249+
func (m *mockChainBridge) GenFileChainLookup(f *proof.File) asset.ChainLookup {
250+
args := m.Called(f)
251+
return args.Get(0).(asset.ChainLookup)
252+
}
253+
254+
func (m *mockChainBridge) GenProofChainLookup(
255+
p *proof.Proof) (asset.ChainLookup, error) {
256+
257+
args := m.Called(p)
258+
if args.Get(0) == nil {
259+
return nil, args.Error(1)
260+
}
261+
return args.Get(0).(asset.ChainLookup), args.Error(1)
262+
}
263+
264+
// mockStateMachineStore is a mock implementation of the StateMachineStore
265+
// interface.
266+
type mockStateMachineStore struct {
267+
mock.Mock
268+
}
269+
270+
func (m *mockStateMachineStore) InsertPendingUpdate(ctx context.Context,
271+
spec asset.Specifier, event SupplyUpdateEvent) error {
272+
273+
args := m.Called(ctx, spec, event)
274+
return args.Error(0)
275+
}
276+
277+
func (m *mockStateMachineStore) InsertSignedCommitTx(ctx context.Context,
278+
spec asset.Specifier, tx SupplyCommitTxn) error {
279+
280+
args := m.Called(ctx, spec, tx)
281+
return args.Error(0)
282+
}
283+
284+
func (m *mockStateMachineStore) CommitState(ctx context.Context,
285+
spec asset.Specifier, state State) error {
286+
287+
args := m.Called(ctx, spec, state)
288+
return args.Error(0)
289+
}
290+
291+
func (m *mockStateMachineStore) FetchState(ctx context.Context,
292+
spec asset.Specifier) (State, lfn.Option[SupplyStateTransition],
293+
error) {
294+
295+
args := m.Called(ctx, spec)
296+
if args.Get(2) != nil {
297+
return nil, lfn.None[SupplyStateTransition](), args.Error(2)
298+
}
299+
state := args.Get(0)
300+
if state == nil {
301+
return nil, args.Get(1).(lfn.Option[SupplyStateTransition]),
302+
args.Error(2)
303+
}
304+
return state.(State),
305+
args.Get(1).(lfn.Option[SupplyStateTransition]), args.Error(2)
306+
}
307+
308+
func (m *mockStateMachineStore) ApplyStateTransition(ctx context.Context,
309+
spec asset.Specifier, transition SupplyStateTransition) error {
310+
311+
args := m.Called(ctx, spec, transition)
312+
return args.Error(0)
313+
}
314+
315+
// mockDaemonAdapters is a mock implementation of the protofsm.DaemonAdapters
316+
// interface.
317+
type mockDaemonAdapters struct {
318+
mock.Mock
319+
320+
confChan chan *chainntnfs.TxConfirmation
321+
spendChan chan *chainntnfs.SpendDetail
322+
}
323+
324+
func newMockDaemonAdapters() *mockDaemonAdapters {
325+
return &mockDaemonAdapters{
326+
confChan: make(chan *chainntnfs.TxConfirmation, 1),
327+
spendChan: make(chan *chainntnfs.SpendDetail, 1),
328+
}
329+
}
330+
331+
func (m *mockDaemonAdapters) BroadcastTransaction(
332+
tx *wire.MsgTx, label string) error {
333+
334+
args := m.Called(tx, label)
335+
return args.Error(0)
336+
}
337+
338+
func (m *mockDaemonAdapters) RegisterConfirmationsNtfn(
339+
txid *chainhash.Hash, pkScript []byte,
340+
numConfs, heightHint uint32, opts ...chainntnfs.NotifierOption,
341+
) (*chainntnfs.ConfirmationEvent, error) {
342+
343+
args := m.Called(txid, pkScript, numConfs, heightHint, opts)
344+
345+
err := args.Error(0)
346+
347+
return &chainntnfs.ConfirmationEvent{
348+
Confirmed: m.confChan,
349+
}, err
350+
}
351+
352+
func (m *mockDaemonAdapters) RegisterSpendNtfn(outpoint *wire.OutPoint,
353+
pkScript []byte, heightHint uint32) (*chainntnfs.SpendEvent, error) {
354+
355+
args := m.Called(outpoint, pkScript, heightHint)
356+
357+
err := args.Error(0)
358+
359+
return &chainntnfs.SpendEvent{
360+
Spend: m.spendChan,
361+
}, err
362+
}
363+
364+
func (m *mockDaemonAdapters) SendMessages(pub btcec.PublicKey,
365+
msgs []lnwire.Message) error {
366+
367+
args := m.Called(pub, msgs)
368+
return args.Error(0)
369+
}
370+
371+
// mockErrorReporter is a mock implementation of the protofsm.ErrorReporter
372+
// interface.
373+
type mockErrorReporter struct {
374+
mock.Mock
375+
reportedError error
376+
}
377+
378+
func (m *mockErrorReporter) ReportError(err error) {
379+
m.Called(err)
380+
m.reportedError = err
381+
}

0 commit comments

Comments
 (0)