Skip to content

Commit 414823f

Browse files
committed
universe/supplycommit: add unit tests
1 parent 83c88c3 commit 414823f

File tree

2 files changed

+1816
-0
lines changed

2 files changed

+1816
-0
lines changed

universe/supplycommit/mock.go

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

0 commit comments

Comments
 (0)