invoices: rename updateInvoiceState to getUpdatedInvoiceState

This commit turns updateInvoiceState "const" by moving preimage update
out of the function while also removing it to getUpdatedInvoiceState.
This commit is contained in:
Andras Banki-Horvath committed 2024-02-19 20:47:23 +01:00
1 parent 342eb4eac2
commit 87044b815c
1 file changed
+13 -8
+13 -8
View File
@@ -2086,7 +2086,7 @@ func (d *DB) addHTLCs(invoices, settleIndex, //nolint:funlen
// change, which depends on having an accurate view of the accepted // change, which depends on having an accurate view of the accepted
// HTLCs. // HTLCs.
if update.State != nil { if update.State != nil {
newState, err := updateInvoiceState( newState, err := getUpdatedInvoiceState(
invoice, hash, *update.State, invoice, hash, *update.State,
) )
if err != nil { if err != nil {
@@ -2327,7 +2327,9 @@ func (d *DB) settleHodlInvoice(invoices, settleIndex kvdb.RwBucket,
} }
// TODO(positiveblue): create a invoice.CanSettleHodlInvoice func. // TODO(positiveblue): create a invoice.CanSettleHodlInvoice func.
newState, err := updateInvoiceState(invoice, hash, *update) newState, err := getUpdatedInvoiceState(
invoice, hash, *update,
)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -2338,6 +2340,7 @@ func (d *DB) settleHodlInvoice(invoices, settleIndex kvdb.RwBucket,
} }
invoice.State = invpkg.ContractSettled invoice.State = invpkg.ContractSettled
invoice.Terms.PaymentPreimage = update.Preimage
timestamp := d.clock.Now() timestamp := d.clock.Now()
// TODO(positiveblue): this logic can be further simplified. // TODO(positiveblue): this logic can be further simplified.
@@ -2414,7 +2417,7 @@ func (d *DB) cancelInvoice(invoices kvdb.RwBucket, invoiceNum []byte,
setID = update.SetID setID = update.SetID
} }
newState, err := updateInvoiceState(invoice, hash, *update) newState, err := getUpdatedInvoiceState(invoice, hash, *update)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -2459,10 +2462,12 @@ func (d *DB) cancelInvoiceStoreUpdate(invoices kvdb.RwBucket, invoiceNum []byte,
return d.serializeAndStoreInvoice(invoices, invoiceNum, invoice) return d.serializeAndStoreInvoice(invoices, invoiceNum, invoice)
} }
// updateInvoiceState validates and processes an invoice state update. The new // getUpdatedInvoiceState validates and processes an invoice state update. The
// state to transition to is returned, so the caller is able to select exactly // new state to transition to is returned, so the caller is able to select
// how the invoice state is updated. // exactly how the invoice state is updated. Note that for AMP invoices this
func updateInvoiceState(invoice *invpkg.Invoice, hash *lntypes.Hash, // function is only used to validate the state transition if we're cancelling
// the invoice.
func getUpdatedInvoiceState(invoice *invpkg.Invoice, hash *lntypes.Hash,
update invpkg.InvoiceStateUpdateDesc) (*invpkg.ContractState, error) { update invpkg.InvoiceStateUpdateDesc) (*invpkg.ContractState, error) {
// Returning to open is never allowed from any state. // Returning to open is never allowed from any state.
@@ -2505,6 +2510,7 @@ func updateInvoiceState(invoice *invpkg.Invoice, hash *lntypes.Hash,
return nil, errors.New("AMP set cannot have " + return nil, errors.New("AMP set cannot have " +
"preimage") "preimage")
} }
return &update.NewState, nil return &update.NewState, nil
} }
@@ -2519,7 +2525,6 @@ func updateInvoiceState(invoice *invpkg.Invoice, hash *lntypes.Hash,
if update.Preimage.Hash() != *hash { if update.Preimage.Hash() != *hash {
return nil, invpkg.ErrInvoicePreimageMismatch return nil, invpkg.ErrInvoicePreimageMismatch
} }
invoice.Terms.PaymentPreimage = update.Preimage
// Permit non-AMP invoices to be accepted without knowing the // Permit non-AMP invoices to be accepted without knowing the
// preimage. When trying to settle we'll have to pass through // preimage. When trying to settle we'll have to pass through