From ef7a2aa5539c9d88fa4828002e72dd5f8b181c57 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Mon, 28 Dec 2015 23:41:58 -0600 Subject: [PATCH] lnwallet: properly set sequence number in tx for CSV usage --- lnwallet/channel.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index a7d8084de..4a4c9e3e0 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -4,6 +4,7 @@ import ( "sync" "li.lan/labs/plasma/chainntfs" + "li.lan/labs/plasma/channeldb" "github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/txscript" @@ -15,6 +16,9 @@ import ( const ( // TODO(roasbeef): make not random value MaxPendingPayments = 10 + + SequenceLockTimeSeconds = uint32(1 << 22) + SequenceLockTimeMask = uint32(0x0000ffff) ) // LightningChannel... @@ -43,7 +47,6 @@ type LightningChannel struct { // newLightningChannel... func newLightningChannel(wallet *LightningWallet, events *chainntnfs.ChainNotifier, - chanDB *ChannelDB, state OpenChannelState) (*LightningChannel, error) { chanDB *channeldb.DB, state channeldb.OpenChannel) (*LightningChannel, error) { return &LightningChannel{ @@ -151,6 +154,9 @@ func createCommitTx(fundingOutput *wire.TxIn, ourKey, theirKey *btcec.PublicKey, // the transaction itself. commitTx := wire.NewMsgTx() 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(channelAmt), payToUsScriptHash)) commitTx.AddTxOut(wire.NewTxOut(int64(channelAmt), payToThemScriptHash)) @@ -161,6 +167,25 @@ func createCommitTx(fundingOutput *wire.TxIn, ourKey, 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) +} + //TODO(j): Creates a CLTV-only funding Tx (reserve is *REQUIRED*) //This works for only CLTV soft-fork (no CSV/segwit soft-fork in yet) //