contractcourt: use graphdb outpoint helpers

Start using the single set of exported write/read functions for
wire.Outpoint.
This commit is contained in:
Elle Mouton 2024-10-22 12:54:25 +02:00
parent 382539a6eb
commit b707fd55b2
No known key found for this signature in database
GPG Key ID: D7D916376026F177
3 changed files with 31 additions and 55 deletions

View File

@ -16,6 +16,7 @@ import (
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/fn"
graphdb "github.com/lightningnetwork/lnd/graph/db"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/labels"
@ -1856,7 +1857,8 @@ func (rs *RetributionStore) Add(ret *retributionInfo) error {
}
var outBuf bytes.Buffer
if err := writeOutpoint(&outBuf, &ret.chanPoint); err != nil {
err = graphdb.WriteOutpoint(&outBuf, &ret.chanPoint)
if err != nil {
return err
}
@ -1907,7 +1909,8 @@ func (rs *RetributionStore) IsBreached(chanPoint *wire.OutPoint) (bool, error) {
}
var chanBuf bytes.Buffer
if err := writeOutpoint(&chanBuf, chanPoint); err != nil {
err := graphdb.WriteOutpoint(&chanBuf, chanPoint)
if err != nil {
return err
}
@ -1947,7 +1950,8 @@ func (rs *RetributionStore) Remove(chanPoint *wire.OutPoint) error {
// Serialize the channel point we are intending to remove.
var chanBuf bytes.Buffer
if err := writeOutpoint(&chanBuf, chanPoint); err != nil {
err = graphdb.WriteOutpoint(&chanBuf, chanPoint)
if err != nil {
return err
}
chanBytes := chanBuf.Bytes()
@ -2017,7 +2021,7 @@ func (ret *retributionInfo) Encode(w io.Writer) error {
return err
}
if err := writeOutpoint(w, &ret.chanPoint); err != nil {
if err := graphdb.WriteOutpoint(w, &ret.chanPoint); err != nil {
return err
}
@ -2057,7 +2061,7 @@ func (ret *retributionInfo) Decode(r io.Reader) error {
}
ret.commitHash = *hash
if err := readOutpoint(r, &ret.chanPoint); err != nil {
if err := graphdb.ReadOutpoint(r, &ret.chanPoint); err != nil {
return err
}
@ -2100,7 +2104,7 @@ func (bo *breachedOutput) Encode(w io.Writer) error {
return err
}
if err := writeOutpoint(w, &bo.outpoint); err != nil {
if err := graphdb.WriteOutpoint(w, &bo.outpoint); err != nil {
return err
}
@ -2131,7 +2135,7 @@ func (bo *breachedOutput) Decode(r io.Reader) error {
}
bo.amt = btcutil.Amount(binary.BigEndian.Uint64(scratch[:8]))
if err := readOutpoint(r, &bo.outpoint); err != nil {
if err := graphdb.ReadOutpoint(r, &bo.outpoint); err != nil {
return err
}

View File

@ -8,6 +8,7 @@ import (
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/channeldb"
graphdb "github.com/lightningnetwork/lnd/graph/db"
"github.com/lightningnetwork/lnd/kvdb"
)
@ -221,7 +222,7 @@ func prefixOutputKey(statePrefix []byte,
return nil, err
}
err := writeOutpoint(&pfxOutputBuffer, &outpoint)
err := graphdb.WriteOutpoint(&pfxOutputBuffer, &outpoint)
if err != nil {
return nil, err
}
@ -738,7 +739,9 @@ func (ns *NurseryStore) ListChannels() ([]wire.OutPoint, error) {
return chanIndex.ForEach(func(chanBytes, _ []byte) error {
var chanPoint wire.OutPoint
err := readOutpoint(bytes.NewReader(chanBytes), &chanPoint)
err := graphdb.ReadOutpoint(
bytes.NewReader(chanBytes), &chanPoint,
)
if err != nil {
return err
}
@ -804,12 +807,13 @@ func (ns *NurseryStore) RemoveChannel(chanPoint *wire.OutPoint) error {
// Serialize the provided channel point, such that we can delete
// the mature channel bucket.
var chanBuffer bytes.Buffer
if err := writeOutpoint(&chanBuffer, chanPoint); err != nil {
err := graphdb.WriteOutpoint(&chanBuffer, chanPoint)
if err != nil {
return err
}
chanBytes := chanBuffer.Bytes()
err := ns.forChanOutputs(tx, chanPoint, func(k, v []byte) error {
err = ns.forChanOutputs(tx, chanPoint, func(k, v []byte) error {
if !bytes.HasPrefix(k, gradPrefix) {
return ErrImmatureChannel
}
@ -959,7 +963,7 @@ func (ns *NurseryStore) createChannelBucket(tx kvdb.RwTx,
// Serialize the provided channel point, as this provides the name of
// the channel bucket of interest.
var chanBuffer bytes.Buffer
if err := writeOutpoint(&chanBuffer, chanPoint); err != nil {
if err := graphdb.WriteOutpoint(&chanBuffer, chanPoint); err != nil {
return nil, err
}
@ -989,7 +993,7 @@ func (ns *NurseryStore) getChannelBucket(tx kvdb.RTx,
// Serialize the provided channel point and return the bucket matching
// the serialized key.
var chanBuffer bytes.Buffer
if err := writeOutpoint(&chanBuffer, chanPoint); err != nil {
if err := graphdb.WriteOutpoint(&chanBuffer, chanPoint); err != nil {
return nil
}
@ -1017,7 +1021,7 @@ func (ns *NurseryStore) getChannelBucketWrite(tx kvdb.RwTx,
// Serialize the provided channel point and return the bucket matching
// the serialized key.
var chanBuffer bytes.Buffer
if err := writeOutpoint(&chanBuffer, chanPoint); err != nil {
if err := graphdb.WriteOutpoint(&chanBuffer, chanPoint); err != nil {
return nil
}
@ -1142,7 +1146,7 @@ func (ns *NurseryStore) createHeightChanBucket(tx kvdb.RwTx,
// Serialize the provided channel point, as this generates the name of
// the subdirectory corresponding to the channel of interest.
var chanBuffer bytes.Buffer
if err := writeOutpoint(&chanBuffer, chanPoint); err != nil {
if err := graphdb.WriteOutpoint(&chanBuffer, chanPoint); err != nil {
return nil, err
}
chanBytes := chanBuffer.Bytes()
@ -1168,7 +1172,7 @@ func (ns *NurseryStore) getHeightChanBucketWrite(tx kvdb.RwTx,
// Serialize the provided channel point, which generates the key for
// looking up the proper height-channel bucket inside the height bucket.
var chanBuffer bytes.Buffer
if err := writeOutpoint(&chanBuffer, chanPoint); err != nil {
if err := graphdb.WriteOutpoint(&chanBuffer, chanPoint); err != nil {
return nil
}
chanBytes := chanBuffer.Bytes()
@ -1312,7 +1316,7 @@ func (ns *NurseryStore) removeOutputFromHeight(tx kvdb.RwTx, height uint32,
}
var chanBuffer bytes.Buffer
if err := writeOutpoint(&chanBuffer, chanPoint); err != nil {
if err := graphdb.WriteOutpoint(&chanBuffer, chanPoint); err != nil {
return err
}

View File

@ -16,6 +16,7 @@ import (
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/fn"
graphdb "github.com/lightningnetwork/lnd/graph/db"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/labels"
"github.com/lightningnetwork/lnd/lnutils"
@ -1466,10 +1467,10 @@ func (k *kidOutput) Encode(w io.Writer) error {
}
op := k.OutPoint()
if err := writeOutpoint(w, &op); err != nil {
if err := graphdb.WriteOutpoint(w, &op); err != nil {
return err
}
if err := writeOutpoint(w, k.OriginChanPoint()); err != nil {
if err := graphdb.WriteOutpoint(w, k.OriginChanPoint()); err != nil {
return err
}
@ -1521,11 +1522,12 @@ func (k *kidOutput) Decode(r io.Reader) error {
}
k.amt = btcutil.Amount(byteOrder.Uint64(scratch[:]))
if err := readOutpoint(io.LimitReader(r, 40), &k.outpoint); err != nil {
err := graphdb.ReadOutpoint(io.LimitReader(r, 40), &k.outpoint)
if err != nil {
return err
}
err := readOutpoint(io.LimitReader(r, 40), &k.originChanPoint)
err = graphdb.ReadOutpoint(io.LimitReader(r, 40), &k.originChanPoint)
if err != nil {
return err
}
@ -1577,40 +1579,6 @@ func (k *kidOutput) Decode(r io.Reader) error {
return nil
}
// TODO(bvu): copied from channeldb, remove repetition
func writeOutpoint(w io.Writer, o *wire.OutPoint) error {
// TODO(roasbeef): make all scratch buffers on the stack
scratch := make([]byte, 4)
// TODO(roasbeef): write raw 32 bytes instead of wasting the extra
// byte.
if err := wire.WriteVarBytes(w, 0, o.Hash[:]); err != nil {
return err
}
byteOrder.PutUint32(scratch, o.Index)
_, err := w.Write(scratch)
return err
}
// TODO(bvu): copied from channeldb, remove repetition
func readOutpoint(r io.Reader, o *wire.OutPoint) error {
scratch := make([]byte, 4)
txid, err := wire.ReadVarBytes(r, 0, 32, "prevout")
if err != nil {
return err
}
copy(o.Hash[:], txid)
if _, err := r.Read(scratch); err != nil {
return err
}
o.Index = byteOrder.Uint32(scratch)
return nil
}
// Compile-time constraint to ensure kidOutput implements the
// Input interface.