htlcswitch/switch_control_test: extend tests + godocs

This commit is contained in:
Conner Fromknecht 2018-08-10 14:01:24 -07:00
parent 875128539c
commit 3f0dfd4e4b
No known key found for this signature in database
GPG Key ID: E7D737B67FA592C7

@ -24,9 +24,10 @@ func genHtlc() (*lnwire.UpdateAddHTLC, error) {
return htlc, nil return htlc, nil
} }
// TestPaymentControlSwitch checks the ability of payment control // TestPaymentControlSwitchFail checks that payment status returns to Grounded
// change states of payments // status after failing, and that ClearForTakeoff allows another HTLC for the
func TestPaymentControlSwitch(t *testing.T) { // same payment hash.
func TestPaymentControlSwitchFail(t *testing.T) {
t.Parallel() t.Parallel()
db, err := initDB() db, err := initDB()
@ -41,10 +42,40 @@ func TestPaymentControlSwitch(t *testing.T) {
t.Fatalf("unable to generate htlc message: %v", err) t.Fatalf("unable to generate htlc message: %v", err)
} }
// Sends base htlc message which initiate base status // Sends base htlc message which initiate StatusInFlight.
// and move it to StatusInFlight and verifies that it if err := pControl.ClearForTakeoff(htlc); err != nil {
// was changed. t.Fatalf("unable to send htlc message: %v", err)
if err := pControl.CheckSend(htlc); err != nil { }
pStatus, err := db.FetchPaymentStatus(htlc.PaymentHash)
if err != nil {
t.Fatalf("unable to fetch payment status: %v", err)
}
if pStatus != channeldb.StatusInFlight {
t.Fatalf("payment status mismatch: expected %v, got %v",
channeldb.StatusInFlight, pStatus)
}
// Fail the payment, which should moved it to Grounded.
if err := pControl.Fail(htlc.PaymentHash); err != nil {
t.Fatalf("unable to fail payment hash: %v", err)
}
// Verify the status is indeed Grounded.
pStatus, err = db.FetchPaymentStatus(htlc.PaymentHash)
if err != nil {
t.Fatalf("unable to fetch payment status: %v", err)
}
if pStatus != channeldb.StatusGrounded {
t.Fatalf("payment status mismatch: expected %v, got %v",
channeldb.StatusGrounded, pStatus)
}
// Sends the htlc again, which should succeed since the prior payment
// failed.
if err := pControl.ClearForTakeoff(htlc); err != nil {
t.Fatalf("unable to send htlc message: %v", err) t.Fatalf("unable to send htlc message: %v", err)
} }
@ -72,56 +103,16 @@ func TestPaymentControlSwitch(t *testing.T) {
t.Fatalf("payment status mismatch: expected %v, got %v", t.Fatalf("payment status mismatch: expected %v, got %v",
channeldb.StatusCompleted, pStatus) channeldb.StatusCompleted, pStatus)
} }
}
// TestPaymentControlSwitchFail checks that payment status returns // Attempt a final payment, which should now fail since the prior
// to initial status after fail // payment succeed.
func TestPaymentControlSwitchFail(t *testing.T) { if err := pControl.ClearForTakeoff(htlc); err != nil {
t.Parallel()
db, err := initDB()
if err != nil {
t.Fatalf("unable to init db: %v", err)
}
pControl := NewPaymentControl(db)
htlc, err := genHtlc()
if err != nil {
t.Fatalf("unable to generate htlc message: %v", err)
}
// Sends base htlc message which initiate StatusInFlight.
if err := pControl.CheckSend(htlc); err != nil {
t.Fatalf("unable to send htlc message: %v", err) t.Fatalf("unable to send htlc message: %v", err)
} }
pStatus, err := db.FetchPaymentStatus(htlc.PaymentHash)
if err != nil {
t.Fatalf("unable to fetch payment status: %v", err)
} }
if pStatus != channeldb.StatusInFlight { // TestPaymentControlSwitchDoubleSend checks the ability of payment control to
t.Fatalf("payment status mismatch: expected %v, got %v", // prevent double sending of htlc message, when message is in StatusInFlight.
channeldb.StatusInFlight, pStatus)
}
// Move payment to completed status, second payment should return error.
pControl.Fail(htlc.PaymentHash)
pStatus, err = db.FetchPaymentStatus(htlc.PaymentHash)
if err != nil {
t.Fatalf("unable to fetch payment status: %v", err)
}
if pStatus != channeldb.StatusGrounded {
t.Fatalf("payment status mismatch: expected %v, got %v",
channeldb.StatusGrounded, pStatus)
}
}
// TestPaymentControlSwitchDoubleSend checks the ability of payment control
// to prevent double sending of htlc message, when message is in StatusInFlight
func TestPaymentControlSwitchDoubleSend(t *testing.T) { func TestPaymentControlSwitchDoubleSend(t *testing.T) {
t.Parallel() t.Parallel()
@ -137,10 +128,9 @@ func TestPaymentControlSwitchDoubleSend(t *testing.T) {
t.Fatalf("unable to generate htlc message: %v", err) t.Fatalf("unable to generate htlc message: %v", err)
} }
// Sends base htlc message which initiate base status // Sends base htlc message which initiate base status and move it to
// and move it to StatusInFlight and verifies that it // StatusInFlight and verifies that it was changed.
// was changed. if err := pControl.ClearForTakeoff(htlc); err != nil {
if err := pControl.CheckSend(htlc); err != nil {
t.Fatalf("unable to send htlc message: %v", err) t.Fatalf("unable to send htlc message: %v", err)
} }
@ -154,16 +144,17 @@ func TestPaymentControlSwitchDoubleSend(t *testing.T) {
channeldb.StatusInFlight, pStatus) channeldb.StatusInFlight, pStatus)
} }
// Tries to initiate double sending of htlc message with the same // Try to initiate double sending of htlc message with the same
// payment hash. // payment hash, should result in error indicating that payment has
if err := pControl.CheckSend(htlc); err != ErrPaymentInFlight { // already been sent.
if err := pControl.ClearForTakeoff(htlc); err != ErrPaymentInFlight {
t.Fatalf("payment control wrong behaviour: " + t.Fatalf("payment control wrong behaviour: " +
"double sending must trigger ErrPaymentInFlight error") "double sending must trigger ErrPaymentInFlight error")
} }
} }
// TestPaymentControlSwitchDoublePay checks the ability of payment control // TestPaymentControlSwitchDoublePay checks the ability of payment control to
// to prevent double payment // prevent double payment.
func TestPaymentControlSwitchDoublePay(t *testing.T) { func TestPaymentControlSwitchDoublePay(t *testing.T) {
t.Parallel() t.Parallel()
@ -180,10 +171,11 @@ func TestPaymentControlSwitchDoublePay(t *testing.T) {
} }
// Sends base htlc message which initiate StatusInFlight. // Sends base htlc message which initiate StatusInFlight.
if err := pControl.CheckSend(htlc); err != nil { if err := pControl.ClearForTakeoff(htlc); err != nil {
t.Fatalf("unable to send htlc message: %v", err) t.Fatalf("unable to send htlc message: %v", err)
} }
// Verify that payment is InFlight.
pStatus, err := db.FetchPaymentStatus(htlc.PaymentHash) pStatus, err := db.FetchPaymentStatus(htlc.PaymentHash)
if err != nil { if err != nil {
t.Fatalf("unable to fetch payment status: %v", err) t.Fatalf("unable to fetch payment status: %v", err)
@ -199,7 +191,18 @@ func TestPaymentControlSwitchDoublePay(t *testing.T) {
t.Fatalf("error shouldn't have been received, got: %v", err) t.Fatalf("error shouldn't have been received, got: %v", err)
} }
if err := pControl.CheckSend(htlc); err != ErrAlreadyPaid { // Verify that payment is Completed.
pStatus, err = db.FetchPaymentStatus(htlc.PaymentHash)
if err != nil {
t.Fatalf("unable to fetch payment status: %v", err)
}
if pStatus != channeldb.StatusCompleted {
t.Fatalf("payment status mismatch: expected %v, got %v",
channeldb.StatusCompleted, pStatus)
}
if err := pControl.ClearForTakeoff(htlc); err != ErrAlreadyPaid {
t.Fatalf("payment control wrong behaviour:" + t.Fatalf("payment control wrong behaviour:" +
" double payment must trigger ErrAlreadyPaid") " double payment must trigger ErrAlreadyPaid")
} }