mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-09-01 02:02:10 +02:00
routing+channeldb: use HTLCAttempt
instead of HTLCAttemptInfo
This commit refactors the params used in lifecycle to prefer `HTLCAttempt` over `HTLCAttemptInfo`. This change is needed as `HTLCAttempt` also wraps settled and failure info, which is useful in the following commits.
This commit is contained in:
committed by
Olaoluwa Osuntokun
parent
a6be939bfa
commit
20e7e801c0
@@ -47,15 +47,15 @@ type HTLCAttemptInfo struct {
|
|||||||
Hash *lntypes.Hash
|
Hash *lntypes.Hash
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewHtlcAttemptInfo creates a htlc attempt.
|
// NewHtlcAttempt creates a htlc attempt.
|
||||||
func NewHtlcAttemptInfo(attemptID uint64, sessionKey *btcec.PrivateKey,
|
func NewHtlcAttempt(attemptID uint64, sessionKey *btcec.PrivateKey,
|
||||||
route route.Route, attemptTime time.Time,
|
route route.Route, attemptTime time.Time,
|
||||||
hash *lntypes.Hash) *HTLCAttemptInfo {
|
hash *lntypes.Hash) *HTLCAttempt {
|
||||||
|
|
||||||
var scratch [btcec.PrivKeyBytesLen]byte
|
var scratch [btcec.PrivKeyBytesLen]byte
|
||||||
copy(scratch[:], sessionKey.Serialize())
|
copy(scratch[:], sessionKey.Serialize())
|
||||||
|
|
||||||
return &HTLCAttemptInfo{
|
info := HTLCAttemptInfo{
|
||||||
AttemptID: attemptID,
|
AttemptID: attemptID,
|
||||||
sessionKey: scratch,
|
sessionKey: scratch,
|
||||||
cachedSessionKey: sessionKey,
|
cachedSessionKey: sessionKey,
|
||||||
@@ -63,6 +63,8 @@ func NewHtlcAttemptInfo(attemptID uint64, sessionKey *btcec.PrivateKey,
|
|||||||
AttemptTime: attemptTime,
|
AttemptTime: attemptTime,
|
||||||
Hash: hash,
|
Hash: hash,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return &HTLCAttempt{HTLCAttemptInfo: info}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SessionKey returns the ephemeral key used for a htlc attempt. This function
|
// SessionKey returns the ephemeral key used for a htlc attempt. This function
|
||||||
|
@@ -38,7 +38,7 @@ func genInfo() (*PaymentCreationInfo, *HTLCAttemptInfo,
|
|||||||
}
|
}
|
||||||
|
|
||||||
rhash := sha256.Sum256(preimage[:])
|
rhash := sha256.Sum256(preimage[:])
|
||||||
attempt := NewHtlcAttemptInfo(
|
attempt := NewHtlcAttempt(
|
||||||
0, priv, *testRoute.Copy(), time.Time{}, nil,
|
0, priv, *testRoute.Copy(), time.Time{}, nil,
|
||||||
)
|
)
|
||||||
return &PaymentCreationInfo{
|
return &PaymentCreationInfo{
|
||||||
@@ -46,7 +46,7 @@ func genInfo() (*PaymentCreationInfo, *HTLCAttemptInfo,
|
|||||||
Value: testRoute.ReceiverAmt(),
|
Value: testRoute.ReceiverAmt(),
|
||||||
CreationTime: time.Unix(time.Now().Unix(), 0),
|
CreationTime: time.Unix(time.Now().Unix(), 0),
|
||||||
PaymentRequest: []byte("hola"),
|
PaymentRequest: []byte("hola"),
|
||||||
}, attempt, preimage, nil
|
}, &attempt.HTLCAttemptInfo, preimage, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestPaymentControlSwitchFail checks that payment status returns to Failed
|
// TestPaymentControlSwitchFail checks that payment status returns to Failed
|
||||||
|
@@ -69,11 +69,11 @@ func makeFakeInfo() (*PaymentCreationInfo, *HTLCAttemptInfo) {
|
|||||||
PaymentRequest: []byte(""),
|
PaymentRequest: []byte(""),
|
||||||
}
|
}
|
||||||
|
|
||||||
a := NewHtlcAttemptInfo(
|
a := NewHtlcAttempt(
|
||||||
44, priv, testRoute, time.Unix(100, 0), &hash,
|
44, priv, testRoute, time.Unix(100, 0), &hash,
|
||||||
)
|
)
|
||||||
|
|
||||||
return c, a
|
return c, &a.HTLCAttemptInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSentPaymentSerialization(t *testing.T) {
|
func TestSentPaymentSerialization(t *testing.T) {
|
||||||
|
@@ -560,9 +560,9 @@ func genInfo() (*channeldb.PaymentCreationInfo, *channeldb.HTLCAttemptInfo,
|
|||||||
CreationTime: time.Unix(time.Now().Unix(), 0),
|
CreationTime: time.Unix(time.Now().Unix(), 0),
|
||||||
PaymentRequest: []byte("hola"),
|
PaymentRequest: []byte("hola"),
|
||||||
},
|
},
|
||||||
channeldb.NewHtlcAttemptInfo(
|
&channeldb.NewHtlcAttempt(
|
||||||
1, priv, testRoute, time.Time{}, nil,
|
1, priv, testRoute, time.Time{}, nil,
|
||||||
), preimage, nil
|
).HTLCAttemptInfo, preimage, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func genPreimage() ([32]byte, error) {
|
func genPreimage() ([32]byte, error) {
|
||||||
|
@@ -105,7 +105,7 @@ func (p *paymentLifecycle) resumePayment() ([32]byte, *route.Route, error) {
|
|||||||
log.Infof("Resuming payment shard %v for payment %v",
|
log.Infof("Resuming payment shard %v for payment %v",
|
||||||
a.AttemptID, p.identifier)
|
a.AttemptID, p.identifier)
|
||||||
|
|
||||||
shardHandler.collectResultAsync(&a.HTLCAttemptInfo)
|
shardHandler.collectResultAsync(&a)
|
||||||
}
|
}
|
||||||
|
|
||||||
// exitWithErr is a helper closure that logs and returns an error.
|
// exitWithErr is a helper closure that logs and returns an error.
|
||||||
@@ -400,7 +400,7 @@ type launchOutcome struct {
|
|||||||
// non-nil error, it means that the attempt was not sent onto the network, so
|
// non-nil error, it means that the attempt was not sent onto the network, so
|
||||||
// no result will be available in the future for it.
|
// no result will be available in the future for it.
|
||||||
func (p *shardHandler) launchShard(rt *route.Route,
|
func (p *shardHandler) launchShard(rt *route.Route,
|
||||||
lastShard bool) (*channeldb.HTLCAttemptInfo, *launchOutcome, error) {
|
lastShard bool) (*channeldb.HTLCAttempt, *launchOutcome, error) {
|
||||||
|
|
||||||
// Using the route received from the payment session, create a new
|
// Using the route received from the payment session, create a new
|
||||||
// shard to send.
|
// shard to send.
|
||||||
@@ -414,7 +414,9 @@ func (p *shardHandler) launchShard(rt *route.Route,
|
|||||||
// of the payment that we attempted to send, such that we can query the
|
// of the payment that we attempted to send, such that we can query the
|
||||||
// Switch for its whereabouts. The route is needed to handle the result
|
// Switch for its whereabouts. The route is needed to handle the result
|
||||||
// when it eventually comes back.
|
// when it eventually comes back.
|
||||||
err = p.router.cfg.Control.RegisterAttempt(p.identifier, attempt)
|
err = p.router.cfg.Control.RegisterAttempt(
|
||||||
|
p.identifier, &attempt.HTLCAttemptInfo,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
@@ -425,7 +427,7 @@ func (p *shardHandler) launchShard(rt *route.Route,
|
|||||||
if sendErr != nil {
|
if sendErr != nil {
|
||||||
// TODO(joostjager): Distinguish unexpected internal errors
|
// TODO(joostjager): Distinguish unexpected internal errors
|
||||||
// from real send errors.
|
// from real send errors.
|
||||||
htlcAttempt, err := p.failAttempt(attempt, sendErr)
|
htlcAttempt, err := p.failAttempt(attempt.AttemptID, sendErr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
@@ -452,7 +454,7 @@ type shardResult struct {
|
|||||||
// collectResultAsync launches a goroutine that will wait for the result of the
|
// collectResultAsync launches a goroutine that will wait for the result of the
|
||||||
// given HTLC attempt to be available then handle its result. It will fail the
|
// given HTLC attempt to be available then handle its result. It will fail the
|
||||||
// payment with the control tower if a terminal error is encountered.
|
// payment with the control tower if a terminal error is encountered.
|
||||||
func (p *shardHandler) collectResultAsync(attempt *channeldb.HTLCAttemptInfo) {
|
func (p *shardHandler) collectResultAsync(attempt *channeldb.HTLCAttempt) {
|
||||||
// errToSend is the error to be sent to sh.shardErrors.
|
// errToSend is the error to be sent to sh.shardErrors.
|
||||||
var errToSend error
|
var errToSend error
|
||||||
|
|
||||||
@@ -507,7 +509,7 @@ func (p *shardHandler) collectResultAsync(attempt *channeldb.HTLCAttemptInfo) {
|
|||||||
// collectResult waits for the result for the given attempt to be available
|
// collectResult waits for the result for the given attempt to be available
|
||||||
// from the Switch, then records the attempt outcome with the control tower. A
|
// from the Switch, then records the attempt outcome with the control tower. A
|
||||||
// shardResult is returned, indicating the final outcome of this HTLC attempt.
|
// shardResult is returned, indicating the final outcome of this HTLC attempt.
|
||||||
func (p *shardHandler) collectResult(attempt *channeldb.HTLCAttemptInfo) (
|
func (p *shardHandler) collectResult(attempt *channeldb.HTLCAttempt) (
|
||||||
*shardResult, error) {
|
*shardResult, error) {
|
||||||
|
|
||||||
// We'll retrieve the hash specific to this shard from the
|
// We'll retrieve the hash specific to this shard from the
|
||||||
@@ -548,7 +550,7 @@ func (p *shardHandler) collectResult(attempt *channeldb.HTLCAttemptInfo) (
|
|||||||
"the Switch, retrying.", attempt.AttemptID,
|
"the Switch, retrying.", attempt.AttemptID,
|
||||||
p.identifier)
|
p.identifier)
|
||||||
|
|
||||||
attempt, cErr := p.failAttempt(attempt, err)
|
attempt, cErr := p.failAttempt(attempt.AttemptID, err)
|
||||||
if cErr != nil {
|
if cErr != nil {
|
||||||
return nil, cErr
|
return nil, cErr
|
||||||
}
|
}
|
||||||
@@ -586,7 +588,7 @@ func (p *shardHandler) collectResult(attempt *channeldb.HTLCAttemptInfo) (
|
|||||||
// In case of a payment failure, fail the attempt with the control
|
// In case of a payment failure, fail the attempt with the control
|
||||||
// tower and return.
|
// tower and return.
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
attempt, err := p.failAttempt(attempt, result.Error)
|
attempt, err := p.failAttempt(attempt.AttemptID, result.Error)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -630,7 +632,7 @@ func (p *shardHandler) collectResult(attempt *channeldb.HTLCAttemptInfo) (
|
|||||||
|
|
||||||
// createNewPaymentAttempt creates a new payment attempt from the given route.
|
// createNewPaymentAttempt creates a new payment attempt from the given route.
|
||||||
func (p *shardHandler) createNewPaymentAttempt(rt *route.Route,
|
func (p *shardHandler) createNewPaymentAttempt(rt *route.Route,
|
||||||
lastShard bool) (*channeldb.HTLCAttemptInfo, error) {
|
lastShard bool) (*channeldb.HTLCAttempt, error) {
|
||||||
|
|
||||||
// Generate a new key to be used for this attempt.
|
// Generate a new key to be used for this attempt.
|
||||||
sessionKey, err := generateNewSessionKey()
|
sessionKey, err := generateNewSessionKey()
|
||||||
@@ -670,7 +672,7 @@ func (p *shardHandler) createNewPaymentAttempt(rt *route.Route,
|
|||||||
|
|
||||||
// We now have all the information needed to populate the current
|
// We now have all the information needed to populate the current
|
||||||
// attempt information.
|
// attempt information.
|
||||||
attempt := channeldb.NewHtlcAttemptInfo(
|
attempt := channeldb.NewHtlcAttempt(
|
||||||
attemptID, sessionKey, *rt, p.router.cfg.Clock.Now(), &hash,
|
attemptID, sessionKey, *rt, p.router.cfg.Clock.Now(), &hash,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -681,7 +683,7 @@ func (p *shardHandler) createNewPaymentAttempt(rt *route.Route,
|
|||||||
// the payment. If this attempt fails, then we'll continue on to the next
|
// the payment. If this attempt fails, then we'll continue on to the next
|
||||||
// available route.
|
// available route.
|
||||||
func (p *shardHandler) sendAttempt(
|
func (p *shardHandler) sendAttempt(
|
||||||
attempt *channeldb.HTLCAttemptInfo) error {
|
attempt *channeldb.HTLCAttempt) error {
|
||||||
|
|
||||||
log.Tracef("Attempting to send payment %v (pid=%v), "+
|
log.Tracef("Attempting to send payment %v (pid=%v), "+
|
||||||
"using route: %v", p.identifier, attempt.AttemptID,
|
"using route: %v", p.identifier, attempt.AttemptID,
|
||||||
@@ -741,7 +743,7 @@ func (p *shardHandler) sendAttempt(
|
|||||||
// the error type, the error is either the final outcome of the payment or we
|
// the error type, the error is either the final outcome of the payment or we
|
||||||
// need to continue with an alternative route. A final outcome is indicated by
|
// need to continue with an alternative route. A final outcome is indicated by
|
||||||
// a non-nil reason value.
|
// a non-nil reason value.
|
||||||
func (p *shardHandler) handleSwitchErr(attempt *channeldb.HTLCAttemptInfo,
|
func (p *shardHandler) handleSwitchErr(attempt *channeldb.HTLCAttempt,
|
||||||
sendErr error) error {
|
sendErr error) error {
|
||||||
|
|
||||||
internalErrorReason := channeldb.FailureReasonError
|
internalErrorReason := channeldb.FailureReasonError
|
||||||
@@ -910,10 +912,10 @@ func (p *shardHandler) handleFailureMessage(rt *route.Route,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// failAttempt calls control tower to fail the current payment attempt.
|
// failAttempt calls control tower to fail the current payment attempt.
|
||||||
func (p *shardHandler) failAttempt(attempt *channeldb.HTLCAttemptInfo,
|
func (p *shardHandler) failAttempt(attemptID uint64,
|
||||||
sendError error) (*channeldb.HTLCAttempt, error) {
|
sendError error) (*channeldb.HTLCAttempt, error) {
|
||||||
|
|
||||||
log.Warnf("Attempt %v for payment %v failed: %v", attempt.AttemptID,
|
log.Warnf("Attempt %v for payment %v failed: %v", attemptID,
|
||||||
p.identifier, sendError)
|
p.identifier, sendError)
|
||||||
|
|
||||||
failInfo := marshallError(
|
failInfo := marshallError(
|
||||||
@@ -924,13 +926,12 @@ func (p *shardHandler) failAttempt(attempt *channeldb.HTLCAttemptInfo,
|
|||||||
// Now that we are failing this payment attempt, cancel the shard with
|
// Now that we are failing this payment attempt, cancel the shard with
|
||||||
// the ShardTracker such that it can derive the correct hash for the
|
// the ShardTracker such that it can derive the correct hash for the
|
||||||
// next attempt.
|
// next attempt.
|
||||||
if err := p.shardTracker.CancelShard(attempt.AttemptID); err != nil {
|
if err := p.shardTracker.CancelShard(attemptID); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return p.router.cfg.Control.FailAttempt(
|
return p.router.cfg.Control.FailAttempt(
|
||||||
p.identifier, attempt.AttemptID,
|
p.identifier, attemptID, failInfo,
|
||||||
failInfo,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user