Skip to content

Commit 239f6b4

Browse files
committed
unit: manager tests
1 parent 45f9b6a commit 239f6b4

File tree

3 files changed

+464
-1
lines changed

3 files changed

+464
-1
lines changed

staticaddr/address/manager_test.go

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package address
2+
3+
import (
4+
"context"
5+
"encoding/hex"
6+
"testing"
7+
8+
"github.com/btcsuite/btcd/btcec/v2"
9+
"github.com/btcsuite/btcd/btcec/v2/schnorr"
10+
"github.com/btcsuite/btcd/btcutil"
11+
"github.com/lightninglabs/loop/loopdb"
12+
"github.com/lightninglabs/loop/staticaddr/script"
13+
"github.com/lightninglabs/loop/swap"
14+
"github.com/lightninglabs/loop/swapserverrpc"
15+
"github.com/lightninglabs/loop/test"
16+
"github.com/lightningnetwork/lnd/input"
17+
"github.com/lightningnetwork/lnd/keychain"
18+
"github.com/stretchr/testify/mock"
19+
"github.com/stretchr/testify/require"
20+
"google.golang.org/grpc"
21+
)
22+
23+
var (
24+
defaultServerPubkeyBytes, _ = hex.DecodeString("021c97a90a411ff2b10dc2a8e32de2f29d2fa49d41bfbb52bd416e460db0747d0d")
25+
26+
defaultServerPubkey, _ = btcec.ParsePubKey(defaultServerPubkeyBytes)
27+
28+
defaultExpiry = uint32(100)
29+
)
30+
31+
type mockStaticAddressClient struct {
32+
mock.Mock
33+
}
34+
35+
func (m *mockStaticAddressClient) ServerNewAddress(ctx context.Context,
36+
in *swapserverrpc.ServerNewAddressRequest, opts ...grpc.CallOption) (
37+
*swapserverrpc.ServerNewAddressResponse, error) {
38+
39+
args := m.Called(ctx, in, opts)
40+
41+
return args.Get(0).(*swapserverrpc.ServerNewAddressResponse),
42+
args.Error(1)
43+
}
44+
45+
// TestManager tests the static address manager generates the corerct static
46+
// taproot address from the given test parameters.
47+
func TestManager(t *testing.T) {
48+
ctxb, cancel := context.WithCancel(context.Background())
49+
defer cancel()
50+
51+
testContext := NewAddressManagerTestContext(t)
52+
53+
// Start the manager.
54+
go func() {
55+
err := testContext.manager.Run(ctxb)
56+
require.NoError(t, err)
57+
}()
58+
59+
// Create the expected static address.
60+
expectedAddress, err := GenerateExpectedTaprootAddress(testContext)
61+
require.NoError(t, err)
62+
63+
// Create a new static address.
64+
taprootAddress, err := testContext.manager.NewAddress(ctxb)
65+
require.NoError(t, err)
66+
67+
// The addresses have to match.
68+
require.Equal(t, expectedAddress.String(), taprootAddress.String())
69+
}
70+
71+
// GenerateExpectedTaprootAddress generates the expected taproot address that
72+
// the predefined parameters are supposed to generate.
73+
func GenerateExpectedTaprootAddress(t *ManagerTestContext) (
74+
*btcutil.AddressTaproot, error) {
75+
76+
keyIndex := int32(0)
77+
_, pubKey := test.CreateKey(keyIndex)
78+
79+
keyDescriptor := &keychain.KeyDescriptor{
80+
KeyLocator: keychain.KeyLocator{
81+
Family: keychain.KeyFamily(swap.StaticAddressKeyFamily),
82+
Index: uint32(keyIndex),
83+
},
84+
PubKey: pubKey,
85+
}
86+
87+
staticAddress, err := script.NewStaticAddress(
88+
input.MuSig2Version100RC2, int64(defaultExpiry),
89+
keyDescriptor.PubKey, defaultServerPubkey,
90+
)
91+
if err != nil {
92+
return nil, err
93+
}
94+
95+
return btcutil.NewAddressTaproot(
96+
schnorr.SerializePubKey(staticAddress.TaprootKey),
97+
t.manager.cfg.ChainParams,
98+
)
99+
}
100+
101+
// ManagerTestContext is a helper struct that contains all the necessary
102+
// components to test the static address manager.
103+
type ManagerTestContext struct {
104+
manager *Manager
105+
context test.Context
106+
mockLnd *test.LndMockServices
107+
mockStaticAddressClient *mockStaticAddressClient
108+
}
109+
110+
// NewAddressManagerTestContext creates a new test context for the static
111+
// address manager.
112+
func NewAddressManagerTestContext(t *testing.T) *ManagerTestContext {
113+
mockLnd := test.NewMockLnd()
114+
lndContext := test.NewContext(t, mockLnd)
115+
116+
dbFixture := loopdb.NewTestDB(t)
117+
118+
store := NewSqlStore(dbFixture.BaseDB)
119+
120+
mockStaticAddressClient := new(mockStaticAddressClient)
121+
122+
mockStaticAddressClient.On(
123+
"ServerNewAddress", mock.Anything, mock.Anything, mock.Anything,
124+
).Return(
125+
&swapserverrpc.ServerNewAddressResponse{
126+
Params: &swapserverrpc.ServerAddressParameters{
127+
ServerKey: defaultServerPubkeyBytes,
128+
Expiry: defaultExpiry,
129+
},
130+
}, nil,
131+
)
132+
133+
cfg := &ManagerConfig{
134+
Store: store,
135+
WalletKit: mockLnd.WalletKit,
136+
ChainParams: mockLnd.ChainParams,
137+
AddressClient: mockStaticAddressClient,
138+
FetchL402: func(context.Context) error { return nil },
139+
}
140+
141+
manager := NewManager(cfg)
142+
143+
return &ManagerTestContext{
144+
manager: manager,
145+
context: lndContext,
146+
mockLnd: mockLnd,
147+
mockStaticAddressClient: mockStaticAddressClient,
148+
}
149+
}

0 commit comments

Comments
 (0)