github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/identity/registry/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 registry
    19  
    20  import (
    21  	"os"
    22  	"testing"
    23  
    24  	"github.com/mysteriumnetwork/node/core/storage/boltdb"
    25  	"github.com/mysteriumnetwork/node/identity"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestRegistrationStatusStorage(t *testing.T) {
    30  	var chainID int64 = 1
    31  	dir, err := os.MkdirTemp("", "consumerTotalsStorageTest")
    32  	assert.NoError(t, err)
    33  	defer os.RemoveAll(dir)
    34  
    35  	bolt, err := boltdb.NewStorage(dir)
    36  	assert.NoError(t, err)
    37  	defer bolt.Close()
    38  	consumerTotalsStorage := NewRegistrationStatusStorage(bolt)
    39  
    40  	mockIdentity := identity.FromAddress("0x001")
    41  	mockStatus := StoredRegistrationStatus{
    42  		RegistrationStatus: Registered,
    43  		Identity:           mockIdentity,
    44  		ChainID:            chainID,
    45  	}
    46  	_, err = consumerTotalsStorage.Get(chainID, identity.FromAddress("0x001"))
    47  	assert.Equal(t, ErrNotFound, err)
    48  
    49  	err = consumerTotalsStorage.Store(mockStatus)
    50  	assert.Nil(t, err)
    51  
    52  	res, err := consumerTotalsStorage.Get(chainID, mockIdentity)
    53  	assert.Nil(t, err)
    54  
    55  	statusesEqual(t, mockStatus, res)
    56  
    57  	mockStatus2 := mockStatus
    58  	mockStatus2.RegistrationStatus = RegistrationError
    59  	mockStatus2.Identity = identity.FromAddress("0x002")
    60  	mockStatus2.ChainID = chainID
    61  	err = consumerTotalsStorage.Store(mockStatus2)
    62  	assert.Nil(t, err)
    63  
    64  	all, err := consumerTotalsStorage.GetAll()
    65  	assert.Nil(t, err)
    66  	statusesEqual(t, mockStatus, all[0])
    67  	statusesEqual(t, mockStatus2, all[1])
    68  
    69  	// should not update the registered provider status
    70  	err = consumerTotalsStorage.Store(StoredRegistrationStatus{
    71  		Identity:           mockIdentity,
    72  		RegistrationStatus: RegistrationError,
    73  		ChainID:            chainID,
    74  	})
    75  	assert.Nil(t, err)
    76  
    77  	res, err = consumerTotalsStorage.Get(chainID, mockIdentity)
    78  	assert.Nil(t, err)
    79  	assert.Equal(t, Registered, res.RegistrationStatus)
    80  
    81  	// should update status
    82  	err = consumerTotalsStorage.Store(StoredRegistrationStatus{
    83  		Identity:           mockStatus2.Identity,
    84  		RegistrationStatus: Registered,
    85  		ChainID:            chainID,
    86  	})
    87  	assert.Nil(t, err)
    88  
    89  	res, err = consumerTotalsStorage.Get(chainID, mockStatus2.Identity)
    90  	assert.Nil(t, err)
    91  	assert.Equal(t, Registered, res.RegistrationStatus)
    92  
    93  	// should not override registered status
    94  	err = consumerTotalsStorage.Store(StoredRegistrationStatus{
    95  		Identity:           mockStatus2.Identity,
    96  		RegistrationStatus: RegistrationError,
    97  		ChainID:            chainID,
    98  	})
    99  	assert.Nil(t, err)
   100  
   101  	res, err = consumerTotalsStorage.Get(chainID, mockStatus2.Identity)
   102  	assert.Nil(t, err)
   103  	assert.Equal(t, Registered, res.RegistrationStatus)
   104  
   105  	// should override the status with Registered
   106  	err = consumerTotalsStorage.Store(StoredRegistrationStatus{
   107  		Identity:           mockStatus2.Identity,
   108  		RegistrationStatus: Registered,
   109  		ChainID:            chainID,
   110  	})
   111  	assert.Nil(t, err)
   112  
   113  	res, err = consumerTotalsStorage.Get(chainID, mockStatus2.Identity)
   114  	assert.Nil(t, err)
   115  	assert.Equal(t, Registered, res.RegistrationStatus)
   116  
   117  	chainID2 := chainID + 1
   118  	// Should not find an entry
   119  	_, err = consumerTotalsStorage.Get(chainID2, mockStatus2.Identity)
   120  	assert.Error(t, ErrNotFound, err)
   121  
   122  	// should not override already existing identity
   123  	err = consumerTotalsStorage.Store(StoredRegistrationStatus{
   124  		Identity:           mockStatus2.Identity,
   125  		RegistrationStatus: RegistrationError,
   126  		ChainID:            chainID2,
   127  	})
   128  
   129  	// Should find both in different chains with different states
   130  	res, err = consumerTotalsStorage.Get(chainID, mockStatus2.Identity)
   131  	assert.Nil(t, err)
   132  	assert.Equal(t, Registered, res.RegistrationStatus)
   133  	res, err = consumerTotalsStorage.Get(chainID2, mockStatus2.Identity)
   134  	assert.Nil(t, err)
   135  	assert.Equal(t, RegistrationError, res.RegistrationStatus)
   136  }
   137  
   138  func statusesEqual(t *testing.T, a, b StoredRegistrationStatus) {
   139  	assert.Equal(t, a.RegistrationStatus, b.RegistrationStatus)
   140  	assert.Equal(t, a.Identity.Address, b.Identity.Address)
   141  	assert.EqualValues(t, a.RegistrationRequest, b.RegistrationRequest)
   142  }