channeldb: properly craft key for reading/writing channel commitments

In this commit, we fix an existing bug that arose due to incorrectly
crafting the key we use to store channel commitments. Before this
commit, we tried to copy to a slice that hadn’t been allocated yet. As
a result, the key would only have the 0x00 or 0x01 as its value. We fix
this by properly crafting the key using the built-in append function.
This commit is contained in:
Olaoluwa Osuntokun
2017-11-10 19:36:05 -08:00
parent 699e5327e1
commit 1fb05e0436

View File

@@ -1475,12 +1475,11 @@ func serializeChanCommit(w io.Writer, c *ChannelCommitment) error {
func putChanCommitment(chanBucket *bolt.Bucket, c *ChannelCommitment, func putChanCommitment(chanBucket *bolt.Bucket, c *ChannelCommitment,
local bool) error { local bool) error {
var key []byte var commitKey []byte
copy(key[:], chanCommitmentKey)
if local { if local {
key = append(key, byte(0x00)) commitKey = append(chanCommitmentKey, byte(0x00))
} else { } else {
key = append(key, byte(0x01)) commitKey = append(chanCommitmentKey, byte(0x01))
} }
var b bytes.Buffer var b bytes.Buffer
@@ -1488,7 +1487,7 @@ func putChanCommitment(chanBucket *bolt.Bucket, c *ChannelCommitment,
return err return err
} }
return chanBucket.Put(key, b.Bytes()) return chanBucket.Put(commitKey, b.Bytes())
} }
func putChanCommitments(chanBucket *bolt.Bucket, channel *OpenChannel) error { func putChanCommitments(chanBucket *bolt.Bucket, channel *OpenChannel) error {
@@ -1581,15 +1580,14 @@ func deserializeChanCommit(r io.Reader) (ChannelCommitment, error) {
} }
func fetchChanCommitment(chanBucket *bolt.Bucket, local bool) (ChannelCommitment, error) { func fetchChanCommitment(chanBucket *bolt.Bucket, local bool) (ChannelCommitment, error) {
var key []byte var commitKey []byte
copy(key[:], chanCommitmentKey)
if local { if local {
key = append(key, byte(0x00)) commitKey = append(chanCommitmentKey, byte(0x00))
} else { } else {
key = append(key, byte(0x01)) commitKey = append(chanCommitmentKey, byte(0x01))
} }
commitBytes := chanBucket.Get(key) commitBytes := chanBucket.Get(commitKey)
if commitBytes == nil { if commitBytes == nil {
return ChannelCommitment{}, ErrNoCommitmentsFound return ChannelCommitment{}, ErrNoCommitmentsFound
} }