2017-05-01 19:58:08 +03:00
|
|
|
package htlcswitch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
2017-05-02 02:29:30 +03:00
|
|
|
"encoding/binary"
|
2017-07-31 00:09:10 +03:00
|
|
|
"fmt"
|
2017-05-01 19:58:08 +03:00
|
|
|
"sync"
|
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
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
"io"
|
2017-05-01 19:58:08 +03:00
|
|
|
"sync/atomic"
|
|
|
|
|
2017-06-29 16:40:45 +03:00
|
|
|
"bytes"
|
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
"github.com/btcsuite/fastsha256"
|
2017-05-01 19:58:08 +03:00
|
|
|
"github.com/go-errors/errors"
|
2017-05-02 02:29:30 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2018-01-17 07:18:53 +03:00
|
|
|
"github.com/lightningnetwork/lnd/contractcourt"
|
2017-05-02 02:29:30 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2017-05-01 19:58:08 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2017-05-02 02:29:30 +03:00
|
|
|
"github.com/roasbeef/btcd/btcec"
|
|
|
|
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/roasbeef/btcd/txscript"
|
|
|
|
"github.com/roasbeef/btcd/wire"
|
2017-11-24 07:31:45 +03:00
|
|
|
"github.com/roasbeef/btcutil"
|
2017-05-01 19:58:08 +03:00
|
|
|
)
|
|
|
|
|
2018-01-17 07:18:53 +03:00
|
|
|
type mockPreimageCache struct {
|
|
|
|
sync.Mutex
|
|
|
|
preimageMap map[[32]byte][]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockPreimageCache) LookupPreimage(hash []byte) ([]byte, bool) {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
var h [32]byte
|
|
|
|
copy(h[:], hash)
|
|
|
|
|
|
|
|
p, ok := m.preimageMap[h]
|
|
|
|
return p, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockPreimageCache) AddPreimage(preimage []byte) error {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
m.preimageMap[sha256.Sum256(preimage[:])] = preimage
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockPreimageCache) SubcribeUpdates() *contractcourt.WitnessSubcription {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-24 07:31:45 +03:00
|
|
|
type mockFeeEstimator struct {
|
|
|
|
byteFeeIn chan btcutil.Amount
|
|
|
|
weightFeeIn chan btcutil.Amount
|
|
|
|
|
|
|
|
quit chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockFeeEstimator) EstimateFeePerByte(numBlocks uint32) (btcutil.Amount, error) {
|
|
|
|
select {
|
|
|
|
case feeRate := <-m.byteFeeIn:
|
|
|
|
return feeRate, nil
|
|
|
|
case <-m.quit:
|
|
|
|
return 0, fmt.Errorf("exiting")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockFeeEstimator) EstimateFeePerWeight(numBlocks uint32) (btcutil.Amount, error) {
|
|
|
|
select {
|
|
|
|
case feeRate := <-m.weightFeeIn:
|
|
|
|
return feeRate, nil
|
|
|
|
case <-m.quit:
|
|
|
|
return 0, fmt.Errorf("exiting")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockFeeEstimator) Start() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (m *mockFeeEstimator) Stop() error {
|
|
|
|
close(m.quit)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ lnwallet.FeeEstimator = (*mockFeeEstimator)(nil)
|
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
type mockServer struct {
|
|
|
|
started int32
|
|
|
|
shutdown int32
|
|
|
|
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-07-09 01:52:51 +03:00
|
|
|
errChan chan error
|
|
|
|
|
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
|
|
|
|
interceptorFuncs []messageInterceptor
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ Peer = (*mockServer)(nil)
|
|
|
|
|
2017-11-12 02:05:09 +03:00
|
|
|
func newMockServer(t testing.TB, name string) *mockServer {
|
2017-06-17 01:08:19 +03:00
|
|
|
var id [33]byte
|
|
|
|
h := sha256.Sum256([]byte(name))
|
|
|
|
copy(id[:], h[:])
|
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
return &mockServer{
|
2017-11-11 02:09:19 +03:00
|
|
|
t: t,
|
2017-07-09 01:46:27 +03:00
|
|
|
id: id,
|
|
|
|
name: name,
|
|
|
|
messages: make(chan lnwire.Message, 3000),
|
2017-07-09 01:52:51 +03:00
|
|
|
quit: make(chan struct{}),
|
2017-07-09 01:46:27 +03:00
|
|
|
registry: newMockRegistry(),
|
|
|
|
htlcSwitch: New(Config{}),
|
|
|
|
interceptorFuncs: make([]messageInterceptor, 0),
|
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
|
|
|
|
}
|
|
|
|
|
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 {
|
2017-06-17 01:08:19 +03:00
|
|
|
hops []ForwardingInfo
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
2017-06-17 01:08:19 +03:00
|
|
|
func newMockHopIterator(hops ...ForwardingInfo) HopIterator {
|
2017-05-02 02:29:30 +03:00
|
|
|
return &mockHopIterator{hops: hops}
|
|
|
|
}
|
|
|
|
|
2017-06-17 01:08:19 +03:00
|
|
|
func (r *mockHopIterator) ForwardingInstructions() ForwardingInfo {
|
|
|
|
h := r.hops[0]
|
|
|
|
r.hops = r.hops[1:]
|
|
|
|
return h
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
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 {
|
2017-06-17 01:08:19 +03:00
|
|
|
if err := hop.encode(w); err != nil {
|
2017-05-02 02:29:30 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-17 01:08:19 +03:00
|
|
|
func (f *ForwardingInfo) encode(w io.Writer) error {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
var _ HopIterator = (*mockHopIterator)(nil)
|
|
|
|
|
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.
|
|
|
|
type mockObfuscator struct{}
|
|
|
|
|
2017-10-11 05:36:52 +03:00
|
|
|
func newMockObfuscator() ErrorEncrypter {
|
2017-06-29 16:40:45 +03:00
|
|
|
return &mockObfuscator{}
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
return &ForwardingError{
|
|
|
|
FailureMessage: failure,
|
|
|
|
}, 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.
|
|
|
|
type mockIteratorDecoder struct{}
|
|
|
|
|
2017-06-29 16:40:45 +03:00
|
|
|
func (p *mockIteratorDecoder) DecodeHopIterator(r io.Reader, meta []byte) (
|
|
|
|
HopIterator, lnwire.FailCode) {
|
|
|
|
|
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[:])
|
|
|
|
|
2017-06-17 01:08:19 +03:00
|
|
|
hops := make([]ForwardingInfo, hopLength)
|
2017-05-02 02:29:30 +03:00
|
|
|
for i := uint32(0); i < hopLength; i++ {
|
2017-06-17 01:08:19 +03:00
|
|
|
f := &ForwardingInfo{}
|
|
|
|
if err := f.decode(r); err != nil {
|
2017-06-29 16:40:45 +03:00
|
|
|
return nil, lnwire.CodeTemporaryChannelFailure
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
2017-06-17 01:08:19 +03:00
|
|
|
hops[i] = *f
|
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
|
|
|
}
|
|
|
|
|
2017-06-17 01:08:19 +03:00
|
|
|
func (f *ForwardingInfo) decode(r io.Reader) error {
|
|
|
|
var net [1]byte
|
|
|
|
if _, err := r.Read(net[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.Network = NetworkHop(net[0])
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (s *mockServer) SendMessage(message lnwire.Message) error {
|
2017-11-11 02:15:19 +03:00
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
select {
|
|
|
|
case s.messages <- message:
|
|
|
|
case <-s.quit:
|
2017-07-09 02:30:20 +03:00
|
|
|
return errors.New("server is stopped")
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *mockServer) readHandler(message lnwire.Message) error {
|
|
|
|
var targetChan lnwire.ChannelID
|
|
|
|
|
|
|
|
switch msg := message.(type) {
|
|
|
|
case *lnwire.UpdateAddHTLC:
|
|
|
|
targetChan = msg.ChanID
|
|
|
|
case *lnwire.UpdateFufillHTLC:
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Dispatch the commitment update message to the proper
|
|
|
|
// channel link dedicated to this channel.
|
|
|
|
link, err := s.htlcSwitch.GetLink(targetChan)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2017-07-12 16:44:17 +03:00
|
|
|
func (s *mockServer) Disconnect(reason error) {
|
2017-07-31 00:09:10 +03:00
|
|
|
fmt.Printf("server %v disconnected due to %v\n", s.name, reason)
|
|
|
|
|
2017-11-11 02:15:19 +03:00
|
|
|
s.t.Fatalf("server %v was disconnected: %v", s.name, reason)
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
2017-11-23 10:15:21 +03:00
|
|
|
func (s *mockServer) WipeChannel(*wire.OutPoint) error {
|
2017-05-03 17:02:22 +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
|
|
|
|
|
|
|
|
peer Peer
|
|
|
|
|
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
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
2017-11-02 23:53:42 +03:00
|
|
|
func newMockChannelLink(htlcSwitch *Switch, chanID lnwire.ChannelID,
|
|
|
|
shortChanID lnwire.ShortChannelID, peer Peer, eligible bool,
|
|
|
|
) *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,
|
|
|
|
packets: make(chan *htlcPacket, 1),
|
|
|
|
peer: peer,
|
2017-12-11 03:19:40 +03:00
|
|
|
eligible: eligible,
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *mockChannelLink) HandleSwitchPacket(packet *htlcPacket) {
|
2017-11-02 23:53:42 +03:00
|
|
|
switch htlc := packet.htlc.(type) {
|
|
|
|
case *lnwire.UpdateAddHTLC:
|
|
|
|
f.htlcSwitch.addCircuit(&PaymentCircuit{
|
|
|
|
PaymentHash: htlc.PaymentHash,
|
|
|
|
IncomingChanID: packet.incomingChanID,
|
|
|
|
IncomingHTLCID: packet.incomingHTLCID,
|
|
|
|
OutgoingChanID: f.shortChanID,
|
|
|
|
OutgoingHTLCID: f.htlcID,
|
|
|
|
ErrorEncrypter: packet.obfuscator,
|
|
|
|
})
|
|
|
|
f.htlcID++
|
|
|
|
}
|
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
f.packets <- packet
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *mockChannelLink) HandleChannelUpdate(lnwire.Message) {
|
|
|
|
}
|
|
|
|
|
2017-06-17 01:08:19 +03:00
|
|
|
func (f *mockChannelLink) UpdateForwardingPolicy(_ ForwardingPolicy) {
|
|
|
|
}
|
|
|
|
|
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-06-17 01:08:19 +03:00
|
|
|
func (f *mockChannelLink) ChanID() lnwire.ChannelID { return f.chanID }
|
|
|
|
func (f *mockChannelLink) ShortChanID() lnwire.ShortChannelID { return f.shortChanID }
|
2017-08-22 09:36:43 +03:00
|
|
|
func (f *mockChannelLink) Bandwidth() lnwire.MilliSatoshi { return 99999999 }
|
2017-06-17 01:08:19 +03:00
|
|
|
func (f *mockChannelLink) Peer() Peer { return f.peer }
|
|
|
|
func (f *mockChannelLink) Start() error { return nil }
|
|
|
|
func (f *mockChannelLink) Stop() {}
|
2017-12-11 03:19:40 +03:00
|
|
|
func (f *mockChannelLink) EligibleToForward() bool { return f.eligible }
|
2017-05-01 19:58:08 +03:00
|
|
|
|
|
|
|
var _ ChannelLink = (*mockChannelLink)(nil)
|
2017-05-02 02:29:30 +03:00
|
|
|
|
|
|
|
type mockInvoiceRegistry struct {
|
|
|
|
sync.Mutex
|
2017-11-12 03:09:14 +03:00
|
|
|
invoices map[chainhash.Hash]channeldb.Invoice
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func newMockRegistry() *mockInvoiceRegistry {
|
|
|
|
return &mockInvoiceRegistry{
|
2017-11-12 03:09:14 +03:00
|
|
|
invoices: make(map[chainhash.Hash]channeldb.Invoice),
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-12 03:09:14 +03:00
|
|
|
func (i *mockInvoiceRegistry) LookupInvoice(rHash chainhash.Hash) (channeldb.Invoice, error) {
|
2017-05-02 02:29:30 +03:00
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
|
|
|
|
invoice, ok := i.invoices[rHash]
|
|
|
|
if !ok {
|
2018-01-09 05:57:11 +03:00
|
|
|
return channeldb.Invoice{}, fmt.Errorf("can't find mock invoice: %x", rHash[:])
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return invoice, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *mockInvoiceRegistry) SettleInvoice(rhash chainhash.Hash) error {
|
2017-11-12 02:05:52 +03:00
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
2017-05-02 02:29:30 +03:00
|
|
|
|
2017-11-12 02:05:52 +03:00
|
|
|
invoice, ok := i.invoices[rhash]
|
|
|
|
if !ok {
|
2018-01-09 05:57:11 +03:00
|
|
|
return fmt.Errorf("can't find mock invoice: %x", rhash[:])
|
2017-05-02 02:29:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
invoice.Terms.Settled = true
|
2017-11-12 03:09:14 +03:00
|
|
|
i.invoices[rhash] = invoice
|
2017-05-02 02:29:30 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-12 03:09:14 +03:00
|
|
|
func (i *mockInvoiceRegistry) AddInvoice(invoice channeldb.Invoice) error {
|
2017-05-02 02:29:30 +03:00
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
|
|
|
|
rhash := fastsha256.Sum256(invoice.Terms.PaymentPreimage[:])
|
|
|
|
i.invoices[chainhash.Hash(rhash)] = invoice
|
2018-01-09 05:57:11 +03:00
|
|
|
|
2017-05-02 02:29:30 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ InvoiceDatabase = (*mockInvoiceRegistry)(nil)
|
|
|
|
|
|
|
|
type mockSigner struct {
|
|
|
|
key *btcec.PrivateKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockSigner) SignOutputRaw(tx *wire.MsgTx, signDesc *lnwallet.SignDescriptor) ([]byte, error) {
|
|
|
|
amt := signDesc.Output.Value
|
|
|
|
witnessScript := signDesc.WitnessScript
|
|
|
|
privKey := m.key
|
|
|
|
|
2017-07-31 00:09:10 +03:00
|
|
|
if !privKey.PubKey().IsEqual(signDesc.PubKey) {
|
|
|
|
return nil, fmt.Errorf("incorrect key passed")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case signDesc.SingleTweak != nil:
|
|
|
|
privKey = lnwallet.TweakPrivKey(privKey,
|
|
|
|
signDesc.SingleTweak)
|
|
|
|
case signDesc.DoubleTweak != nil:
|
|
|
|
privKey = lnwallet.DeriveRevocationPrivKey(privKey,
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return sig[:len(sig)-1], nil
|
|
|
|
}
|
|
|
|
func (m *mockSigner) ComputeInputScript(tx *wire.MsgTx, signDesc *lnwallet.SignDescriptor) (*lnwallet.InputScript, error) {
|
|
|
|
|
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:
|
|
|
|
privKey = lnwallet.TweakPrivKey(privKey,
|
|
|
|
signDesc.SingleTweak)
|
|
|
|
case signDesc.DoubleTweak != nil:
|
|
|
|
privKey = lnwallet.DeriveRevocationPrivKey(privKey,
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return &lnwallet.InputScript{
|
|
|
|
Witness: witnessScript,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockNotifier struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash, numConfs uint32) (*chainntnfs.ConfirmationEvent, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
func (m *mockNotifier) RegisterBlockEpochNtfn() (*chainntnfs.BlockEpochEvent, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockNotifier) Start() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockNotifier) Stop() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (m *mockNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint) (*chainntnfs.SpendEvent, error) {
|
|
|
|
return &chainntnfs.SpendEvent{
|
|
|
|
Spend: make(chan *chainntnfs.SpendDetail),
|
|
|
|
}, nil
|
|
|
|
}
|
2018-01-31 02:53:39 +03:00
|
|
|
|
|
|
|
type mockTicker struct {
|
|
|
|
ticker <-chan time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockTicker) Start() <-chan time.Time {
|
|
|
|
return m.ticker
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockTicker) Stop() {
|
|
|
|
}
|