Skip to content

Commit

Permalink
Change to RW lock for chains
Browse files Browse the repository at this point in the history
  • Loading branch information
poktblade committed May 22, 2022
1 parent 374ad87 commit aad7ff3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ func NewHostedChains(generate bool) *types.HostedBlockchains {
// return the map
return &types.HostedBlockchains{
M: m,
L: sync.Mutex{},
L: sync.RWMutex{},
}
}

Expand Down Expand Up @@ -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 (
Expand Down
14 changes: 7 additions & 7 deletions x/pocketcore/types/hostedBlockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ 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
}

// "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 {
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions x/pocketcore/types/hostedBlockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
Expand Down Expand Up @@ -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,
},
}
Expand Down

0 comments on commit aad7ff3

Please sign in to comment.