mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-09-11 16:02:00 +02:00
channeldb: flatten the payment-htlcs-bucket
This commit is contained in:
@@ -368,14 +368,10 @@ func (p *PaymentControl) RegisterAttempt(paymentHash lntypes.Hash,
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create bucket for this attempt. Fail if the bucket already
|
err = htlcsBucket.Put(
|
||||||
// exists.
|
htlcBucketKey(htlcAttemptInfoKey, htlcIDBytes),
|
||||||
htlcBucket, err := htlcsBucket.CreateBucket(htlcIDBytes)
|
htlcInfoBytes,
|
||||||
if err != nil {
|
)
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = htlcBucket.Put(htlcAttemptInfoKey, htlcInfoBytes)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -427,8 +423,8 @@ func (p *PaymentControl) FailAttempt(hash lntypes.Hash,
|
|||||||
func (p *PaymentControl) updateHtlcKey(paymentHash lntypes.Hash,
|
func (p *PaymentControl) updateHtlcKey(paymentHash lntypes.Hash,
|
||||||
attemptID uint64, key, value []byte) (*MPPayment, error) {
|
attemptID uint64, key, value []byte) (*MPPayment, error) {
|
||||||
|
|
||||||
htlcIDBytes := make([]byte, 8)
|
aid := make([]byte, 8)
|
||||||
binary.BigEndian.PutUint64(htlcIDBytes, attemptID)
|
binary.BigEndian.PutUint64(aid, attemptID)
|
||||||
|
|
||||||
var payment *MPPayment
|
var payment *MPPayment
|
||||||
err := kvdb.Batch(p.db.Backend, func(tx kvdb.RwTx) error {
|
err := kvdb.Batch(p.db.Backend, func(tx kvdb.RwTx) error {
|
||||||
@@ -456,23 +452,22 @@ func (p *PaymentControl) updateHtlcKey(paymentHash lntypes.Hash,
|
|||||||
return fmt.Errorf("htlcs bucket not found")
|
return fmt.Errorf("htlcs bucket not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
htlcBucket := htlcsBucket.NestedReadWriteBucket(htlcIDBytes)
|
if htlcsBucket.Get(htlcBucketKey(htlcAttemptInfoKey, aid)) == nil {
|
||||||
if htlcBucket == nil {
|
|
||||||
return fmt.Errorf("HTLC with ID %v not registered",
|
return fmt.Errorf("HTLC with ID %v not registered",
|
||||||
attemptID)
|
attemptID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure the shard is not already failed or settled.
|
// Make sure the shard is not already failed or settled.
|
||||||
if htlcBucket.Get(htlcFailInfoKey) != nil {
|
if htlcsBucket.Get(htlcBucketKey(htlcFailInfoKey, aid)) != nil {
|
||||||
return ErrAttemptAlreadyFailed
|
return ErrAttemptAlreadyFailed
|
||||||
}
|
}
|
||||||
|
|
||||||
if htlcBucket.Get(htlcSettleInfoKey) != nil {
|
if htlcsBucket.Get(htlcBucketKey(htlcSettleInfoKey, aid)) != nil {
|
||||||
return ErrAttemptAlreadySettled
|
return ErrAttemptAlreadySettled
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add or update the key for this htlc.
|
// Add or update the key for this htlc.
|
||||||
err = htlcBucket.Put(key, value)
|
err = htlcsBucket.Put(htlcBucketKey(key, aid), value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@@ -35,14 +35,11 @@ var (
|
|||||||
// | |
|
// | |
|
||||||
// | |--payment-htlcs-bucket (shard-bucket)
|
// | |--payment-htlcs-bucket (shard-bucket)
|
||||||
// | | |
|
// | | |
|
||||||
// | | |-- <htlc attempt ID>
|
// | | |-- ai<htlc attempt ID>: <htlc attempt info>
|
||||||
// | | | |--htlc-attempt-info-key: <htlc attempt info>
|
// | | |-- si<htlc attempt ID>: <(optional) settle info>
|
||||||
// | | | |--htlc-settle-info-key: <(optional) settle info>
|
// | | |-- fi<htlc attempt ID>: <(optional) fail info>
|
||||||
// | | | |--htlc-fail-info-key: <(optional) fail info>
|
|
||||||
// | | |
|
// | | |
|
||||||
// | | |-- <htlc attempt ID>
|
// | | ...
|
||||||
// | | | |
|
|
||||||
// | | ... ...
|
|
||||||
// | |
|
// | |
|
||||||
// | |
|
// | |
|
||||||
// | |--duplicate-bucket (only for old, completed payments)
|
// | |--duplicate-bucket (only for old, completed payments)
|
||||||
@@ -50,9 +47,9 @@ var (
|
|||||||
// | |-- <seq-num>
|
// | |-- <seq-num>
|
||||||
// | | |--sequence-key: <sequence number>
|
// | | |--sequence-key: <sequence number>
|
||||||
// | | |--creation-info-key: <creation info>
|
// | | |--creation-info-key: <creation info>
|
||||||
// | | |--attempt-info-key: <attempt info>
|
// | | |--ai: <attempt info>
|
||||||
// | | |--settle-info-key: <settle info>
|
// | | |--si: <settle info>
|
||||||
// | | |--fail-info-key: <fail info>
|
// | | |--fi: <fail info>
|
||||||
// | |
|
// | |
|
||||||
// | |-- <seq-num>
|
// | |-- <seq-num>
|
||||||
// | | |
|
// | | |
|
||||||
@@ -77,17 +74,19 @@ var (
|
|||||||
// about the HTLCs that were attempted for a payment.
|
// about the HTLCs that were attempted for a payment.
|
||||||
paymentHtlcsBucket = []byte("payment-htlcs-bucket")
|
paymentHtlcsBucket = []byte("payment-htlcs-bucket")
|
||||||
|
|
||||||
// htlcAttemptInfoKey is a key used in a HTLC's sub-bucket to store the
|
// htlcAttemptInfoKey is the key used as the prefix of an HTLC attempt
|
||||||
// info about the attempt that was done for the HTLC in question.
|
// to store the info about the attempt that was done for the HTLC in
|
||||||
htlcAttemptInfoKey = []byte("htlc-attempt-info")
|
// question. The HTLC attempt ID is concatenated at the end.
|
||||||
|
htlcAttemptInfoKey = []byte("ai")
|
||||||
|
|
||||||
// htlcSettleInfoKey is a key used in a HTLC's sub-bucket to store the
|
// htlcSettleInfoKey is the key used as the prefix of an HTLC attempt
|
||||||
// settle info, if any.
|
// settle info, if any. The HTLC attempt ID is concatenated at the end.
|
||||||
htlcSettleInfoKey = []byte("htlc-settle-info")
|
htlcSettleInfoKey = []byte("si")
|
||||||
|
|
||||||
// htlcFailInfoKey is a key used in a HTLC's sub-bucket to store
|
// htlcFailInfoKey is the key used as the prefix of an HTLC attempt
|
||||||
// failure information, if any.
|
// failure information, if any.The HTLC attempt ID is concatenated at
|
||||||
htlcFailInfoKey = []byte("htlc-fail-info")
|
// the end.
|
||||||
|
htlcFailInfoKey = []byte("fi")
|
||||||
|
|
||||||
// paymentFailInfoKey is a key used in the payment's sub-bucket to
|
// paymentFailInfoKey is a key used in the payment's sub-bucket to
|
||||||
// store information about the reason a payment failed.
|
// store information about the reason a payment failed.
|
||||||
@@ -230,6 +229,15 @@ type PaymentCreationInfo struct {
|
|||||||
PaymentRequest []byte
|
PaymentRequest []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// htlcBucketKey creates a composite key from prefix and id where the result is
|
||||||
|
// simply the two concatenated.
|
||||||
|
func htlcBucketKey(prefix, id []byte) []byte {
|
||||||
|
key := make([]byte, len(prefix)+len(id))
|
||||||
|
copy(key, prefix)
|
||||||
|
copy(key[len(prefix):], id)
|
||||||
|
return key
|
||||||
|
}
|
||||||
|
|
||||||
// FetchPayments returns all sent payments found in the DB.
|
// FetchPayments returns all sent payments found in the DB.
|
||||||
//
|
//
|
||||||
// nolint: dupl
|
// nolint: dupl
|
||||||
@@ -378,80 +386,93 @@ func fetchPayment(bucket kvdb.RBucket) (*MPPayment, error) {
|
|||||||
// fetchHtlcAttempts retrives all htlc attempts made for the payment found in
|
// fetchHtlcAttempts retrives all htlc attempts made for the payment found in
|
||||||
// the given bucket.
|
// the given bucket.
|
||||||
func fetchHtlcAttempts(bucket kvdb.RBucket) ([]HTLCAttempt, error) {
|
func fetchHtlcAttempts(bucket kvdb.RBucket) ([]HTLCAttempt, error) {
|
||||||
htlcs := make([]HTLCAttempt, 0)
|
htlcsMap := make(map[uint64]*HTLCAttempt)
|
||||||
|
|
||||||
err := bucket.ForEach(func(k, _ []byte) error {
|
attemptInfoCount := 0
|
||||||
aid := byteOrder.Uint64(k)
|
err := bucket.ForEach(func(k, v []byte) error {
|
||||||
htlcBucket := bucket.NestedReadBucket(k)
|
aid := byteOrder.Uint64(k[len(k)-8:])
|
||||||
|
|
||||||
attemptInfo, err := fetchHtlcAttemptInfo(
|
if _, ok := htlcsMap[aid]; !ok {
|
||||||
htlcBucket,
|
htlcsMap[aid] = &HTLCAttempt{}
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
attemptInfo.AttemptID = aid
|
|
||||||
|
|
||||||
htlc := HTLCAttempt{
|
|
||||||
HTLCAttemptInfo: *attemptInfo,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Settle info might be nil.
|
var err error
|
||||||
htlc.Settle, err = fetchHtlcSettleInfo(htlcBucket)
|
switch {
|
||||||
if err != nil {
|
case bytes.HasPrefix(k, htlcAttemptInfoKey):
|
||||||
return err
|
attemptInfo, err := readHtlcAttemptInfo(v)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
attemptInfo.AttemptID = aid
|
||||||
|
htlcsMap[aid].HTLCAttemptInfo = *attemptInfo
|
||||||
|
attemptInfoCount++
|
||||||
|
|
||||||
|
case bytes.HasPrefix(k, htlcSettleInfoKey):
|
||||||
|
htlcsMap[aid].Settle, err = readHtlcSettleInfo(v)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
case bytes.HasPrefix(k, htlcFailInfoKey):
|
||||||
|
htlcsMap[aid].Failure, err = readHtlcFailInfo(v)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unknown htlc attempt key")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Failure info might be nil.
|
|
||||||
htlc.Failure, err = fetchHtlcFailInfo(htlcBucket)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
htlcs = append(htlcs, htlc)
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return htlcs, nil
|
// Sanity check that all htlcs have an attempt info.
|
||||||
}
|
if attemptInfoCount != len(htlcsMap) {
|
||||||
|
|
||||||
// fetchHtlcAttemptInfo fetches the payment attempt info for this htlc from the
|
|
||||||
// bucket.
|
|
||||||
func fetchHtlcAttemptInfo(bucket kvdb.RBucket) (*HTLCAttemptInfo, error) {
|
|
||||||
b := bucket.Get(htlcAttemptInfoKey)
|
|
||||||
if b == nil {
|
|
||||||
return nil, errNoAttemptInfo
|
return nil, errNoAttemptInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
keys := make([]uint64, len(htlcsMap))
|
||||||
|
i := 0
|
||||||
|
for k := range htlcsMap {
|
||||||
|
keys[i] = k
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort HTLC attempts by their attempt ID. This is needed because in the
|
||||||
|
// DB we store the attempts with keys prefixed by their status which
|
||||||
|
// changes order (groups them together by status).
|
||||||
|
sort.Slice(keys, func(i, j int) bool {
|
||||||
|
return keys[i] < keys[j]
|
||||||
|
})
|
||||||
|
|
||||||
|
htlcs := make([]HTLCAttempt, len(htlcsMap))
|
||||||
|
for i, key := range keys {
|
||||||
|
htlcs[i] = *htlcsMap[key]
|
||||||
|
}
|
||||||
|
|
||||||
|
return htlcs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// readHtlcAttemptInfo reads the payment attempt info for this htlc.
|
||||||
|
func readHtlcAttemptInfo(b []byte) (*HTLCAttemptInfo, error) {
|
||||||
r := bytes.NewReader(b)
|
r := bytes.NewReader(b)
|
||||||
return deserializeHTLCAttemptInfo(r)
|
return deserializeHTLCAttemptInfo(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// fetchHtlcSettleInfo retrieves the settle info for the htlc. If the htlc isn't
|
// readHtlcSettleInfo reads the settle info for the htlc. If the htlc isn't
|
||||||
// settled, nil is returned.
|
// settled, nil is returned.
|
||||||
func fetchHtlcSettleInfo(bucket kvdb.RBucket) (*HTLCSettleInfo, error) {
|
func readHtlcSettleInfo(b []byte) (*HTLCSettleInfo, error) {
|
||||||
b := bucket.Get(htlcSettleInfoKey)
|
|
||||||
if b == nil {
|
|
||||||
// Settle info is optional.
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
r := bytes.NewReader(b)
|
r := bytes.NewReader(b)
|
||||||
return deserializeHTLCSettleInfo(r)
|
return deserializeHTLCSettleInfo(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// fetchHtlcFailInfo retrieves the failure info for the htlc. If the htlc hasn't
|
// readHtlcFailInfo reads the failure info for the htlc. If the htlc hasn't
|
||||||
// failed, nil is returned.
|
// failed, nil is returned.
|
||||||
func fetchHtlcFailInfo(bucket kvdb.RBucket) (*HTLCFailInfo, error) {
|
func readHtlcFailInfo(b []byte) (*HTLCFailInfo, error) {
|
||||||
b := bucket.Get(htlcFailInfoKey)
|
|
||||||
if b == nil {
|
|
||||||
// Fail info is optional.
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
r := bytes.NewReader(b)
|
r := bytes.NewReader(b)
|
||||||
return deserializeHTLCFailInfo(r)
|
return deserializeHTLCFailInfo(r)
|
||||||
}
|
}
|
||||||
@@ -797,8 +818,21 @@ func (d *DB) DeletePayments(failedOnly, failedHtlcsOnly bool) error {
|
|||||||
)
|
)
|
||||||
|
|
||||||
for _, aid := range htlcIDs {
|
for _, aid := range htlcIDs {
|
||||||
err := htlcsBucket.DeleteNestedBucket(aid)
|
if err := htlcsBucket.Delete(
|
||||||
if err != nil {
|
htlcBucketKey(htlcAttemptInfoKey, aid),
|
||||||
|
); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := htlcsBucket.Delete(
|
||||||
|
htlcBucketKey(htlcFailInfoKey, aid),
|
||||||
|
); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := htlcsBucket.Delete(
|
||||||
|
htlcBucketKey(htlcSettleInfoKey, aid),
|
||||||
|
); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user