github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/session/pingpong/hermes_promise_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/ethereum/go-ethereum/common"
    26  	"github.com/mysteriumnetwork/node/core/storage/boltdb"
    27  	"github.com/mysteriumnetwork/node/identity"
    28  	"github.com/mysteriumnetwork/payments/crypto"
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  func TestHermesPromiseStorage(t *testing.T) {
    33  	dir, err := os.MkdirTemp("", "hermesPromiseStorageTest")
    34  	assert.NoError(t, err)
    35  	defer os.RemoveAll(dir)
    36  
    37  	bolt, err := boltdb.NewStorage(dir)
    38  	assert.NoError(t, err)
    39  	defer bolt.Close()
    40  
    41  	hermesStorage := NewHermesPromiseStorage(bolt)
    42  
    43  	id := identity.FromAddress("0x44440954558C5bFA0D4153B0002B1d1E3E3f5Ff5")
    44  	firstHermes := common.HexToAddress("0x000000acc1")
    45  	secondHermes := common.HexToAddress("0x000000acc2")
    46  
    47  	firstPromise := HermesPromise{
    48  		ChannelID:   "1",
    49  		Identity:    id,
    50  		HermesID:    firstHermes,
    51  		Promise:     crypto.Promise{Amount: big.NewInt(1), Fee: big.NewInt(1), ChainID: 1},
    52  		R:           "some r",
    53  		AgreementID: big.NewInt(123),
    54  	}
    55  
    56  	secondPromise := HermesPromise{
    57  		ChannelID:   "2",
    58  		Identity:    id,
    59  		HermesID:    secondHermes,
    60  		Promise:     crypto.Promise{Amount: big.NewInt(2), Fee: big.NewInt(2), ChainID: 1},
    61  		R:           "some other r",
    62  		AgreementID: big.NewInt(1234),
    63  	}
    64  
    65  	// check if errors are wrapped correctly
    66  	_, err = hermesStorage.Get(1, "unknown_id")
    67  	assert.Equal(t, ErrNotFound, err)
    68  
    69  	promises, err := hermesStorage.List(HermesPromiseFilter{})
    70  	assert.Equal(t, []HermesPromise{}, promises)
    71  	assert.NoError(t, err)
    72  
    73  	// store and check that promise is stored correctly
    74  	err = hermesStorage.Store(firstPromise)
    75  	assert.NoError(t, err)
    76  
    77  	promise, err := hermesStorage.Get(1, firstPromise.ChannelID)
    78  	assert.NoError(t, err)
    79  	assert.EqualValues(t, firstPromise, promise)
    80  
    81  	promises, err = hermesStorage.List(HermesPromiseFilter{
    82  		ChainID: 1,
    83  	})
    84  	assert.Equal(t, []HermesPromise{firstPromise}, promises)
    85  	assert.NoError(t, err)
    86  
    87  	// overwrite the promise, check if it is overwritten
    88  	err = hermesStorage.Store(secondPromise)
    89  	assert.NoError(t, err)
    90  
    91  	promise, err = hermesStorage.Get(1, secondPromise.ChannelID)
    92  	assert.NoError(t, err)
    93  	assert.EqualValues(t, secondPromise, promise)
    94  
    95  	promises, err = hermesStorage.List(HermesPromiseFilter{
    96  		ChainID: 1,
    97  	})
    98  	assert.Equal(t, []HermesPromise{firstPromise, secondPromise}, promises)
    99  	assert.NoError(t, err)
   100  
   101  	overwritingPromise := firstPromise
   102  	overwritingPromise.Promise.Amount = big.NewInt(0)
   103  	err = hermesStorage.Store(overwritingPromise)
   104  	assert.Equal(t, err, ErrAttemptToOverwrite)
   105  }
   106  
   107  func TestHermesPromiseStorageDelete(t *testing.T) {
   108  	dir, err := os.MkdirTemp("", "hermesPromiseStorageTest")
   109  	assert.NoError(t, err)
   110  	defer os.RemoveAll(dir)
   111  
   112  	bolt, err := boltdb.NewStorage(dir)
   113  	assert.NoError(t, err)
   114  	defer bolt.Close()
   115  
   116  	hermesStorage := NewHermesPromiseStorage(bolt)
   117  
   118  	id := identity.FromAddress("0x44440954558C5bFA0D4153B0002B1d1E3E3f5Ff5")
   119  	firstHermes := common.HexToAddress("0x000000acc1")
   120  
   121  	firstPromise := HermesPromise{
   122  		ChannelID:   "1",
   123  		Identity:    id,
   124  		HermesID:    firstHermes,
   125  		Promise:     crypto.Promise{Amount: big.NewInt(1), Fee: big.NewInt(1), ChainID: 1},
   126  		R:           "some r",
   127  		AgreementID: big.NewInt(123),
   128  	}
   129  
   130  	// should error since no such promise
   131  	err = hermesStorage.Delete(firstPromise)
   132  	assert.Error(t, err)
   133  
   134  	err = hermesStorage.Store(firstPromise)
   135  	assert.NoError(t, err)
   136  
   137  	promise, err := hermesStorage.Get(1, firstPromise.ChannelID)
   138  	assert.NoError(t, err)
   139  	assert.EqualValues(t, firstPromise, promise)
   140  
   141  	err = hermesStorage.Delete(firstPromise)
   142  	assert.NoError(t, err)
   143  
   144  	_, err = hermesStorage.Get(1, firstPromise.ChannelID)
   145  	assert.Error(t, err)
   146  }