github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/session/pingpong/consumer_totals_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 "testing" 23 24 "github.com/ethereum/go-ethereum/common" 25 "github.com/mysteriumnetwork/node/eventbus" 26 "github.com/mysteriumnetwork/node/identity" 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func TestConsumerTotalStorage(t *testing.T) { 31 consumerTotalsStorage := NewConsumerTotalsStorage(eventbus.New()) 32 33 channelAddress := identity.FromAddress("someAddress") 34 hermesAddress := common.HexToAddress("someOtherAddress") 35 var amount = big.NewInt(12) 36 37 // check if errors are wrapped correctly 38 _, err := consumerTotalsStorage.Get(1, channelAddress, hermesAddress) 39 assert.Equal(t, ErrNotFound, err) 40 41 // store and check that total is stored correctly 42 err = consumerTotalsStorage.Store(1, channelAddress, hermesAddress, amount) 43 assert.NoError(t, err) 44 45 a, err := consumerTotalsStorage.Get(1, channelAddress, hermesAddress) 46 assert.NoError(t, err) 47 assert.Equal(t, amount, a) 48 49 var newAmount = big.NewInt(123) 50 // overwrite the amount, check if it is overwritten 51 err = consumerTotalsStorage.Store(1, channelAddress, hermesAddress, newAmount) 52 assert.NoError(t, err) 53 54 a, err = consumerTotalsStorage.Get(1, channelAddress, hermesAddress) 55 assert.NoError(t, err) 56 assert.EqualValues(t, newAmount, a) 57 58 var newLowerAmount = big.NewInt(120) 59 // overwrite the amount with lower, check that it is not overwritten 60 err = consumerTotalsStorage.Store(1, channelAddress, hermesAddress, newLowerAmount) 61 assert.NoError(t, err) 62 63 a, err = consumerTotalsStorage.Get(1, channelAddress, hermesAddress) 64 assert.NoError(t, err) 65 assert.EqualValues(t, newAmount, a) 66 67 someOtherChannel := identity.FromAddress("someOtherChannel") 68 // store two amounts, check if both are gotten correctly 69 err = consumerTotalsStorage.Store(1, someOtherChannel, hermesAddress, amount) 70 assert.NoError(t, err) 71 72 a, err = consumerTotalsStorage.Get(1, channelAddress, hermesAddress) 73 assert.NoError(t, err) 74 assert.EqualValues(t, newAmount, a) 75 76 addAmount := big.NewInt(10) 77 err = consumerTotalsStorage.Add(1, channelAddress, hermesAddress, addAmount) 78 assert.NoError(t, err) 79 80 a, err = consumerTotalsStorage.Get(1, channelAddress, hermesAddress) 81 assert.NoError(t, err) 82 assert.EqualValues(t, new(big.Int).Add(newAmount, addAmount), a) 83 84 a, err = consumerTotalsStorage.Get(1, someOtherChannel, hermesAddress) 85 assert.NoError(t, err) 86 assert.EqualValues(t, amount, a) 87 88 someUnusedChannel := identity.FromAddress("someUnusedChannel") 89 90 addAmount = big.NewInt(15) 91 err = consumerTotalsStorage.Add(1, someUnusedChannel, hermesAddress, addAmount) 92 assert.NoError(t, err) 93 94 a, err = consumerTotalsStorage.Get(1, someUnusedChannel, hermesAddress) 95 assert.NoError(t, err) 96 assert.EqualValues(t, addAmount, a) 97 }