Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
payments+switch: sender-side check to not pay same preimage twice
Create a PaymentStatus enum encapsulating the cases: Grounded, InFlight, and Completed. Add a db bucket channeldb/payments mapping PaymentHash -> PaymentStatus. Add abstract the functionality behind an interface. Implement methods performing checks/state transitions, and unit test the behavior. Integrate checks/transitions into switch. Add an integration test to test the fault tolerance of when failing in any of the states outlined above. Add migration to retroactively Complete all previous payments.
- Loading branch information
Showing
14 changed files
with
774 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package channeldb | ||
|
||
import ( | ||
"crypto/sha256" | ||
"testing" | ||
) | ||
|
||
func TestPaymentStatusesMigration(t *testing.T) { | ||
t.Parallel() | ||
|
||
fakePayment := makeFakePayment() | ||
paymentHash := sha256.Sum256(fakePayment.PaymentPreimage[:]) | ||
|
||
// Add fake payment to the test database and verifies that it was created | ||
// and there is only one payment and its status is not "Completed". | ||
beforeMigrationFunc := func(d *DB) { | ||
if err := d.AddPayment(fakePayment); err != nil { | ||
t.Fatalf("unable to add payment: %v", err) | ||
} | ||
|
||
payments, err := d.FetchAllPayments() | ||
if err != nil { | ||
t.Fatalf("unable to fetch payments: %v", err) | ||
} | ||
|
||
if len(payments) != 1 { | ||
t.Fatalf("wrong qty of paymets: expected 1, got %v", | ||
len(payments)) | ||
} | ||
|
||
paymentStatus, err := d.FetchPaymentStatus(paymentHash) | ||
if err != nil { | ||
t.Fatalf("unable to fetch payment status: %v", err) | ||
} | ||
|
||
// we should receive default status if we have any in database | ||
if paymentStatus != StatusGrounded { | ||
t.Fatalf("wrong payment status: expected %v, got %v", | ||
StatusGrounded.String(), paymentStatus.String()) | ||
} | ||
} | ||
|
||
// Verify that was created payment status "Completed" for our one fake | ||
// payment. | ||
afterMigrationFunc := func(d *DB) { | ||
meta, err := d.FetchMeta(nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if meta.DbVersionNumber != 1 { | ||
t.Fatal("migration 'paymentStatusesMigration' wasn't applied") | ||
} | ||
|
||
paymentStatus, err := d.FetchPaymentStatus(paymentHash) | ||
if err != nil { | ||
t.Fatalf("unable to fetch payment status: %v", err) | ||
} | ||
|
||
if paymentStatus != StatusCompleted { | ||
t.Fatalf("wrong payment status: expected %v, got %v", | ||
StatusCompleted.String(), paymentStatus.String()) | ||
} | ||
} | ||
|
||
applyMigration(t, | ||
beforeMigrationFunc, | ||
afterMigrationFunc, | ||
paymentStatusesMigration, | ||
false) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.