channeldb: persist channel metadata (satoshis sent and received)

This commit adds persistence for a channel’s
TotalSatoshis[Sent|Received] fields. Also, the functions that perform
this persistence were renamed from [put|delete|fetch]ChanTotalFlow to
[put|delete|fetch]ChanAmountsTransferred.
This commit is contained in:
bryanvu 2016-11-15 23:59:55 -08:00 committed by Olaoluwa Osuntokun
parent 1199786498
commit 61f0d87138

@ -63,7 +63,7 @@ var (
minFeePerKbPrefix = []byte("mfp") minFeePerKbPrefix = []byte("mfp")
updatePrefix = []byte("uup") updatePrefix = []byte("uup")
satSentPrefix = []byte("ssp") satSentPrefix = []byte("ssp")
satRecievedPrefix = []byte("srp") satReceivedPrefix = []byte("srp")
netFeesPrefix = []byte("ntp") netFeesPrefix = []byte("ntp")
// chanIDKey stores the node, and channelID for an active channel. // chanIDKey stores the node, and channelID for an active channel.
@ -377,6 +377,9 @@ func (c *OpenChannel) UpdateCommitment(newCommitment *wire.MsgTx,
if err := putChanCapacity(chanBucket, c); err != nil { if err := putChanCapacity(chanBucket, c); err != nil {
return err return err
} }
if err := putChanAmountsTransferred(chanBucket, c); err != nil {
return err
}
if err := putChanNumUpdates(chanBucket, c); err != nil { if err := putChanNumUpdates(chanBucket, c); err != nil {
return err return err
} }
@ -646,7 +649,7 @@ func putOpenChannel(openChanBucket *bolt.Bucket, nodeChanBucket *bolt.Bucket,
if err := putChanNumUpdates(openChanBucket, channel); err != nil { if err := putChanNumUpdates(openChanBucket, channel); err != nil {
return err return err
} }
if err := putChanTotalFlow(openChanBucket, channel); err != nil { if err := putChanAmountsTransferred(openChanBucket, channel); err != nil {
return err return err
} }
@ -725,7 +728,7 @@ func fetchOpenChannel(openChanBucket *bolt.Bucket, nodeChanBucket *bolt.Bucket,
if err = fetchChanNumUpdates(openChanBucket, channel); err != nil { if err = fetchChanNumUpdates(openChanBucket, channel); err != nil {
return nil, err return nil, err
} }
if err = fetchChanTotalFlow(openChanBucket, channel); err != nil { if err = fetchChanAmountsTransferred(openChanBucket, channel); err != nil {
return nil, err return nil, err
} }
@ -746,7 +749,7 @@ func deleteOpenChannel(openChanBucket *bolt.Bucket, nodeChanBucket *bolt.Bucket,
if err := deleteChanNumUpdates(openChanBucket, channelID); err != nil { if err := deleteChanNumUpdates(openChanBucket, channelID); err != nil {
return err return err
} }
if err := deleteChanTotalFlow(openChanBucket, channelID); err != nil { if err := deleteChanAmountsTransferred(openChanBucket, channelID); err != nil {
return err return err
} }
@ -926,7 +929,7 @@ func fetchChanNumUpdates(openChanBucket *bolt.Bucket, channel *OpenChannel) erro
return nil return nil
} }
func putChanTotalFlow(openChanBucket *bolt.Bucket, channel *OpenChannel) error { func putChanAmountsTransferred(openChanBucket *bolt.Bucket, channel *OpenChannel) error {
scratch1 := make([]byte, 8) scratch1 := make([]byte, 8)
scratch2 := make([]byte, 8) scratch2 := make([]byte, 8)
@ -944,12 +947,12 @@ func putChanTotalFlow(openChanBucket *bolt.Bucket, channel *OpenChannel) error {
return err return err
} }
copy(keyPrefix[:3], satRecievedPrefix) copy(keyPrefix[:3], satReceivedPrefix)
byteOrder.PutUint64(scratch2, uint64(channel.TotalSatoshisReceived)) byteOrder.PutUint64(scratch2, uint64(channel.TotalSatoshisReceived))
return openChanBucket.Put(keyPrefix, scratch2) return openChanBucket.Put(keyPrefix, scratch2)
} }
func deleteChanTotalFlow(openChanBucket *bolt.Bucket, chanID []byte) error { func deleteChanAmountsTransferred(openChanBucket *bolt.Bucket, chanID []byte) error {
keyPrefix := make([]byte, 3+len(chanID)) keyPrefix := make([]byte, 3+len(chanID))
copy(keyPrefix[3:], chanID) copy(keyPrefix[3:], chanID)
@ -958,11 +961,11 @@ func deleteChanTotalFlow(openChanBucket *bolt.Bucket, chanID []byte) error {
return err return err
} }
copy(keyPrefix[:3], satRecievedPrefix) copy(keyPrefix[:3], satReceivedPrefix)
return openChanBucket.Delete(keyPrefix) return openChanBucket.Delete(keyPrefix)
} }
func fetchChanTotalFlow(openChanBucket *bolt.Bucket, channel *OpenChannel) error { func fetchChanAmountsTransferred(openChanBucket *bolt.Bucket, channel *OpenChannel) error {
var b bytes.Buffer var b bytes.Buffer
if err := writeOutpoint(&b, channel.ChanID); err != nil { if err := writeOutpoint(&b, channel.ChanID); err != nil {
return err return err
@ -975,7 +978,7 @@ func fetchChanTotalFlow(openChanBucket *bolt.Bucket, channel *OpenChannel) error
totalSentBytes := openChanBucket.Get(keyPrefix) totalSentBytes := openChanBucket.Get(keyPrefix)
channel.TotalSatoshisSent = byteOrder.Uint64(totalSentBytes) channel.TotalSatoshisSent = byteOrder.Uint64(totalSentBytes)
copy(keyPrefix[:3], satRecievedPrefix) copy(keyPrefix[:3], satReceivedPrefix)
totalReceivedBytes := openChanBucket.Get(keyPrefix) totalReceivedBytes := openChanBucket.Get(keyPrefix)
channel.TotalSatoshisReceived = byteOrder.Uint64(totalReceivedBytes) channel.TotalSatoshisReceived = byteOrder.Uint64(totalReceivedBytes)