From aad7ff3de0466beb584723a37fb48d4d81609c7b Mon Sep 17 00:00:00 2001 From: poktblade Date: Sat, 21 May 2022 23:23:28 -0500 Subject: [PATCH] Change to RW lock for chains --- app/common_test.go | 2 +- app/config.go | 4 ++-- x/pocketcore/types/hostedBlockchain.go | 14 +++++++------- x/pocketcore/types/hostedBlockchain_test.go | 12 ++++++------ 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/app/common_test.go b/app/common_test.go index 16513ae2..0ca9e77a 100644 --- a/app/common_test.go +++ b/app/common_test.go @@ -291,7 +291,7 @@ func GetApp(logger log.Logger, db dbm.DB, traceWriter io.Writer) *PocketCoreApp ID: sdk.PlaceholderHash, URL: sdk.PlaceholderURL, }} - p := NewPocketCoreApp(GenState, getInMemoryKeybase(), getInMemoryTMClient(), &pocketTypes.HostedBlockchains{M: m, L: sync.Mutex{}}, logger, db, false, 5000000, bam.SetPruning(store.PruneNothing)) + p := NewPocketCoreApp(GenState, getInMemoryKeybase(), getInMemoryTMClient(), &pocketTypes.HostedBlockchains{M: m, L: sync.RWMutex{}}, logger, db, false, 5000000, bam.SetPruning(store.PruneNothing)) return p } return creator(logger, db, traceWriter) diff --git a/app/config.go b/app/config.go index e6ec79ed..5ff248e7 100644 --- a/app/config.go +++ b/app/config.go @@ -675,7 +675,7 @@ func NewHostedChains(generate bool) *types.HostedBlockchains { // return the map return &types.HostedBlockchains{ M: m, - L: sync.Mutex{}, + L: sync.RWMutex{}, } } @@ -711,7 +711,7 @@ func generateChainsJson(chainsPath string) *types.HostedBlockchains { m[chain.ID] = chain } // return the map - return &types.HostedBlockchains{M: m, L: sync.Mutex{}} + return &types.HostedBlockchains{M: m, L: sync.RWMutex{}} } const ( diff --git a/x/pocketcore/types/hostedBlockchain.go b/x/pocketcore/types/hostedBlockchain.go index 2a9250dd..fc339a14 100644 --- a/x/pocketcore/types/hostedBlockchain.go +++ b/x/pocketcore/types/hostedBlockchain.go @@ -20,13 +20,13 @@ type BasicAuth struct { // HostedBlockchains" - An object that represents the local hosted non-native blockchains type HostedBlockchains struct { M map[string]HostedBlockchain // M[addr] -> addr, url - L sync.Mutex + L sync.RWMutex } // "Contains" - Checks to see if the hosted chain is within the HostedBlockchains object func (c *HostedBlockchains) Contains(id string) bool { - c.L.Lock() - defer c.L.Unlock() + c.L.RLock() + defer c.L.RUnlock() // quick map check _, found := c.M[id] return found @@ -34,8 +34,8 @@ func (c *HostedBlockchains) Contains(id string) bool { // "GetChainURL" - Returns the url or error of the hosted blockchain using the hex network identifier func (c *HostedBlockchains) GetChain(id string) (chain HostedBlockchain, err sdk.Error) { - c.L.Lock() - defer c.L.Unlock() + c.L.RLock() + defer c.L.RUnlock() // map check res, found := c.M[id] if !found { @@ -55,8 +55,8 @@ func (c *HostedBlockchains) GetChainURL(id string) (url string, err sdk.Error) { // "Validate" - Validates the hosted blockchain object func (c *HostedBlockchains) Validate() error { - c.L.Lock() - defer c.L.Unlock() + c.L.RLock() + defer c.L.RUnlock() // loop through all of the chains for _, chain := range c.M { // validate not empty diff --git a/x/pocketcore/types/hostedBlockchain_test.go b/x/pocketcore/types/hostedBlockchain_test.go index 8d18bf60..842bbdc4 100644 --- a/x/pocketcore/types/hostedBlockchain_test.go +++ b/x/pocketcore/types/hostedBlockchain_test.go @@ -17,7 +17,7 @@ func TestHostedBlockchains_GetChainURL(t *testing.T) { } hb := HostedBlockchains{ M: map[string]HostedBlockchain{testHostedBlockchain.ID: testHostedBlockchain}, - L: sync.Mutex{}, + L: sync.RWMutex{}, } u, err := hb.GetChainURL(ethereum) assert.Nil(t, err) @@ -34,7 +34,7 @@ func TestHostedBlockchains_ContainsFromString(t *testing.T) { } hb := HostedBlockchains{ M: map[string]HostedBlockchain{testHostedBlockchain.ID: testHostedBlockchain}, - L: sync.Mutex{}, + L: sync.RWMutex{}, } assert.True(t, hb.Contains(ethereum)) assert.False(t, hb.Contains(bitcoin)) @@ -66,22 +66,22 @@ func TestHostedBlockchains_Validate(t *testing.T) { }{ { name: "Invalid HostedBlockchain, no URL", - hc: &HostedBlockchains{M: map[string]HostedBlockchain{HCNoURL.URL: HCNoURL}, L: sync.Mutex{}}, + hc: &HostedBlockchains{M: map[string]HostedBlockchain{HCNoURL.URL: HCNoURL}, L: sync.RWMutex{}}, hasError: true, }, { name: "Invalid HostedBlockchain, no URL", - hc: &HostedBlockchains{M: map[string]HostedBlockchain{HCNoHash.URL: HCNoHash}, L: sync.Mutex{}}, + hc: &HostedBlockchains{M: map[string]HostedBlockchain{HCNoHash.URL: HCNoHash}, L: sync.RWMutex{}}, hasError: true, }, { name: "Invalid HostedBlockchain, invalid ID", - hc: &HostedBlockchains{M: map[string]HostedBlockchain{HCInvalidHash.URL: HCInvalidHash}, L: sync.Mutex{}}, + hc: &HostedBlockchains{M: map[string]HostedBlockchain{HCInvalidHash.URL: HCInvalidHash}, L: sync.RWMutex{}}, hasError: true, }, { name: "Valid HostedBlockchain", - hc: &HostedBlockchains{M: map[string]HostedBlockchain{testHostedBlockchain.ID: testHostedBlockchain}, L: sync.Mutex{}}, + hc: &HostedBlockchains{M: map[string]HostedBlockchain{testHostedBlockchain.ID: testHostedBlockchain}, L: sync.RWMutex{}}, hasError: false, }, }