2017-05-01 19:58:08 +03:00
|
|
|
package htlcswitch
|
|
|
|
|
|
|
|
import (
|
2018-06-01 06:31:40 +03:00
|
|
|
"bytes"
|
2017-05-01 19:58:08 +03:00
|
|
|
"crypto/sha256"
|
2017-05-02 02:29:30 +03:00
|
|
|
"encoding/binary"
|
2017-07-31 00:09:10 +03:00
|
|
|
"fmt"
|
2018-06-01 06:31:40 +03:00
|
|
|
"io"
|
2017-12-11 02:38:17 +03:00
|
|
|
"io/ioutil"
|
2018-07-05 23:27:35 +03:00
|
|
|
"net"
|
2019-02-21 16:52:13 +03:00
|
|
|
"os"
|
2017-05-01 19:58:08 +03:00
|
|
|
"sync"
|
2018-06-01 06:31:40 +03:00
|
|
|
"sync/atomic"
|
2017-11-11 02:09:19 +03:00
|
|
|
"testing"
|
2018-01-31 02:53:39 +03:00
|
|
|
"time"
|
2017-05-01 19:58:08 +03:00
|
|
|
|
2018-07-18 05:24:04 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/txscript"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
2017-05-01 19:58:08 +03:00
|
|
|
"github.com/go-errors/errors"
|
2019-01-11 13:19:16 +03:00
|
|
|
sphinx "github.com/lightningnetwork/lightning-onion"
|
2017-05-02 02:29:30 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2019-12-09 19:50:11 +03:00
|
|
|
"github.com/lightningnetwork/lnd/clock"
|
2018-01-17 07:18:53 +03:00
|
|
|
"github.com/lightningnetwork/lnd/contractcourt"
|
2019-08-31 00:05:53 +03:00
|
|
|
"github.com/lightningnetwork/lnd/htlcswitch/hop"
|
2019-01-16 17:47:43 +03:00
|
|
|
"github.com/lightningnetwork/lnd/input"
|
2019-02-21 16:52:13 +03:00
|
|
|
"github.com/lightningnetwork/lnd/invoices"
|
2018-06-08 06:18:10 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnpeer"
|
2019-01-15 13:31:22 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2019-10-31 05:43:05 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
|
2017-05-01 19:58:08 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2018-08-02 12:14:55 +03:00
|
|
|
"github.com/lightningnetwork/lnd/ticker"
|
2017-05-01 19:58:08 +03:00
|
|
|
)
|
|
|
|
|
2018-01-17 07:18:53 +03:00
|
|
|
type mockPreimageCache struct {
|
|
|
|
sync.Mutex
|
2019-02-20 04:06:00 +03:00
|
|
|
preimageMap map[lntypes.Hash]lntypes.Preimage
|
2018-01-17 07:18:53 +03:00
|
|
|
}
|
|
|
|
|
2019-02-20 04:06:00 +03:00
|
|
|
func newMockPreimageCache() *mockPreimageCache {
|
|
|
|
return &mockPreimageCache{
|
|
|
|
preimageMap: make(map[lntypes.Hash]lntypes.Preimage),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockPreimageCache) LookupPreimage(
|
|
|
|
hash lntypes.Hash) (lntypes.Preimage, bool) {
|
|
|
|
|
2018-01-17 07:18:53 +03:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2019-02-20 04:06:00 +03:00
|
|
|
p, ok := m.preimageMap[hash]
|
2018-01-17 07:18:53 +03:00
|
|
|
return p, ok
|
|
|
|
}
|
|
|
|
|
2019-02-20 04:06:00 +03:00
|
|
|
func (m *mockPreimageCache) AddPreimages(preimages ...lntypes.Preimage) error {
|
2018-01-17 07:18:53 +03:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2019-02-20 04:05:04 +03:00
|
|
|
for _, preimage := range preimages {
|
2019-02-20 04:06:00 +03:00
|
|
|
m.preimageMap[preimage.Hash()] = preimage
|
2019-02-20 04:05:04 +03:00
|
|
|
}
|
2018-01-17 07:18:53 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
func (m *mockPreimageCache) SubscribeUpdates() *contractcourt.WitnessSubscription {
|
2018-01-17 07:18:53 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-24 07:31:45 +03:00
|
|
|
type mockFeeEstimator struct {
|
2019-10-31 05:43:05 +03:00
|
|
|
byteFeeIn chan chainfee.SatPerKWeight
|
2017-11-24 07:31:45 +03:00
|
|
|
|
|
|
|
quit chan struct{}
|
|
|
|
}
|
|
|
|
|
2018-07-28 04:20:58 +03:00
|
|
|
func (m *mockFeeEstimator) EstimateFeePerKW(
|
2019-10-31 05:43:05 +03:00
|
|
|
numBlocks uint32) (chainfee.SatPerKWeight, error) {
|
2018-07-28 04:20:58 +03:00
|
|
|
|
2017-11-24 07:31:45 +03:00
|
|
|
select {
|
|
|
|
case feeRate := <-m.byteFeeIn:
|
|
|
|
return feeRate, nil
|
|
|
|
case <-m.quit:
|
|
|
|
return 0, fmt.Errorf("exiting")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
func (m *mockFeeEstimator) RelayFeePerKW() chainfee.SatPerKWeight {
|
2018-10-29 11:31:44 +03:00
|
|
|
return 1e3
|
|
|
|
}
|
|
|
|
|
2017-11-24 07:31:45 +03:00
|
|
|
func (m *mockFeeEstimator) Start() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (m *mockFeeEstimator) Stop() error {
|
|
|
|
close(m.quit)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
var _ chainfee.Estimator = (*mockFeeEstimator)(nil)
|
2017-11-24 07:31:45 +03:00
|
|
|
|
2018-02-28 09:18:52 +03:00
|
|
|
type mockForwardingLog struct {
|
|
|
|
sync.Mutex
|
2018-02-28 09:19:21 +03:00
|
|
|
|
2018-02-28 09:18:52 +03:00
|
|
|
events map[time.Time]channeldb.ForwardingEvent
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockForwardingLog) AddForwardingEvents(events []channeldb.ForwardingEvent) error {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
for _, event := range events {
|
|
|
|
m.events[event.Timestamp] = event
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
type mockServer struct {
|
2018-06-01 01:41:41 +03:00
|
|
|
started int32 // To be used atomically.
|
|
|
|
shutdown int32 // To be used atomically.
|
2017-05-01 19:58:08 +03:00
|
|
|
wg sync.WaitGroup
|
2017-07-09 01:52:51 +03:00
|
|
|
quit chan struct{}
|
2017-05-01 19:58:08 +03:00
|
|
|
|
2017-11-12 02:05:09 +03:00
|
|
|
t testing.TB
|
2017-11-11 02:09:19 +03:00
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
name string
|
|
|
|
messages chan lnwire.Message
|
|
|
|
|
2017-06-17 01:08:19 +03:00
|
|
|
id [33]byte
|
2017-05-01 19:58:08 +03:00
|
|
|
htlcSwitch *Switch
|
|
|
|
|
2017-07-09 01:46:27 +03:00
|
|
|
registry *mockInvoiceRegistry
|
2019-02-09 19:22:32 +03:00
|
|
|
pCache *mockPreimageCache
|
2017-07-09 01:46:27 +03:00
|
|
|
interceptorFuncs []messageInterceptor
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
2018-06-08 06:18:10 +03:00
|
|
|
var _ lnpeer.Peer = (*mockServer)(nil)
|
2017-05-01 19:58:08 +03:00
|
|
|
|
2018-08-12 16:20:57 +03:00
|
|
|
func initDB() (*channeldb.DB, error) {
|
|
|
|
tempPath, err := ioutil.TempDir("", "switchdb")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err := channeldb.Open(tempPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return db, err
|
|
|
|
}
|
|
|
|
|
2018-06-01 06:31:40 +03:00
|
|
|
func initSwitchWithDB(startingHeight uint32, db *channeldb.DB) (*Switch, error) {
|
2018-08-12 16:20:57 +03:00
|
|
|
var err error
|
2017-12-11 02:38:17 +03:00
|
|
|
|
2018-08-12 16:20:57 +03:00
|
|
|
if db == nil {
|
|
|
|
db, err = initDB()
|
2017-12-11 02:38:17 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-01 06:31:40 +03:00
|
|
|
cfg := Config{
|
2017-12-11 02:38:17 +03:00
|
|
|
DB: db,
|
|
|
|
SwitchPackager: channeldb.NewSwitchPackager(),
|
2018-03-06 23:31:05 +03:00
|
|
|
FwdingLog: &mockForwardingLog{
|
|
|
|
events: make(map[time.Time]channeldb.ForwardingEvent),
|
|
|
|
},
|
2018-05-08 06:00:32 +03:00
|
|
|
FetchLastChannelUpdate: func(lnwire.ShortChannelID) (*lnwire.ChannelUpdate, error) {
|
|
|
|
return nil, nil
|
|
|
|
},
|
2019-09-19 22:46:44 +03:00
|
|
|
Notifier: &mockNotifier{},
|
|
|
|
FwdEventTicker: ticker.NewForce(DefaultFwdEventInterval),
|
|
|
|
LogEventTicker: ticker.NewForce(DefaultLogInterval),
|
|
|
|
AckEventTicker: ticker.NewForce(DefaultAckInterval),
|
2020-02-19 18:34:47 +03:00
|
|
|
HtlcNotifier: &mockHTLCNotifier{},
|
2020-04-14 20:49:26 +03:00
|
|
|
Clock: clock.NewDefaultClock(),
|
|
|
|
HTLCExpiry: time.Hour,
|
2018-06-01 06:31:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return New(cfg, startingHeight)
|
2017-12-11 02:38:17 +03:00
|
|
|
}
|
|
|
|
|
2018-06-01 06:31:40 +03:00
|
|
|
func newMockServer(t testing.TB, name string, startingHeight uint32,
|
2018-06-30 02:02:59 +03:00
|
|
|
db *channeldb.DB, defaultDelta uint32) (*mockServer, error) {
|
2018-06-01 06:31:40 +03:00
|
|
|
|
2017-06-17 01:08:19 +03:00
|
|
|
var id [33]byte
|
|
|
|
h := sha256.Sum256([]byte(name))
|
|
|
|
copy(id[:], h[:])
|
|
|
|
|
2019-02-09 19:22:32 +03:00
|
|
|
pCache := newMockPreimageCache()
|
|
|
|
|
2018-06-01 06:31:40 +03:00
|
|
|
htlcSwitch, err := initSwitchWithDB(startingHeight, db)
|
2017-12-11 02:38:17 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-09 19:22:32 +03:00
|
|
|
registry := newMockRegistry(defaultDelta)
|
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
return &mockServer{
|
2017-12-11 02:38:17 +03:00
|
|
|
t: t,
|
|
|
|
id: id,
|
|
|
|
name: name,
|
|
|
|
messages: make(chan lnwire.Message, 3000),
|
|
|
|
quit: make(chan struct{}),
|
2019-02-09 19:22:32 +03:00
|
|
|
registry: registry,
|
2017-12-11 02:38:17 +03:00
|
|
|
htlcSwitch: htlcSwitch,
|
2019-02-09 19:22:32 +03:00
|
|
|
pCache: pCache,
|
2017-07-09 01:46:27 +03:00
|
|
|
interceptorFuncs: make([]messageInterceptor, 0),
|
2017-12-11 02:38:17 +03:00
|
|
|
}, nil
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *mockServer) Start() error {
|
|
|
|
if !atomic.CompareAndSwapInt32(&s.started, 0, 1) {
|
2017-07-09 02:30:20 +03:00
|
|
|
return errors.New("mock server already started")
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
if err := s.htlcSwitch.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-01 19:58:08 +03:00
|
|
|
|
|
|
|
s.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer s.wg.Done()
|
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
defer func() {
|
|
|
|
s.htlcSwitch.Stop()
|
|
|
|
}()
|
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-s.messages:
|
2017-07-09 01:46:27 +03:00
|
|
|
var shouldSkip bool
|
|
|
|
|
|
|
|
for _, interceptor := range s.interceptorFuncs {
|
|
|
|
skip, err := interceptor(msg)
|
|
|
|
if err != nil {
|
2017-11-11 02:09:19 +03:00
|
|
|
s.t.Fatalf("%v: error in the "+
|
|
|
|
"interceptor: %v", s.name, err)
|
2017-07-09 01:46:27 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
shouldSkip = shouldSkip || skip
|
|
|
|
}
|
|
|
|
|
|
|
|
if shouldSkip {
|
|
|
|
continue
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.readHandler(msg); err != nil {
|
2017-11-11 02:09:19 +03:00
|
|
|
s.t.Fatal(err)
|
2017-07-09 02:30:20 +03:00
|
|
|
return
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
case <-s.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-26 03:11:40 +03:00
|
|
|
func (s *mockServer) QuitSignal() <-chan struct{} {
|
|
|
|
return s.quit
|
|
|
|
}
|
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
// mockHopIterator represents the test version of hop iterator which instead
|
|
|
|
// of encrypting the path in onion blob just stores the path as a list of hops.
|
|
|
|
type mockHopIterator struct {
|
2019-11-05 02:10:15 +03:00
|
|
|
hops []*hop.Payload
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
2019-11-05 02:10:15 +03:00
|
|
|
func newMockHopIterator(hops ...*hop.Payload) hop.Iterator {
|
2017-05-02 02:29:30 +03:00
|
|
|
return &mockHopIterator{hops: hops}
|
|
|
|
}
|
|
|
|
|
2019-11-05 02:10:15 +03:00
|
|
|
func (r *mockHopIterator) HopPayload() (*hop.Payload, error) {
|
2017-06-17 01:08:19 +03:00
|
|
|
h := r.hops[0]
|
|
|
|
r.hops = r.hops[1:]
|
2019-07-31 07:52:17 +03:00
|
|
|
return h, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *mockHopIterator) ExtraOnionBlob() []byte {
|
|
|
|
return nil
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
2018-02-24 04:18:25 +03:00
|
|
|
func (r *mockHopIterator) ExtractErrorEncrypter(
|
2019-09-05 14:35:39 +03:00
|
|
|
extracter hop.ErrorEncrypterExtracter) (hop.ErrorEncrypter,
|
|
|
|
lnwire.FailCode) {
|
2018-02-24 04:18:25 +03:00
|
|
|
|
|
|
|
return extracter(nil)
|
|
|
|
}
|
|
|
|
|
2017-06-17 01:08:19 +03:00
|
|
|
func (r *mockHopIterator) EncodeNextHop(w io.Writer) error {
|
2017-05-02 02:29:30 +03:00
|
|
|
var hopLength [4]byte
|
|
|
|
binary.BigEndian.PutUint32(hopLength[:], uint32(len(r.hops)))
|
|
|
|
|
|
|
|
if _, err := w.Write(hopLength[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, hop := range r.hops {
|
2019-11-05 02:10:15 +03:00
|
|
|
fwdInfo := hop.ForwardingInfo()
|
|
|
|
if err := encodeFwdInfo(w, &fwdInfo); err != nil {
|
2017-05-02 02:29:30 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-31 00:11:20 +03:00
|
|
|
func encodeFwdInfo(w io.Writer, f *hop.ForwardingInfo) error {
|
2017-06-17 01:08:19 +03:00
|
|
|
if _, err := w.Write([]byte{byte(f.Network)}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Write(w, binary.BigEndian, f.NextHop); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Write(w, binary.BigEndian, f.AmountToForward); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Write(w, binary.BigEndian, f.OutgoingCTLV); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-05 14:35:39 +03:00
|
|
|
var _ hop.Iterator = (*mockHopIterator)(nil)
|
2017-05-02 02:29:30 +03:00
|
|
|
|
2017-06-29 16:40:45 +03:00
|
|
|
// mockObfuscator mock implementation of the failure obfuscator which only
|
|
|
|
// encodes the failure and do not makes any onion obfuscation.
|
2018-02-24 04:18:25 +03:00
|
|
|
type mockObfuscator struct {
|
|
|
|
ogPacket *sphinx.OnionPacket
|
2019-10-09 17:37:25 +03:00
|
|
|
failure lnwire.FailureMessage
|
2018-02-24 04:18:25 +03:00
|
|
|
}
|
2017-06-29 16:40:45 +03:00
|
|
|
|
2017-12-11 02:38:17 +03:00
|
|
|
// NewMockObfuscator initializes a dummy mockObfuscator used for testing.
|
2019-09-05 14:35:39 +03:00
|
|
|
func NewMockObfuscator() hop.ErrorEncrypter {
|
2017-06-29 16:40:45 +03:00
|
|
|
return &mockObfuscator{}
|
|
|
|
}
|
|
|
|
|
2018-02-24 04:18:25 +03:00
|
|
|
func (o *mockObfuscator) OnionPacket() *sphinx.OnionPacket {
|
|
|
|
return o.ogPacket
|
|
|
|
}
|
|
|
|
|
2019-09-05 14:35:39 +03:00
|
|
|
func (o *mockObfuscator) Type() hop.EncrypterType {
|
|
|
|
return hop.EncrypterTypeMock
|
2018-02-24 04:18:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *mockObfuscator) Encode(w io.Writer) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *mockObfuscator) Decode(r io.Reader) error {
|
2018-03-12 23:37:49 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-05 14:35:39 +03:00
|
|
|
func (o *mockObfuscator) Reextract(
|
|
|
|
extracter hop.ErrorEncrypterExtracter) error {
|
|
|
|
|
2018-02-24 04:18:25 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-11 05:36:52 +03:00
|
|
|
func (o *mockObfuscator) EncryptFirstHop(failure lnwire.FailureMessage) (
|
2017-06-29 16:40:45 +03:00
|
|
|
lnwire.OpaqueReason, error) {
|
|
|
|
|
2019-10-09 17:37:25 +03:00
|
|
|
o.failure = failure
|
|
|
|
|
2017-06-29 16:40:45 +03:00
|
|
|
var b bytes.Buffer
|
|
|
|
if err := lnwire.EncodeFailure(&b, failure, 0); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2017-10-11 05:36:52 +03:00
|
|
|
func (o *mockObfuscator) IntermediateEncrypt(reason lnwire.OpaqueReason) lnwire.OpaqueReason {
|
2017-06-29 16:40:45 +03:00
|
|
|
return reason
|
2019-05-01 04:28:39 +03:00
|
|
|
}
|
2017-06-29 16:40:45 +03:00
|
|
|
|
2019-05-01 04:28:39 +03:00
|
|
|
func (o *mockObfuscator) EncryptMalformedError(reason lnwire.OpaqueReason) lnwire.OpaqueReason {
|
|
|
|
return reason
|
2017-06-29 16:40:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// mockDeobfuscator mock implementation of the failure deobfuscator which
|
|
|
|
// only decodes the failure do not makes any onion obfuscation.
|
|
|
|
type mockDeobfuscator struct{}
|
|
|
|
|
2017-10-11 05:36:52 +03:00
|
|
|
func newMockDeobfuscator() ErrorDecrypter {
|
2017-06-29 16:40:45 +03:00
|
|
|
return &mockDeobfuscator{}
|
|
|
|
}
|
|
|
|
|
2017-10-11 05:36:52 +03:00
|
|
|
func (o *mockDeobfuscator) DecryptError(reason lnwire.OpaqueReason) (*ForwardingError, error) {
|
|
|
|
|
2017-06-29 16:40:45 +03:00
|
|
|
r := bytes.NewReader(reason)
|
|
|
|
failure, err := lnwire.DecodeFailure(r, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-10-11 05:36:52 +03:00
|
|
|
|
2020-01-14 16:07:42 +03:00
|
|
|
return NewForwardingError(failure, 1), nil
|
2017-06-29 16:40:45 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 05:36:52 +03:00
|
|
|
var _ ErrorDecrypter = (*mockDeobfuscator)(nil)
|
2017-06-29 16:40:45 +03:00
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
// mockIteratorDecoder test version of hop iterator decoder which decodes the
|
|
|
|
// encoded array of hops.
|
2018-02-24 04:18:25 +03:00
|
|
|
type mockIteratorDecoder struct {
|
|
|
|
mu sync.RWMutex
|
2017-05-02 02:29:30 +03:00
|
|
|
|
2019-09-05 14:35:39 +03:00
|
|
|
responses map[[32]byte][]hop.DecodeHopIteratorResponse
|
2019-05-01 04:28:39 +03:00
|
|
|
|
|
|
|
decodeFail bool
|
2018-02-24 04:18:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func newMockIteratorDecoder() *mockIteratorDecoder {
|
|
|
|
return &mockIteratorDecoder{
|
2019-09-05 14:35:39 +03:00
|
|
|
responses: make(map[[32]byte][]hop.DecodeHopIteratorResponse),
|
2018-02-24 04:18:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *mockIteratorDecoder) DecodeHopIterator(r io.Reader, rHash []byte,
|
2019-09-05 14:35:39 +03:00
|
|
|
cltv uint32) (hop.Iterator, lnwire.FailCode) {
|
2017-06-29 16:40:45 +03:00
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
var b [4]byte
|
|
|
|
_, err := r.Read(b[:])
|
|
|
|
if err != nil {
|
2017-06-29 16:40:45 +03:00
|
|
|
return nil, lnwire.CodeTemporaryChannelFailure
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
hopLength := binary.BigEndian.Uint32(b[:])
|
|
|
|
|
2019-11-05 02:10:15 +03:00
|
|
|
hops := make([]*hop.Payload, hopLength)
|
2017-05-02 02:29:30 +03:00
|
|
|
for i := uint32(0); i < hopLength; i++ {
|
2019-11-05 02:10:15 +03:00
|
|
|
var f hop.ForwardingInfo
|
|
|
|
if err := decodeFwdInfo(r, &f); err != nil {
|
2017-06-29 16:40:45 +03:00
|
|
|
return nil, lnwire.CodeTemporaryChannelFailure
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
2019-11-05 02:10:15 +03:00
|
|
|
var nextHopBytes [8]byte
|
|
|
|
binary.BigEndian.PutUint64(nextHopBytes[:], f.NextHop.ToUint64())
|
|
|
|
|
|
|
|
hops[i] = hop.NewLegacyPayload(&sphinx.HopData{
|
|
|
|
Realm: [1]byte{}, // hop.BitcoinNetwork
|
|
|
|
NextAddress: nextHopBytes,
|
|
|
|
ForwardAmount: uint64(f.AmountToForward),
|
|
|
|
OutgoingCltv: f.OutgoingCTLV,
|
|
|
|
})
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
2017-06-29 16:40:45 +03:00
|
|
|
return newMockHopIterator(hops...), lnwire.CodeNone
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
2018-02-24 04:18:25 +03:00
|
|
|
func (p *mockIteratorDecoder) DecodeHopIterators(id []byte,
|
2019-09-05 14:35:39 +03:00
|
|
|
reqs []hop.DecodeHopIteratorRequest) (
|
|
|
|
[]hop.DecodeHopIteratorResponse, error) {
|
2018-02-24 04:18:25 +03:00
|
|
|
|
|
|
|
idHash := sha256.Sum256(id)
|
|
|
|
|
|
|
|
p.mu.RLock()
|
|
|
|
if resps, ok := p.responses[idHash]; ok {
|
|
|
|
p.mu.RUnlock()
|
|
|
|
return resps, nil
|
|
|
|
}
|
|
|
|
p.mu.RUnlock()
|
|
|
|
|
|
|
|
batchSize := len(reqs)
|
|
|
|
|
2019-09-05 14:35:39 +03:00
|
|
|
resps := make([]hop.DecodeHopIteratorResponse, 0, batchSize)
|
2018-02-24 04:18:25 +03:00
|
|
|
for _, req := range reqs {
|
|
|
|
iterator, failcode := p.DecodeHopIterator(
|
|
|
|
req.OnionReader, req.RHash, req.IncomingCltv,
|
|
|
|
)
|
|
|
|
|
2019-05-01 04:28:39 +03:00
|
|
|
if p.decodeFail {
|
|
|
|
failcode = lnwire.CodeTemporaryChannelFailure
|
|
|
|
}
|
|
|
|
|
2019-09-05 14:35:39 +03:00
|
|
|
resp := hop.DecodeHopIteratorResponse{
|
2018-02-24 04:18:25 +03:00
|
|
|
HopIterator: iterator,
|
|
|
|
FailCode: failcode,
|
|
|
|
}
|
|
|
|
resps = append(resps, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.mu.Lock()
|
|
|
|
p.responses[idHash] = resps
|
|
|
|
p.mu.Unlock()
|
|
|
|
|
|
|
|
return resps, nil
|
|
|
|
}
|
|
|
|
|
2019-08-31 00:11:20 +03:00
|
|
|
func decodeFwdInfo(r io.Reader, f *hop.ForwardingInfo) error {
|
2017-06-17 01:08:19 +03:00
|
|
|
var net [1]byte
|
|
|
|
if _, err := r.Read(net[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-31 00:05:53 +03:00
|
|
|
f.Network = hop.Network(net[0])
|
2017-06-17 01:08:19 +03:00
|
|
|
|
|
|
|
if err := binary.Read(r, binary.BigEndian, &f.NextHop); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Read(r, binary.BigEndian, &f.AmountToForward); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Read(r, binary.BigEndian, &f.OutgoingCTLV); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
// messageInterceptor is function that handles the incoming peer messages and
|
2017-07-09 01:46:27 +03:00
|
|
|
// may decide should the peer skip the message or not.
|
|
|
|
type messageInterceptor func(m lnwire.Message) (bool, error)
|
2017-05-01 19:58:08 +03:00
|
|
|
|
|
|
|
// Record is used to set the function which will be triggered when new
|
|
|
|
// lnwire message was received.
|
2017-07-09 01:46:27 +03:00
|
|
|
func (s *mockServer) intersect(f messageInterceptor) {
|
|
|
|
s.interceptorFuncs = append(s.interceptorFuncs, f)
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
2018-06-08 06:18:10 +03:00
|
|
|
func (s *mockServer) SendMessage(sync bool, msgs ...lnwire.Message) error {
|
2017-11-11 02:15:19 +03:00
|
|
|
|
2018-06-08 06:18:10 +03:00
|
|
|
for _, msg := range msgs {
|
|
|
|
select {
|
|
|
|
case s.messages <- msg:
|
|
|
|
case <-s.quit:
|
|
|
|
return errors.New("server is stopped")
|
|
|
|
}
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-06 04:08:22 +03:00
|
|
|
func (s *mockServer) SendMessageLazy(sync bool, msgs ...lnwire.Message) error {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
func (s *mockServer) readHandler(message lnwire.Message) error {
|
|
|
|
var targetChan lnwire.ChannelID
|
|
|
|
|
|
|
|
switch msg := message.(type) {
|
|
|
|
case *lnwire.UpdateAddHTLC:
|
|
|
|
targetChan = msg.ChanID
|
2018-02-07 06:11:11 +03:00
|
|
|
case *lnwire.UpdateFulfillHTLC:
|
2017-05-01 19:58:08 +03:00
|
|
|
targetChan = msg.ChanID
|
|
|
|
case *lnwire.UpdateFailHTLC:
|
|
|
|
targetChan = msg.ChanID
|
2017-06-29 16:40:45 +03:00
|
|
|
case *lnwire.UpdateFailMalformedHTLC:
|
|
|
|
targetChan = msg.ChanID
|
2017-05-01 19:58:08 +03:00
|
|
|
case *lnwire.RevokeAndAck:
|
|
|
|
targetChan = msg.ChanID
|
|
|
|
case *lnwire.CommitSig:
|
|
|
|
targetChan = msg.ChanID
|
2017-09-07 16:04:07 +03:00
|
|
|
case *lnwire.FundingLocked:
|
|
|
|
// Ignore
|
|
|
|
return nil
|
2017-07-09 02:30:20 +03:00
|
|
|
case *lnwire.ChannelReestablish:
|
|
|
|
targetChan = msg.ChanID
|
2017-11-24 07:31:45 +03:00
|
|
|
case *lnwire.UpdateFee:
|
|
|
|
targetChan = msg.ChanID
|
2017-05-01 19:58:08 +03:00
|
|
|
default:
|
2017-11-11 02:15:19 +03:00
|
|
|
return fmt.Errorf("unknown message type: %T", msg)
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
2018-08-21 05:23:15 +03:00
|
|
|
// Dispatch the commitment update message to the proper channel link
|
|
|
|
// dedicated to this channel. If the link is not found, we will discard
|
|
|
|
// the message.
|
2017-05-01 19:58:08 +03:00
|
|
|
link, err := s.htlcSwitch.GetLink(targetChan)
|
|
|
|
if err != nil {
|
2018-08-21 05:23:15 +03:00
|
|
|
return nil
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create goroutine for this, in order to be able to properly stop
|
|
|
|
// the server when handler stacked (server unavailable)
|
2017-11-11 02:15:19 +03:00
|
|
|
link.HandleChannelUpdate(message)
|
2017-05-01 19:58:08 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-17 01:08:19 +03:00
|
|
|
func (s *mockServer) PubKey() [33]byte {
|
2017-05-01 19:58:08 +03:00
|
|
|
return s.id
|
|
|
|
}
|
|
|
|
|
2018-06-08 06:18:10 +03:00
|
|
|
func (s *mockServer) IdentityKey() *btcec.PublicKey {
|
|
|
|
pubkey, _ := btcec.ParsePubKey(s.id[:], btcec.S256())
|
|
|
|
return pubkey
|
|
|
|
}
|
|
|
|
|
2018-07-05 23:27:35 +03:00
|
|
|
func (s *mockServer) Address() net.Addr {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-26 12:12:58 +03:00
|
|
|
func (s *mockServer) AddNewChannel(channel *channeldb.OpenChannel,
|
2018-07-05 23:27:35 +03:00
|
|
|
cancel <-chan struct{}) error {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-03 03:39:29 +03:00
|
|
|
func (s *mockServer) WipeChannel(*wire.OutPoint) {}
|
2017-05-03 17:02:22 +03:00
|
|
|
|
2019-11-08 16:31:47 +03:00
|
|
|
func (s *mockServer) LocalFeatures() *lnwire.FeatureVector {
|
2019-09-11 15:41:08 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-08 16:31:47 +03:00
|
|
|
func (s *mockServer) RemoteFeatures() *lnwire.FeatureVector {
|
2019-09-11 15:41:08 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
func (s *mockServer) Stop() error {
|
2017-05-01 19:58:08 +03:00
|
|
|
if !atomic.CompareAndSwapInt32(&s.shutdown, 0, 1) {
|
2017-07-09 02:30:20 +03:00
|
|
|
return nil
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
close(s.quit)
|
|
|
|
s.wg.Wait()
|
2017-07-09 02:30:20 +03:00
|
|
|
|
|
|
|
return nil
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *mockServer) String() string {
|
2017-06-17 01:08:19 +03:00
|
|
|
return s.name
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type mockChannelLink struct {
|
2017-11-02 23:53:42 +03:00
|
|
|
htlcSwitch *Switch
|
|
|
|
|
2017-06-17 01:08:19 +03:00
|
|
|
shortChanID lnwire.ShortChannelID
|
|
|
|
|
|
|
|
chanID lnwire.ChannelID
|
|
|
|
|
2018-06-08 06:18:10 +03:00
|
|
|
peer lnpeer.Peer
|
2017-06-17 01:08:19 +03:00
|
|
|
|
2017-12-11 02:38:17 +03:00
|
|
|
mailBox MailBox
|
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
packets chan *htlcPacket
|
2017-12-11 03:19:40 +03:00
|
|
|
|
|
|
|
eligible bool
|
2017-11-02 23:53:42 +03:00
|
|
|
|
|
|
|
htlcID uint64
|
2019-04-19 12:11:16 +03:00
|
|
|
|
2020-01-14 16:07:42 +03:00
|
|
|
checkHtlcTransitResult *LinkError
|
2019-10-09 17:58:51 +03:00
|
|
|
|
2020-01-14 16:07:42 +03:00
|
|
|
checkHtlcForwardResult *LinkError
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
2017-12-11 02:38:17 +03:00
|
|
|
// completeCircuit is a helper method for adding the finalized payment circuit
|
|
|
|
// to the switch's circuit map. In testing, this should be executed after
|
|
|
|
// receiving an htlc from the downstream packets channel.
|
|
|
|
func (f *mockChannelLink) completeCircuit(pkt *htlcPacket) error {
|
|
|
|
switch htlc := pkt.htlc.(type) {
|
|
|
|
case *lnwire.UpdateAddHTLC:
|
|
|
|
pkt.outgoingChanID = f.shortChanID
|
|
|
|
pkt.outgoingHTLCID = f.htlcID
|
|
|
|
htlc.ID = f.htlcID
|
|
|
|
|
|
|
|
keystone := Keystone{pkt.inKey(), pkt.outKey()}
|
|
|
|
if err := f.htlcSwitch.openCircuits(keystone); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
f.htlcID++
|
|
|
|
|
|
|
|
case *lnwire.UpdateFulfillHTLC, *lnwire.UpdateFailHTLC:
|
|
|
|
err := f.htlcSwitch.teardownCircuit(pkt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f.mailBox.AckPacket(pkt.inKey())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *mockChannelLink) deleteCircuit(pkt *htlcPacket) error {
|
|
|
|
return f.htlcSwitch.deleteCircuits(pkt.inKey())
|
|
|
|
}
|
|
|
|
|
2017-11-02 23:53:42 +03:00
|
|
|
func newMockChannelLink(htlcSwitch *Switch, chanID lnwire.ChannelID,
|
2018-06-08 06:18:10 +03:00
|
|
|
shortChanID lnwire.ShortChannelID, peer lnpeer.Peer, eligible bool,
|
2017-11-02 23:53:42 +03:00
|
|
|
) *mockChannelLink {
|
2017-06-17 01:08:19 +03:00
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
return &mockChannelLink{
|
2017-11-02 23:53:42 +03:00
|
|
|
htlcSwitch: htlcSwitch,
|
2017-06-17 01:08:19 +03:00
|
|
|
chanID: chanID,
|
|
|
|
shortChanID: shortChanID,
|
|
|
|
peer: peer,
|
2017-12-11 03:19:40 +03:00
|
|
|
eligible: eligible,
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-11 02:38:17 +03:00
|
|
|
func (f *mockChannelLink) HandleSwitchPacket(pkt *htlcPacket) error {
|
|
|
|
f.mailBox.AddPacket(pkt)
|
|
|
|
return nil
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *mockChannelLink) HandleChannelUpdate(lnwire.Message) {
|
|
|
|
}
|
|
|
|
|
2017-06-17 01:08:19 +03:00
|
|
|
func (f *mockChannelLink) UpdateForwardingPolicy(_ ForwardingPolicy) {
|
|
|
|
}
|
2019-09-27 17:21:34 +03:00
|
|
|
func (f *mockChannelLink) CheckHtlcForward([32]byte, lnwire.MilliSatoshi,
|
2020-01-14 16:07:42 +03:00
|
|
|
lnwire.MilliSatoshi, uint32, uint32, uint32) *LinkError {
|
2019-10-09 17:58:51 +03:00
|
|
|
|
|
|
|
return f.checkHtlcForwardResult
|
2018-04-04 06:09:51 +03:00
|
|
|
}
|
2017-06-17 01:08:19 +03:00
|
|
|
|
2019-09-27 17:21:34 +03:00
|
|
|
func (f *mockChannelLink) CheckHtlcTransit(payHash [32]byte,
|
2019-04-19 12:11:16 +03:00
|
|
|
amt lnwire.MilliSatoshi, timeout uint32,
|
2020-01-14 16:07:42 +03:00
|
|
|
heightNow uint32) *LinkError {
|
2019-04-19 12:11:16 +03:00
|
|
|
|
2019-10-09 17:58:51 +03:00
|
|
|
return f.checkHtlcTransitResult
|
2019-04-19 12:11:16 +03:00
|
|
|
}
|
|
|
|
|
2017-08-22 09:36:43 +03:00
|
|
|
func (f *mockChannelLink) Stats() (uint64, lnwire.MilliSatoshi, lnwire.MilliSatoshi) {
|
2017-05-01 19:58:08 +03:00
|
|
|
return 0, 0, 0
|
|
|
|
}
|
|
|
|
|
2017-12-11 02:38:17 +03:00
|
|
|
func (f *mockChannelLink) AttachMailBox(mailBox MailBox) {
|
|
|
|
f.mailBox = mailBox
|
|
|
|
f.packets = mailBox.PacketOutBox()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *mockChannelLink) Start() error {
|
|
|
|
f.mailBox.ResetMessages()
|
|
|
|
f.mailBox.ResetPackets()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:30:12 +03:00
|
|
|
func (f *mockChannelLink) ChanID() lnwire.ChannelID { return f.chanID }
|
|
|
|
func (f *mockChannelLink) ShortChanID() lnwire.ShortChannelID { return f.shortChanID }
|
|
|
|
func (f *mockChannelLink) Bandwidth() lnwire.MilliSatoshi { return 99999999 }
|
2018-06-08 06:18:10 +03:00
|
|
|
func (f *mockChannelLink) Peer() lnpeer.Peer { return f.peer }
|
2018-10-30 12:36:27 +03:00
|
|
|
func (f *mockChannelLink) ChannelPoint() *wire.OutPoint { return &wire.OutPoint{} }
|
2018-05-02 02:30:12 +03:00
|
|
|
func (f *mockChannelLink) Stop() {}
|
|
|
|
func (f *mockChannelLink) EligibleToForward() bool { return f.eligible }
|
|
|
|
func (f *mockChannelLink) setLiveShortChanID(sid lnwire.ShortChannelID) { f.shortChanID = sid }
|
|
|
|
func (f *mockChannelLink) UpdateShortChanID() (lnwire.ShortChannelID, error) {
|
|
|
|
f.eligible = true
|
|
|
|
return f.shortChanID, nil
|
|
|
|
}
|
2017-05-01 19:58:08 +03:00
|
|
|
|
|
|
|
var _ ChannelLink = (*mockChannelLink)(nil)
|
2017-05-02 02:29:30 +03:00
|
|
|
|
2019-02-21 16:52:13 +03:00
|
|
|
func newDB() (*channeldb.DB, func(), error) {
|
|
|
|
// First, create a temporary directory to be used for the duration of
|
|
|
|
// this test.
|
|
|
|
tempDirName, err := ioutil.TempDir("", "channeldb")
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
2019-02-21 16:52:13 +03:00
|
|
|
// Next, create channeldb for the first time.
|
|
|
|
cdb, err := channeldb.Open(tempDirName)
|
|
|
|
if err != nil {
|
|
|
|
os.RemoveAll(tempDirName)
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2017-05-02 02:29:30 +03:00
|
|
|
|
2019-02-21 16:52:13 +03:00
|
|
|
cleanUp := func() {
|
|
|
|
cdb.Close()
|
|
|
|
os.RemoveAll(tempDirName)
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
2019-02-21 16:52:13 +03:00
|
|
|
return cdb, cleanUp, nil
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
2019-04-19 12:11:16 +03:00
|
|
|
const testInvoiceCltvExpiry = 6
|
2019-04-05 12:13:52 +03:00
|
|
|
|
2019-02-21 16:52:13 +03:00
|
|
|
type mockInvoiceRegistry struct {
|
|
|
|
settleChan chan lntypes.Hash
|
2018-04-25 06:43:55 +03:00
|
|
|
|
2019-02-21 16:52:13 +03:00
|
|
|
registry *invoices.InvoiceRegistry
|
2017-05-02 02:29:30 +03:00
|
|
|
|
2019-02-21 16:52:13 +03:00
|
|
|
cleanup func()
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockRegistry(minDelta uint32) *mockInvoiceRegistry {
|
|
|
|
cdb, cleanup, err := newDB()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
2019-12-04 20:47:53 +03:00
|
|
|
registry := invoices.NewRegistry(
|
|
|
|
cdb,
|
2019-12-09 19:50:11 +03:00
|
|
|
invoices.NewInvoiceExpiryWatcher(clock.NewDefaultClock()),
|
2019-12-04 20:47:53 +03:00
|
|
|
&invoices.RegistryConfig{
|
|
|
|
FinalCltvRejectDelta: 5,
|
|
|
|
},
|
|
|
|
)
|
2019-02-21 16:52:13 +03:00
|
|
|
registry.Start()
|
2017-05-02 02:29:30 +03:00
|
|
|
|
2019-02-21 16:52:13 +03:00
|
|
|
return &mockInvoiceRegistry{
|
|
|
|
registry: registry,
|
|
|
|
cleanup: cleanup,
|
|
|
|
}
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
2019-08-20 15:54:39 +03:00
|
|
|
func (i *mockInvoiceRegistry) LookupInvoice(rHash lntypes.Hash) (
|
|
|
|
channeldb.Invoice, error) {
|
|
|
|
|
2019-02-21 16:52:13 +03:00
|
|
|
return i.registry.LookupInvoice(rHash)
|
|
|
|
}
|
2019-01-11 13:19:16 +03:00
|
|
|
|
2019-02-11 14:01:05 +03:00
|
|
|
func (i *mockInvoiceRegistry) SettleHodlInvoice(preimage lntypes.Preimage) error {
|
|
|
|
return i.registry.SettleHodlInvoice(preimage)
|
|
|
|
}
|
|
|
|
|
2019-02-20 14:11:15 +03:00
|
|
|
func (i *mockInvoiceRegistry) NotifyExitHopHtlc(rhash lntypes.Hash,
|
2019-04-16 13:11:20 +03:00
|
|
|
amt lnwire.MilliSatoshi, expiry uint32, currentHeight int32,
|
2019-08-08 16:48:31 +03:00
|
|
|
circuitKey channeldb.CircuitKey, hodlChan chan<- interface{},
|
2020-02-06 20:35:10 +03:00
|
|
|
payload invoices.Payload) (invoices.HtlcResolution, error) {
|
2019-01-11 13:19:16 +03:00
|
|
|
|
2019-04-16 13:11:20 +03:00
|
|
|
event, err := i.registry.NotifyExitHopHtlc(
|
2019-11-05 02:10:32 +03:00
|
|
|
rhash, amt, expiry, currentHeight, circuitKey, hodlChan,
|
|
|
|
payload,
|
2019-04-16 13:11:20 +03:00
|
|
|
)
|
2019-02-21 16:52:13 +03:00
|
|
|
if err != nil {
|
2019-02-11 14:01:05 +03:00
|
|
|
return nil, err
|
2019-02-21 16:52:13 +03:00
|
|
|
}
|
|
|
|
if i.settleChan != nil {
|
|
|
|
i.settleChan <- rhash
|
2019-01-11 13:19:16 +03:00
|
|
|
}
|
|
|
|
|
2019-02-11 14:01:05 +03:00
|
|
|
return event, nil
|
2019-01-11 13:19:16 +03:00
|
|
|
}
|
|
|
|
|
2019-02-21 16:52:13 +03:00
|
|
|
func (i *mockInvoiceRegistry) CancelInvoice(payHash lntypes.Hash) error {
|
|
|
|
return i.registry.CancelInvoice(payHash)
|
|
|
|
}
|
2017-05-02 02:29:30 +03:00
|
|
|
|
2019-02-21 16:52:13 +03:00
|
|
|
func (i *mockInvoiceRegistry) AddInvoice(invoice channeldb.Invoice,
|
|
|
|
paymentHash lntypes.Hash) error {
|
2018-01-09 05:57:11 +03:00
|
|
|
|
2019-02-21 16:52:13 +03:00
|
|
|
_, err := i.registry.AddInvoice(&invoice, paymentHash)
|
|
|
|
return err
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
2019-02-11 14:01:05 +03:00
|
|
|
func (i *mockInvoiceRegistry) HodlUnsubscribeAll(subscriber chan<- interface{}) {
|
|
|
|
i.registry.HodlUnsubscribeAll(subscriber)
|
|
|
|
}
|
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
var _ InvoiceDatabase = (*mockInvoiceRegistry)(nil)
|
|
|
|
|
|
|
|
type mockSigner struct {
|
|
|
|
key *btcec.PrivateKey
|
|
|
|
}
|
|
|
|
|
2020-04-06 03:06:38 +03:00
|
|
|
func (m *mockSigner) SignOutputRaw(tx *wire.MsgTx,
|
|
|
|
signDesc *input.SignDescriptor) (input.Signature, error) {
|
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
amt := signDesc.Output.Value
|
|
|
|
witnessScript := signDesc.WitnessScript
|
|
|
|
privKey := m.key
|
|
|
|
|
2018-02-18 02:29:01 +03:00
|
|
|
if !privKey.PubKey().IsEqual(signDesc.KeyDesc.PubKey) {
|
2017-07-31 00:09:10 +03:00
|
|
|
return nil, fmt.Errorf("incorrect key passed")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case signDesc.SingleTweak != nil:
|
2019-01-16 17:47:43 +03:00
|
|
|
privKey = input.TweakPrivKey(privKey,
|
2017-07-31 00:09:10 +03:00
|
|
|
signDesc.SingleTweak)
|
|
|
|
case signDesc.DoubleTweak != nil:
|
2019-01-16 17:47:43 +03:00
|
|
|
privKey = input.DeriveRevocationPrivKey(privKey,
|
2017-07-31 00:09:10 +03:00
|
|
|
signDesc.DoubleTweak)
|
|
|
|
}
|
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
sig, err := txscript.RawTxInWitnessSignature(tx, signDesc.SigHashes,
|
2017-11-06 16:27:58 +03:00
|
|
|
signDesc.InputIndex, amt, witnessScript, signDesc.HashType,
|
2017-07-31 00:09:10 +03:00
|
|
|
privKey)
|
2017-05-02 02:29:30 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-06 03:06:38 +03:00
|
|
|
return btcec.ParseDERSignature(sig[:len(sig)-1], btcec.S256())
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
2019-01-16 17:47:43 +03:00
|
|
|
func (m *mockSigner) ComputeInputScript(tx *wire.MsgTx, signDesc *input.SignDescriptor) (*input.Script, error) {
|
2017-05-02 02:29:30 +03:00
|
|
|
|
2017-07-31 00:09:10 +03:00
|
|
|
// TODO(roasbeef): expose tweaked signer from lnwallet so don't need to
|
|
|
|
// duplicate this code?
|
|
|
|
|
|
|
|
privKey := m.key
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case signDesc.SingleTweak != nil:
|
2019-01-16 17:47:43 +03:00
|
|
|
privKey = input.TweakPrivKey(privKey,
|
2017-07-31 00:09:10 +03:00
|
|
|
signDesc.SingleTweak)
|
|
|
|
case signDesc.DoubleTweak != nil:
|
2019-01-16 17:47:43 +03:00
|
|
|
privKey = input.DeriveRevocationPrivKey(privKey,
|
2017-07-31 00:09:10 +03:00
|
|
|
signDesc.DoubleTweak)
|
|
|
|
}
|
|
|
|
|
2017-08-25 04:55:27 +03:00
|
|
|
witnessScript, err := txscript.WitnessSignature(tx, signDesc.SigHashes,
|
2017-05-02 02:29:30 +03:00
|
|
|
signDesc.InputIndex, signDesc.Output.Value, signDesc.Output.PkScript,
|
2017-11-06 16:27:58 +03:00
|
|
|
signDesc.HashType, privKey, true)
|
2017-05-02 02:29:30 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-16 17:47:43 +03:00
|
|
|
return &input.Script{
|
2017-05-02 02:29:30 +03:00
|
|
|
Witness: witnessScript,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockNotifier struct {
|
2018-03-26 20:09:00 +03:00
|
|
|
epochChan chan *chainntnfs.BlockEpoch
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
2018-05-31 08:15:59 +03:00
|
|
|
func (m *mockNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash, _ []byte,
|
2018-03-26 20:09:00 +03:00
|
|
|
numConfs uint32, heightHint uint32) (*chainntnfs.ConfirmationEvent, error) {
|
2017-05-02 02:29:30 +03:00
|
|
|
return nil, nil
|
|
|
|
}
|
2018-08-09 10:05:27 +03:00
|
|
|
func (m *mockNotifier) RegisterBlockEpochNtfn(
|
|
|
|
bestBlock *chainntnfs.BlockEpoch) (*chainntnfs.BlockEpochEvent, error) {
|
2018-03-26 20:09:00 +03:00
|
|
|
return &chainntnfs.BlockEpochEvent{
|
|
|
|
Epochs: m.epochChan,
|
|
|
|
Cancel: func() {},
|
|
|
|
}, nil
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockNotifier) Start() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-30 12:54:33 +03:00
|
|
|
func (m *mockNotifier) Started() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
func (m *mockNotifier) Stop() error {
|
|
|
|
return nil
|
|
|
|
}
|
2018-03-26 20:09:00 +03:00
|
|
|
|
2018-07-18 05:24:04 +03:00
|
|
|
func (m *mockNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint, _ []byte,
|
2018-07-17 10:13:06 +03:00
|
|
|
heightHint uint32) (*chainntnfs.SpendEvent, error) {
|
2018-03-26 20:09:00 +03:00
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
return &chainntnfs.SpendEvent{
|
|
|
|
Spend: make(chan *chainntnfs.SpendDetail),
|
|
|
|
}, nil
|
|
|
|
}
|
2019-06-07 17:42:26 +03:00
|
|
|
|
|
|
|
type mockCircuitMap struct {
|
|
|
|
lookup chan *PaymentCircuit
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ CircuitMap = (*mockCircuitMap)(nil)
|
|
|
|
|
|
|
|
func (m *mockCircuitMap) OpenCircuits(...Keystone) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockCircuitMap) TrimOpenCircuits(chanID lnwire.ShortChannelID,
|
|
|
|
start uint64) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockCircuitMap) DeleteCircuits(inKeys ...CircuitKey) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockCircuitMap) CommitCircuits(
|
|
|
|
circuit ...*PaymentCircuit) (*CircuitFwdActions, error) {
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockCircuitMap) CloseCircuit(outKey CircuitKey) (*PaymentCircuit,
|
|
|
|
error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockCircuitMap) FailCircuit(inKey CircuitKey) (*PaymentCircuit,
|
|
|
|
error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockCircuitMap) LookupCircuit(inKey CircuitKey) *PaymentCircuit {
|
|
|
|
return <-m.lookup
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockCircuitMap) LookupOpenCircuit(outKey CircuitKey) *PaymentCircuit {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockCircuitMap) LookupByPaymentHash(hash [32]byte) []*PaymentCircuit {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockCircuitMap) NumPending() int {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockCircuitMap) NumOpen() int {
|
|
|
|
return 0
|
|
|
|
}
|
2019-06-19 12:12:10 +03:00
|
|
|
|
|
|
|
type mockOnionErrorDecryptor struct {
|
|
|
|
sourceIdx int
|
|
|
|
message []byte
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockOnionErrorDecryptor) DecryptError(encryptedData []byte) (
|
|
|
|
*sphinx.DecryptedError, error) {
|
|
|
|
|
|
|
|
return &sphinx.DecryptedError{
|
|
|
|
SenderIdx: m.sourceIdx,
|
|
|
|
Message: m.message,
|
|
|
|
}, m.err
|
|
|
|
}
|
2020-02-19 18:34:47 +03:00
|
|
|
|
|
|
|
var _ htlcNotifier = (*mockHTLCNotifier)(nil)
|
|
|
|
|
|
|
|
type mockHTLCNotifier struct{}
|
|
|
|
|
|
|
|
func (h *mockHTLCNotifier) NotifyForwardingEvent(key HtlcKey, info HtlcInfo,
|
|
|
|
eventType HtlcEventType) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *mockHTLCNotifier) NotifyLinkFailEvent(key HtlcKey, info HtlcInfo,
|
|
|
|
eventType HtlcEventType, linkErr *LinkError, incoming bool) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *mockHTLCNotifier) NotifyForwardingFailEvent(key HtlcKey,
|
|
|
|
eventType HtlcEventType) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *mockHTLCNotifier) NotifySettleEvent(key HtlcKey, eventType HtlcEventType) {
|
|
|
|
}
|