Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle self-retrieval correctly #134

Merged
merged 1 commit into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions retrievalmarket/impl/providerstates/provider_states.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ func ProcessPayment(ctx fsm.Context, environment ProviderDealEnvironment, deal r
return ctx.Trigger(rm.ProviderEventSaveVoucherFailed, err)
}

// received = 0 / err = nil indicates that the voucher was already saved, but this may be ok
// if we are making a deal with ourself - in this case, we'll instead calculate received
// but subtracting from fund sent
if big.Cmp(received, big.Zero()) == 0 {
received = big.Sub(payment.PaymentVoucher.Amount, deal.FundsReceived)
}

// check if all payments are received to continue the deal, or send updated required payment
if received.LessThan(paymentOwed) {
err := environment.DealStream(deal.Identifier()).WriteDealResponse(rm.DealResponse{
Expand Down
17 changes: 17 additions & 0 deletions retrievalmarket/impl/providerstates/provider_states_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ func TestProcessPayment(t *testing.T) {

payCh := address.TestAddress
voucher := testnet.MakeTestSignedVoucher()
voucher.Amount = big.Add(defaultFundsReceived, defaultPaymentPerInterval)
dealPayment := retrievalmarket.DealPayment{
ID: dealID,
PaymentChannel: payCh,
Expand Down Expand Up @@ -295,6 +296,22 @@ func TestProcessPayment(t *testing.T) {
require.Empty(t, dealState.Message)
})

t.Run("voucher already saved", func(t *testing.T) {
node := testnodes.NewTestRetrievalProviderNode()
err := node.ExpectVoucher(payCh, voucher, nil, defaultPaymentPerInterval, big.Zero(), nil)
require.NoError(t, err)
dealState := makeDealState(retrievalmarket.DealStatusFundsNeeded)
dealState.TotalSent = defaultTotalSent + defaultCurrentInterval
dealStreamParams := testnet.TestDealStreamParams{
PaymentReader: testnet.StubbedDealPaymentReader(dealPayment),
}
runProcessPayment(t, node, dealStreamParams, dealState)
require.Equal(t, dealState.Status, retrievalmarket.DealStatusOngoing)
require.Equal(t, dealState.FundsReceived, big.Add(defaultFundsReceived, defaultPaymentPerInterval))
require.Equal(t, dealState.CurrentInterval, defaultCurrentInterval+defaultIntervalIncrease)
require.Empty(t, dealState.Message)
})

t.Run("failure processing payment", func(t *testing.T) {
node := testnodes.NewTestRetrievalProviderNode()
message := "your money's no good here"
Expand Down