2016-12-22 23:24:48 +03:00
|
|
|
package lnwire
|
|
|
|
|
2017-04-17 01:19:39 +03:00
|
|
|
import "testing"
|
|
|
|
|
|
|
|
// TestChannelIDOutPointConversion ensures that the IsChanPoint always
|
|
|
|
// recognizes its seed OutPoint for all possible values of an output index.
|
|
|
|
func TestChannelIDOutPointConversion(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-04-17 01:19:39 +03:00
|
|
|
testChanPoint := *outpoint1
|
|
|
|
|
|
|
|
// For a given OutPoint, we'll run through all the possible output
|
|
|
|
// index values, mutating our test outpoint to match that output index.
|
2017-09-18 20:42:12 +03:00
|
|
|
var prevChanID ChannelID
|
2017-04-17 01:19:39 +03:00
|
|
|
for i := uint32(0); i < MaxFundingTxOutputs; i++ {
|
|
|
|
testChanPoint.Index = i
|
|
|
|
|
|
|
|
// With the output index mutated, we'll convert it into a
|
|
|
|
// ChannelID.
|
|
|
|
cid := NewChanIDFromOutPoint(&testChanPoint)
|
|
|
|
|
|
|
|
// Once the channel point has been converted to a channelID, it
|
|
|
|
// should recognize its original outpoint.
|
|
|
|
if !cid.IsChanPoint(&testChanPoint) {
|
|
|
|
t.Fatalf("channelID not recognized as seed channel "+
|
|
|
|
"point: cid=%v, op=%v", cid, testChanPoint)
|
|
|
|
}
|
2017-09-18 20:42:12 +03:00
|
|
|
|
|
|
|
// We also ensure that the channel ID itself have changed
|
|
|
|
// between iterations. This case is meant to catch an issue
|
|
|
|
// where the transformation function itself is a no-op.
|
|
|
|
if prevChanID == cid {
|
|
|
|
t.Fatalf("#%v: channelID not modified: old=%v, new=%v",
|
|
|
|
i, prevChanID, cid)
|
|
|
|
}
|
|
|
|
|
|
|
|
prevChanID = cid
|
2016-12-22 23:24:48 +03:00
|
|
|
}
|
2017-04-17 01:19:39 +03:00
|
|
|
}
|
2016-12-22 23:24:48 +03:00
|
|
|
|
2017-09-18 20:45:59 +03:00
|
|
|
// TestGenPossibleOutPoints ensures that the GenPossibleOutPoints generates a
|
|
|
|
// valid set of outpoints for a channelID. A set of outpoints is valid iff, the
|
2017-04-17 01:19:39 +03:00
|
|
|
// root outpoint (the outpoint that generated the ChannelID) is included in the
|
|
|
|
// returned set of outpoints.
|
|
|
|
func TestGenPossibleOutPoints(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-04-17 01:19:39 +03:00
|
|
|
// We'll first convert out test outpoint into a ChannelID.
|
|
|
|
testChanPoint := *outpoint1
|
2017-09-18 20:42:12 +03:00
|
|
|
testChanPoint.Index = 24
|
2017-04-17 01:19:39 +03:00
|
|
|
chanID := NewChanIDFromOutPoint(&testChanPoint)
|
2016-12-22 23:24:48 +03:00
|
|
|
|
2017-04-17 01:19:39 +03:00
|
|
|
// With the chan ID created, we'll generate all possible root outpoints
|
|
|
|
// given this channel ID.
|
|
|
|
possibleOutPoints := chanID.GenPossibleOutPoints()
|
2016-12-22 23:24:48 +03:00
|
|
|
|
2017-04-17 01:19:39 +03:00
|
|
|
// If we run through the generated possible outpoints, the original
|
|
|
|
// root outpoint MUST be found in this generated set.
|
|
|
|
var opFound bool
|
|
|
|
for _, op := range possibleOutPoints {
|
|
|
|
if op == testChanPoint {
|
|
|
|
opFound = true
|
|
|
|
break
|
2016-12-22 23:24:48 +03:00
|
|
|
}
|
|
|
|
}
|
2017-04-17 01:19:39 +03:00
|
|
|
|
|
|
|
// If we weren't able to locate the original outpoint in the set of
|
|
|
|
// possible outpoints, then we'll fail the test.
|
|
|
|
if !opFound {
|
|
|
|
t.Fatalf("possible outpoints did not contain the root outpoint")
|
|
|
|
}
|
2016-12-22 23:24:48 +03:00
|
|
|
}
|