diff --git a/cmd/di.go b/cmd/di.go index 0991356712..7db1b36e54 100644 --- a/cmd/di.go +++ b/cmd/di.go @@ -162,7 +162,6 @@ type Dependencies struct { Reporter *feedback.Reporter ProviderInvoiceStorage *pingpong.ProviderInvoiceStorage - ConsumerInvoiceStorage *pingpong.ConsumerInvoiceStorage ConsumerTotalsStorage *pingpong.ConsumerTotalsStorage AccountantPromiseStorage *pingpong.AccountantPromiseStorage ConsumerBalanceTracker *pingpong.ConsumerBalanceTracker @@ -387,7 +386,6 @@ func (di *Dependencies) bootstrapStorage(path string) error { invoiceStorage := pingpong.NewInvoiceStorage(di.Storage) di.ProviderInvoiceStorage = pingpong.NewProviderInvoiceStorage(invoiceStorage) - di.ConsumerInvoiceStorage = pingpong.NewConsumerInvoiceStorage(invoiceStorage) di.ConsumerTotalsStorage = pingpong.NewConsumerTotalsStorage(di.Storage) di.AccountantPromiseStorage = pingpong.NewAccountantPromiseStorage(di.Storage) return nil @@ -512,7 +510,6 @@ func (di *Dependencies) bootstrapNodeComponents(nodeOptions node.Options, tequil di.Keystore, nodeOptions, di.SignerFactory, - di.ConsumerInvoiceStorage, di.ConsumerTotalsStorage, nodeOptions.Transactor.ChannelImplementation, nodeOptions.Transactor.RegistryAddress, diff --git a/session/pingpong/exchange_message_tracker.go b/session/pingpong/exchange_message_tracker.go index b0aade524d..952bf845ab 100644 --- a/session/pingpong/exchange_message_tracker.go +++ b/session/pingpong/exchange_message_tracker.go @@ -72,14 +72,14 @@ type ExchangeMessageTracker struct { channelAddress identity.Identity receivedFirst bool - deps ExchangeMessageTrackerDeps + lastInvoice crypto.Invoice + deps ExchangeMessageTrackerDeps } // ExchangeMessageTrackerDeps contains all the dependencies for the exchange message tracker. type ExchangeMessageTrackerDeps struct { InvoiceChan chan crypto.Invoice PeerExchangeMessageSender PeerExchangeMessageSender - ConsumerInvoiceStorage consumerInvoiceStorage ConsumerTotalsStorage consumerTotalsStorage TimeTracker timeTracker Ks *keystore.KeyStore @@ -92,8 +92,9 @@ type ExchangeMessageTrackerDeps struct { // NewExchangeMessageTracker returns a new instance of exchange message tracker. func NewExchangeMessageTracker(emtd ExchangeMessageTrackerDeps) *ExchangeMessageTracker { return &ExchangeMessageTracker{ - stop: make(chan struct{}), - deps: emtd, + stop: make(chan struct{}), + deps: emtd, + lastInvoice: crypto.Invoice{}, } } @@ -127,11 +128,7 @@ func (emt *ExchangeMessageTracker) Start() error { return err } - err = emt.deps.ConsumerInvoiceStorage.Store(emt.deps.Identity, emt.deps.Peer, invoice) - if err != nil { - return errors.Wrap(err, "could not store invoice") - } - + emt.lastInvoice = invoice } } } @@ -197,24 +194,14 @@ func (emt *ExchangeMessageTracker) isInvoiceOK(invoice crypto.Invoice) error { } func (emt *ExchangeMessageTracker) calculateAmountToPromise(invoice crypto.Invoice) (toPromise uint64, diff uint64, err error) { - previous, err := emt.deps.ConsumerInvoiceStorage.Get(emt.deps.Identity, emt.deps.Peer) - if err != nil { - if err == ErrNotFound { - // do nothing, really - log.Debug().Msg("No previous invoice found, assuming zero") - } else { - return 0, 0, errors.Wrap(err, fmt.Sprintf("could not get previous total for peer %q", invoice.Provider)) - } - } - - diff = invoice.AgreementTotal - previous.AgreementTotal + diff = invoice.AgreementTotal - emt.lastInvoice.AgreementTotal totalPromised, err := emt.getGrandTotalPromised() if err != nil { return 0, 0, err } // This is a new agreement, we need to take in the agreement total and just add it to total promised - if previous.AgreementID != invoice.AgreementID { + if emt.lastInvoice.AgreementID != invoice.AgreementID { diff = invoice.AgreementTotal } @@ -247,11 +234,7 @@ func (emt *ExchangeMessageTracker) issueExchangeMessage(invoice crypto.Invoice) // TODO: we'd probably want to check if we have enough balance here err = emt.incrementGrandTotalPromised(diff) - if err != nil { - return errors.Wrap(err, "could not increment grand total") - } - - return emt.deps.ConsumerTotalsStorage.Store(emt.deps.Peer.Address, invoice.AgreementTotal) + return errors.Wrap(err, "could not increment grand total") } // Stop stops the message tracker. diff --git a/session/pingpong/exchange_message_tracker_test.go b/session/pingpong/exchange_message_tracker_test.go index c671e18334..54a05c6436 100644 --- a/session/pingpong/exchange_message_tracker_test.go +++ b/session/pingpong/exchange_message_tracker_test.go @@ -65,12 +65,10 @@ func Test_ExchangeMessageTracker_Start_Stop(t *testing.T) { defer bolt.Close() tracker := session.NewTracker(time.Now) - invoiceStorage := NewConsumerInvoiceStorage(NewInvoiceStorage(bolt)) totalsStorage := NewConsumerTotalsStorage(bolt) deps := ExchangeMessageTrackerDeps{ InvoiceChan: invoiceChan, PeerExchangeMessageSender: mockSender, - ConsumerInvoiceStorage: invoiceStorage, ConsumerTotalsStorage: totalsStorage, TimeTracker: &tracker, Ks: ks, @@ -113,12 +111,10 @@ func Test_ExchangeMessageTracker_SendsMessage(t *testing.T) { defer bolt.Close() tracker := session.NewTracker(time.Now) - invoiceStorage := NewConsumerInvoiceStorage(NewInvoiceStorage(bolt)) totalsStorage := NewConsumerTotalsStorage(bolt) deps := ExchangeMessageTrackerDeps{ InvoiceChan: invoiceChan, PeerExchangeMessageSender: mockSender, - ConsumerInvoiceStorage: invoiceStorage, ConsumerTotalsStorage: totalsStorage, TimeTracker: &tracker, Publisher: &mockPublisher{}, @@ -181,12 +177,10 @@ func Test_ExchangeMessageTracker_SendsMessage_OnFreeService(t *testing.T) { defer bolt.Close() tracker := session.NewTracker(time.Now) - invoiceStorage := NewConsumerInvoiceStorage(NewInvoiceStorage(bolt)) totalsStorage := NewConsumerTotalsStorage(bolt) deps := ExchangeMessageTrackerDeps{ InvoiceChan: invoiceChan, PeerExchangeMessageSender: mockSender, - ConsumerInvoiceStorage: invoiceStorage, ConsumerTotalsStorage: totalsStorage, TimeTracker: &tracker, Publisher: &mockPublisher{}, @@ -245,13 +239,11 @@ func Test_ExchangeMessageTracker_BubblesErrors(t *testing.T) { defer bolt.Close() tracker := session.NewTracker(time.Now) - invoiceStorage := NewConsumerInvoiceStorage(NewInvoiceStorage(bolt)) totalsStorage := NewConsumerTotalsStorage(bolt) deps := ExchangeMessageTrackerDeps{ InvoiceChan: invoiceChan, Publisher: &mockPublisher{}, PeerExchangeMessageSender: mockSender, - ConsumerInvoiceStorage: invoiceStorage, ConsumerTotalsStorage: totalsStorage, TimeTracker: &tracker, Ks: ks, @@ -479,9 +471,9 @@ func TestExchangeMessageTracker_incrementGrandTotalPromised(t *testing.T) { func TestExchangeMessageTracker_calculateAmountToPromise(t *testing.T) { type fields struct { - peer identity.Identity - consumerInvoiceStorage *mockConsumerInvoiceStorage - consumerTotalsStorage *mockConsumerTotalsStorage + peer identity.Identity + lastInvoice crypto.Invoice + consumerTotalsStorage *mockConsumerTotalsStorage } tests := []struct { name string @@ -491,20 +483,9 @@ func TestExchangeMessageTracker_calculateAmountToPromise(t *testing.T) { wantDiff uint64 wantErr bool }{ - { - name: "bubbles invoice storage errors", - fields: fields{ - consumerInvoiceStorage: &mockConsumerInvoiceStorage{ - err: errors.New("explosions everywhere"), - }, - }, - invoice: crypto.Invoice{}, - wantErr: true, - }, { name: "bubbles totals storage errors", fields: fields{ - consumerInvoiceStorage: &mockConsumerInvoiceStorage{}, consumerTotalsStorage: &mockConsumerTotalsStorage{ err: errors.New("explosions everywhere"), }, @@ -513,11 +494,8 @@ func TestExchangeMessageTracker_calculateAmountToPromise(t *testing.T) { wantErr: true, }, { - name: "ignores bolt not found errors", + name: "assumes zero", fields: fields{ - consumerInvoiceStorage: &mockConsumerInvoiceStorage{ - err: ErrNotFound, - }, consumerTotalsStorage: &mockConsumerTotalsStorage{}, }, invoice: crypto.Invoice{AgreementTotal: 10}, @@ -528,9 +506,6 @@ func TestExchangeMessageTracker_calculateAmountToPromise(t *testing.T) { { name: "calculates correctly with different grand total", fields: fields{ - consumerInvoiceStorage: &mockConsumerInvoiceStorage{ - err: ErrNotFound, - }, consumerTotalsStorage: &mockConsumerTotalsStorage{ res: 100, }, @@ -543,9 +518,7 @@ func TestExchangeMessageTracker_calculateAmountToPromise(t *testing.T) { { name: "calculates correctly with previous invoice", fields: fields{ - consumerInvoiceStorage: &mockConsumerInvoiceStorage{ - res: crypto.Invoice{AgreementID: 111, AgreementTotal: 111}, - }, + lastInvoice: crypto.Invoice{AgreementID: 111, AgreementTotal: 111}, consumerTotalsStorage: &mockConsumerTotalsStorage{ res: 100, }, @@ -560,11 +533,11 @@ func TestExchangeMessageTracker_calculateAmountToPromise(t *testing.T) { t.Run(tt.name, func(t *testing.T) { emt := &ExchangeMessageTracker{ deps: ExchangeMessageTrackerDeps{ - ConsumerTotalsStorage: tt.fields.consumerTotalsStorage, - Peer: tt.fields.peer, - ConsumerInvoiceStorage: tt.fields.consumerInvoiceStorage, + ConsumerTotalsStorage: tt.fields.consumerTotalsStorage, + Peer: tt.fields.peer, }, } + emt.lastInvoice = tt.fields.lastInvoice gotToPromise, gotDiff, err := emt.calculateAmountToPromise(tt.invoice) if (err != nil) != tt.wantErr { t.Errorf("ExchangeMessageTracker.calculateAmountToPromise() error = %v, wantErr %v", err, tt.wantErr) @@ -604,16 +577,14 @@ func TestExchangeMessageTracker_issueExchangeMessage_publishesEvents(t *testing. }, ConsumerTotalsStorage: &mockConsumerTotalsStorage{}, Peer: peerID, - ConsumerInvoiceStorage: &mockConsumerInvoiceStorage{ - res: crypto.Invoice{ - AgreementTotal: 10, - }, - }, - Ks: ks, - Identity: identity.FromAddress(acc.Address.Hex()), - Publisher: mp, + Ks: ks, + Identity: identity.FromAddress(acc.Address.Hex()), + Publisher: mp, }, } + emt.lastInvoice = crypto.Invoice{ + AgreementTotal: 10, + } err = emt.issueExchangeMessage(crypto.Invoice{ AgreementTotal: 15, Hashlock: "0x441Da57A51e42DAB7Daf55909Af93A9b00eEF23C", @@ -646,7 +617,7 @@ func TestExchangeMessageTracker_issueExchangeMessage(t *testing.T) { keystore *keystore.KeyStore identity identity.Identity peer identity.Identity - consumerInvoiceStorage *mockConsumerInvoiceStorage + lastInvoice crypto.Invoice consumerTotalsStorage *mockConsumerTotalsStorage } type args struct { @@ -659,21 +630,6 @@ func TestExchangeMessageTracker_issueExchangeMessage(t *testing.T) { wantErr bool wantMsg *crypto.ExchangeMessage }{ - { - name: "bubbles calculation errors", - fields: fields{ - identity: identity.FromAddress(acc.Address.Hex()), - peer: peerID, - keystore: ks, - peerExchangeMessageSender: &MockPeerExchangeMessageSender{ - chanToWriteTo: make(chan crypto.ExchangeMessage, 10), - }, - consumerInvoiceStorage: &mockConsumerInvoiceStorage{ - err: errors.New("explosions everywhere"), - }, - }, - wantErr: true, - }, { name: "bubbles exchange message creation errors", fields: fields{ @@ -683,8 +639,7 @@ func TestExchangeMessageTracker_issueExchangeMessage(t *testing.T) { peerExchangeMessageSender: &MockPeerExchangeMessageSender{ chanToWriteTo: make(chan crypto.ExchangeMessage, 10), }, - consumerInvoiceStorage: &mockConsumerInvoiceStorage{}, - consumerTotalsStorage: &mockConsumerTotalsStorage{}, + consumerTotalsStorage: &mockConsumerTotalsStorage{}, }, wantErr: true, }, @@ -698,8 +653,7 @@ func TestExchangeMessageTracker_issueExchangeMessage(t *testing.T) { chanToWriteTo: make(chan crypto.ExchangeMessage, 10), mockError: errors.New("explosions everywhere"), }, - consumerInvoiceStorage: &mockConsumerInvoiceStorage{}, - consumerTotalsStorage: &mockConsumerTotalsStorage{}, + consumerTotalsStorage: &mockConsumerTotalsStorage{}, }, wantErr: false, }, @@ -712,8 +666,7 @@ func TestExchangeMessageTracker_issueExchangeMessage(t *testing.T) { peerExchangeMessageSender: &MockPeerExchangeMessageSender{ chanToWriteTo: make(chan crypto.ExchangeMessage, 10), }, - consumerInvoiceStorage: &mockConsumerInvoiceStorage{}, - consumerTotalsStorage: &mockConsumerTotalsStorage{}, + consumerTotalsStorage: &mockConsumerTotalsStorage{}, }, args: args{ invoice: crypto.Invoice{ @@ -731,12 +684,12 @@ func TestExchangeMessageTracker_issueExchangeMessage(t *testing.T) { PeerExchangeMessageSender: tt.fields.peerExchangeMessageSender, ConsumerTotalsStorage: tt.fields.consumerTotalsStorage, Peer: tt.fields.peer, - ConsumerInvoiceStorage: tt.fields.consumerInvoiceStorage, Ks: tt.fields.keystore, Identity: tt.fields.identity, Publisher: &mockPublisher{}, }, } + emt.lastInvoice = tt.fields.lastInvoice if err := emt.issueExchangeMessage(tt.args.invoice); (err != nil) != tt.wantErr { t.Errorf("ExchangeMessageTracker.issueExchangeMessage() error = %v, wantErr %v", err, tt.wantErr) } diff --git a/session/pingpong/factory.go b/session/pingpong/factory.go index 4db1e799cc..f58e686caa 100644 --- a/session/pingpong/factory.go +++ b/session/pingpong/factory.go @@ -109,7 +109,6 @@ func BackwardsCompatibleExchangeFactoryFunc( keystore *keystore.KeyStore, options node.Options, signer identity.SignerFactory, - invoiceStorage consumerInvoiceStorage, totalStorage consumerTotalsStorage, channelImplementation string, registryAddress string, publisher eventbus.Publisher) func(paymentInfo *promise.PaymentInfo, @@ -153,7 +152,6 @@ func BackwardsCompatibleExchangeFactoryFunc( deps := ExchangeMessageTrackerDeps{ InvoiceChan: invoices, PeerExchangeMessageSender: NewExchangeSender(dialog), - ConsumerInvoiceStorage: invoiceStorage, ConsumerTotalsStorage: totalStorage, TimeTracker: &timeTracker, Ks: keystore, diff --git a/session/pingpong/invoice_storage.go b/session/pingpong/invoice_storage.go index d6118b5184..d8d40ba3cc 100644 --- a/session/pingpong/invoice_storage.go +++ b/session/pingpong/invoice_storage.go @@ -33,7 +33,6 @@ type bucketName string const receivedInvoices bucketName = "received_invoices" const sentInvoices bucketName = "sent_invoices" -const agreementIDCounter bucketName = "agreement_id_counter" const agreementRBucket bucketName = "agreement_r" type genericInvoiceStorage interface { @@ -43,33 +42,10 @@ type genericInvoiceStorage interface { type providerSpecificInvoiceStorage interface { genericInvoiceStorage - GetNewAgreementID(identity.Identity) (uint64, error) StoreR(providerID identity.Identity, agreementID uint64, r string) error GetR(providerID identity.Identity, agreementID uint64) (string, error) } -// ConsumerInvoiceStorage allows the consumer to store received invoices. -type ConsumerInvoiceStorage struct { - gis genericInvoiceStorage -} - -// NewConsumerInvoiceStorage allows the consumer to store invoices. -func NewConsumerInvoiceStorage(gis genericInvoiceStorage) *ConsumerInvoiceStorage { - return &ConsumerInvoiceStorage{ - gis: gis, - } -} - -// Store stores the given invoice -func (cis *ConsumerInvoiceStorage) Store(consumerIdentity, providerIdentity identity.Identity, invoice crypto.Invoice) error { - return cis.gis.StoreInvoice(string(receivedInvoices), consumerIdentity.Address+providerIdentity.Address, invoice) -} - -// Get returns the stored invoice -func (cis *ConsumerInvoiceStorage) Get(consumerIdentity, providerIdentity identity.Identity) (crypto.Invoice, error) { - return cis.gis.GetInvoice(string(receivedInvoices), consumerIdentity.Address+providerIdentity.Address) -} - // ProviderInvoiceStorage allows the provider to store sent invoices. type ProviderInvoiceStorage struct { gis providerSpecificInvoiceStorage @@ -92,11 +68,6 @@ func (pis *ProviderInvoiceStorage) Get(providerIdentity, consumerIdentity identi return pis.gis.GetInvoice(string(sentInvoices), providerIdentity.Address+consumerIdentity.Address) } -// GetNewAgreementID returns a new agreement id for the provider. -func (pis *ProviderInvoiceStorage) GetNewAgreementID(providerID identity.Identity) (uint64, error) { - return pis.gis.GetNewAgreementID(providerID) -} - // StoreR stores the given R. func (pis *ProviderInvoiceStorage) StoreR(providerID identity.Identity, agreementID uint64, r string) error { return pis.gis.StoreR(providerID, agreementID, r) @@ -134,25 +105,6 @@ func (is *InvoiceStorage) StoreInvoice(bucket string, key string, invoice crypto return errors.Wrap(is.bolt.SetValue(bucket, key, invoice), "could not save invoice") } -// GetNewAgreementID generates a new agreement id. -func (is *InvoiceStorage) GetNewAgreementID(providerID identity.Identity) (uint64, error) { - is.lock.Lock() - defer is.lock.Unlock() - - var res uint64 = 1 - err := is.bolt.GetValue(string(agreementIDCounter), providerID.Address, &res) - if err != nil { - if err.Error() != errBoltNotFound { - return res, errors.Wrap(err, "could not get agreement id") - } - } - err = is.bolt.SetValue(string(agreementIDCounter), providerID.Address, res+1) - if err != nil { - err = errors.Wrap(err, "could not set agreement id") - } - return res, err -} - func (is *InvoiceStorage) getRKey(providerID identity.Identity, agreementID uint64) string { return fmt.Sprintf("%v_%v", providerID.Address, agreementID) } diff --git a/session/pingpong/invoice_storage_test.go b/session/pingpong/invoice_storage_test.go index 41a72ee846..e497795e2a 100644 --- a/session/pingpong/invoice_storage_test.go +++ b/session/pingpong/invoice_storage_test.go @@ -47,65 +47,6 @@ var invoiceTwo = crypto.Invoice{ Provider: identityTwo.Address, } -func TestConsumerInvoiceStorage(t *testing.T) { - consumerID := identity.FromAddress("0xconsumer") - dir, err := ioutil.TempDir("", "consumerInvoiceTest") - assert.NoError(t, err) - defer os.RemoveAll(dir) - - bolt, err := boltdb.NewStorage(dir) - assert.NoError(t, err) - defer bolt.Close() - - genericStorage := NewInvoiceStorage(bolt) - - consumerStorage := NewConsumerInvoiceStorage(genericStorage) - - // check if errors are wrapped correctly - _, err = consumerStorage.Get(consumerID, identityOne) - assert.Equal(t, ErrNotFound, err) - - // store and check that invoice is stored correctly - err = consumerStorage.Store(consumerID, identityOne, invoiceOne) - assert.NoError(t, err) - - invoice, err := consumerStorage.Get(consumerID, identityOne) - assert.NoError(t, err) - assert.EqualValues(t, invoiceOne, invoice) - - // overwrite the invoice, check if it is overwritten - err = consumerStorage.Store(consumerID, identityOne, invoiceTwo) - assert.NoError(t, err) - - invoice, err = consumerStorage.Get(consumerID, identityOne) - assert.NoError(t, err) - assert.EqualValues(t, invoiceTwo, invoice) - - // store two invoices, check if both are gotten correctly - err = consumerStorage.Store(consumerID, identityTwo, invoiceOne) - assert.NoError(t, err) - - invoice, err = consumerStorage.Get(consumerID, identityOne) - assert.NoError(t, err) - assert.EqualValues(t, invoiceTwo, invoice) - - invoice, err = consumerStorage.Get(consumerID, identityTwo) - assert.NoError(t, err) - assert.EqualValues(t, invoiceOne, invoice) - - // check for possibility for multiple consumers to store invoices - consumerTwo := identity.FromAddress("0xconsumerTwo") - _, err = consumerStorage.Get(consumerTwo, identityTwo) - assert.Equal(t, ErrNotFound, err) - - err = consumerStorage.Store(consumerTwo, identityTwo, invoiceOne) - assert.NoError(t, err) - - invoice, err = consumerStorage.Get(consumerTwo, identityTwo) - assert.NoError(t, err) - assert.EqualValues(t, invoiceOne, invoice) -} - func TestProviderInvoiceStorage(t *testing.T) { providerID := identity.FromAddress("0xprovider") dir, err := ioutil.TempDir("", "providerInvoiceTest")