lnwallet: embed channel state within ChannelReservation

This commit is contained in:
Olaoluwa Osuntokun 2015-12-18 21:39:51 -06:00
parent 6bb37448f0
commit 2f7a801dcb
2 changed files with 80 additions and 79 deletions

@ -1,11 +1,8 @@
package wallet package wallet
import ( import (
"fmt"
"sync" "sync"
"bytes"
"encoding/binary"
"github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
@ -13,19 +10,11 @@ import (
// ChannelReservation... // ChannelReservation...
type ChannelReservation struct { type ChannelReservation struct {
FundingType FundingType sync.RWMutex // All fields below owned by the lnwallet.
//All of these are *our* values/requirements
//Their requirements can be the same or lower
FundingAmount btcutil.Amount //The amount we want to fund with
ReserveAmount btcutil.Amount //Our reserve. assume symmetric reserve amounts
MinFeePerKb btcutil.Amount
MinTotalFundingAmount btcutil.Amount //Our minimum value for the entire channel
//for CLTV it is nLockTime, for CSV it's nSequence, for segwit it's not needed //for CLTV it is nLockTime, for CSV it's nSequence, for segwit it's not needed
FundingLockTime uint32 fundingLockTime uint32
fundingAmount btcutil.Amount
sync.RWMutex // All fields below owned by the lnwallet.
//Current state of the channel, progesses through until complete //Current state of the channel, progesses through until complete
//Makes sure we can't go backwards and only accept messages once //Makes sure we can't go backwards and only accept messages once
@ -34,38 +23,25 @@ type ChannelReservation struct {
theirInputs []*wire.TxIn theirInputs []*wire.TxIn
ourInputs []*wire.TxIn ourInputs []*wire.TxIn
//NOTE(j): FundRequest assumes there is only one change (see ChangePkScript) // NOTE(j): FundRequest assumes there is only one change (see ChangePkScript)
theirChange []*wire.TxOut theirChange []*wire.TxOut
ourChange []*wire.TxOut ourChange []*wire.TxOut
ourKey *btcec.PrivateKey theirMultiSigKey *btcec.PublicKey
theirKey *btcec.PublicKey
// In order of sorted inputs. Sorting is done in accordance // In order of sorted inputs. Sorting is done in accordance
// to BIP-69: https://github.com/bitcoin/bips/blob/master/bip-0069.mediawiki. // to BIP-69: https://github.com/bitcoin/bips/blob/master/bip-0069.mediawiki.
theirSigs [][]byte ourFundingSigs [][]byte
ourSigs [][]byte theirFundingSigs [][]byte
normalizedTxID wire.ShaHash ourCommitmentSig []byte
theirCommitmentSig []byte
fundingTx *wire.MsgTx partialState *OpenChannelState
// TODO(roasbef): time locks, who pays fee etc.
// TODO(roasbeef): record Bob's ln-ID?
completedFundingTx *btcutil.Tx
reservationID uint64 reservationID uint64
wallet *LightningWallet wallet *LightningWallet
//For CSV/CLTV revocation
ourRevocation []byte
theirRevocation []byte
theirRevocationHash []byte
//Final delivery address (P2PKH for now)
ourDeliveryAddress []byte
theirDeliveryAddress []byte
chanOpen chan *LightningChannel chanOpen chan *LightningChannel
} }
@ -249,22 +225,32 @@ func (r *ChannelReservation) DeserializeCSVRefundRevocation() error {
func newChannelReservation(t FundingType, fundingAmt btcutil.Amount, func newChannelReservation(t FundingType, fundingAmt btcutil.Amount,
minFeeRate btcutil.Amount, wallet *LightningWallet, id uint64) *ChannelReservation { minFeeRate btcutil.Amount, wallet *LightningWallet, id uint64) *ChannelReservation {
return &ChannelReservation{ return &ChannelReservation{
FundingType: t, fundingAmount: fundingAmt,
FundingAmount: fundingAmt, // TODO(roasbeef): assumes balanced symmetric channels.
MinFeePerKb: minFeeRate, partialState: &OpenChannelState{
capacity: fundingAmt * 2,
fundingType: t,
},
wallet: wallet, wallet: wallet,
reservationID: id, reservationID: id,
} }
} }
// OurFunds... // OurFunds...
func (r *ChannelReservation) OurFunds() ([]*wire.TxIn, []*wire.TxOut, *btcec.PublicKey) { func (r *ChannelReservation) OurFunds() ([]*wire.TxIn, []*wire.TxOut) {
r.RLock() r.RLock()
defer r.RUnlock() defer r.RUnlock()
return r.ourInputs, r.ourChange, r.ourKey.PubKey() return r.ourInputs, r.ourChange
} }
// AddCounterPartyFunds... func (r *ChannelReservation) OurKeys() (*btcec.PrivateKey, *btcec.PrivateKey) {
r.RLock()
defer r.RUnlock()
return r.partialState.multiSigKey, r.partialState.ourCommitKey
}
// AddFunds...
// TODO(roasbeef): add commitment txns, etc.
func (r *ChannelReservation) AddFunds(theirInputs []*wire.TxIn, theirChangeOutputs []*wire.TxOut, multiSigKey *btcec.PublicKey) error { func (r *ChannelReservation) AddFunds(theirInputs []*wire.TxIn, theirChangeOutputs []*wire.TxOut, multiSigKey *btcec.PublicKey) error {
errChan := make(chan error, 1) errChan := make(chan error, 1)
@ -279,22 +265,36 @@ func (r *ChannelReservation) AddFunds(theirInputs []*wire.TxIn, theirChangeOutpu
return <-errChan return <-errChan
} }
// OurSigs... // OurFundingSigs...
func (r *ChannelReservation) OurSigs() [][]byte { func (r *ChannelReservation) OurFundingSigs() [][]byte {
r.RLock() r.RLock()
defer r.RUnlock() defer r.RUnlock()
return r.ourSigs return r.ourFundingSigs
}
// OurCommitmentSig
func (r *ChannelReservation) OurCommitmentSig() []byte {
r.RLock()
defer r.RUnlock()
return r.ourCommitmentSig
} }
// TheirFunds... // TheirFunds...
// TODO(roasbeef): return error if accessors not yet populated? // TODO(roasbeef): return error if accessors not yet populated?
func (r *ChannelReservation) TheirFunds() ([]*wire.TxIn, []*wire.TxOut, *btcec.PublicKey) { func (r *ChannelReservation) TheirFunds() ([]*wire.TxIn, []*wire.TxOut) {
r.RLock() r.RLock()
defer r.RUnlock() defer r.RUnlock()
return r.theirInputs, r.theirChange, r.theirKey return r.theirInputs, r.theirChange
}
func (r *ChannelReservation) TheirKeys() (*btcec.PublicKey, *btcec.PublicKey) {
r.RLock()
defer r.RUnlock()
return r.theirMultiSigKey, r.partialState.theirCommitKey
} }
// CompleteFundingReservation... // CompleteFundingReservation...
// TODO(roasbeef): add commit sig also
func (r *ChannelReservation) CompleteReservation(theirSigs [][]byte) error { func (r *ChannelReservation) CompleteReservation(theirSigs [][]byte) error {
errChan := make(chan error, 1) errChan := make(chan error, 1)
@ -308,10 +308,10 @@ func (r *ChannelReservation) CompleteReservation(theirSigs [][]byte) error {
} }
// FinalFundingTransaction... // FinalFundingTransaction...
func (r *ChannelReservation) FinalFundingTx() *btcutil.Tx { func (r *ChannelReservation) FundingTx() *wire.MsgTx {
r.RLock() r.RLock()
defer r.RUnlock() defer r.RUnlock()
return r.completedFundingTx return r.partialState.fundingTx
} }
// RequestFundingReserveCancellation... // RequestFundingReserveCancellation...

@ -311,15 +311,15 @@ func (l *LightningWallet) handleFundingReserveRequest(req *initFundingReserveMsg
l.limboMtx.Lock() l.limboMtx.Lock()
id := l.nextFundingID id := l.nextFundingID
partialState := newChannelReservation(req.fundingType, req.fundingAmount, req.minFeeRate, l, id) reservation := newChannelReservation(req.fundingType, req.fundingAmount, req.minFeeRate, l, id)
l.nextFundingID++ l.nextFundingID++
l.fundingLimbo[id] = partialState l.fundingLimbo[id] = reservation
l.limboMtx.Unlock() l.limboMtx.Unlock()
// Grab the mutex on the ChannelReservation to ensure thead-safety // Grab the mutex on the ChannelReservation to ensure thead-safety
partialState.Lock() reservation.Lock()
defer partialState.Unlock() defer reservation.Unlock()
// Find all unlocked unspent outputs with greater than 6 confirmations. // Find all unlocked unspent outputs with greater than 6 confirmations.
maxConfs := int32(math.MaxInt32) maxConfs := int32(math.MaxInt32)
@ -361,7 +361,7 @@ func (l *LightningWallet) handleFundingReserveRequest(req *initFundingReserveMsg
// Lock the selected coins. These coins are now "reserved", this // Lock the selected coins. These coins are now "reserved", this
// prevents concurrent funding requests from referring to and this // prevents concurrent funding requests from referring to and this
// double-spending the same set of coins. // double-spending the same set of coins.
partialState.ourInputs = make([]*wire.TxIn, len(selectedCoins.Coins())) reservation.ourInputs = make([]*wire.TxIn, len(selectedCoins.Coins()))
for i, coin := range selectedCoins.Coins() { for i, coin := range selectedCoins.Coins() {
txout := wire.NewOutPoint(coin.Hash(), coin.Index()) txout := wire.NewOutPoint(coin.Hash(), coin.Index())
l.wallet.LockOutpoint(*txout) l.wallet.LockOutpoint(*txout)
@ -369,12 +369,12 @@ func (l *LightningWallet) handleFundingReserveRequest(req *initFundingReserveMsg
// Empty sig script, we'll actually sign if this reservation is // Empty sig script, we'll actually sign if this reservation is
// queued up to be completed (the other side accepts). // queued up to be completed (the other side accepts).
outPoint := wire.NewOutPoint(coin.Hash(), coin.Index()) outPoint := wire.NewOutPoint(coin.Hash(), coin.Index())
partialState.ourInputs[i] = wire.NewTxIn(outPoint, nil) reservation.ourInputs[i] = wire.NewTxIn(outPoint, nil)
} }
// Create some possibly neccessary change outputs. // Create some possibly neccessary change outputs.
selectedTotalValue := coinset.NewCoinSet(selectedCoins.Coins()).TotalValue() selectedTotalValue := coinset.NewCoinSet(selectedCoins.Coins()).TotalValue()
partialState.ourChange = make([]*wire.TxOut, 0, len(selectedCoins.Coins())) reservation.ourChange = make([]*wire.TxOut, 0, len(selectedCoins.Coins()))
if selectedTotalValue > req.fundingAmount { if selectedTotalValue > req.fundingAmount {
// Change is necessary. Query for an available change address to // Change is necessary. Query for an available change address to
// send the remainder to. // send the remainder to.
@ -396,7 +396,7 @@ func (l *LightningWallet) handleFundingReserveRequest(req *initFundingReserveMsg
// between chain-client and wallet. // between chain-client and wallet.
//changeAddr, err := l.wallet.NewChangeAddress(waddrmgr.DefaultAccountNum) //changeAddr, err := l.wallet.NewChangeAddress(waddrmgr.DefaultAccountNum)
partialState.ourChange = append(partialState.ourChange, reservation.ourChange = append(reservation.ourChange,
wire.NewTxOut(int64(changeAmount), changeAddrScript)) wire.NewTxOut(int64(changeAmount), changeAddrScript))
} }
@ -409,12 +409,12 @@ func (l *LightningWallet) handleFundingReserveRequest(req *initFundingReserveMsg
return return
} }
partialState.ourKey = multiSigKey reservation.partialState.multiSigKey = multiSigKey
// Funding reservation request succesfully handled. The funding inputs // Funding reservation request succesfully handled. The funding inputs
// will be marked as unavailable until the reservation is either // will be marked as unavailable until the reservation is either
// completed, or cancecled. // completed, or cancecled.
req.resp <- partialState req.resp <- reservation
req.err <- nil req.err <- nil
} }
@ -468,7 +468,7 @@ func (l *LightningWallet) handleFundingCounterPartyFunds(req *addCounterPartyFun
// Create a blank, fresh transaction. Soon to be a complete funding // Create a blank, fresh transaction. Soon to be a complete funding
// transaction which will allow opening a lightning channel. // transaction which will allow opening a lightning channel.
pendingReservation.fundingTx = wire.NewMsgTx() pendingReservation.partialState.fundingTx = wire.NewMsgTx()
// First, add all multi-party inputs to the transaction // First, add all multi-party inputs to the transaction
// TODO(roasbeef); handle case that tx doesn't exist, fake input // TODO(roasbeef); handle case that tx doesn't exist, fake input
@ -477,21 +477,21 @@ func (l *LightningWallet) handleFundingCounterPartyFunds(req *addCounterPartyFun
// is unspent // is unspent
// * or, something like getutxo? // * or, something like getutxo?
for _, ourInput := range pendingReservation.ourInputs { for _, ourInput := range pendingReservation.ourInputs {
pendingReservation.fundingTx.AddTxIn(ourInput) pendingReservation.partialState.fundingTx.AddTxIn(ourInput)
} }
pendingReservation.theirInputs = req.theirInputs pendingReservation.theirInputs = req.theirInputs
for _, theirInput := range pendingReservation.theirInputs { for _, theirInput := range pendingReservation.theirInputs {
pendingReservation.fundingTx.AddTxIn(theirInput) pendingReservation.partialState.fundingTx.AddTxIn(theirInput)
} }
// Next, add all multi-party outputs to the transaction. This includes // Next, add all multi-party outputs to the transaction. This includes
// change outputs for both side. // change outputs for both side.
for _, ourChangeOutput := range pendingReservation.ourChange { for _, ourChangeOutput := range pendingReservation.ourChange {
pendingReservation.fundingTx.AddTxOut(ourChangeOutput) pendingReservation.partialState.fundingTx.AddTxOut(ourChangeOutput)
} }
pendingReservation.theirChange = req.theirChangeOutputs pendingReservation.theirChange = req.theirChangeOutputs
for _, theirChangeOutput := range pendingReservation.theirChange { for _, theirChangeOutput := range pendingReservation.theirChange {
pendingReservation.fundingTx.AddTxOut(theirChangeOutput) pendingReservation.partialState.fundingTx.AddTxOut(theirChangeOutput)
} }
// Finally, add the 2-of-2 multi-sig output which will set up the lightning // Finally, add the 2-of-2 multi-sig output which will set up the lightning
@ -502,8 +502,8 @@ func (l *LightningWallet) handleFundingCounterPartyFunds(req *addCounterPartyFun
keys := make([]*btcutil.AddressPubKey, 2) keys := make([]*btcutil.AddressPubKey, 2)
ourKey := pendingReservation.ourKey.PubKey().SerializeCompressed() ourKey := pendingReservation.ourKey.PubKey().SerializeCompressed()
keys[0], _ = btcutil.NewAddressPubKey(ourKey, ActiveNetParams) keys[0], _ = btcutil.NewAddressPubKey(ourKey, ActiveNetParams)
pendingReservation.theirKey = req.theirKey pendingReservation.theirMultiSigKey = req.theirKey
keys[1], _ = btcutil.NewAddressPubKey(pendingReservation.theirKey.SerializeCompressed(), ActiveNetParams) keys[1], _ = btcutil.NewAddressPubKey(pendingReservation.theirMultiSigKey.SerializeCompressed(), ActiveNetParams)
multiSigScript, err := txscript.MultiSigScript(keys, 2) multiSigScript, err := txscript.MultiSigScript(keys, 2)
if err != nil { if err != nil {
req.err <- err req.err <- err
@ -511,22 +511,22 @@ func (l *LightningWallet) handleFundingCounterPartyFunds(req *addCounterPartyFun
} }
multiSigOut := wire.NewTxOut(int64(pendingReservation.FundingAmount), multiSigOut := wire.NewTxOut(int64(pendingReservation.FundingAmount),
multiSigScript) multiSigScript)
pendingReservation.fundingTx.AddTxOut(multiSigOut) pendingReservation.partialState.fundingTx.AddTxOut(multiSigOut)
// Sort the transaction. Since both side agree to a cannonical // Sort the transaction. Since both side agree to a cannonical
// ordering, by sorting we no longer need to send the entire // ordering, by sorting we no longer need to send the entire
// transaction. Only signatures will be exchanged. // transaction. Only signatures will be exchanged.
txsort.InPlaceSort(pendingReservation.fundingTx) txsort.InPlaceSort(pendingReservation.partialState.fundingTx)
// Now that the transaction has been cannonically sorted, compute the // Now that the transaction has been cannonically sorted, compute the
// normalized transation ID before we attach our signatures. // normalized transation ID before we attach our signatures.
// TODO(roasbeef): this isn't the normalized txid, this isn't recursive... // TODO(roasbeef): this isn't the normalized txid, this isn't recursive...
pendingReservation.normalizedTxID = pendingReservation.fundingTx.TxSha() // pendingReservation.normalizedTxID = pendingReservation.fundingTx.TxSha()
// Now, sign all inputs that are ours, collecting the signatures in // Now, sign all inputs that are ours, collecting the signatures in
// order of the inputs. // order of the inputs.
pendingReservation.ourSigs = make([][]byte, 0, len(pendingReservation.ourInputs)) pendingReservation.ourFundingSigs = make([][]byte, 0, len(pendingReservation.ourInputs))
for i, txIn := range pendingReservation.fundingTx.TxIn { for i, txIn := range pendingReservation.partialState.fundingTx.TxIn {
// Does the wallet know about the txin? // Does the wallet know about the txin?
txDetail, _ := l.wallet.TxStore.TxDetails(&txIn.PreviousOutPoint.Hash) txDetail, _ := l.wallet.TxStore.TxDetails(&txIn.PreviousOutPoint.Hash)
if txDetail == nil { if txDetail == nil {
@ -555,7 +555,7 @@ func (l *LightningWallet) handleFundingCounterPartyFunds(req *addCounterPartyFun
return return
} }
sigscript, err := txscript.SignatureScript(pendingReservation.fundingTx, i, sigscript, err := txscript.SignatureScript(pendingReservation.partialState.fundingTx, i,
prevOut.PkScript, txscript.SigHashAll, privkey, prevOut.PkScript, txscript.SigHashAll, privkey,
ai.Compressed()) ai.Compressed())
if err != nil { if err != nil {
@ -563,8 +563,8 @@ func (l *LightningWallet) handleFundingCounterPartyFunds(req *addCounterPartyFun
return return
} }
pendingReservation.fundingTx.TxIn[i].SignatureScript = sigscript pendingReservation.partialState.fundingTx.TxIn[i].SignatureScript = sigscript
pendingReservation.ourSigs = append(pendingReservation.ourSigs, sigscript) pendingReservation.ourFundingSigs = append(pendingReservation.ourFundingSigs, sigscript)
} }
req.err <- nil req.err <- nil
@ -586,11 +586,11 @@ func (l *LightningWallet) handleFundingCounterPartySigs(msg *addCounterPartySigs
// Now we can complete the funding transaction by adding their // Now we can complete the funding transaction by adding their
// signatures to their inputs. // signatures to their inputs.
pendingReservation.theirSigs = msg.theirSigs pendingReservation.theirFundingSigs = msg.theirSigs
fundingTx := pendingReservation.fundingTx fundingTx := pendingReservation.partialState.fundingTx
for i, txin := range fundingTx.TxIn { for i, txin := range fundingTx.TxIn {
if txin.SignatureScript == nil { if txin.SignatureScript == nil {
txin.SignatureScript = pendingReservation.theirSigs[i] txin.SignatureScript = pendingReservation.theirFundingSigs[i]
/*// Fetch the alleged previous output along with the /*// Fetch the alleged previous output along with the
// pkscript referenced by this input. // pkscript referenced by this input.
@ -623,12 +623,12 @@ func (l *LightningWallet) handleFundingCounterPartySigs(msg *addCounterPartySigs
} }
} }
// Now that all signatures are in place, and valid record the regular txid. // Funding complete, this entry can be removed from limbo.
pendingReservation.completedFundingTx = btcutil.NewTx(pendingReservation.fundingTx)
txID := pendingReservation.fundingTx.TxSha()
l.limboMtx.Lock() l.limboMtx.Lock()
delete(l.fundingLimbo, pendingReservation.reservationID) delete(l.fundingLimbo, pendingReservation.reservationID)
// TODO(roasbeef): unlock outputs here, Store.InsertTx will handle marking
// input in unconfirmed tx, so future coin selects don't pick it up
// * also record location of change address so can use AddCredit
l.limboMtx.Unlock() l.limboMtx.Unlock()
// Add the complete funding transaction to the DB, in it's open bucket // Add the complete funding transaction to the DB, in it's open bucket
@ -646,6 +646,7 @@ func (l *LightningWallet) handleFundingCounterPartySigs(msg *addCounterPartySigs
// Create a new sub-bucket within the open channel bucket // Create a new sub-bucket within the open channel bucket
// specifically for this channel. // specifically for this channel.
// TODO(roasbeef): should def be indexed by LNID, cuz mal etc. // TODO(roasbeef): should def be indexed by LNID, cuz mal etc.
txID := pendingReservation.partialState.fundingTx.TxSha()
chanBucket, err := openChanBucket.CreateBucketIfNotExists(txID.Bytes()) chanBucket, err := openChanBucket.CreateBucketIfNotExists(txID.Bytes())
if err != nil { if err != nil {
return err return err