channeldb: explicitly store if we're the channel initiator or not

This commit modifies the existing OpenChannel struct slightly to store
a bool which indicates if we were the one who initially initiated the
channel or not. This information is relevant as in the current draft of
the specification, much of the fee related negotiation is contingent on
who initiated the channel.
This commit is contained in:
Olaoluwa Osuntokun 2016-11-15 18:50:24 -08:00
parent 9b41d814dc
commit bc3e16dd13
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2
2 changed files with 32 additions and 3 deletions

@ -147,6 +147,11 @@ type OpenChannel struct {
// The outpoint of the final funding transaction.
// ChanType denotes which type of channel this is.
ChanType ChannelType
// IsInitiator is a bool which indicates if we were the original
// initiator for the channel. This value may affect how higher levels
// negotiate fees, or close the channel.
IsInitiator bool
FundingOutpoint *wire.OutPoint
OurMultiSigKey *btcec.PublicKey
@ -1134,6 +1139,16 @@ func putChanFundingInfo(nodeChanBucket *bolt.Bucket, channel *OpenChannel) error
return err
}
var boolByte [1]byte
if channel.IsInitiator {
boolByte[0] = 1
} else {
boolByte[0] = 0
}
if _, err := b.Write(boolByte[:]); err != nil {
return err
}
if _, err := b.Write([]byte{uint8(channel.ChanType)}); err != nil {
return err
}
@ -1195,6 +1210,16 @@ func fetchChanFundingInfo(nodeChanBucket *bolt.Bucket, channel *OpenChannel) err
unixSecs := byteOrder.Uint64(scratch)
channel.CreationTime = time.Unix(int64(unixSecs), 0)
var boolByte [1]byte
if _, err := io.ReadFull(infoBytes, boolByte[:]); err != nil {
return err
}
if boolByte[0] == 1 {
channel.IsInitiator = true
} else {
channel.IsInitiator = false
}
var chanType [1]byte
if _, err := io.ReadFull(infoBytes, chanType[:]); err != nil {
return err

@ -133,6 +133,7 @@ func createTestChannelState(cdb *DB) (*OpenChannel, error) {
}
return &OpenChannel{
IsInitiator: true,
ChanType: SingleFunder,
IdentityPub: pubKey,
ChanID: id,
@ -207,17 +208,20 @@ func TestOpenChannelPutGetDelete(t *testing.T) {
}
if state.MinFeePerKb != newState.MinFeePerKb {
t.Fatalf("fee/kb doens't match")
if state.IsInitiator != newState.IsInitiator {
t.Fatalf("initiator status doesn't match")
}
if state.ChanType != newState.ChanType {
t.Fatalf("channel type doesn't match")
}
if !bytes.Equal(state.OurCommitKey.SerializeCompressed(),
newState.OurCommitKey.SerializeCompressed()) {
t.Fatalf("our commit key dont't match")
t.Fatalf("our commit key doesn't match")
}
if !bytes.Equal(state.TheirCommitKey.SerializeCompressed(),
newState.TheirCommitKey.SerializeCompressed()) {
t.Fatalf("their commit key dont't match")
t.Fatalf("their commit key doesn't match")
}
if state.Capacity != newState.Capacity {
@ -343,7 +347,7 @@ func TestOpenChannelPutGetDelete(t *testing.T) {
func TestChannelStateTransition(t *testing.T) {
cdb, cleanUp, err := makeTestDB()
if err != nil {
t.Fatalf("uanble to make test database: %v", err)
t.Fatalf("unable to make test database: %v", err)
}
defer cleanUp()