channeldb: properly use a read-transaction in FetchChannelEdgesByOutpoint

Before this commit, we’d unnecessarily use a write transaction within
the FetchChannelEdgesByOutpoint. This is wasteful as the function only
actually reads items from the database, and doesn’t attempt any
mutations at all.
This commit is contained in:
Olaoluwa Osuntokun 2018-01-28 14:48:28 -08:00
parent bde3828208
commit a94648e9dc
No known key found for this signature in database
GPG Key ID: 964EA263DD637C21

@ -1375,31 +1375,31 @@ func (c *ChannelGraph) FetchChannelEdgesByOutpoint(op *wire.OutPoint) (*ChannelE
policy2 *ChannelEdgePolicy policy2 *ChannelEdgePolicy
) )
err := c.db.Update(func(tx *bolt.Tx) error { err := c.db.View(func(tx *bolt.Tx) error {
// First, grab the node bucket. This will be used to populate // First, grab the node bucket. This will be used to populate
// the Node pointers in each edge read from disk. // the Node pointers in each edge read from disk.
nodes, err := tx.CreateBucketIfNotExists(nodeBucket) nodes := tx.Bucket(nodeBucket)
if err != nil { if nodes == nil {
return err return ErrGraphNotFound
} }
// Next, grab the edge bucket which stores the edges, and also // Next, grab the edge bucket which stores the edges, and also
// the index itself so we can group the directed edges together // the index itself so we can group the directed edges together
// logically. // logically.
edges, err := tx.CreateBucketIfNotExists(edgeBucket) edges := tx.Bucket(edgeBucket)
if err != nil { if edges == nil {
return err return ErrGraphNoEdgesFound
} }
edgeIndex, err := edges.CreateBucketIfNotExists(edgeIndexBucket) edgeIndex := edges.Bucket(edgeIndexBucket)
if err != nil { if edgeIndex == nil {
return err return ErrGraphNoEdgesFound
} }
// If the channel's outpoint doesn't exist within the outpoint // If the channel's outpoint doesn't exist within the outpoint
// index, then the edge does not exist. // index, then the edge does not exist.
chanIndex, err := edges.CreateBucketIfNotExists(channelPointBucket) chanIndex := edges.Bucket(channelPointBucket)
if err != nil { if chanIndex == nil {
return err return ErrGraphNoEdgesFound
} }
var b bytes.Buffer var b bytes.Buffer
if err := writeOutpoint(&b, op); err != nil { if err := writeOutpoint(&b, op); err != nil {