lnwire: add new 32-byte persistent/pending channel ID's

This commit does to things: moves the prior ShortChannelID struct into
a new short_channel_id.go file, and also implements the new ChannelID’s
currently used within he specification.

These new ID’s are 32-bytes in length and used during initial channel
funding as well as during normal channel updates. During initial
channel funding, the ID is to be a random 32-byte string, while once
normal channel operation has began, the ID is to be (txid XOR index),
where index is the index of the funding outpoint.
This commit is contained in:
Olaoluwa Osuntokun
2017-04-16 15:19:39 -07:00
parent 1bb225a9bd
commit 8147151fbf
4 changed files with 200 additions and 59 deletions

View File

@@ -1,39 +1,56 @@
package lnwire
import (
"reflect"
"testing"
import "testing"
"github.com/davecgh/go-spew/spew"
)
// TestChannelIDOutPointConversion ensures that the IsChanPoint always
// recognizes its seed OutPoint for all possible values of an output index.
func TestChannelIDOutPointConversion(t *testing.T) {
testChanPoint := *outpoint1
func TestChannelIDEncoding(t *testing.T) {
var testCases = []ShortChannelID{
{
BlockHeight: (1 << 24) - 1,
TxIndex: (1 << 24) - 1,
TxPosition: (1 << 16) - 1,
},
{
BlockHeight: 2304934,
TxIndex: 2345,
TxPosition: 5,
},
{
BlockHeight: 9304934,
TxIndex: 2345,
TxPosition: 5233,
},
}
// For a given OutPoint, we'll run through all the possible output
// index values, mutating our test outpoint to match that output index.
for i := uint32(0); i < MaxFundingTxOutputs; i++ {
testChanPoint.Index = i
for _, testCase := range testCases {
chanInt := testCase.ToUint64()
// With the output index mutated, we'll convert it into a
// ChannelID.
cid := NewChanIDFromOutPoint(&testChanPoint)
newChanID := NewShortChanIDFromInt(chanInt)
if !reflect.DeepEqual(testCase, newChanID) {
t.Fatalf("chan ID's don't match: expected %v got %v",
spew.Sdump(testCase), spew.Sdump(newChanID))
// 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)
}
}
}
// TestGenPossibleOutPoints ensures taht the GenPossibleOutPoints generates a
// vali set of outpoints for a channelID. A set of outpoints is valid iff, the
// root outpoint (the outpoint that generated the ChannelID) is included in the
// returned set of outpoints.
func TestGenPossibleOutPoints(t *testing.T) {
// We'll first convert out test outpoint into a ChannelID.
testChanPoint := *outpoint1
chanID := NewChanIDFromOutPoint(&testChanPoint)
// With the chan ID created, we'll generate all possible root outpoints
// given this channel ID.
possibleOutPoints := chanID.GenPossibleOutPoints()
// 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
}
}
// 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")
}
}