From 1159079679caa26ff5b21ff14251347b870a454c Mon Sep 17 00:00:00 2001 From: hannahhoward Date: Mon, 2 Mar 2020 17:58:49 -0800 Subject: [PATCH] fix(retrievalmarket): handle self-retrieval correctly in the case of retrieval from one's one node, payment vouchers are already saved, indicating a received delta of zero -- in this case, instead of reading the delta, inspect the actual voucher amount difference from funds received --- .../impl/providerstates/provider_states.go | 7 +++++++ .../impl/providerstates/provider_states_test.go | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/retrievalmarket/impl/providerstates/provider_states.go b/retrievalmarket/impl/providerstates/provider_states.go index 4e6a8cfe..98a4be18 100644 --- a/retrievalmarket/impl/providerstates/provider_states.go +++ b/retrievalmarket/impl/providerstates/provider_states.go @@ -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{ diff --git a/retrievalmarket/impl/providerstates/provider_states_test.go b/retrievalmarket/impl/providerstates/provider_states_test.go index e1d3b398..4a807560 100644 --- a/retrievalmarket/impl/providerstates/provider_states_test.go +++ b/retrievalmarket/impl/providerstates/provider_states_test.go @@ -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, @@ -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"