Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keys holder renaming #4484

Merged
merged 2 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,5 +482,5 @@ var ErrDBIsClosed = errors.New("DB is closed")
// ErrNilEnableEpochsHandler signals that a nil enable epochs handler was provided
var ErrNilEnableEpochsHandler = errors.New("nil enable epochs handler")

// ErrNilKeysHolder signals that a nil keys holder has been provided
var ErrNilKeysHolder = errors.New("nil keys holder")
// ErrNilManagedPeersHolder signals that a nil managed peers holder has been provided
var ErrNilManagedPeersHolder = errors.New("nil managed peers holder")
8 changes: 4 additions & 4 deletions factory/cryptoComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ type cryptoComponents struct {
blockSignKeyGen crypto.KeyGenerator
txSignKeyGen crypto.KeyGenerator
messageSignVerifier vm.MessageSignVerifier
keysHolder heartbeat.KeysHolder // TODO (next PR) rename this to ManagedKeysHolder
managedPeersHolder heartbeat.ManagedPeersHolder
keysHandler consensus.KeysHandler
cryptoParams
}
Expand Down Expand Up @@ -181,14 +181,14 @@ func (ccf *cryptoComponentsFactory) Create() (*cryptoComponents, error) {
// TODO: refactor the logic for isMainMachine
redundancyLevel := int(ccf.prefsConfig.Preferences.RedundancyLevel)
isMainMachine := redundancyLevel == mainMachineRedundancyLevel
argsKeysHolder := keysManagement.ArgsVirtualPeersHolder{
argsManagedPeersHolder := keysManagement.ArgsManagedPeersHolder{
KeyGenerator: blockSignKeyGen,
P2PIdentityGenerator: p2pCrypto.NewIdentityGenerator(),
IsMainMachine: isMainMachine,
MaxRoundsWithoutReceivedMessages: redundancyLevel,
PrefsConfig: ccf.prefsConfig,
}
keysHolder, err := keysManagement.NewVirtualPeersHolder(argsKeysHolder)
managedPeersHolder, err := keysManagement.NewManagedPeersHolder(argsManagedPeersHolder)
if err != nil {
return nil, err
}
Expand All @@ -203,7 +203,7 @@ func (ccf *cryptoComponentsFactory) Create() (*cryptoComponents, error) {
blockSignKeyGen: blockSignKeyGen,
txSignKeyGen: txSignKeyGen,
messageSignVerifier: messageSignVerifier,
keysHolder: keysHolder,
managedPeersHolder: managedPeersHolder,
keysHandler: keysManagement.NewKeysHandler(),
cryptoParams: *cp,
}, nil
Expand Down
13 changes: 6 additions & 7 deletions factory/cryptoComponentsHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ func (mcc *managedCryptoComponents) CheckSubcomponents() error {
if check.IfNil(mcc.cryptoComponents.messageSignVerifier) {
return errors.ErrNilMessageSignVerifier
}
if check.IfNil(mcc.cryptoComponents.keysHolder) {
return errors.ErrNilKeysHolder
if check.IfNil(mcc.cryptoComponents.managedPeersHolder) {
return errors.ErrNilManagedPeersHolder
}

return nil
Expand Down Expand Up @@ -270,17 +270,16 @@ func (mcc *managedCryptoComponents) MessageSignVerifier() vm.MessageSignVerifier
return mcc.cryptoComponents.messageSignVerifier
}

// KeysHolder returns the managed keys holder
// TODO (next PR) rename this to ManagedKeysHolder
func (mcc *managedCryptoComponents) KeysHolder() heartbeat.KeysHolder {
// ManagedPeersHolder returns the managed peers holder
func (mcc *managedCryptoComponents) ManagedPeersHolder() heartbeat.ManagedPeersHolder {
mcc.mutCryptoComponents.RLock()
defer mcc.mutCryptoComponents.RUnlock()

if mcc.cryptoComponents == nil {
return nil
}

return mcc.cryptoComponents.keysHolder
return mcc.cryptoComponents.managedPeersHolder
}

// KeysHandler returns the handler that manages keys either in single sign mode or multi key mode
Expand All @@ -307,7 +306,7 @@ func (mcc *managedCryptoComponents) Clone() interface{} {
blockSignKeyGen: mcc.BlockSignKeyGen(),
txSignKeyGen: mcc.TxSignKeyGen(),
messageSignVerifier: mcc.MessageSignVerifier(),
keysHolder: mcc.KeysHolder(),
managedPeersHolder: mcc.ManagedPeersHolder(),
keysHandler: mcc.KeysHandler(),
cryptoParams: mcc.cryptoParams,
}
Expand Down
4 changes: 2 additions & 2 deletions factory/cryptoComponentsHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestManagedCryptoComponents_CreateShouldWork(t *testing.T) {
require.Nil(t, managedCryptoComponents.BlockSignKeyGen())
require.Nil(t, managedCryptoComponents.TxSignKeyGen())
require.Nil(t, managedCryptoComponents.MessageSignVerifier())
require.Nil(t, managedCryptoComponents.KeysHolder())
require.Nil(t, managedCryptoComponents.ManagedPeersHolder())

err = managedCryptoComponents.Create()
require.NoError(t, err)
Expand All @@ -53,7 +53,7 @@ func TestManagedCryptoComponents_CreateShouldWork(t *testing.T) {
require.NotNil(t, managedCryptoComponents.BlockSignKeyGen())
require.NotNil(t, managedCryptoComponents.TxSignKeyGen())
require.NotNil(t, managedCryptoComponents.MessageSignVerifier())
require.NotNil(t, managedCryptoComponents.KeysHolder())
require.NotNil(t, managedCryptoComponents.ManagedPeersHolder())
}

func TestManagedCryptoComponents_CheckSubcomponents(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion factory/heartbeatV2Components.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (hcf *heartbeatV2ComponentsFactory) Create() (*heartbeatV2Components, error
HardforkTimeBetweenSends: time.Second * time.Duration(cfg.HardforkTimeBetweenSendsInSec),
HardforkTriggerPubKey: hcf.coreComponents.HardforkTriggerPubKey(),
PeerTypeProvider: peerTypeProvider,
KeysHolder: hcf.cryptoComponents.KeysHolder(),
ManagedPeersHolder: hcf.cryptoComponents.ManagedPeersHolder(),
PeerAuthenticationTimeBetweenChecks: time.Second * time.Duration(cfg.PeerAuthenticationTimeBetweenChecksInSec),
ShardCoordinator: hcf.processComponents.ShardCoordinator(),
}
Expand Down
2 changes: 1 addition & 1 deletion factory/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ type CryptoComponentsHolder interface {
BlockSignKeyGen() crypto.KeyGenerator
TxSignKeyGen() crypto.KeyGenerator
MessageSignVerifier() vm.MessageSignVerifier
KeysHolder() heartbeat.KeysHolder
ManagedPeersHolder() heartbeat.ManagedPeersHolder
KeysHandler() consensus.KeysHandler
Clone() interface{}
IsInterfaceNil() bool
Expand Down
66 changes: 33 additions & 33 deletions factory/mock/cryptoComponentsMock.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ import (

// CryptoComponentsMock -
type CryptoComponentsMock struct {
PubKey crypto.PublicKey
PrivKey crypto.PrivateKey
PubKeyString string
PrivKeyBytes []byte
PubKeyBytes []byte
BlockSig crypto.SingleSigner
TxSig crypto.SingleSigner
MultiSig crypto.MultiSigner
PeerSignHandler crypto.PeerSignatureHandler
BlKeyGen crypto.KeyGenerator
TxKeyGen crypto.KeyGenerator
MsgSigVerifier vm.MessageSignVerifier
KeysHolderField heartbeat.KeysHolder
KeysHandlerField consensus.KeysHandler
mutMultiSig sync.RWMutex
PubKey crypto.PublicKey
PrivKey crypto.PrivateKey
PubKeyString string
PrivKeyBytes []byte
PubKeyBytes []byte
BlockSig crypto.SingleSigner
TxSig crypto.SingleSigner
MultiSig crypto.MultiSigner
PeerSignHandler crypto.PeerSignatureHandler
BlKeyGen crypto.KeyGenerator
TxKeyGen crypto.KeyGenerator
MsgSigVerifier vm.MessageSignVerifier
ManagedPeersHolderField heartbeat.ManagedPeersHolder
KeysHandlerField consensus.KeysHandler
mutMultiSig sync.RWMutex
}

// PublicKey -
Expand Down Expand Up @@ -103,9 +103,9 @@ func (ccm *CryptoComponentsMock) MessageSignVerifier() vm.MessageSignVerifier {
return ccm.MsgSigVerifier
}

// KeysHolder -
func (ccm *CryptoComponentsMock) KeysHolder() heartbeat.KeysHolder {
return ccm.KeysHolderField
// ManagedPeersHolder -
func (ccm *CryptoComponentsMock) ManagedPeersHolder() heartbeat.ManagedPeersHolder {
return ccm.ManagedPeersHolderField
}

// KeysHandler -
Expand All @@ -116,21 +116,21 @@ func (ccm *CryptoComponentsMock) KeysHandler() consensus.KeysHandler {
// Clone -
func (ccm *CryptoComponentsMock) Clone() interface{} {
return &CryptoComponentsMock{
PubKey: ccm.PubKey,
PrivKey: ccm.PrivKey,
PubKeyString: ccm.PubKeyString,
PrivKeyBytes: ccm.PrivKeyBytes,
PubKeyBytes: ccm.PubKeyBytes,
BlockSig: ccm.BlockSig,
TxSig: ccm.TxSig,
MultiSig: ccm.MultiSig,
PeerSignHandler: ccm.PeerSignHandler,
BlKeyGen: ccm.BlKeyGen,
TxKeyGen: ccm.TxKeyGen,
MsgSigVerifier: ccm.MsgSigVerifier,
KeysHolderField: ccm.KeysHolderField,
KeysHandlerField: ccm.KeysHandlerField,
mutMultiSig: sync.RWMutex{},
PubKey: ccm.PubKey,
PrivKey: ccm.PrivKey,
PubKeyString: ccm.PubKeyString,
PrivKeyBytes: ccm.PrivKeyBytes,
PubKeyBytes: ccm.PubKeyBytes,
BlockSig: ccm.BlockSig,
TxSig: ccm.TxSig,
MultiSig: ccm.MultiSig,
PeerSignHandler: ccm.PeerSignHandler,
BlKeyGen: ccm.BlKeyGen,
TxKeyGen: ccm.TxKeyGen,
MsgSigVerifier: ccm.MsgSigVerifier,
ManagedPeersHolderField: ccm.ManagedPeersHolderField,
KeysHandlerField: ccm.KeysHandlerField,
mutMultiSig: sync.RWMutex{},
}
}

Expand Down
4 changes: 2 additions & 2 deletions heartbeat/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ var ErrNilHeartbeatMonitor = errors.New("nil heartbeat monitor")
// ErrNilHeartbeatSenderInfoProvider signals that a nil heartbeat sender info provider was provided
var ErrNilHeartbeatSenderInfoProvider = errors.New("nil heartbeat sender info provider")

// ErrNilKeysHolder signals that a nil keys holder has been provided
var ErrNilKeysHolder = errors.New("nil keys holder")
// ErrNilManagedPeersHolder signals that a nil managed peers holder has been provided
var ErrNilManagedPeersHolder = errors.New("nil managed peers holder")

// ErrInvalidConfiguration signals that an invalid configuration has been provided
var ErrInvalidConfiguration = errors.New("invalid configuration")
6 changes: 3 additions & 3 deletions heartbeat/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ type NodesCoordinator interface {
IsInterfaceNil() bool
}

// KeysHolder defines the operations of an entity that holds virtual identities for a node
type KeysHolder interface {
AddVirtualPeer(privateKeyBytes []byte) error
// ManagedPeersHolder defines the operations of an entity that holds managed identities for a node
type ManagedPeersHolder interface {
AddManagedPeer(privateKeyBytes []byte) error
GetPrivateKey(pkBytes []byte) (crypto.PrivateKey, error)
GetP2PIdentity(pkBytes []byte) ([]byte, core.PeerID, error)
GetMachineID(pkBytes []byte) (string, error)
Expand Down
10 changes: 5 additions & 5 deletions heartbeat/sender/heartbeatSenderFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ type argHeartbeatSenderFactory struct {
peerSubType core.P2PPeerSubType
currentBlockProvider heartbeat.CurrentBlockProvider
peerTypeProvider heartbeat.PeerTypeProviderHandler
keysHolder heartbeat.KeysHolder
managedPeersHolder heartbeat.ManagedPeersHolder
shardCoordinator process.ShardCoordinator
nodesCoordinator heartbeat.NodesCoordinator
}

func createHeartbeatSender(args argHeartbeatSenderFactory) (heartbeatSenderHandler, error) {
isMultikey, err := isMultikeyMode(args.privKey, args.keysHolder, args.nodesCoordinator)
isMultikey, err := isMultikeyMode(args.privKey, args.managedPeersHolder, args.nodesCoordinator)
if err != nil {
return nil, fmt.Errorf("%w while creating heartbeat sender", err)
}
Expand Down Expand Up @@ -78,21 +78,21 @@ func createMultikeyHeartbeatSender(args argHeartbeatSenderFactory) (*multikeyHea
identity: args.identity,
peerSubType: args.peerSubType,
currentBlockProvider: args.currentBlockProvider,
keysHolder: args.keysHolder,
managedPeersHolder: args.managedPeersHolder,
shardCoordinator: args.shardCoordinator,
}

return newMultikeyHeartbeatSender(argsSender)
}

func isMultikeyMode(privKey crypto.PrivateKey, keysHolder heartbeat.KeysHolder, nodesCoordinator heartbeat.NodesCoordinator) (bool, error) {
func isMultikeyMode(privKey crypto.PrivateKey, managedPeersHolder heartbeat.ManagedPeersHolder, nodesCoordinator heartbeat.NodesCoordinator) (bool, error) {
pk := privKey.GeneratePublic()
pkBytes, err := pk.ToByteArray()
if err != nil {
return false, err
}

keysMap := keysHolder.GetManagedKeysByCurrentNode()
keysMap := managedPeersHolder.GetManagedKeysByCurrentNode()
isMultikey := len(keysMap) > 0

_, _, err = nodesCoordinator.GetValidatorWithPublicKey(pkBytes)
Expand Down
8 changes: 4 additions & 4 deletions heartbeat/sender/heartbeatSenderFactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func createMockHeartbeatSenderFactoryArgs() argHeartbeatSenderFactory {
peerSubType: core.RegularPeer,
currentBlockProvider: &mock.CurrentBlockProviderStub{},
peerTypeProvider: &mock.PeerTypeProviderStub{},
keysHolder: &testscommon.KeysHolderStub{},
managedPeersHolder: &testscommon.ManagedPeersHolderStub{},
shardCoordinator: createShardCoordinatorInShard(0),
nodesCoordinator: &shardingMocks.NodesCoordinatorStub{},
}
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestHeartbeatSenderFactory_createHeartbeatSender(t *testing.T) {
return nil, 0, nil
},
}
args.keysHolder = &testscommon.KeysHolderStub{
args.managedPeersHolder = &testscommon.ManagedPeersHolderStub{
GetManagedKeysByCurrentNodeCalled: func() map[string]crypto.PrivateKey {
keygen := signing.NewKeyGenerator(&mcl.SuiteBLS12{})
sk, pk := keygen.GeneratePair()
Expand Down Expand Up @@ -104,7 +104,7 @@ func TestHeartbeatSenderFactory_createHeartbeatSender(t *testing.T) {
return nil, 0, errors.New("not validator")
},
}
args.keysHolder = &testscommon.KeysHolderStub{
args.managedPeersHolder = &testscommon.ManagedPeersHolderStub{
GetManagedKeysByCurrentNodeCalled: func() map[string]crypto.PrivateKey {
return make(map[string]crypto.PrivateKey)
},
Expand All @@ -123,7 +123,7 @@ func TestHeartbeatSenderFactory_createHeartbeatSender(t *testing.T) {
return nil, 0, errors.New("not validator")
},
}
args.keysHolder = &testscommon.KeysHolderStub{
args.managedPeersHolder = &testscommon.ManagedPeersHolderStub{
GetManagedKeysByCurrentNodeCalled: func() map[string]crypto.PrivateKey {
keygen := signing.NewKeyGenerator(&mcl.SuiteBLS12{})
sk1, pk1 := keygen.GeneratePair()
Expand Down
28 changes: 14 additions & 14 deletions heartbeat/sender/multikeyHeartbeatSender.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ type argMultikeyHeartbeatSender struct {
identity string
peerSubType core.P2PPeerSubType
currentBlockProvider heartbeat.CurrentBlockProvider
keysHolder heartbeat.KeysHolder
managedPeersHolder heartbeat.ManagedPeersHolder
shardCoordinator process.ShardCoordinator
}

type multikeyHeartbeatSender struct {
commonHeartbeatSender
baseVersionNumber string
keysHolder heartbeat.KeysHolder
shardCoordinator process.ShardCoordinator
baseVersionNumber string
managedPeersHolder heartbeat.ManagedPeersHolder
shardCoordinator process.ShardCoordinator
}

// newMultikeyHeartbeatSender creates a new instance of type multikeyHeartbeatSender
Expand All @@ -48,9 +48,9 @@ func newMultikeyHeartbeatSender(args argMultikeyHeartbeatSender) (*multikeyHeart
identity: args.identity,
peerSubType: args.peerSubType,
},
baseVersionNumber: args.baseVersionNumber,
keysHolder: args.keysHolder,
shardCoordinator: args.shardCoordinator,
baseVersionNumber: args.baseVersionNumber,
managedPeersHolder: args.managedPeersHolder,
shardCoordinator: args.shardCoordinator,
}, nil
}

Expand Down Expand Up @@ -81,8 +81,8 @@ func checkMultikeyHeartbeatSenderArgs(args argMultikeyHeartbeatSender) error {
if check.IfNil(args.currentBlockProvider) {
return heartbeat.ErrNilCurrentBlockProvider
}
if check.IfNil(args.keysHolder) {
return heartbeat.ErrNilKeysHolder
if check.IfNil(args.managedPeersHolder) {
return heartbeat.ErrNilManagedPeersHolder
}
if check.IfNil(args.shardCoordinator) {
return heartbeat.ErrNilShardCoordinator
Expand Down Expand Up @@ -129,7 +129,7 @@ func (sender *multikeyHeartbeatSender) execute() error {
}

func (sender *multikeyHeartbeatSender) sendMultiKeysInfo() error {
managedKeys := sender.keysHolder.GetManagedKeysByCurrentNode()
managedKeys := sender.managedPeersHolder.GetManagedKeysByCurrentNode()
for pk := range managedKeys {
pkBytes := []byte(pk)
shouldSend := sender.processIfShouldSend(pkBytes)
Expand All @@ -149,12 +149,12 @@ func (sender *multikeyHeartbeatSender) sendMultiKeysInfo() error {
func (sender *multikeyHeartbeatSender) sendMessageForKey(pkBytes []byte) error {
time.Sleep(delayedBroadcast)

name, identity, err := sender.keysHolder.GetNameAndIdentity(pkBytes)
name, identity, err := sender.managedPeersHolder.GetNameAndIdentity(pkBytes)
if err != nil {
return err
}

machineID, err := sender.keysHolder.GetMachineID(pkBytes)
machineID, err := sender.managedPeersHolder.GetMachineID(pkBytes)
if err != nil {
return err
}
Expand All @@ -171,7 +171,7 @@ func (sender *multikeyHeartbeatSender) sendMessageForKey(pkBytes []byte) error {
return err
}

p2pSk, pid, err := sender.keysHolder.GetP2PIdentity(pkBytes)
p2pSk, pid, err := sender.managedPeersHolder.GetP2PIdentity(pkBytes)
if err != nil {
return err
}
Expand All @@ -182,7 +182,7 @@ func (sender *multikeyHeartbeatSender) sendMessageForKey(pkBytes []byte) error {
}

func (sender *multikeyHeartbeatSender) processIfShouldSend(pk []byte) bool {
if !sender.keysHolder.IsKeyManagedByCurrentNode(pk) {
if !sender.managedPeersHolder.IsKeyManagedByCurrentNode(pk) {
return false
}
_, shardID, err := sender.peerTypeProvider.ComputeForPubKey(pk)
Expand Down
Loading