From 46328945623ff9c3a210d3f825a970a4748c8a7c Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Fri, 5 Feb 2016 12:32:23 -0800 Subject: [PATCH 1/4] lnwallet: move teardown of rpctest before setup to ensure proper cleanup * Previously, if the call to SetUp(..) returned an error, then the test harness would fail to stop the running bcd process, and clean up the test directories. This would cause any subsequent tests to fail. This commit remedies this scenario. --- lnwallet/wallet_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lnwallet/wallet_test.go b/lnwallet/wallet_test.go index 64267bb1..6ea406f2 100644 --- a/lnwallet/wallet_test.go +++ b/lnwallet/wallet_test.go @@ -573,13 +573,13 @@ func TestLightningWallet(t *testing.T) { // up this node with a chain length of 125, so we have plentyyy of BTC // to play around with. miningNode, err := rpctest.New(netParams, nil, nil) + defer miningNode.TearDown() if err != nil { t.Fatalf("unable to create mining node: %v", err) } if err := miningNode.SetUp(true, 25); err != nil { t.Fatalf("unable to set up mining node: %v", err) } - defer miningNode.TearDown() // Funding via 5 outputs with 4BTC each. testDir, lnwallet, err := createTestWallet(miningNode, netParams) From fa05ee9a2272faa866df487e0d9c86747f96f9d3 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Fri, 5 Feb 2016 12:33:22 -0800 Subject: [PATCH 2/4] lnwallet: move pubKey comparison for funding output spend into spendMultiSig func * This change makes the spendMultiSig function testable independent of the reservation workflow. --- lnwallet/channel.go | 8 +------- lnwallet/script_utils.go | 18 +++++++++++++----- lnwallet/wallet.go | 17 +++++------------ 3 files changed, 19 insertions(+), 24 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 72e51762..791b38d8 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -189,16 +189,10 @@ func (c *ChannelUpdate) VerifyNewCommitmentSigs(ourSig, theirSig []byte) error { // public keys in descending order. So we do a quick comparison in order // ensure the signatures appear on the Script Virual Machine stack in // the correct order. - // TODO(roasbeef): func redeemScript := channelState.FundingRedeemScript ourKey := channelState.OurCommitKey.PubKey().SerializeCompressed() theirKey := channelState.TheirCommitKey.SerializeCompressed() - if bytes.Compare(ourKey, theirKey) == -1 { - scriptSig, err = spendMultiSig(redeemScript, theirSig, ourSig) - } else { - scriptSig, err = spendMultiSig(redeemScript, ourSig, theirSig) - } - + scriptSig, err = spendMultiSig(redeemScript, ourKey, ourSig, theirKey, theirSig) if err != nil { return err } diff --git a/lnwallet/script_utils.go b/lnwallet/script_utils.go index a854767e..406cbd1a 100644 --- a/lnwallet/script_utils.go +++ b/lnwallet/script_utils.go @@ -67,6 +67,7 @@ func fundMultiSigOut(aPub, bPub []byte, amt int64) ([]byte, *wire.TxOut, error) if err != nil { return nil, nil, err } + pkScript, err := scriptHashPkScript(redeemScript) if err != nil { return nil, nil, err @@ -77,16 +78,23 @@ func fundMultiSigOut(aPub, bPub []byte, amt int64) ([]byte, *wire.TxOut, error) // spendMultiSig generates the scriptSig required to redeem the 2-of-2 p2sh // multi-sig output. -func spendMultiSig(redeemScript, sigA, sigB []byte) ([]byte, error) { +func spendMultiSig(redeemScript, pubA, sigA, pubB, sigB []byte) ([]byte, error) { bldr := txscript.NewScriptBuilder() // add a 0 for some multisig fun bldr.AddOp(txscript.OP_0) - // add sigA - bldr.AddData(sigA) - // add sigB - bldr.AddData(sigB) + // When initially generating the redeemScript, we sorted the serialized + // public keys in descending order. So we do a quick comparison in order + // ensure the signatures appear on the Script Virual Machine stack in + // the correct order. + if bytes.Compare(pubA, pubB) == -1 { + bldr.AddData(sigB) + bldr.AddData(sigA) + } else { + bldr.AddData(sigA) + bldr.AddData(sigB) + } // preimage goes on AT THE ENDDDD bldr.AddData(redeemScript) diff --git a/lnwallet/wallet.go b/lnwallet/wallet.go index f7af9107..372b6cf3 100644 --- a/lnwallet/wallet.go +++ b/lnwallet/wallet.go @@ -1,7 +1,6 @@ package lnwallet import ( - "bytes" "encoding/hex" "errors" "fmt" @@ -934,18 +933,12 @@ func (l *LightningWallet) handleFundingCounterPartySigs(msg *addCounterPartySigs // Next, create the spending scriptSig, and then verify that the script // is complete, allowing us to spend from the funding transaction. - // - // When initially generating the redeemScript, we sorted the serialized - // public keys in descending order. So we do a quick comparison in order - // ensure the signatures appear on the Script Virual Machine stack in - // the correct order. - var scriptSig []byte theirCommitSig := msg.theirCommitmentSig - if bytes.Compare(ourKey.PubKey().SerializeCompressed(), theirKey.SerializeCompressed()) == -1 { - scriptSig, err = spendMultiSig(redeemScript, theirCommitSig, ourCommitSig) - } else { - scriptSig, err = spendMultiSig(redeemScript, ourCommitSig, theirCommitSig) - } + ourKeySer := ourKey.PubKey().SerializeCompressed() + theirKeySer := theirKey.SerializeCompressed() + scriptSig, err := spendMultiSig(redeemScript, ourKeySer, ourCommitSig, + theirKeySer, + theirCommitSig) if err != nil { msg.err <- err return From 81a4887d1138fccc134dc028835af79532a9e643 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Fri, 5 Feb 2016 20:24:17 -0800 Subject: [PATCH 3/4] lnwallet: move lockTimeToSequence to script_utils.go --- lnwallet/channel.go | 19 ------------------- lnwallet/script_utils.go | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 791b38d8..7a99ee5a 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -597,22 +597,3 @@ func createCommitTx(fundingOutput *wire.TxIn, selfKey, theirKey *btcec.PublicKey return commitTx, nil } - -// lockTimeToSequence converts the passed relative locktime to a sequence -// number in accordance to BIP-68. -// See: https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki -// * (Compatibility) -func lockTimeToSequence(isSeconds bool, locktime uint32) uint32 { - if !isSeconds { - // The locktime is to be expressed in confirmations. Apply the - // mask to restrict the number of confirmations to 65,535 or - // 1.25 years. - return SequenceLockTimeMask & locktime - } - - // Set the 22nd bit which indicates the lock time is in seconds, then - // shift the locktime over by 9 since the time granularity is in - // 512-second intervals (2^9). This results in a max lock-time of - // 33,554,431 seconds, or 1.06 years. - return SequenceLockTimeSeconds | (locktime >> 9) -} diff --git a/lnwallet/script_utils.go b/lnwallet/script_utils.go index 406cbd1a..6cd65f6f 100644 --- a/lnwallet/script_utils.go +++ b/lnwallet/script_utils.go @@ -212,6 +212,25 @@ func receiverHTLCScript(absoluteTimeout, relativeTimeout uint32, senderKey, return builder.Script() } +// lockTimeToSequence converts the passed relative locktime to a sequence +// number in accordance to BIP-68. +// See: https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki +// * (Compatibility) +func lockTimeToSequence(isSeconds bool, locktime uint32) uint32 { + if !isSeconds { + // The locktime is to be expressed in confirmations. Apply the + // mask to restrict the number of confirmations to 65,535 or + // 1.25 years. + return SequenceLockTimeMask & locktime + } + + // Set the 22nd bit which indicates the lock time is in seconds, then + // shift the locktime over by 9 since the time granularity is in + // 512-second intervals (2^9). This results in a max lock-time of + // 33,554,431 seconds, or 1.06 years. + return SequenceLockTimeSeconds | (locktime >> 9) +} + // commitScriptToSelf constructs the public key script for the output on the // commitment transaction paying to the "owner" of said commitment transaction. // If the other party learns of the pre-image to the revocation hash, then they From ab16933575c31ff59472e19b9b2e3a69d289fd40 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Fri, 5 Feb 2016 20:24:32 -0800 Subject: [PATCH 4/4] lnwallet: commit tx should have final sequence num --- lnwallet/channel.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 7a99ee5a..93ad4938 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -589,9 +589,6 @@ func createCommitTx(fundingOutput *wire.TxIn, selfKey, theirKey *btcec.PublicKey commitTx := wire.NewMsgTx() commitTx.Version = 2 commitTx.AddTxIn(fundingOutput) - // TODO(roasbeef): we default to blocks, make configurable as part of - // channel reservation. - commitTx.TxIn[0].Sequence = lockTimeToSequence(false, csvTimeout) commitTx.AddTxOut(wire.NewTxOut(int64(amountToSelf), payToUsScriptHash)) commitTx.AddTxOut(wire.NewTxOut(int64(amountToThem), payToThemScriptHash))