Skip to content

Commit

Permalink
peer: add channel close fee negotiation
Browse files Browse the repository at this point in the history
This commit adds the fee negotiation procedure performed
on channel shutdown. The current algorithm picks an ideal
a fee based on the FeeEstimator and commit weigth, then
accepts the remote's fee if it is at most 50%-200% away
from the ideal. The fee negotiation procedure is similar
both as sender and receiver of the initial shutdown
message, and this commit also make both sides use the
same code path for handling these messages.
  • Loading branch information
halseth committed Aug 7, 2017
1 parent b616616 commit 60bb5c3
Show file tree
Hide file tree
Showing 4 changed files with 1,256 additions and 198 deletions.
158 changes: 158 additions & 0 deletions mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package main

import (
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/roasbeef/btcd/btcec"
"github.com/roasbeef/btcd/chaincfg"
"github.com/roasbeef/btcd/chaincfg/chainhash"
"github.com/roasbeef/btcd/txscript"
"github.com/roasbeef/btcd/wire"
"github.com/roasbeef/btcutil"
)

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

sig, err := txscript.RawTxInWitnessSignature(tx, signDesc.SigHashes,
signDesc.InputIndex, amt, witnessScript, txscript.SigHashAll, privKey)
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) {

witnessScript, err := txscript.WitnessScript(tx, signDesc.SigHashes,
signDesc.InputIndex, signDesc.Output.Value, signDesc.Output.PkScript,
txscript.SigHashAll, m.key, true)
if err != nil {
return nil, err
}

return &lnwallet.InputScript{
Witness: witnessScript,
}, nil
}

type mockNotfier struct {
confChannel chan *chainntnfs.TxConfirmation
}

func (m *mockNotfier) RegisterConfirmationsNtfn(txid *chainhash.Hash, numConfs,
heightHint uint32) (*chainntnfs.ConfirmationEvent, error) {
return &chainntnfs.ConfirmationEvent{
Confirmed: m.confChannel,
}, nil
}
func (m *mockNotfier) RegisterBlockEpochNtfn() (*chainntnfs.BlockEpochEvent,
error) {
return nil, nil
}

func (m *mockNotfier) Start() error {
return nil
}

func (m *mockNotfier) Stop() error {
return nil
}
func (m *mockNotfier) RegisterSpendNtfn(outpoint *wire.OutPoint,
heightHint uint32) (*chainntnfs.SpendEvent, error) {
return &chainntnfs.SpendEvent{
Spend: make(chan *chainntnfs.SpendDetail),
Cancel: func() {},
}, nil
}

type mockChainIO struct{}

func (m *mockChainIO) GetBestBlock() (*chainhash.Hash, int32, error) {
return nil, 0, nil
}

func (m *mockChainIO) GetUtxo(op *wire.OutPoint,
heightHint uint32) (*wire.TxOut, error) {
return nil, nil
}

func (m *mockChainIO) GetBlockHash(blockHeight int64) (*chainhash.Hash, error) {
return nil, nil
}

func (m *mockChainIO) GetBlock(
blockHash *chainhash.Hash) (*wire.MsgBlock, error) {
return nil, nil
}

type mockWalletController struct {
publTxChan chan *wire.MsgTx
}

func (m *mockWalletController) FetchInputInfo(
prevOut *wire.OutPoint) (*wire.TxOut, error) {
return nil, nil
}
func (m *mockWalletController) ConfirmedBalance(confs int32,
witness bool) (btcutil.Amount, error) {
return 0, nil
}
func (m *mockWalletController) NewAddress(addrType lnwallet.AddressType,
change bool) (btcutil.Address, error) {
randBytes := []byte("1234567")
pubkeyHash := btcutil.Hash160(randBytes)
return btcutil.NewAddressWitnessPubKeyHash(pubkeyHash,
&chaincfg.SimNetParams)
}
func (m *mockWalletController) GetPrivKey(
a btcutil.Address) (*btcec.PrivateKey, error) {
return nil, nil
}
func (m *mockWalletController) NewRawKey() (*btcec.PublicKey, error) {
return nil, nil
}
func (m *mockWalletController) FetchRootKey() (*btcec.PrivateKey, error) {
return nil, nil
}
func (m *mockWalletController) SendOutputs(
outputs []*wire.TxOut) (*chainhash.Hash, error) {
return nil, nil
}
func (m *mockWalletController) ListUnspentWitness(
confirms int32) ([]*lnwallet.Utxo, error) {
return nil, nil
}
func (m *mockWalletController) ListTransactionDetails() ([]*lnwallet.TransactionDetail, error) {
return nil, nil
}
func (m *mockWalletController) LockOutpoint(o wire.OutPoint) {

}
func (m *mockWalletController) UnlockOutpoint(o wire.OutPoint) {

}
func (m *mockWalletController) PublishTransaction(tx *wire.MsgTx) error {
m.publTxChan <- tx
return nil
}
func (m *mockWalletController) SubscribeTransactions() (lnwallet.TransactionSubscription, error) {
return nil, nil
}
func (m *mockWalletController) IsSynced() (bool, error) {
return false, nil
}
func (m *mockWalletController) Start() error {
return nil
}
func (m *mockWalletController) Stop() error {
return nil
}

0 comments on commit 60bb5c3

Please sign in to comment.