lnwallet/test: move test cases out of function

This commit is contained in:
Joost Jager 2020-05-28 13:02:18 +02:00
parent 3b7f084509
commit 98ec8bf502
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7

@ -341,111 +341,8 @@ func (tc *testContext) extractFundingInput() (*Utxo, *wire.TxOut, error) {
return &block1Utxo, txout, nil return &block1Utxo, txout, nil
} }
// TestCommitmentAndHTLCTransactions checks the test vectors specified in
// BOLT 03, Appendix C. This deterministically generates commitment and second
// level HTLC transactions and checks that they match the expected values.
func TestCommitmentAndHTLCTransactions(t *testing.T) {
t.Parallel()
tc, err := newTestContext()
if err != nil {
t.Fatal(err)
}
// Generate random some keys that don't actually matter but need to be set.
var (
identityKey *btcec.PublicKey
localDelayBasePoint *btcec.PublicKey
)
generateKeys := []**btcec.PublicKey{
&identityKey,
&localDelayBasePoint,
}
for _, keyRef := range generateKeys {
privkey, err := btcec.NewPrivateKey(btcec.S256())
if err != nil {
t.Fatalf("Failed to generate new key: %v", err)
}
*keyRef = privkey.PubKey()
}
// Manually construct a new LightningChannel.
channelState := channeldb.OpenChannel{
ChanType: channeldb.SingleFunderTweaklessBit,
ChainHash: *tc.netParams.GenesisHash,
FundingOutpoint: tc.fundingOutpoint,
ShortChannelID: tc.shortChanID,
IsInitiator: true,
IdentityPub: identityKey,
LocalChanCfg: channeldb.ChannelConfig{
ChannelConstraints: channeldb.ChannelConstraints{
DustLimit: tc.dustLimit,
MaxPendingAmount: lnwire.NewMSatFromSatoshis(tc.fundingAmount),
MaxAcceptedHtlcs: input.MaxHTLCNumber,
CsvDelay: tc.localCsvDelay,
},
MultiSigKey: keychain.KeyDescriptor{
PubKey: tc.localFundingPubKey,
},
PaymentBasePoint: keychain.KeyDescriptor{
PubKey: tc.localPaymentBasePoint,
},
HtlcBasePoint: keychain.KeyDescriptor{
PubKey: tc.localPaymentBasePoint,
},
DelayBasePoint: keychain.KeyDescriptor{
PubKey: localDelayBasePoint,
},
},
RemoteChanCfg: channeldb.ChannelConfig{
MultiSigKey: keychain.KeyDescriptor{
PubKey: tc.remoteFundingPubKey,
},
PaymentBasePoint: keychain.KeyDescriptor{
PubKey: tc.remotePaymentBasePoint,
},
HtlcBasePoint: keychain.KeyDescriptor{
PubKey: tc.remotePaymentBasePoint,
},
},
Capacity: tc.fundingAmount,
RevocationProducer: shachain.NewRevocationProducer(zeroHash),
}
signer := &input.MockSigner{
Privkeys: []*btcec.PrivateKey{
tc.localFundingPrivKey, tc.localPaymentPrivKey,
},
NetParams: tc.netParams,
}
// Construct a LightningChannel manually because we don't have nor need all
// of the dependencies.
channel := LightningChannel{
channelState: &channelState,
Signer: signer,
commitBuilder: NewCommitmentBuilder(&channelState),
}
err = channel.createSignDesc()
if err != nil {
t.Fatalf("Failed to generate channel sign descriptor: %v", err)
}
// The commitmentPoint is technically hidden in the spec, but we need it to
// generate the correct tweak.
tweak := input.SingleTweakBytes(tc.commitmentPoint, tc.localPaymentBasePoint)
keys := &CommitmentKeyRing{
CommitPoint: tc.commitmentPoint,
LocalCommitKeyTweak: tweak,
LocalHtlcKeyTweak: tweak,
LocalHtlcKey: tc.localPaymentPubKey,
RemoteHtlcKey: tc.remotePaymentPubKey,
ToLocalKey: tc.localDelayPubKey,
ToRemoteKey: tc.remotePaymentPubKey,
RevocationKey: tc.localRevocationPubKey,
}
// testCases encode the raw test vectors specified in Appendix C of BOLT 03. // testCases encode the raw test vectors specified in Appendix C of BOLT 03.
testCases := []struct { var testCases = []struct {
commitment channeldb.ChannelCommitment commitment channeldb.ChannelCommitment
htlcDescs []htlcDesc htlcDescs []htlcDesc
expectedCommitmentTxHex string expectedCommitmentTxHex string
@ -730,6 +627,109 @@ func TestCommitmentAndHTLCTransactions(t *testing.T) {
}, },
} }
// TestCommitmentAndHTLCTransactions checks the test vectors specified in
// BOLT 03, Appendix C. This deterministically generates commitment and second
// level HTLC transactions and checks that they match the expected values.
func TestCommitmentAndHTLCTransactions(t *testing.T) {
t.Parallel()
tc, err := newTestContext()
if err != nil {
t.Fatal(err)
}
// Generate random some keys that don't actually matter but need to be set.
var (
identityKey *btcec.PublicKey
localDelayBasePoint *btcec.PublicKey
)
generateKeys := []**btcec.PublicKey{
&identityKey,
&localDelayBasePoint,
}
for _, keyRef := range generateKeys {
privkey, err := btcec.NewPrivateKey(btcec.S256())
if err != nil {
t.Fatalf("Failed to generate new key: %v", err)
}
*keyRef = privkey.PubKey()
}
// Manually construct a new LightningChannel.
channelState := channeldb.OpenChannel{
ChanType: channeldb.SingleFunderTweaklessBit,
ChainHash: *tc.netParams.GenesisHash,
FundingOutpoint: tc.fundingOutpoint,
ShortChannelID: tc.shortChanID,
IsInitiator: true,
IdentityPub: identityKey,
LocalChanCfg: channeldb.ChannelConfig{
ChannelConstraints: channeldb.ChannelConstraints{
DustLimit: tc.dustLimit,
MaxPendingAmount: lnwire.NewMSatFromSatoshis(tc.fundingAmount),
MaxAcceptedHtlcs: input.MaxHTLCNumber,
CsvDelay: tc.localCsvDelay,
},
MultiSigKey: keychain.KeyDescriptor{
PubKey: tc.localFundingPubKey,
},
PaymentBasePoint: keychain.KeyDescriptor{
PubKey: tc.localPaymentBasePoint,
},
HtlcBasePoint: keychain.KeyDescriptor{
PubKey: tc.localPaymentBasePoint,
},
DelayBasePoint: keychain.KeyDescriptor{
PubKey: localDelayBasePoint,
},
},
RemoteChanCfg: channeldb.ChannelConfig{
MultiSigKey: keychain.KeyDescriptor{
PubKey: tc.remoteFundingPubKey,
},
PaymentBasePoint: keychain.KeyDescriptor{
PubKey: tc.remotePaymentBasePoint,
},
HtlcBasePoint: keychain.KeyDescriptor{
PubKey: tc.remotePaymentBasePoint,
},
},
Capacity: tc.fundingAmount,
RevocationProducer: shachain.NewRevocationProducer(zeroHash),
}
signer := &input.MockSigner{
Privkeys: []*btcec.PrivateKey{
tc.localFundingPrivKey, tc.localPaymentPrivKey,
},
NetParams: tc.netParams,
}
// Construct a LightningChannel manually because we don't have nor need all
// of the dependencies.
channel := LightningChannel{
channelState: &channelState,
Signer: signer,
commitBuilder: NewCommitmentBuilder(&channelState),
}
err = channel.createSignDesc()
if err != nil {
t.Fatalf("Failed to generate channel sign descriptor: %v", err)
}
// The commitmentPoint is technically hidden in the spec, but we need it to
// generate the correct tweak.
tweak := input.SingleTweakBytes(tc.commitmentPoint, tc.localPaymentBasePoint)
keys := &CommitmentKeyRing{
CommitPoint: tc.commitmentPoint,
LocalCommitKeyTweak: tweak,
LocalHtlcKeyTweak: tweak,
LocalHtlcKey: tc.localPaymentPubKey,
RemoteHtlcKey: tc.remotePaymentPubKey,
ToLocalKey: tc.localDelayPubKey,
ToRemoteKey: tc.remotePaymentPubKey,
RevocationKey: tc.localRevocationPubKey,
}
for i, test := range testCases { for i, test := range testCases {
expectedCommitmentTx, err := txFromHex(test.expectedCommitmentTxHex) expectedCommitmentTx, err := txFromHex(test.expectedCommitmentTxHex)
if err != nil { if err != nil {