github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/session/pingpong/invoice_storage_test.go (about) 1 /* 2 * Copyright (C) 2019 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package pingpong 19 20 import ( 21 "math/big" 22 "os" 23 "testing" 24 25 "github.com/mysteriumnetwork/node/core/storage/boltdb" 26 "github.com/mysteriumnetwork/node/identity" 27 "github.com/mysteriumnetwork/payments/crypto" 28 "github.com/stretchr/testify/assert" 29 ) 30 31 var identityOne = identity.FromAddress("0xf3d7B9597F8c4137Fa9F73e1Db533CEDC61e844f") 32 var identityTwo = identity.FromAddress("0x3D32e7D08BE7E5c3870679b3A7Ef60e9422196Ab") 33 34 var invoiceOne = crypto.Invoice{ 35 AgreementID: big.NewInt(1), 36 AgreementTotal: big.NewInt(1), 37 TransactorFee: big.NewInt(1), 38 Hashlock: "hashlock1", 39 Provider: identityOne.Address, 40 } 41 42 var invoiceTwo = crypto.Invoice{ 43 AgreementID: big.NewInt(2), 44 AgreementTotal: big.NewInt(2), 45 TransactorFee: big.NewInt(2), 46 Hashlock: "hashlock2", 47 Provider: identityTwo.Address, 48 } 49 50 func TestProviderInvoiceStorage(t *testing.T) { 51 providerID := identity.FromAddress("0xprovider") 52 dir, err := os.MkdirTemp("", "providerInvoiceTest") 53 assert.NoError(t, err) 54 defer os.RemoveAll(dir) 55 56 bolt, err := boltdb.NewStorage(dir) 57 assert.NoError(t, err) 58 defer bolt.Close() 59 60 genericStorage := NewInvoiceStorage(bolt) 61 62 providerStorage := NewProviderInvoiceStorage(genericStorage) 63 64 // check if errors are wrapped correctly 65 _, err = providerStorage.Get(providerID, identityOne) 66 assert.Equal(t, ErrNotFound, err) 67 68 // store and check that invoice is stored correctly 69 err = providerStorage.Store(providerID, identityOne, invoiceOne) 70 assert.NoError(t, err) 71 72 invoice, err := providerStorage.Get(providerID, identityOne) 73 assert.NoError(t, err) 74 assert.EqualValues(t, invoiceOne, invoice) 75 76 // overwrite the invoice, check if it is overwritten 77 err = providerStorage.Store(providerID, identityOne, invoiceTwo) 78 assert.NoError(t, err) 79 80 invoice, err = providerStorage.Get(providerID, identityOne) 81 assert.NoError(t, err) 82 assert.EqualValues(t, invoiceTwo, invoice) 83 84 // store two invoices, check if both are gotten correctly 85 err = providerStorage.Store(providerID, identityTwo, invoiceOne) 86 assert.NoError(t, err) 87 88 invoice, err = providerStorage.Get(providerID, identityOne) 89 assert.NoError(t, err) 90 assert.EqualValues(t, invoiceTwo, invoice) 91 92 invoice, err = providerStorage.Get(providerID, identityTwo) 93 assert.NoError(t, err) 94 assert.EqualValues(t, invoiceOne, invoice) 95 96 // check if multiple providers can actually store their invoices 97 providerTwo := identity.FromAddress("0xproviderTwo") 98 99 // store and check that invoice is stored correctly 100 err = providerStorage.Store(providerTwo, identityOne, invoiceOne) 101 assert.NoError(t, err) 102 103 invoice, err = providerStorage.Get(providerTwo, identityOne) 104 assert.NoError(t, err) 105 assert.EqualValues(t, invoiceOne, invoice) 106 107 _, err = providerStorage.Get(providerTwo, identityTwo) 108 assert.Equal(t, ErrNotFound, err) 109 110 // test R storage 111 var agreementID1 = big.NewInt(1) 112 r1 := "my r" 113 err = providerStorage.StoreR(providerID, agreementID1, r1) 114 assert.NoError(t, err) 115 116 var agreementID2 = big.NewInt(1222) 117 r2 := "my other r" 118 err = providerStorage.StoreR(providerID, agreementID2, r2) 119 assert.NoError(t, err) 120 121 r, err := providerStorage.GetR(providerID, agreementID2) 122 assert.NoError(t, err) 123 assert.Equal(t, r2, r) 124 125 r, err = providerStorage.GetR(providerID, agreementID1) 126 assert.NoError(t, err) 127 assert.Equal(t, r1, r) 128 129 // check if multiple providers can actually store their R's 130 r, err = providerStorage.GetR(providerTwo, agreementID2) 131 assert.Equal(t, ErrNotFound, err) 132 133 err = providerStorage.StoreR(providerTwo, agreementID2, r2) 134 assert.NoError(t, err) 135 136 r, err = providerStorage.GetR(providerID, agreementID2) 137 assert.NoError(t, err) 138 assert.Equal(t, r2, r) 139 }