htlcswitch: update tests utilities to be aware of new commitment design

This commit is contained in:
Olaoluwa Osuntokun 2017-07-30 14:09:10 -07:00
parent 2d1a598b66
commit 402112e6ee
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2
3 changed files with 129 additions and 111 deletions

@ -3,6 +3,7 @@ package htlcswitch
import ( import (
"crypto/sha256" "crypto/sha256"
"encoding/binary" "encoding/binary"
"fmt"
"sync" "sync"
"testing" "testing"
@ -314,6 +315,8 @@ func (s *mockServer) PubKey() [33]byte {
} }
func (s *mockServer) Disconnect(reason error) { func (s *mockServer) Disconnect(reason error) {
fmt.Printf("server %v disconnected due to %v\n", s.name, reason)
s.Stop() s.Stop()
s.t.Fatalf("server %v was disconnected", s.name) s.t.Fatalf("server %v was disconnected", s.name)
} }
@ -438,8 +441,22 @@ func (m *mockSigner) SignOutputRaw(tx *wire.MsgTx, signDesc *lnwallet.SignDescri
witnessScript := signDesc.WitnessScript witnessScript := signDesc.WitnessScript
privKey := m.key privKey := m.key
if !privKey.PubKey().IsEqual(signDesc.PubKey) {
return nil, fmt.Errorf("incorrect key passed")
}
switch {
case signDesc.SingleTweak != nil:
privKey = lnwallet.TweakPrivKey(privKey,
signDesc.SingleTweak)
case signDesc.DoubleTweak != nil:
privKey = lnwallet.DeriveRevocationPrivKey(privKey,
signDesc.DoubleTweak)
}
sig, err := txscript.RawTxInWitnessSignature(tx, signDesc.SigHashes, sig, err := txscript.RawTxInWitnessSignature(tx, signDesc.SigHashes,
signDesc.InputIndex, amt, witnessScript, txscript.SigHashAll, privKey) signDesc.InputIndex, amt, witnessScript, txscript.SigHashAll,
privKey)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -448,9 +465,23 @@ func (m *mockSigner) SignOutputRaw(tx *wire.MsgTx, signDesc *lnwallet.SignDescri
} }
func (m *mockSigner) ComputeInputScript(tx *wire.MsgTx, signDesc *lnwallet.SignDescriptor) (*lnwallet.InputScript, error) { func (m *mockSigner) ComputeInputScript(tx *wire.MsgTx, signDesc *lnwallet.SignDescriptor) (*lnwallet.InputScript, error) {
// TODO(roasbeef): expose tweaked signer from lnwallet so don't need to
// duplicate this code?
privKey := m.key
switch {
case signDesc.SingleTweak != nil:
privKey = lnwallet.TweakPrivKey(privKey,
signDesc.SingleTweak)
case signDesc.DoubleTweak != nil:
privKey = lnwallet.DeriveRevocationPrivKey(privKey,
signDesc.DoubleTweak)
}
witnessScript, err := txscript.WitnessScript(tx, signDesc.SigHashes, witnessScript, err := txscript.WitnessScript(tx, signDesc.SigHashes,
signDesc.InputIndex, signDesc.Output.Value, signDesc.Output.PkScript, signDesc.InputIndex, signDesc.Output.Value, signDesc.Output.PkScript,
txscript.SigHashAll, m.key, true) txscript.SigHashAll, privKey, true)
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -807,8 +807,8 @@ func (s *Switch) AddLink(link ChannelLink) error {
} }
} }
// addLink is used to add the newly created channel link and start // addLink is used to add the newly created channel link and start use it to
// use it to handle the channel updates. // handle the channel updates.
func (s *Switch) addLink(link ChannelLink) error { func (s *Switch) addLink(link ChannelLink) error {
// First we'll add the link to the linkIndex which lets us quickly look // First we'll add the link to the linkIndex which lets us quickly look
// up a channel when we need to close or register it, and the // up a channel when we need to close or register it, and the
@ -958,7 +958,7 @@ func (s *Switch) getLinks(destination [33]byte) ([]ChannelLink, error) {
links, ok := s.interfaceIndex[destination] links, ok := s.interfaceIndex[destination]
if !ok { if !ok {
return nil, errors.Errorf("unable to locate channel link by"+ return nil, errors.Errorf("unable to locate channel link by"+
"destination hop id %v", destination) "destination hop id %x", destination)
} }
channelLinks := make([]ChannelLink, 0, len(links)) channelLinks := make([]ChannelLink, 0, len(links))

@ -76,9 +76,11 @@ func generateRandomBytes(n int) ([]byte, error) {
// createTestChannel creates the channel and returns our and remote channels // createTestChannel creates the channel and returns our and remote channels
// representations. // representations.
func createTestChannel(alicePrivKey, bobPrivKey []byte, aliceAmount, bobAmount, //
feePerKw btcutil.Amount, chanID lnwire.ShortChannelID) ( // TODO(roasbeef): need to factor out, similar func re-used in many parts of codebase
*lnwallet.LightningChannel, *lnwallet.LightningChannel, func(), error) { func createTestChannel(alicePrivKey, bobPrivKey []byte,
aliceAmount, bobAmount btcutil.Amount,
chanID lnwire.ShortChannelID) (*lnwallet.LightningChannel, *lnwallet.LightningChannel, func(), error) {
aliceKeyPriv, aliceKeyPub := btcec.PrivKeyFromBytes(btcec.S256(), alicePrivKey) aliceKeyPriv, aliceKeyPub := btcec.PrivKeyFromBytes(btcec.S256(), alicePrivKey)
bobKeyPriv, bobKeyPub := btcec.PrivKeyFromBytes(btcec.S256(), bobPrivKey) bobKeyPriv, bobKeyPub := btcec.PrivKeyFromBytes(btcec.S256(), bobPrivKey)
@ -88,16 +90,6 @@ func createTestChannel(alicePrivKey, bobPrivKey []byte, aliceAmount, bobAmount,
bobDustLimit := btcutil.Amount(800) bobDustLimit := btcutil.Amount(800)
csvTimeoutAlice := uint32(5) csvTimeoutAlice := uint32(5)
csvTimeoutBob := uint32(4) csvTimeoutBob := uint32(4)
commitFee := (feePerKw * btcutil.Amount(724)) / 1000
witnessScript, _, err := lnwallet.GenFundingPkScript(
aliceKeyPub.SerializeCompressed(),
bobKeyPub.SerializeCompressed(),
int64(channelCapacity),
)
if err != nil {
return nil, nil, nil, err
}
var hash [sha256.Size]byte var hash [sha256.Size]byte
randomSeed, err := generateRandomBytes(sha256.Size) randomSeed, err := generateRandomBytes(sha256.Size)
@ -112,49 +104,46 @@ func createTestChannel(alicePrivKey, bobPrivKey []byte, aliceAmount, bobAmount,
} }
fundingTxIn := wire.NewTxIn(prevOut, nil, nil) fundingTxIn := wire.NewTxIn(prevOut, nil, nil)
bobRoot := lnwallet.DeriveRevocationRoot(bobKeyPriv, bobKeyPub, aliceCfg := channeldb.ChannelConfig{
aliceKeyPub) ChannelConstraints: channeldb.ChannelConstraints{
bobPreimageProducer := shachain.NewRevocationProducer(*bobRoot) DustLimit: aliceDustLimit,
},
CsvDelay: uint16(csvTimeoutAlice),
MultiSigKey: aliceKeyPub,
RevocationBasePoint: aliceKeyPub,
PaymentBasePoint: aliceKeyPub,
DelayBasePoint: aliceKeyPub,
}
bobCfg := channeldb.ChannelConfig{
ChannelConstraints: channeldb.ChannelConstraints{
DustLimit: bobDustLimit,
},
CsvDelay: uint16(csvTimeoutBob),
MultiSigKey: bobKeyPub,
RevocationBasePoint: bobKeyPub,
PaymentBasePoint: bobKeyPub,
DelayBasePoint: bobKeyPub,
}
bobRoot := lnwallet.DeriveRevocationRoot(bobKeyPriv, hash, aliceKeyPub)
bobPreimageProducer := shachain.NewRevocationProducer(bobRoot)
bobFirstRevoke, err := bobPreimageProducer.AtIndex(0) bobFirstRevoke, err := bobPreimageProducer.AtIndex(0)
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, err
} }
bobRevokeKey := lnwallet.DeriveRevocationPubkey(aliceKeyPub, bobCommitPoint := lnwallet.ComputeCommitmentPoint(bobFirstRevoke[:])
bobFirstRevoke[:])
aliceRoot := lnwallet.DeriveRevocationRoot(aliceKeyPriv, aliceKeyPub, aliceRoot := lnwallet.DeriveRevocationRoot(aliceKeyPriv, hash, bobKeyPub)
bobKeyPub) alicePreimageProducer := shachain.NewRevocationProducer(aliceRoot)
alicePreimageProducer := shachain.NewRevocationProducer(*aliceRoot)
aliceFirstRevoke, err := alicePreimageProducer.AtIndex(0) aliceFirstRevoke, err := alicePreimageProducer.AtIndex(0)
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, err
} }
aliceRevokeKey := lnwallet.DeriveRevocationPubkey(bobKeyPub, aliceCommitPoint := lnwallet.ComputeCommitmentPoint(aliceFirstRevoke[:])
aliceFirstRevoke[:])
aliceCommitTx, err := lnwallet.CreateCommitTx( aliceCommitTx, bobCommitTx, err := lnwallet.CreateCommitmentTxns(aliceAmount,
fundingTxIn, bobAmount, &aliceCfg, &bobCfg, aliceCommitPoint, bobCommitPoint,
aliceKeyPub, fundingTxIn)
bobKeyPub,
aliceRevokeKey,
csvTimeoutAlice,
aliceAmount-commitFee,
bobAmount,
lnwallet.DefaultDustLimit(),
)
if err != nil {
return nil, nil, nil, err
}
bobCommitTx, err := lnwallet.CreateCommitTx(
fundingTxIn,
bobKeyPub,
aliceKeyPub,
bobRevokeKey,
csvTimeoutBob,
bobAmount,
aliceAmount-commitFee,
lnwallet.DefaultDustLimit(),
)
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, err
} }
@ -174,63 +163,47 @@ func createTestChannel(alicePrivKey, bobPrivKey []byte, aliceAmount, bobAmount,
var obsfucator [lnwallet.StateHintSize]byte var obsfucator [lnwallet.StateHintSize]byte
copy(obsfucator[:], aliceFirstRevoke[:]) copy(obsfucator[:], aliceFirstRevoke[:])
estimator := &lnwallet.StaticFeeEstimator{24, 6}
feePerKw := btcutil.Amount(estimator.EstimateFeePerWeight(1) * 1000)
commitFee := (feePerKw * btcutil.Amount(724)) / 1000
aliceChannelState := &channeldb.OpenChannel{ aliceChannelState := &channeldb.OpenChannel{
IdentityPub: aliceKeyPub, LocalChanCfg: aliceCfg,
ChanID: prevOut, RemoteChanCfg: bobCfg,
CommitFee: commitFee, IdentityPub: aliceKeyPub,
FeePerKw: feePerKw, FundingOutpoint: *prevOut,
ChanType: channeldb.SingleFunder, ChanType: channeldb.SingleFunder,
IsInitiator: true, FeePerKw: feePerKw,
StateHintObsfucator: obsfucator, IsInitiator: true,
OurCommitKey: aliceKeyPub, Capacity: channelCapacity,
TheirCommitKey: bobKeyPub, LocalBalance: aliceAmount - commitFee,
Capacity: channelCapacity, RemoteBalance: bobAmount,
OurBalance: aliceAmount - commitFee, CommitTx: *aliceCommitTx,
TheirBalance: bobAmount, CommitSig: bytes.Repeat([]byte{1}, 71),
OurCommitTx: aliceCommitTx, RemoteCurrentRevocation: bobCommitPoint,
OurCommitSig: bytes.Repeat([]byte{1}, 71), RevocationProducer: alicePreimageProducer,
FundingOutpoint: prevOut, RevocationStore: shachain.NewRevocationStore(),
OurMultiSigKey: aliceKeyPub, ShortChanID: chanID,
TheirMultiSigKey: bobKeyPub, Db: dbAlice,
FundingWitnessScript: witnessScript,
LocalCsvDelay: csvTimeoutAlice,
RemoteCsvDelay: csvTimeoutBob,
TheirCurrentRevocation: bobRevokeKey,
RevocationProducer: alicePreimageProducer,
RevocationStore: shachain.NewRevocationStore(),
TheirDustLimit: bobDustLimit,
OurDustLimit: aliceDustLimit,
ShortChanID: chanID,
Db: dbAlice,
} }
bobChannelState := &channeldb.OpenChannel{ bobChannelState := &channeldb.OpenChannel{
IdentityPub: bobKeyPub, LocalChanCfg: bobCfg,
ChanID: prevOut, RemoteChanCfg: aliceCfg,
CommitFee: commitFee, IdentityPub: bobKeyPub,
FeePerKw: feePerKw, FeePerKw: feePerKw,
ChanType: channeldb.SingleFunder, FundingOutpoint: *prevOut,
IsInitiator: false, ChanType: channeldb.SingleFunder,
StateHintObsfucator: obsfucator, IsInitiator: false,
OurCommitKey: bobKeyPub, Capacity: channelCapacity,
TheirCommitKey: aliceKeyPub, LocalBalance: bobAmount,
Capacity: channelCapacity, RemoteBalance: aliceAmount - commitFee,
OurBalance: bobAmount, CommitTx: *bobCommitTx,
TheirBalance: aliceAmount - commitFee, CommitSig: bytes.Repeat([]byte{1}, 71),
OurCommitTx: bobCommitTx, RemoteCurrentRevocation: aliceCommitPoint,
OurCommitSig: bytes.Repeat([]byte{1}, 71), RevocationProducer: bobPreimageProducer,
FundingOutpoint: prevOut, RevocationStore: shachain.NewRevocationStore(),
OurMultiSigKey: bobKeyPub, ShortChanID: chanID,
TheirMultiSigKey: aliceKeyPub, Db: dbBob,
FundingWitnessScript: witnessScript,
LocalCsvDelay: csvTimeoutBob,
RemoteCsvDelay: csvTimeoutAlice,
TheirCurrentRevocation: aliceRevokeKey,
RevocationProducer: bobPreimageProducer,
RevocationStore: shachain.NewRevocationStore(),
TheirDustLimit: aliceDustLimit,
OurDustLimit: bobDustLimit,
ShortChanID: chanID,
Db: dbBob,
} }
cleanUpFunc := func() { cleanUpFunc := func() {
@ -240,10 +213,6 @@ func createTestChannel(alicePrivKey, bobPrivKey []byte, aliceAmount, bobAmount,
aliceSigner := &mockSigner{aliceKeyPriv} aliceSigner := &mockSigner{aliceKeyPriv}
bobSigner := &mockSigner{bobKeyPriv} bobSigner := &mockSigner{bobKeyPriv}
estimator := &lnwallet.StaticFeeEstimator{
FeeRate: 24,
Confirmation: 6,
}
channelAlice, err := lnwallet.NewLightningChannel(aliceSigner, channelAlice, err := lnwallet.NewLightningChannel(aliceSigner,
nil, estimator, aliceChannelState) nil, estimator, aliceChannelState)
@ -256,6 +225,24 @@ func createTestChannel(alicePrivKey, bobPrivKey []byte, aliceAmount, bobAmount,
return nil, nil, nil, err return nil, nil, nil, err
} }
// Now that the channel are open, simulate the start of a session by
// having Alice and Bob extend their revocation windows to each other.
aliceNextRevoke, err := channelAlice.NextRevocationkey()
if err != nil {
return nil, nil, nil, err
}
if err := channelBob.InitNextRevocation(aliceNextRevoke); err != nil {
return nil, nil, nil, err
}
bobNextRevoke, err := channelBob.NextRevocationkey()
if err != nil {
return nil, nil, nil, err
}
if err := channelAlice.InitNextRevocation(bobNextRevoke); err != nil {
return nil, nil, nil, err
}
return channelAlice, channelBob, cleanUpFunc, nil return channelAlice, channelBob, cleanUpFunc, nil
} }
@ -538,13 +525,13 @@ func newThreeHopNetwork(t *testing.T, aliceToBob,
// Create lightning channels between Alice<->Bob and Bob<->Carol // Create lightning channels between Alice<->Bob and Bob<->Carol
aliceChannel, firstBobChannel, fCleanUp, err := createTestChannel( aliceChannel, firstBobChannel, fCleanUp, err := createTestChannel(
alicePrivKey, bobPrivKey, aliceToBob, aliceToBob, 0, firstChanID) alicePrivKey, bobPrivKey, aliceToBob, aliceToBob, firstChanID)
if err != nil { if err != nil {
t.Fatalf("unable to create alice<->bob channel: %v", err) t.Fatalf("unable to create alice<->bob channel: %v", err)
} }
secondBobChannel, carolChannel, sCleanUp, err := createTestChannel( secondBobChannel, carolChannel, sCleanUp, err := createTestChannel(
bobPrivKey, carolPrivKey, bobToCarol, bobToCarol, 0, secondChanID) bobPrivKey, carolPrivKey, bobToCarol, bobToCarol, secondChanID)
if err != nil { if err != nil {
t.Fatalf("unable to create bob<->carol channel: %v", err) t.Fatalf("unable to create bob<->carol channel: %v", err)
} }