From eec7871686e936fe62c868e9f4ab7199e25a19c6 Mon Sep 17 00:00:00 2001 From: hannahhoward Date: Tue, 14 Jan 2020 15:18:31 -0800 Subject: [PATCH] test(retrievalmarket): provide context for numbers make clear what numbers expected in tests of payment processing mean for retrieval deals --- .../impl/clientstates/client_states_test.go | 32 +++++++------- .../providerstates/provider_states_test.go | 42 ++++++++++--------- 2 files changed, 40 insertions(+), 34 deletions(-) diff --git a/retrievalmarket/impl/clientstates/client_states_test.go b/retrievalmarket/impl/clientstates/client_states_test.go index 863c8d2c1..13b0517fd 100644 --- a/retrievalmarket/impl/clientstates/client_states_test.go +++ b/retrievalmarket/impl/clientstates/client_states_test.go @@ -202,9 +202,9 @@ func TestProcessPaymentRequested(t *testing.T) { f(dealState) require.Empty(t, dealState.Message) require.Equal(t, dealState.PaymentRequested, tokenamount.FromInt(0)) - require.Equal(t, dealState.FundsSpent, tokenamount.FromInt(3000000)) - require.Equal(t, dealState.BytesPaidFor, uint64(6000)) - require.Equal(t, dealState.CurrentInterval, uint64(1500)) + require.Equal(t, dealState.FundsSpent, tokenamount.Add(defaultFundsSpent, defaultPaymentRequested)) + require.Equal(t, dealState.BytesPaidFor, defaultTotalReceived) + require.Equal(t, dealState.CurrentInterval, defaultCurrentInterval+defaultIntervalIncrease) require.Equal(t, dealState.Status, retrievalmarket.DealStatusOngoing) }) @@ -217,9 +217,9 @@ func TestProcessPaymentRequested(t *testing.T) { f(dealState) require.Empty(t, dealState.Message) require.Equal(t, dealState.PaymentRequested, tokenamount.FromInt(0)) - require.Equal(t, dealState.FundsSpent, tokenamount.FromInt(3000000)) - require.Equal(t, dealState.BytesPaidFor, uint64(6000)) - require.Equal(t, dealState.CurrentInterval, uint64(1500)) + require.Equal(t, dealState.FundsSpent, tokenamount.Add(defaultFundsSpent, defaultPaymentRequested)) + require.Equal(t, dealState.BytesPaidFor, defaultTotalReceived) + require.Equal(t, dealState.CurrentInterval, defaultCurrentInterval+defaultIntervalIncrease) require.Equal(t, dealState.Status, retrievalmarket.DealStatusCompleted) }) @@ -250,7 +250,8 @@ func TestProcessPaymentRequested(t *testing.T) { t.Run("more bytes since last payment than interval works, can charge more", func(t *testing.T) { dealState := makeDealState(retrievalmarket.DealStatusFundsNeeded) dealState.BytesPaidFor = defaultBytesPaidFor - 500 - dealState.PaymentRequested = tokenamount.FromInt(750000) + largerPaymentRequested := tokenamount.FromInt(750000) + dealState.PaymentRequested = largerPaymentRequested fe := environment(testnet.TestDealStreamParams{}, testnodes.TestRetrievalClientNodeParams{ Voucher: testVoucher, @@ -259,9 +260,9 @@ func TestProcessPaymentRequested(t *testing.T) { f(dealState) require.Empty(t, dealState.Message) require.Equal(t, dealState.PaymentRequested, tokenamount.FromInt(0)) - require.Equal(t, dealState.FundsSpent, tokenamount.FromInt(3250000)) - require.Equal(t, dealState.BytesPaidFor, uint64(6000)) - require.Equal(t, dealState.CurrentInterval, uint64(1500)) + require.Equal(t, dealState.FundsSpent, tokenamount.Add(defaultFundsSpent, largerPaymentRequested)) + require.Equal(t, dealState.BytesPaidFor, defaultTotalReceived) + require.Equal(t, dealState.CurrentInterval, defaultCurrentInterval+defaultIntervalIncrease) require.Equal(t, dealState.Status, retrievalmarket.DealStatusOngoing) }) @@ -279,7 +280,8 @@ func TestProcessPaymentRequested(t *testing.T) { t.Run("too little payment requested works but records correctly", func(t *testing.T) { dealState := makeDealState(retrievalmarket.DealStatusFundsNeeded) - dealState.PaymentRequested = tokenamount.FromInt(250000) + smallerPaymentRequested := tokenamount.FromInt(250000) + dealState.PaymentRequested = smallerPaymentRequested fe := environment(testnet.TestDealStreamParams{}, testnodes.TestRetrievalClientNodeParams{ Voucher: testVoucher, }) @@ -287,9 +289,11 @@ func TestProcessPaymentRequested(t *testing.T) { f(dealState) require.Empty(t, dealState.Message) require.Equal(t, dealState.PaymentRequested, tokenamount.FromInt(0)) - require.Equal(t, dealState.FundsSpent, tokenamount.FromInt(2750000)) - require.Equal(t, dealState.BytesPaidFor, uint64(5500)) - require.Equal(t, dealState.CurrentInterval, uint64(1000)) + require.Equal(t, dealState.FundsSpent, tokenamount.Add(defaultFundsSpent, smallerPaymentRequested)) + // only records change for those bytes paid for + require.Equal(t, dealState.BytesPaidFor, defaultBytesPaidFor+500) + // no interval increase + require.Equal(t, dealState.CurrentInterval, defaultCurrentInterval) require.Equal(t, dealState.Status, retrievalmarket.DealStatusOngoing) }) diff --git a/retrievalmarket/impl/providerstates/provider_states_test.go b/retrievalmarket/impl/providerstates/provider_states_test.go index a52694c01..66f8627c3 100644 --- a/retrievalmarket/impl/providerstates/provider_states_test.go +++ b/retrievalmarket/impl/providerstates/provider_states_test.go @@ -158,7 +158,7 @@ func TestSendBlocks(t *testing.T) { dealState := makeDealState(retrievalmarket.DealStatusAccepted) expectedDealResponse := retrievalmarket.DealResponse{ Status: retrievalmarket.DealStatusFundsNeeded, - PaymentOwed: tokenamount.FromInt(500000), + PaymentOwed: defaultPaymentPerInterval, Blocks: blocks, ID: dealState.ID, } @@ -168,7 +168,7 @@ func TestSendBlocks(t *testing.T) { f := providerstates.SendBlocks(ctx, fe, *dealState) f(dealState) require.Equal(t, dealState.Status, retrievalmarket.DealStatusFundsNeeded) - require.Equal(t, dealState.TotalSent, uint64(6000)) + require.Equal(t, dealState.TotalSent, defaultTotalSent+defaultCurrentInterval) require.Empty(t, dealState.Message) }) @@ -177,7 +177,7 @@ func TestSendBlocks(t *testing.T) { dealState := makeDealState(retrievalmarket.DealStatusAccepted) expectedDealResponse := retrievalmarket.DealResponse{ Status: retrievalmarket.DealStatusFundsNeededLastPayment, - PaymentOwed: tokenamount.FromInt(500000), + PaymentOwed: defaultPaymentPerInterval, Blocks: blocks, ID: dealState.ID, } @@ -187,7 +187,7 @@ func TestSendBlocks(t *testing.T) { f := providerstates.SendBlocks(ctx, fe, *dealState) f(dealState) require.Equal(t, dealState.Status, retrievalmarket.DealStatusFundsNeededLastPayment) - require.Equal(t, dealState.TotalSent, uint64(6000)) + require.Equal(t, dealState.TotalSent, defaultTotalSent+defaultCurrentInterval) require.Empty(t, dealState.Message) }) @@ -233,10 +233,10 @@ func TestProcessPayment(t *testing.T) { voucher := testnet.MakeTestSignedVoucher() t.Run("it works", func(t *testing.T) { node := testnodes.NewTestRetrievalProviderNode() - err := node.ExpectVoucher(payCh, voucher, nil, tokenamount.FromInt(500000), tokenamount.FromInt(500000), nil) + err := node.ExpectVoucher(payCh, voucher, nil, defaultPaymentPerInterval, defaultPaymentPerInterval, nil) require.NoError(t, err) dealState := makeDealState(retrievalmarket.DealStatusFundsNeeded) - dealState.TotalSent = uint64(6000) + dealState.TotalSent = defaultTotalSent + defaultCurrentInterval dealPayment := retrievalmarket.DealPayment{ ID: dealState.ID, PaymentChannel: payCh, @@ -249,16 +249,16 @@ func TestProcessPayment(t *testing.T) { node.VerifyExpectations(t) f(dealState) require.Equal(t, dealState.Status, retrievalmarket.DealStatusOngoing) - require.Equal(t, dealState.FundsReceived, tokenamount.FromInt(3000000)) - require.Equal(t, dealState.CurrentInterval, uint64(1500)) + require.Equal(t, dealState.FundsReceived, tokenamount.Add(defaultFundsReceived, defaultPaymentPerInterval)) + require.Equal(t, dealState.CurrentInterval, defaultCurrentInterval+defaultIntervalIncrease) require.Empty(t, dealState.Message) }) t.Run("it completes", func(t *testing.T) { node := testnodes.NewTestRetrievalProviderNode() - err := node.ExpectVoucher(payCh, voucher, nil, tokenamount.FromInt(500000), tokenamount.FromInt(500000), nil) + err := node.ExpectVoucher(payCh, voucher, nil, defaultPaymentPerInterval, defaultPaymentPerInterval, nil) require.NoError(t, err) dealState := makeDealState(retrievalmarket.DealStatusFundsNeededLastPayment) - dealState.TotalSent = uint64(6000) + dealState.TotalSent = defaultTotalSent + defaultCurrentInterval dealPayment := retrievalmarket.DealPayment{ ID: dealState.ID, PaymentChannel: payCh, @@ -271,17 +271,18 @@ func TestProcessPayment(t *testing.T) { node.VerifyExpectations(t) f(dealState) require.Equal(t, dealState.Status, retrievalmarket.DealStatusCompleted) - require.Equal(t, dealState.FundsReceived, tokenamount.FromInt(3000000)) - require.Equal(t, dealState.CurrentInterval, uint64(1500)) + require.Equal(t, dealState.FundsReceived, tokenamount.Add(defaultFundsReceived, defaultPaymentPerInterval)) + require.Equal(t, dealState.CurrentInterval, defaultCurrentInterval+defaultIntervalIncrease) require.Empty(t, dealState.Message) }) t.Run("not enough funds sent", func(t *testing.T) { node := testnodes.NewTestRetrievalProviderNode() - err := node.ExpectVoucher(payCh, voucher, nil, tokenamount.FromInt(500000), tokenamount.FromInt(400000), nil) + smallerPayment := tokenamount.FromInt(400000) + err := node.ExpectVoucher(payCh, voucher, nil, defaultPaymentPerInterval, smallerPayment, nil) require.NoError(t, err) dealState := makeDealState(retrievalmarket.DealStatusFundsNeeded) - dealState.TotalSent = uint64(6000) + dealState.TotalSent = defaultTotalSent + defaultCurrentInterval dealPayment := retrievalmarket.DealPayment{ ID: dealState.ID, PaymentChannel: payCh, @@ -292,25 +293,25 @@ func TestProcessPayment(t *testing.T) { ResponseWriter: testnet.ExpectDealResponseWriter(t, rm.DealResponse{ ID: dealState.ID, Status: retrievalmarket.DealStatusFundsNeeded, - PaymentOwed: tokenamount.FromInt(100000), + PaymentOwed: tokenamount.Sub(defaultPaymentPerInterval, smallerPayment), }), }) f := providerstates.ProcessPayment(ctx, fe, *dealState) node.VerifyExpectations(t) f(dealState) require.Equal(t, dealState.Status, retrievalmarket.DealStatusFundsNeeded) - require.Equal(t, dealState.FundsReceived, tokenamount.FromInt(2900000)) - require.Equal(t, dealState.CurrentInterval, uint64(1000)) + require.Equal(t, dealState.FundsReceived, tokenamount.Add(defaultFundsReceived, smallerPayment)) + require.Equal(t, dealState.CurrentInterval, defaultCurrentInterval) require.Empty(t, dealState.Message) }) t.Run("failure processing payment", func(t *testing.T) { node := testnodes.NewTestRetrievalProviderNode() message := "your money's no good here" - err := node.ExpectVoucher(payCh, voucher, nil, tokenamount.FromInt(500000), tokenamount.FromInt(0), errors.New(message)) + err := node.ExpectVoucher(payCh, voucher, nil, defaultPaymentPerInterval, tokenamount.FromInt(0), errors.New(message)) require.NoError(t, err) dealState := makeDealState(retrievalmarket.DealStatusFundsNeeded) - dealState.TotalSent = uint64(6000) + dealState.TotalSent = defaultTotalSent + defaultCurrentInterval dealPayment := retrievalmarket.DealPayment{ ID: dealState.ID, PaymentChannel: payCh, @@ -334,7 +335,7 @@ func TestProcessPayment(t *testing.T) { t.Run("failure reading payment", func(t *testing.T) { node := testnodes.NewTestRetrievalProviderNode() dealState := makeDealState(retrievalmarket.DealStatusFundsNeeded) - dealState.TotalSent = uint64(6000) + dealState.TotalSent = defaultTotalSent + defaultCurrentInterval fe := environment(node, testnet.TestDealStreamParams{ PaymentReader: testnet.FailDealPaymentReader, }) @@ -413,6 +414,7 @@ func (te *testProviderDealEnvironment) NextBlock(_ context.Context) (rm.Block, b var defaultCurrentInterval = uint64(1000) var defaultIntervalIncrease = uint64(500) var defaultPricePerByte = tokenamount.FromInt(500) +var defaultPaymentPerInterval = tokenamount.Mul(defaultPricePerByte, tokenamount.FromInt(defaultCurrentInterval)) var defaultTotalSent = uint64(5000) var defaultFundsReceived = tokenamount.FromInt(2500000)