-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4024 from ElrondNetwork/feat/refactor-peers-mbs
Feat/refactor peers mbs
- Loading branch information
Showing
146 changed files
with
6,666 additions
and
917 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package dataPool | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/ElrondNetwork/elrond-go-core/core/check" | ||
"github.com/ElrondNetwork/elrond-go/dataRetriever" | ||
"github.com/ElrondNetwork/elrond-go/state" | ||
) | ||
|
||
var _ dataRetriever.ValidatorInfoCacher = (*validatorInfoMapCacher)(nil) | ||
|
||
type validatorInfoMapCacher struct { | ||
mutValidatorInfo sync.RWMutex | ||
validatorInfoForEpoch map[string]*state.ShardValidatorInfo | ||
} | ||
|
||
// NewCurrentEpochValidatorInfoPool returns a new validator info pool to be used for the current epoch | ||
func NewCurrentEpochValidatorInfoPool() *validatorInfoMapCacher { | ||
return &validatorInfoMapCacher{ | ||
mutValidatorInfo: sync.RWMutex{}, | ||
validatorInfoForEpoch: make(map[string]*state.ShardValidatorInfo), | ||
} | ||
} | ||
|
||
// Clean creates a new validator info pool | ||
func (vimc *validatorInfoMapCacher) Clean() { | ||
vimc.mutValidatorInfo.Lock() | ||
vimc.validatorInfoForEpoch = make(map[string]*state.ShardValidatorInfo) | ||
vimc.mutValidatorInfo.Unlock() | ||
} | ||
|
||
// GetValidatorInfo gets the validator info for the given hash | ||
func (vimc *validatorInfoMapCacher) GetValidatorInfo(validatorInfoHash []byte) (*state.ShardValidatorInfo, error) { | ||
vimc.mutValidatorInfo.RLock() | ||
defer vimc.mutValidatorInfo.RUnlock() | ||
|
||
validatorInfo, ok := vimc.validatorInfoForEpoch[string(validatorInfoHash)] | ||
if !ok { | ||
return nil, dataRetriever.ErrValidatorInfoNotFoundInEpochPool | ||
} | ||
|
||
return validatorInfo, nil | ||
} | ||
|
||
// AddValidatorInfo adds the validator info for the given hash | ||
func (vimc *validatorInfoMapCacher) AddValidatorInfo(validatorInfoHash []byte, validatorInfo *state.ShardValidatorInfo) { | ||
if check.IfNil(validatorInfo) { | ||
return | ||
} | ||
|
||
vimc.mutValidatorInfo.Lock() | ||
vimc.validatorInfoForEpoch[string(validatorInfoHash)] = validatorInfo | ||
vimc.mutValidatorInfo.Unlock() | ||
} | ||
|
||
// IsInterfaceNil returns true if underlying object is nil | ||
func (vimc *validatorInfoMapCacher) IsInterfaceNil() bool { | ||
return vimc == nil | ||
} |
34 changes: 34 additions & 0 deletions
34
dataRetriever/dataPool/currentEpochValidatorInfoPool_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package dataPool | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ElrondNetwork/elrond-go/dataRetriever" | ||
"github.com/ElrondNetwork/elrond-go/state" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCurrentEpochValidatorInfoPool_AddGetCleanTx(t *testing.T) { | ||
t.Parallel() | ||
|
||
validatorInfoHash := []byte("hash") | ||
validatorInfo := &state.ShardValidatorInfo{} | ||
currentValidatorInfoPool := NewCurrentEpochValidatorInfoPool() | ||
require.False(t, currentValidatorInfoPool.IsInterfaceNil()) | ||
|
||
currentValidatorInfoPool.AddValidatorInfo(validatorInfoHash, validatorInfo) | ||
currentValidatorInfoPool.AddValidatorInfo(validatorInfoHash, nil) | ||
|
||
validatorInfoFromPool, err := currentValidatorInfoPool.GetValidatorInfo([]byte("wrong hash")) | ||
require.Nil(t, validatorInfoFromPool) | ||
require.Equal(t, dataRetriever.ErrValidatorInfoNotFoundInEpochPool, err) | ||
|
||
validatorInfoFromPool, err = currentValidatorInfoPool.GetValidatorInfo(validatorInfoHash) | ||
require.Nil(t, err) | ||
require.Equal(t, validatorInfo, validatorInfoFromPool) | ||
|
||
currentValidatorInfoPool.Clean() | ||
validatorInfoFromPool, err = currentValidatorInfoPool.GetValidatorInfo(validatorInfoHash) | ||
require.Nil(t, validatorInfoFromPool) | ||
require.Equal(t, dataRetriever.ErrValidatorInfoNotFoundInEpochPool, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.