lnwallet: update workflow around contributions

This commit is contained in:
Olaoluwa Osuntokun 2015-12-22 22:31:17 -06:00
parent e762d328fa
commit d24831dc29

@ -31,15 +31,6 @@ const (
) )
var ( var (
// Namespace bucket keys.
lightningNamespaceKey = []byte("ln-wallet")
waddrmgrNamespaceKey = []byte("waddrmgr")
wtxmgrNamespaceKey = []byte("wtxmgr")
openChannelBucket = []byte("o-chans")
closedChannelBucket = []byte("c-chans")
fundingTxKey = []byte("funding")
// Error types // Error types
ErrInsufficientFunds = errors.New("not enough available outputs to " + ErrInsufficientFunds = errors.New("not enough available outputs to " +
"create funding transaction") "create funding transaction")
@ -97,14 +88,7 @@ type addContributionMsg struct {
pendingFundingID uint64 pendingFundingID uint64
// TODO(roasbeef): Should also carry SPV proofs in we're in SPV mode // TODO(roasbeef): Should also carry SPV proofs in we're in SPV mode
theirInputs []*wire.TxIn contribution *ChannelContribution
theirChangeOutputs []*wire.TxOut
theirMultiSigKey *btcec.PublicKey
theirCommitKey *btcec.PublicKey
deliveryAddress btcutil.Address
revocationHash [wire.HashSize]byte
csvDelay int64
err chan error // Buffered err chan error // Buffered
} }
@ -301,7 +285,7 @@ out:
l.wg.Done() l.wg.Done()
} }
// RequestFundingReservation... // InitChannelReservation...
func (l *LightningWallet) InitChannelReservation(a btcutil.Amount, t FundingType) (*ChannelReservation, error) { func (l *LightningWallet) InitChannelReservation(a btcutil.Amount, t FundingType) (*ChannelReservation, error) {
errChan := make(chan error, 1) errChan := make(chan error, 1)
respChan := make(chan *ChannelReservation, 1) respChan := make(chan *ChannelReservation, 1)
@ -332,7 +316,10 @@ func (l *LightningWallet) handleFundingReserveRequest(req *initFundingReserveMsg
reservation.Lock() reservation.Lock()
defer reservation.Unlock() defer reservation.Unlock()
ourContribution := reservation.ourContribution
// Find all unlocked unspent outputs with greater than 6 confirmations. // Find all unlocked unspent outputs with greater than 6 confirmations.
// TODO(roasbeef): make 6 a config paramter?
maxConfs := int32(math.MaxInt32) maxConfs := int32(math.MaxInt32)
unspentOutputs, err := l.wallet.ListUnspent(6, maxConfs, nil) unspentOutputs, err := l.wallet.ListUnspent(6, maxConfs, nil)
if err != nil { if err != nil {
@ -372,7 +359,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.
reservation.ourInputs = make([]*wire.TxIn, len(selectedCoins.Coins())) ourContribution.Inputs = 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)
@ -380,13 +367,13 @@ 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())
reservation.ourInputs[i] = wire.NewTxIn(outPoint, nil) ourContribution.Inputs[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()
reservation.ourChange = make([]*wire.TxOut, 0, len(selectedCoins.Coins()))
if selectedTotalValue > req.fundingAmount { if selectedTotalValue > req.fundingAmount {
ourContribution.ChangeOutputs = make([]*wire.TxOut, 1)
// 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.
changeAmount := selectedTotalValue - req.fundingAmount changeAmount := selectedTotalValue - req.fundingAmount
@ -407,8 +394,8 @@ 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)
reservation.ourChange = append(reservation.ourChange, ourContribution.ChangeOutputs[0] = wire.NewTxOut(int64(changeAmount),
wire.NewTxOut(int64(changeAmount), changeAddrScript)) changeAddrScript)
} }
// TODO(roasbeef): re-calculate fees here to minFeePerKB, may need more inputs // TODO(roasbeef): re-calculate fees here to minFeePerKB, may need more inputs
@ -431,7 +418,9 @@ func (l *LightningWallet) handleFundingReserveRequest(req *initFundingReserveMsg
return return
} }
reservation.partialState.multiSigKey = multiSigKey reservation.partialState.multiSigKey = multiSigKey
ourContribution.MultiSigKey = multiSigKey.PubKey()
reservation.partialState.ourCommitKey = commitKey reservation.partialState.ourCommitKey = commitKey
ourContribution.CommitKey = commitKey.PubKey()
// Generate a fresh address to be used in the case of a cooperative // Generate a fresh address to be used in the case of a cooperative
// channel close. // channel close.
@ -445,6 +434,7 @@ func (l *LightningWallet) handleFundingReserveRequest(req *initFundingReserveMsg
return return
} }
reservation.partialState.ourDeliveryAddress = addrs[0].Address() reservation.partialState.ourDeliveryAddress = addrs[0].Address()
ourContribution.DeliveryAddress = addrs[0].Address()
// Create a new shaChain for verifiable transaction revocations. // Create a new shaChain for verifiable transaction revocations.
shaChain, err := revocation.NewHyperShaChainFromSeed(nil, 0) shaChain, err := revocation.NewHyperShaChainFromSeed(nil, 0)
@ -454,6 +444,7 @@ func (l *LightningWallet) handleFundingReserveRequest(req *initFundingReserveMsg
return return
} }
reservation.partialState.ourShaChain = shaChain reservation.partialState.ourShaChain = shaChain
ourContribution.RevocationHash = shaChain.CurrentRevocationHash()
// 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
@ -482,7 +473,7 @@ func (l *LightningWallet) handleFundingCancelRequest(req *fundingReserveCancelMs
// Mark all previously locked outpoints as usuable for future funding // Mark all previously locked outpoints as usuable for future funding
// requests. // requests.
for _, unusedInput := range pendingReservation.ourInputs { for _, unusedInput := range pendingReservation.ourContribution.Inputs {
l.wallet.UnlockOutpoint(unusedInput.PreviousOutPoint) l.wallet.UnlockOutpoint(unusedInput.PreviousOutPoint)
} }
@ -513,6 +504,11 @@ func (l *LightningWallet) handleContributionMsg(req *addContributionMsg) {
// 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.partialState.fundingTx = wire.NewMsgTx() pendingReservation.partialState.fundingTx = wire.NewMsgTx()
fundingTx := pendingReservation.partialState.fundingTx
pendingReservation.theirContribution = req.contribution
theirContribution := req.contribution
ourContribution := pendingReservation.ourContribution
// 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
@ -520,29 +516,26 @@ func (l *LightningWallet) handleContributionMsg(req *addContributionMsg) {
// * actually, pure SPV would need fraud proofs right? must prove input // * actually, pure SPV would need fraud proofs right? must prove input
// is unspent // is unspent
// * or, something like getutxo? // * or, something like getutxo?
for _, ourInput := range pendingReservation.ourInputs { for _, ourInput := range ourContribution.Inputs {
pendingReservation.partialState.fundingTx.AddTxIn(ourInput) fundingTx.AddTxIn(ourInput)
} }
pendingReservation.theirInputs = req.theirInputs for _, theirInput := range theirContribution.Inputs {
for _, theirInput := range pendingReservation.theirInputs { 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 ourContribution.ChangeOutputs {
pendingReservation.partialState.fundingTx.AddTxOut(ourChangeOutput) fundingTx.AddTxOut(ourChangeOutput)
} }
pendingReservation.theirChange = req.theirChangeOutputs for _, theirChangeOutput := range theirContribution.ChangeOutputs {
for _, theirChangeOutput := range pendingReservation.theirChange { 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
// channel. // channel.
ourKey := pendingReservation.partialState.multiSigKey ourKey := pendingReservation.partialState.multiSigKey
pendingReservation.theirMultiSigKey = req.theirMultiSigKey theirKey := theirContribution.MultiSigKey
theirKey := pendingReservation.theirMultiSigKey
channelCapacity := int64(pendingReservation.partialState.capacity) channelCapacity := int64(pendingReservation.partialState.capacity)
redeemScript, multiSigOut, err := fundMultiSigOut(ourKey.PubKey().SerializeCompressed(), redeemScript, multiSigOut, err := fundMultiSigOut(ourKey.PubKey().SerializeCompressed(),
@ -552,7 +545,7 @@ func (l *LightningWallet) handleContributionMsg(req *addContributionMsg) {
return return
} }
pendingReservation.partialState.fundingRedeemScript = redeemScript pendingReservation.partialState.fundingRedeemScript = redeemScript
pendingReservation.partialState.fundingTx.AddTxOut(multiSigOut) 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
@ -566,8 +559,8 @@ func (l *LightningWallet) handleContributionMsg(req *addContributionMsg) {
// 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.ourFundingSigs = make([][]byte, 0, len(pendingReservation.ourInputs)) pendingReservation.ourFundingSigs = make([][]byte, 0, len(ourContribution.Inputs))
for i, txIn := range pendingReservation.partialState.fundingTx.TxIn { for i, txIn := range 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 {
@ -612,13 +605,13 @@ func (l *LightningWallet) handleContributionMsg(req *addContributionMsg) {
// revocation hash (we don't yet know the pre-image so we can't add it // revocation hash (we don't yet know the pre-image so we can't add it
// to the chain). // to the chain).
pendingReservation.partialState.theirShaChain = revocation.NewHyperShaChain() pendingReservation.partialState.theirShaChain = revocation.NewHyperShaChain()
pendingReservation.partialState.theirCurrentRevocation = req.revocationHash pendingReservation.partialState.theirCurrentRevocation = theirContribution.RevocationHash
// Grab the hash of the current pre-image in our chain, this is needed // Grab the hash of the current pre-image in our chain, this is needed
// for out commitment tx. // for out commitment tx.
// TODO(roasbeef): grab partial state above to avoid long attr chain // TODO(roasbeef): grab partial state above to avoid long attr chain
ourCurrentRevokeHash := pendingReservation.partialState.ourShaChain.CurrentRevocationHash() ourCurrentRevokeHash := pendingReservation.partialState.ourShaChain.CurrentRevocationHash()
pendingReservation.ourRevokeHash = ourCurrentRevokeHash ourContribution.RevocationHash = ourCurrentRevokeHash
// Create the txIn to our commitment transaction. In the process, we // Create the txIn to our commitment transaction. In the process, we
// need to locate the index of the multi-sig output on the funding tx // need to locate the index of the multi-sig output on the funding tx
@ -629,18 +622,18 @@ func (l *LightningWallet) handleContributionMsg(req *addContributionMsg) {
fundingTxIn := wire.NewTxIn(wire.NewOutPoint(&fundingNTxid, multiSigIndex), nil) fundingTxIn := wire.NewTxIn(wire.NewOutPoint(&fundingNTxid, multiSigIndex), nil)
// With the funding tx complete, create both commitment transactions. // With the funding tx complete, create both commitment transactions.
initialBalance := pendingReservation.fundingAmount initialBalance := ourContribution.FundingAmount
pendingReservation.fundingLockTime = req.csvDelay pendingReservation.fundingLockTime = theirContribution.CsvDelay
ourCommitKey := pendingReservation.partialState.ourCommitKey.PubKey() ourCommitKey := ourContribution.CommitKey
theirCommitKey := req.theirCommitKey theirCommitKey := theirContribution.CommitKey
ourCommitTx, err := createCommitTx(fundingTxIn, ourCommitKey, theirCommitKey, ourCommitTx, err := createCommitTx(fundingTxIn, ourCommitKey, theirCommitKey,
ourCurrentRevokeHash, req.csvDelay, initialBalance) ourCurrentRevokeHash, theirContribution.CsvDelay, initialBalance)
if err != nil { if err != nil {
req.err <- err req.err <- err
return return
} }
theirCommitTx, err := createCommitTx(fundingTxIn, theirCommitKey, ourCommitKey, theirCommitTx, err := createCommitTx(fundingTxIn, theirCommitKey, ourCommitKey,
req.revocationHash, req.csvDelay, initialBalance) theirContribution.RevocationHash, theirContribution.CsvDelay, initialBalance)
if err != nil { if err != nil {
req.err <- err req.err <- err
return return
@ -652,14 +645,14 @@ func (l *LightningWallet) handleContributionMsg(req *addContributionMsg) {
// Generate a signature for their version of the initial commitment // Generate a signature for their version of the initial commitment
// transaction. // transaction.
fundingTx := pendingReservation.partialState.fundingTx sigTheirCommit, err := txscript.RawTxInSignature(theirCommitTx, 0, multiSigOut.PkScript,
sigTheirCommit, err := txscript.RawTxInSignature(fundingTx, 0, multiSigOut.PkScript,
txscript.SigHashAll, ourKey) txscript.SigHashAll, ourKey)
if err != nil { if err != nil {
req.err <- err req.err <- err
return return
} }
pendingReservation.partialState.theirCommitSig = sigTheirCommit pendingReservation.partialState.theirCommitSig = sigTheirCommit
pendingReservation.ourCommitmentSig = sigTheirCommit
req.err <- nil req.err <- nil
} }
@ -720,6 +713,7 @@ func (l *LightningWallet) handleFundingCounterPartySigs(msg *addCounterPartySigs
// At this point, wen calso record and verify their isgnature for our // At this point, wen calso record and verify their isgnature for our
// commitment transaction. // commitment transaction.
pendingReservation.partialState.theirCommitSig = msg.theirCommitmentSig pendingReservation.partialState.theirCommitSig = msg.theirCommitmentSig
// TODO(roasbeef): verify // TODO(roasbeef): verify
//commitSig := msg.theirCommitmentSig //commitSig := msg.theirCommitmentSig