Files
lspd/lsps2/mocks.go
2024-02-26 09:32:04 +01:00

185 lines
5.1 KiB
Go

package lsps2
import (
"context"
"errors"
"fmt"
"time"
"github.com/GoWebProd/uuid7"
"github.com/breez/lspd/chain"
"github.com/breez/lspd/common"
"github.com/breez/lspd/history"
"github.com/breez/lspd/lightning"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/wire"
)
var ErrNotImplemented = errors.New("not implemented")
type mockNodesService struct {
node *common.Node
err error
}
func (m *mockNodesService) GetNode(token string) (*common.Node, error) {
return m.node, m.err
}
func (m *mockNodesService) GetNodes() []*common.Node {
return []*common.Node{m.node}
}
type mockOpeningService struct {
menu []*common.OpeningFeeParams
err error
invalid bool
isCurrentChainFeeCheaper bool
}
func (m *mockOpeningService) GetFeeParamsMenu(
token string,
privateKey *btcec.PrivateKey,
) ([]*common.OpeningFeeParams, error) {
return m.menu, m.err
}
func (m *mockOpeningService) ValidateOpeningFeeParams(
params *common.OpeningFeeParams,
publicKey *btcec.PublicKey,
) bool {
return !m.invalid
}
func (m *mockOpeningService) IsCurrentChainFeeCheaper(
token string,
params *common.OpeningFeeParams,
) bool {
return m.isCurrentChainFeeCheaper
}
type mockLsps2Store struct {
err error
req *RegisterBuy
registrations map[uint64]*BuyRegistration
delay time.Duration
}
func (s *mockLsps2Store) RegisterBuy(ctx context.Context, req *RegisterBuy) error {
s.req = req
return s.err
}
func (s *mockLsps2Store) GetBuyRegistration(ctx context.Context, scid lightning.ShortChannelID) (*BuyRegistration, error) {
if s.delay.Nanoseconds() != 0 {
<-time.After(s.delay)
}
reg, ok := s.registrations[uint64(scid)]
if !ok {
return nil, ErrNotFound
}
return reg, nil
}
func (s *mockLsps2Store) SetChannelOpened(ctx context.Context, channelOpened *ChannelOpened) error {
return s.err
}
func (s *mockLsps2Store) SetCompleted(ctx context.Context, registrationId uuid7.UUID) error {
return nil
}
func (s *mockLsps2Store) SavePromises(ctx context.Context, req *SavePromises) error {
return nil
}
func (s *mockLsps2Store) RemoveUnusedExpired(ctx context.Context, before time.Time) error {
return nil
}
type mockHistoryStore struct{}
func (s *mockHistoryStore) UpdateChannels(ctx context.Context, updates []*history.ChannelUpdate) error {
return nil
}
func (s *mockHistoryStore) InsertForwards(ctx context.Context, forwards []*history.Forward, nodeId []byte) error {
return nil
}
func (s *mockHistoryStore) UpdateForwards(ctx context.Context, forwards []*history.Forward, nodeId []byte) error {
return nil
}
func (s *mockHistoryStore) FetchClnForwardOffsets(ctx context.Context, nodeId []byte) (uint64, uint64, error) {
return 0, 0, ErrNotImplemented
}
func (s *mockHistoryStore) SetClnForwardOffsets(ctx context.Context, nodeId []byte, created uint64, updated uint64) error {
return nil
}
func (s *mockHistoryStore) FetchLndForwardOffset(ctx context.Context, nodeId []byte) (*time.Time, error) {
return nil, ErrNotImplemented
}
func (s *mockHistoryStore) AddOpenChannelHtlc(ctx context.Context, htlc *history.OpenChannelHtlc) error {
return nil
}
type mockLightningClient struct {
openResponses []*wire.OutPoint
openRequests []*lightning.OpenChannelRequest
getChanRequests []*wire.OutPoint
getChanResponses []*lightning.GetChannelResult
}
func (c *mockLightningClient) GetInfo() (*lightning.GetInfoResult, error) {
return nil, ErrNotImplemented
}
func (c *mockLightningClient) IsConnected(destination []byte) (bool, error) {
return false, ErrNotImplemented
}
func (c *mockLightningClient) OpenChannel(req *lightning.OpenChannelRequest) (*wire.OutPoint, error) {
c.openRequests = append(c.openRequests, req)
if len(c.openResponses) < len(c.openRequests) {
return nil, fmt.Errorf("no responses available")
}
res := c.openResponses[len(c.openRequests)-1]
if res == nil {
return nil, fmt.Errorf("no response available")
}
return res, nil
}
func (c *mockLightningClient) GetChannel(peerID []byte, channelPoint wire.OutPoint) (*lightning.GetChannelResult, error) {
c.getChanRequests = append(c.getChanRequests, &channelPoint)
if len(c.getChanResponses) < len(c.getChanRequests) {
return nil, fmt.Errorf("no responses available")
}
res := c.getChanResponses[len(c.getChanRequests)-1]
if res == nil {
return nil, fmt.Errorf("no response available")
}
return res, nil
}
func (c *mockLightningClient) GetPeerId(scid *lightning.ShortChannelID) ([]byte, error) {
return nil, ErrNotImplemented
}
func (c *mockLightningClient) GetClosedChannels(nodeID string, channelPoints map[string]uint64) (map[string]uint64, error) {
return nil, ErrNotImplemented
}
func (c *mockLightningClient) WaitOnline(peerID []byte, deadline time.Time) error {
return ErrNotImplemented
}
func (c *mockLightningClient) WaitChannelActive(peerID []byte, deadline time.Time) error {
return ErrNotImplemented
}
func (c *mockLightningClient) ListChannels() ([]*lightning.Channel, error) {
return nil, ErrNotImplemented
}
type mockFeeEstimator struct {
}
func (f *mockFeeEstimator) EstimateFeeRate(context.Context, chain.FeeStrategy) (*chain.FeeEstimation, error) {
return nil, ErrNotImplemented
}