diff --git a/consensus/interface.go b/consensus/interface.go index f130c101472..a5fb1f20e25 100644 --- a/consensus/interface.go +++ b/consensus/interface.go @@ -152,8 +152,8 @@ type ScheduledProcessor interface { // SignatureHandler defines the behaviour of a component that handles signatures in consensus type SignatureHandler interface { Reset(pubKeys []string) error - CreateSignatureShare(msg []byte, index uint16, epoch uint32) ([]byte, error) - CreateSignatureShareWithPrivateKey(message []byte, index uint16, epoch uint32, privateKeyBytes []byte) ([]byte, error) + CreateSignatureShareForPublicKey(message []byte, index uint16, epoch uint32, publicKeyBytes []byte) ([]byte, error) + CreateSignatureForPublicKey(message []byte, publicKeyBytes []byte) ([]byte, error) StoreSignatureShare(index uint16, sig []byte) error SignatureShare(index uint16) ([]byte, error) VerifySignatureShare(index uint16, sig []byte, msg []byte, epoch uint32) error diff --git a/consensus/mock/consensusDataContainerMock.go b/consensus/mock/consensusDataContainerMock.go index eb93be5e2eb..b408040d8f2 100644 --- a/consensus/mock/consensusDataContainerMock.go +++ b/consensus/mock/consensusDataContainerMock.go @@ -4,7 +4,6 @@ import ( "github.com/ElrondNetwork/elrond-go-core/data" "github.com/ElrondNetwork/elrond-go-core/hashing" "github.com/ElrondNetwork/elrond-go-core/marshal" - crypto "github.com/ElrondNetwork/elrond-go-crypto" cryptoCommon "github.com/ElrondNetwork/elrond-go/common/crypto" "github.com/ElrondNetwork/elrond-go/consensus" "github.com/ElrondNetwork/elrond-go/epochStart" @@ -24,8 +23,6 @@ type ConsensusCoreMock struct { chronologyHandler consensus.ChronologyHandler hasher hashing.Hasher marshalizer marshal.Marshalizer - blsPrivateKey crypto.PrivateKey - blsSingleSigner crypto.SingleSigner multiSignerContainer cryptoCommon.MultiSignerContainer roundHandler consensus.RoundHandler shardCoordinator sharding.Coordinator @@ -121,11 +118,6 @@ func (ccm *ConsensusCoreMock) SetBlockchain(blockChain data.ChainHandler) { ccm.blockChain = blockChain } -// SetSingleSigner - -func (ccm *ConsensusCoreMock) SetSingleSigner(signer crypto.SingleSigner) { - ccm.blsSingleSigner = signer -} - // SetBlockProcessor - func (ccm *ConsensusCoreMock) SetBlockProcessor(blockProcessor process.BlockProcessor) { ccm.blockProcessor = blockProcessor @@ -181,16 +173,6 @@ func (ccm *ConsensusCoreMock) SetValidatorGroupSelector(validatorGroupSelector n ccm.validatorGroupSelector = validatorGroupSelector } -// PrivateKey - -func (ccm *ConsensusCoreMock) PrivateKey() crypto.PrivateKey { - return ccm.blsPrivateKey -} - -// SingleSigner returns the bls single signer stored in the ConsensusStore -func (ccm *ConsensusCoreMock) SingleSigner() crypto.SingleSigner { - return ccm.blsSingleSigner -} - // PeerHonestyHandler - func (ccm *ConsensusCoreMock) PeerHonestyHandler() consensus.PeerHonestyHandler { return ccm.peerHonestyHandler diff --git a/consensus/mock/mockTestInitializer.go b/consensus/mock/mockTestInitializer.go index ce275ac47d8..415f18276fd 100644 --- a/consensus/mock/mockTestInitializer.go +++ b/consensus/mock/mockTestInitializer.go @@ -171,12 +171,6 @@ func InitConsensusCoreWithMultiSigner(multiSigner crypto.MultiSigner) *Consensus chronologyHandlerMock := InitChronologyHandlerMock() hasherMock := &hashingMocks.HasherMock{} marshalizerMock := MarshalizerMock{} - blsPrivateKeyMock := &PrivateKeyMock{} - blsSingleSignerMock := &SingleSignerMock{ - SignStub: func(private crypto.PrivateKey, msg []byte) (bytes []byte, e error) { - return make([]byte, 0), nil - }, - } roundHandlerMock := &RoundHandlerMock{} shardCoordinatorMock := ShardCoordinatorMock{} syncTimerMock := &SyncTimerMock{} @@ -216,8 +210,6 @@ func InitConsensusCoreWithMultiSigner(multiSigner crypto.MultiSigner) *Consensus chronologyHandler: chronologyHandlerMock, hasher: hasherMock, marshalizer: marshalizerMock, - blsPrivateKey: blsPrivateKeyMock, - blsSingleSigner: blsSingleSignerMock, multiSignerContainer: multiSignerContainer, roundHandler: roundHandlerMock, shardCoordinator: shardCoordinatorMock, diff --git a/consensus/mock/signatureHandlerStub.go b/consensus/mock/signatureHandlerStub.go index c1d25eb0195..3ece94f9412 100644 --- a/consensus/mock/signatureHandlerStub.go +++ b/consensus/mock/signatureHandlerStub.go @@ -2,15 +2,15 @@ package mock // SignatureHandlerStub implements SignatureHandler interface type SignatureHandlerStub struct { - ResetCalled func(pubKeys []string) error - CreateSignatureShareCalled func(msg []byte, index uint16, epoch uint32) ([]byte, error) - CreateSignatureShareWithPrivateKeyCalled func(message []byte, index uint16, epoch uint32, privateKeyBytes []byte) ([]byte, error) - StoreSignatureShareCalled func(index uint16, sig []byte) error - SignatureShareCalled func(index uint16) ([]byte, error) - VerifySignatureShareCalled func(index uint16, sig []byte, msg []byte, epoch uint32) error - AggregateSigsCalled func(bitmap []byte, epoch uint32) ([]byte, error) - SetAggregatedSigCalled func(_ []byte) error - VerifyCalled func(msg []byte, bitmap []byte, epoch uint32) error + ResetCalled func(pubKeys []string) error + CreateSignatureShareForPublicKeyCalled func(message []byte, index uint16, epoch uint32, publicKeyBytes []byte) ([]byte, error) + CreateSignatureForPublicKeyCalled func(message []byte, publicKeyBytes []byte) ([]byte, error) + StoreSignatureShareCalled func(index uint16, sig []byte) error + SignatureShareCalled func(index uint16) ([]byte, error) + VerifySignatureShareCalled func(index uint16, sig []byte, msg []byte, epoch uint32) error + AggregateSigsCalled func(bitmap []byte, epoch uint32) ([]byte, error) + SetAggregatedSigCalled func(_ []byte) error + VerifyCalled func(msg []byte, bitmap []byte, epoch uint32) error } // Reset - @@ -22,19 +22,19 @@ func (stub *SignatureHandlerStub) Reset(pubKeys []string) error { return nil } -// CreateSignatureShare - -func (stub *SignatureHandlerStub) CreateSignatureShare(msg []byte, index uint16, epoch uint32) ([]byte, error) { - if stub.CreateSignatureShareCalled != nil { - return stub.CreateSignatureShareCalled(msg, index, epoch) +// CreateSignatureShareForPublicKey - +func (stub *SignatureHandlerStub) CreateSignatureShareForPublicKey(message []byte, index uint16, epoch uint32, publicKeyBytes []byte) ([]byte, error) { + if stub.CreateSignatureShareForPublicKeyCalled != nil { + return stub.CreateSignatureShareForPublicKeyCalled(message, index, epoch, publicKeyBytes) } - return []byte("sigShare"), nil + return make([]byte, 0), nil } -// CreateSignatureShareWithPrivateKey - -func (stub *SignatureHandlerStub) CreateSignatureShareWithPrivateKey(message []byte, index uint16, epoch uint32, privateKeyBytes []byte) ([]byte, error) { - if stub.CreateSignatureShareWithPrivateKeyCalled != nil { - return stub.CreateSignatureShareWithPrivateKeyCalled(message, index, epoch, privateKeyBytes) +// CreateSignatureForPublicKey - +func (stub *SignatureHandlerStub) CreateSignatureForPublicKey(message []byte, publicKeyBytes []byte) ([]byte, error) { + if stub.CreateSignatureForPublicKeyCalled != nil { + return stub.CreateSignatureForPublicKeyCalled(message, publicKeyBytes) } return make([]byte, 0), nil diff --git a/consensus/signing/errors.go b/consensus/signing/errors.go index 01af6e1773b..0a18c55e25f 100644 --- a/consensus/signing/errors.go +++ b/consensus/signing/errors.go @@ -8,14 +8,11 @@ var ErrInvalidSignature = errors.New("invalid signature was provided") // ErrNilElement is raised when searching for a specific element but found nil var ErrNilElement = errors.New("element is nil") -// ErrIndexNotSelected is raised when a not selected index is used for multi-signing -var ErrIndexNotSelected = errors.New("index is not selected") - // ErrNilBitmap is raised when a nil bitmap is used var ErrNilBitmap = errors.New("bitmap is nil") -// ErrNoPrivateKeySet is raised when no private key was set -var ErrNoPrivateKeySet = errors.New("no private key was set") +// ErrNilKeysHandler is raised when a nil keys handler was provided +var ErrNilKeysHandler = errors.New("nil keys handler") // ErrNoPublicKeySet is raised when no public key was set for a multisignature var ErrNoPublicKeySet = errors.New("no public key was set") @@ -29,6 +26,9 @@ var ErrNilPublicKeys = errors.New("public keys are nil") // ErrNilMultiSignerContainer is raised when a nil multi signer container has been provided var ErrNilMultiSignerContainer = errors.New("multi signer container is nil") +// ErrNilSingleSigner signals that a nil single signer was provided +var ErrNilSingleSigner = errors.New("nil single signer") + // ErrIndexOutOfBounds is raised when an out of bound index is used var ErrIndexOutOfBounds = errors.New("index is out of bounds") diff --git a/consensus/signing/signing.go b/consensus/signing/signing.go index 3a84c841754..13d204adc31 100644 --- a/consensus/signing/signing.go +++ b/consensus/signing/signing.go @@ -6,19 +6,20 @@ import ( "github.com/ElrondNetwork/elrond-go-core/core/check" crypto "github.com/ElrondNetwork/elrond-go-crypto" cryptoCommon "github.com/ElrondNetwork/elrond-go/common/crypto" + "github.com/ElrondNetwork/elrond-go/consensus" ) // ArgsSignatureHolder defines the arguments needed to create a new signature holder component type ArgsSignatureHolder struct { PubKeys []string - PrivKeyBytes []byte MultiSignerContainer cryptoCommon.MultiSignerContainer + SingleSigner crypto.SingleSigner KeyGenerator crypto.KeyGenerator + KeysHandler consensus.KeysHandler } type signatureHolderData struct { pubKeys [][]byte - privKey []byte sigShares [][]byte aggSig []byte } @@ -27,7 +28,9 @@ type signatureHolder struct { data *signatureHolderData mutSigningData sync.RWMutex multiSignerContainer cryptoCommon.MultiSignerContainer + singleSigner crypto.SingleSigner keyGen crypto.KeyGenerator + keysHandler consensus.KeysHandler } // NewSignatureHolder will create a new signature holder component @@ -47,7 +50,6 @@ func NewSignatureHolder(args ArgsSignatureHolder) (*signatureHolder, error) { data := &signatureHolderData{ pubKeys: pubKeysBytes, - privKey: args.PrivKeyBytes, sigShares: sigShares, } @@ -55,7 +57,9 @@ func NewSignatureHolder(args ArgsSignatureHolder) (*signatureHolder, error) { data: data, mutSigningData: sync.RWMutex{}, multiSignerContainer: args.MultiSignerContainer, + singleSigner: args.SingleSigner, keyGen: args.KeyGenerator, + keysHandler: args.KeysHandler, }, nil } @@ -63,8 +67,11 @@ func checkArgs(args ArgsSignatureHolder) error { if check.IfNil(args.MultiSignerContainer) { return ErrNilMultiSignerContainer } - if len(args.PrivKeyBytes) == 0 { - return ErrNoPrivateKeySet + if check.IfNil(args.SingleSigner) { + return ErrNilSingleSigner + } + if check.IfNil(args.KeysHandler) { + return ErrNilKeysHandler } if check.IfNil(args.KeyGenerator) { return ErrNilKeyGenerator @@ -78,14 +85,11 @@ func checkArgs(args ArgsSignatureHolder) error { // Create generates a signature holder component and initializes corresponding fields func (sh *signatureHolder) Create(pubKeys []string) (*signatureHolder, error) { - sh.mutSigningData.RLock() - privKey := sh.data.privKey - sh.mutSigningData.RUnlock() - args := ArgsSignatureHolder{ PubKeys: pubKeys, - PrivKeyBytes: privKey, + KeysHandler: sh.keysHandler, MultiSignerContainer: sh.multiSignerContainer, + SingleSigner: sh.singleSigner, KeyGenerator: sh.keyGen, } return NewSignatureHolder(args) @@ -107,11 +111,8 @@ func (sh *signatureHolder) Reset(pubKeys []string) error { sh.mutSigningData.Lock() defer sh.mutSigningData.Unlock() - privKey := sh.data.privKey - data := &signatureHolderData{ pubKeys: pubKeysBytes, - privKey: privKey, sigShares: sigShares, } @@ -120,21 +121,19 @@ func (sh *signatureHolder) Reset(pubKeys []string) error { return nil } -// CreateSignatureShare returns a signature over a message -func (sh *signatureHolder) CreateSignatureShare(message []byte, selfIndex uint16, epoch uint32) ([]byte, error) { - sh.mutSigningData.RLock() - privateKeyBytes := sh.data.privKey - sh.mutSigningData.RUnlock() - - return sh.CreateSignatureShareWithPrivateKey(message, selfIndex, epoch, privateKeyBytes) -} - -// CreateSignatureShareWithPrivateKey returns a signature over a message providing the private key bytes -func (sh *signatureHolder) CreateSignatureShareWithPrivateKey(message []byte, index uint16, epoch uint32, privateKeyBytes []byte) ([]byte, error) { +// CreateSignatureShareForPublicKey returns a signature over a message using the managed private key that was selected based on the provided +// publicKeyBytes argument +func (sh *signatureHolder) CreateSignatureShareForPublicKey(message []byte, index uint16, epoch uint32, publicKeyBytes []byte) ([]byte, error) { if message == nil { return nil, ErrNilMessage } + privateKey := sh.keysHandler.GetHandledPrivateKey(publicKeyBytes) + privateKeyBytes, err := privateKey.ToByteArray() + if err != nil { + return nil, err + } + sh.mutSigningData.Lock() defer sh.mutSigningData.Unlock() @@ -153,6 +152,14 @@ func (sh *signatureHolder) CreateSignatureShareWithPrivateKey(message []byte, in return sigShareBytes, nil } +// CreateSignatureForPublicKey returns a signature over a message using the managed private key that was selected based on the provided +// publicKeyBytes argument +func (sh *signatureHolder) CreateSignatureForPublicKey(message []byte, publicKeyBytes []byte) ([]byte, error) { + privateKey := sh.keysHandler.GetHandledPrivateKey(publicKeyBytes) + + return sh.singleSigner.Sign(privateKey, message) +} + // VerifySignatureShare will verify the signature share based on the specified index func (sh *signatureHolder) VerifySignatureShare(index uint16, sig []byte, message []byte, epoch uint32) error { if len(sig) == 0 { diff --git a/consensus/signing/signing_test.go b/consensus/signing/signing_test.go index 304361ee909..70d637d69af 100644 --- a/consensus/signing/signing_test.go +++ b/consensus/signing/signing_test.go @@ -7,16 +7,19 @@ import ( "github.com/ElrondNetwork/elrond-go-core/core/check" crypto "github.com/ElrondNetwork/elrond-go-crypto" "github.com/ElrondNetwork/elrond-go/consensus/signing" + "github.com/ElrondNetwork/elrond-go/testscommon" "github.com/ElrondNetwork/elrond-go/testscommon/cryptoMocks" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func createMockArgsSignatureHolder() signing.ArgsSignatureHolder { return signing.ArgsSignatureHolder{ PubKeys: []string{"pubkey1"}, - PrivKeyBytes: []byte("privKey"), + KeysHandler: &testscommon.KeysHandlerStub{}, MultiSignerContainer: &cryptoMocks.MultiSignerContainerMock{}, KeyGenerator: &cryptoMocks.KeyGenStub{}, + SingleSigner: &cryptoMocks.SingleSignerStub{}, } } @@ -33,7 +36,16 @@ func TestNewSigner(t *testing.T) { require.Nil(t, signer) require.Equal(t, signing.ErrNilMultiSignerContainer, err) }) + t.Run("nil single signer", func(t *testing.T) { + t.Parallel() + + args := createMockArgsSignatureHolder() + args.SingleSigner = nil + signer, err := signing.NewSignatureHolder(args) + require.Nil(t, signer) + require.Equal(t, signing.ErrNilSingleSigner, err) + }) t.Run("nil key generator", func(t *testing.T) { t.Parallel() @@ -44,18 +56,16 @@ func TestNewSigner(t *testing.T) { require.Nil(t, signer) require.Equal(t, signing.ErrNilKeyGenerator, err) }) - - t.Run("nil private key", func(t *testing.T) { + t.Run("nil keys handler", func(t *testing.T) { t.Parallel() args := createMockArgsSignatureHolder() - args.PrivKeyBytes = nil + args.KeysHandler = nil signer, err := signing.NewSignatureHolder(args) require.Nil(t, signer) - require.Equal(t, signing.ErrNoPrivateKeySet, err) + require.Equal(t, signing.ErrNilKeysHandler, err) }) - t.Run("no public keys", func(t *testing.T) { t.Parallel() @@ -66,7 +76,6 @@ func TestNewSigner(t *testing.T) { require.Nil(t, signer) require.Equal(t, signing.ErrNoPublicKeySet, err) }) - t.Run("should work", func(t *testing.T) { t.Parallel() @@ -145,21 +154,21 @@ func TestSignatureHolder_Reset(t *testing.T) { }) } -func TestSignatureHolder_CreateSignatureShare(t *testing.T) { +func TestSignatureHolder_CreateSignatureShareForPublicKey(t *testing.T) { t.Parallel() selfIndex := uint16(0) epoch := uint32(0) + pkBytes := []byte("public key bytes") t.Run("nil message", func(t *testing.T) { t.Parallel() signer, _ := signing.NewSignatureHolder(createMockArgsSignatureHolder()) - sigShare, err := signer.CreateSignatureShare(nil, selfIndex, epoch) + sigShare, err := signer.CreateSignatureShareForPublicKey(nil, selfIndex, epoch, pkBytes) require.Nil(t, sigShare) require.Equal(t, signing.ErrNilMessage, err) }) - t.Run("create sig share failed", func(t *testing.T) { t.Parallel() @@ -174,15 +183,33 @@ func TestSignatureHolder_CreateSignatureShare(t *testing.T) { args.MultiSignerContainer = cryptoMocks.NewMultiSignerContainerMock(multiSigner) signer, _ := signing.NewSignatureHolder(args) - sigShare, err := signer.CreateSignatureShare([]byte("msg1"), selfIndex, epoch) + sigShare, err := signer.CreateSignatureShareForPublicKey([]byte("msg1"), selfIndex, epoch, pkBytes) require.Nil(t, sigShare) require.Equal(t, expectedErr, err) }) + t.Run("failed to get current multi signer", func(t *testing.T) { + t.Parallel() + + args := createMockArgsSignatureHolder() + + expectedErr := errors.New("expected error") + args.MultiSignerContainer = &cryptoMocks.MultiSignerContainerStub{ + GetMultiSignerCalled: func(epoch uint32) (crypto.MultiSigner, error) { + return nil, expectedErr + }, + } + + signer, _ := signing.NewSignatureHolder(args) + sigShare, err := signer.CreateSignatureShareForPublicKey([]byte("message"), uint16(0), epoch, pkBytes) + require.Nil(t, sigShare) + require.Equal(t, expectedErr, err) + }) t.Run("should work", func(t *testing.T) { t.Parallel() args := createMockArgsSignatureHolder() + getHandledPrivateKeyCalled := false expectedSigShare := []byte("sigShare") multiSigner := &cryptoMocks.MultiSignerStub{ @@ -190,12 +217,21 @@ func TestSignatureHolder_CreateSignatureShare(t *testing.T) { return expectedSigShare, nil }, } + args.KeysHandler = &testscommon.KeysHandlerStub{ + GetHandledPrivateKeyCalled: func(providedPkBytes []byte) crypto.PrivateKey { + assert.Equal(t, pkBytes, providedPkBytes) + getHandledPrivateKeyCalled = true + + return &cryptoMocks.PrivateKeyStub{} + }, + } args.MultiSignerContainer = cryptoMocks.NewMultiSignerContainerMock(multiSigner) signer, _ := signing.NewSignatureHolder(args) - sigShare, err := signer.CreateSignatureShare([]byte("msg1"), selfIndex, epoch) + sigShare, err := signer.CreateSignatureShareForPublicKey([]byte("msg1"), selfIndex, epoch, pkBytes) require.Nil(t, err) require.Equal(t, expectedSigShare, sigShare) + assert.True(t, getHandledPrivateKeyCalled) }) } @@ -285,36 +321,16 @@ func TestSignatureHolder_StoreSignatureShare(t *testing.T) { t.Parallel() ownIndex := uint16(2) - epoch := uint32(0) - msg := []byte("message") t.Run("index out of bounds", func(t *testing.T) { t.Parallel() - signer, _ := signing.NewSignatureHolder(createMockArgsSignatureHolder()) - err := signer.StoreSignatureShare(uint16(2), []byte("sigShare")) - require.Equal(t, signing.ErrIndexOutOfBounds, err) - }) - - t.Run("failed to get current multi signer", func(t *testing.T) { - t.Parallel() - - args := createMockArgsSignatureHolder() - - expectedErr := errors.New("expected error") - args.MultiSignerContainer = &cryptoMocks.MultiSignerContainerStub{ - GetMultiSignerCalled: func(epoch uint32) (crypto.MultiSigner, error) { - return nil, expectedErr - }, - } - - signer, _ := signing.NewSignatureHolder(args) + signer, err := signing.NewSignatureHolder(createMockArgsSignatureHolder()) + require.Nil(t, err) - sigShare, err := signer.CreateSignatureShare(msg, uint16(0), epoch) - require.Nil(t, sigShare) - require.Equal(t, expectedErr, err) + err = signer.StoreSignatureShare(uint16(2), []byte("sigShare")) + require.Equal(t, signing.ErrIndexOutOfBounds, err) }) - t.Run("should work", func(t *testing.T) { t.Parallel() @@ -330,10 +346,9 @@ func TestSignatureHolder_StoreSignatureShare(t *testing.T) { signer, _ := signing.NewSignatureHolder(args) - sigShare, err := signer.CreateSignatureShare(msg, uint16(0), epoch) - require.Nil(t, err) + sigShare := []byte("signature share") - err = signer.StoreSignatureShare(ownIndex, sigShare) + err := signer.StoreSignatureShare(ownIndex, sigShare) require.Nil(t, err) sigShareRead, err := signer.SignatureShare(ownIndex) @@ -620,3 +635,32 @@ func TestSignatureHolder_Verify(t *testing.T) { require.Nil(t, err) }) } + +func TestSignatureHolder_CreateSignatureForPublicKey(t *testing.T) { + t.Parallel() + + args := createMockArgsSignatureHolder() + getHandledPrivateKeyCalled := false + pkBytes := []byte("public key bytes") + + expectedSigShare := []byte("sigShare") + args.KeysHandler = &testscommon.KeysHandlerStub{ + GetHandledPrivateKeyCalled: func(providedPkBytes []byte) crypto.PrivateKey { + assert.Equal(t, pkBytes, providedPkBytes) + getHandledPrivateKeyCalled = true + + return &cryptoMocks.PrivateKeyStub{} + }, + } + args.SingleSigner = &cryptoMocks.SingleSignerStub{ + SignCalled: func(private crypto.PrivateKey, msg []byte) ([]byte, error) { + return expectedSigShare, nil + }, + } + + signer, _ := signing.NewSignatureHolder(args) + sigShare, err := signer.CreateSignatureForPublicKey([]byte("msg1"), pkBytes) + require.Nil(t, err) + require.Equal(t, expectedSigShare, sigShare) + assert.True(t, getHandledPrivateKeyCalled) +} diff --git a/consensus/spos/bls/blsSubroundsFactory.go b/consensus/spos/bls/blsSubroundsFactory.go index 4cd32d5ac10..9b68fccc61f 100644 --- a/consensus/spos/bls/blsSubroundsFactory.go +++ b/consensus/spos/bls/blsSubroundsFactory.go @@ -139,7 +139,7 @@ func (fct *factory) generateStartRoundSubround() error { return err } - subroundStartRound, err := NewSubroundStartRound( + subroundStartRoundInstance, err := NewSubroundStartRound( subround, fct.worker.Extend, processingThresholdPercent, @@ -150,12 +150,12 @@ func (fct *factory) generateStartRoundSubround() error { return err } - err = subroundStartRound.SetOutportHandler(fct.outportHandler) + err = subroundStartRoundInstance.SetOutportHandler(fct.outportHandler) if err != nil { return err } - fct.consensusCore.Chronology().AddSubround(subroundStartRound) + fct.consensusCore.Chronology().AddSubround(subroundStartRoundInstance) return nil } @@ -180,7 +180,7 @@ func (fct *factory) generateBlockSubround() error { return err } - subroundBlock, err := NewSubroundBlock( + subroundBlockInstance, err := NewSubroundBlock( subround, fct.worker.Extend, processingThresholdPercent, @@ -189,10 +189,10 @@ func (fct *factory) generateBlockSubround() error { return err } - fct.worker.AddReceivedMessageCall(MtBlockBodyAndHeader, subroundBlock.receivedBlockBodyAndHeader) - fct.worker.AddReceivedMessageCall(MtBlockBody, subroundBlock.receivedBlockBody) - fct.worker.AddReceivedMessageCall(MtBlockHeader, subroundBlock.receivedBlockHeader) - fct.consensusCore.Chronology().AddSubround(subroundBlock) + fct.worker.AddReceivedMessageCall(MtBlockBodyAndHeader, subroundBlockInstance.receivedBlockBodyAndHeader) + fct.worker.AddReceivedMessageCall(MtBlockBody, subroundBlockInstance.receivedBlockBody) + fct.worker.AddReceivedMessageCall(MtBlockHeader, subroundBlockInstance.receivedBlockHeader) + fct.consensusCore.Chronology().AddSubround(subroundBlockInstance) return nil } diff --git a/consensus/spos/bls/subroundBlock.go b/consensus/spos/bls/subroundBlock.go index 30c341203dc..463e26f684c 100644 --- a/consensus/spos/bls/subroundBlock.go +++ b/consensus/spos/bls/subroundBlock.go @@ -338,8 +338,7 @@ func (sr *subroundBlock) createHeader() (data.HeaderHandler, error) { return nil, errGetLeader } - privateKey := sr.GetMessageSigningPrivateKey([]byte(leader)) - randSeed, err := sr.SingleSigner().Sign(privateKey, prevRandSeed) + randSeed, err := sr.SignatureHandler().CreateSignatureForPublicKey(prevRandSeed, []byte(leader)) if err != nil { return nil, err } diff --git a/consensus/spos/bls/subroundBlock_test.go b/consensus/spos/bls/subroundBlock_test.go index ed6f7139f4a..845fde10d69 100644 --- a/consensus/spos/bls/subroundBlock_test.go +++ b/consensus/spos/bls/subroundBlock_test.go @@ -866,8 +866,6 @@ func TestSubroundBlock_CreateHeaderNilCurrentHeader(t *testing.T) { _ = sr.SendBlockBody(body, marshalizedBody) _ = sr.SendBlockHeader(header, marshalizedHeader) - oldRand := sr.BlockChain().GetGenesisHeader().GetRandSeed() - newRand, _ := sr.SingleSigner().Sign(sr.PrivateKey(), oldRand) expectedHeader, _ := container.BlockProcessor().CreateNewHeader(uint64(sr.RoundHandler().Index()), uint64(1)) err := expectedHeader.SetTimeStamp(uint64(sr.RoundHandler().TimeStamp().Unix())) require.Nil(t, err) @@ -877,7 +875,7 @@ func TestSubroundBlock_CreateHeaderNilCurrentHeader(t *testing.T) { require.Nil(t, err) err = expectedHeader.SetPrevRandSeed(sr.BlockChain().GetGenesisHeader().GetRandSeed()) require.Nil(t, err) - err = expectedHeader.SetRandSeed(newRand) + err = expectedHeader.SetRandSeed(make([]byte, 0)) require.Nil(t, err) err = expectedHeader.SetMiniBlockHeaderHandlers(header.GetMiniBlockHeaderHandlers()) require.Nil(t, err) @@ -902,8 +900,6 @@ func TestSubroundBlock_CreateHeaderNotNilCurrentHeader(t *testing.T) { _ = sr.SendBlockBody(body, marshalizedBody) _ = sr.SendBlockHeader(header, marshalizedHeader) - oldRand := sr.BlockChain().GetGenesisHeader().GetRandSeed() - newRand, _ := sr.SingleSigner().Sign(sr.PrivateKey(), oldRand) expectedHeader, _ := container.BlockProcessor().CreateNewHeader( uint64(sr.RoundHandler().Index()), sr.BlockChain().GetCurrentBlockHeader().GetNonce()+1) @@ -913,7 +909,7 @@ func TestSubroundBlock_CreateHeaderNotNilCurrentHeader(t *testing.T) { require.Nil(t, err) err = expectedHeader.SetPrevHash(sr.BlockChain().GetCurrentBlockHeaderHash()) require.Nil(t, err) - err = expectedHeader.SetRandSeed(newRand) + err = expectedHeader.SetRandSeed(make([]byte, 0)) require.Nil(t, err) err = expectedHeader.SetMiniBlockHeaderHandlers(header.GetMiniBlockHeaderHandlers()) require.Nil(t, err) @@ -955,15 +951,13 @@ func TestSubroundBlock_CreateHeaderMultipleMiniBlocks(t *testing.T) { _ = sr.SendBlockBody(body, marshalizedBody) _ = sr.SendBlockHeader(header, marshalizedHeader) - oldRand := sr.BlockChain().GetCurrentBlockHeader().GetRandSeed() - newRand, _ := sr.SingleSigner().Sign(sr.PrivateKey(), oldRand) expectedHeader := &block.Header{ Round: uint64(sr.RoundHandler().Index()), TimeStamp: uint64(sr.RoundHandler().TimeStamp().Unix()), RootHash: []byte{}, Nonce: sr.BlockChain().GetCurrentBlockHeader().GetNonce() + 1, PrevHash: sr.BlockChain().GetCurrentBlockHeaderHash(), - RandSeed: newRand, + RandSeed: make([]byte, 0), MiniBlockHeaders: mbHeaders, ChainID: chainID, } diff --git a/consensus/spos/bls/subroundEndRound.go b/consensus/spos/bls/subroundEndRound.go index eb6317d5a0a..decd3ec7508 100644 --- a/consensus/spos/bls/subroundEndRound.go +++ b/consensus/spos/bls/subroundEndRound.go @@ -500,9 +500,7 @@ func (sr *subroundEndRound) signBlockHeader() ([]byte, error) { return nil, errGetLeader } - privateKey := sr.GetMessageSigningPrivateKey([]byte(leader)) - - return sr.SingleSigner().Sign(privateKey, marshalizedHdr) + return sr.SignatureHandler().CreateSignatureForPublicKey(marshalizedHdr, []byte(leader)) } func (sr *subroundEndRound) updateMetricsForLeader() { diff --git a/consensus/spos/bls/subroundEndRound_test.go b/consensus/spos/bls/subroundEndRound_test.go index f3045ca40d3..1dd9153ce2f 100644 --- a/consensus/spos/bls/subroundEndRound_test.go +++ b/consensus/spos/bls/subroundEndRound_test.go @@ -539,14 +539,14 @@ func TestSubroundEndRound_CheckIfSignatureIsFilled(t *testing.T) { expectedSignature := []byte("signature") container := mock.InitConsensusCore() - singleSigner := &mock.SingleSignerMock{ - SignStub: func(private crypto.PrivateKey, msg []byte) ([]byte, error) { + signatureHandler := &mock.SignatureHandlerStub{ + CreateSignatureForPublicKeyCalled: func(publicKeyBytes []byte, msg []byte) ([]byte, error) { var receivedHdr block.Header _ = container.Marshalizer().Unmarshal(&receivedHdr, msg) return expectedSignature, nil }, } - container.SetSingleSigner(singleSigner) + container.SetSignatureHandler(signatureHandler) bm := &mock.BroadcastMessengerMock{ BroadcastBlockCalled: func(handler data.BodyHandler, handler2 data.HeaderHandler) error { return errors.New("error") diff --git a/consensus/spos/bls/subroundSignature.go b/consensus/spos/bls/subroundSignature.go index 7f736ebfa91..9e11787d505 100644 --- a/consensus/spos/bls/subroundSignature.go +++ b/consensus/spos/bls/subroundSignature.go @@ -82,9 +82,14 @@ func (sr *subroundSignature) doSignatureJob(_ context.Context) bool { return false } - signatureShare, err := sr.SignatureHandler().CreateSignatureShare(sr.GetData(), uint16(selfIndex), sr.Header.GetEpoch()) + signatureShare, err := sr.SignatureHandler().CreateSignatureShareForPublicKey( + sr.GetData(), + uint16(selfIndex), + sr.Header.GetEpoch(), + []byte(sr.SelfPubKey()), + ) if err != nil { - log.Debug("doSignatureJob.CreateSignatureShare", "error", err.Error()) + log.Debug("doSignatureJob.CreateSignatureShareForPublicKey", "error", err.Error()) return false } @@ -362,23 +367,20 @@ func (sr *subroundSignature) doSignatureJobForManagedKeys() bool { continue } - managedPrivateKey := sr.GetMessageSigningPrivateKey(pkBytes) selfIndex, err := sr.ConsensusGroupIndex(pk) if err != nil { log.Warn("doSignatureJobForManagedKeys: index not found", "pk", pkBytes) continue } - // TODO EN-13215 merge SignatureHandler with KeysHandler - managedPrivateKeyBytes, err := managedPrivateKey.ToByteArray() - if err != nil { - log.Warn("doSignatureJobForManagedKeys: can not recover the private key bytes", "pk", pkBytes) - continue - } - - signatureShare, err := sr.SignatureHandler().CreateSignatureShareWithPrivateKey(sr.GetData(), uint16(selfIndex), sr.Header.GetEpoch(), managedPrivateKeyBytes) + signatureShare, err := sr.SignatureHandler().CreateSignatureShareForPublicKey( + sr.GetData(), + uint16(selfIndex), + sr.Header.GetEpoch(), + pkBytes, + ) if err != nil { - log.Debug("doSignatureJobForManagedKeys.CreateAndAddSignatureShareForKey", "error", err.Error()) + log.Debug("doSignatureJobForManagedKeys.CreateSignatureShareForPublicKey", "error", err.Error()) return false } diff --git a/consensus/spos/bls/subroundSignature_test.go b/consensus/spos/bls/subroundSignature_test.go index f569edf49af..4d5da6723d1 100644 --- a/consensus/spos/bls/subroundSignature_test.go +++ b/consensus/spos/bls/subroundSignature_test.go @@ -278,7 +278,7 @@ func TestSubroundSignature_DoSignatureJob(t *testing.T) { err := errors.New("create signature share error") signatureHandler := &mock.SignatureHandlerStub{ - CreateSignatureShareCalled: func(msg []byte, index uint16, epoch uint32) ([]byte, error) { + CreateSignatureShareForPublicKeyCalled: func(msg []byte, index uint16, epoch uint32, publicKeyBytes []byte) ([]byte, error) { return nil, err }, } @@ -288,7 +288,7 @@ func TestSubroundSignature_DoSignatureJob(t *testing.T) { assert.False(t, r) signatureHandler = &mock.SignatureHandlerStub{ - CreateSignatureShareCalled: func(msg []byte, index uint16, epoch uint32) ([]byte, error) { + CreateSignatureShareForPublicKeyCalled: func(msg []byte, index uint16, epoch uint32, publicKeyBytes []byte) ([]byte, error) { return []byte("SIG"), nil }, } diff --git a/consensus/spos/consensusCore.go b/consensus/spos/consensusCore.go index dac0c8aa7c6..7b40a13e3b8 100644 --- a/consensus/spos/consensusCore.go +++ b/consensus/spos/consensusCore.go @@ -4,7 +4,6 @@ import ( "github.com/ElrondNetwork/elrond-go-core/data" "github.com/ElrondNetwork/elrond-go-core/hashing" "github.com/ElrondNetwork/elrond-go-core/marshal" - crypto "github.com/ElrondNetwork/elrond-go-crypto" cryptoCommon "github.com/ElrondNetwork/elrond-go/common/crypto" "github.com/ElrondNetwork/elrond-go/consensus" "github.com/ElrondNetwork/elrond-go/epochStart" @@ -24,8 +23,6 @@ type ConsensusCore struct { chronologyHandler consensus.ChronologyHandler hasher hashing.Hasher marshalizer marshal.Marshalizer - blsPrivateKey crypto.PrivateKey - blsSingleSigner crypto.SingleSigner multiSignerContainer cryptoCommon.MultiSignerContainer roundHandler consensus.RoundHandler shardCoordinator sharding.Coordinator @@ -50,8 +47,6 @@ type ConsensusCoreArgs struct { ChronologyHandler consensus.ChronologyHandler Hasher hashing.Hasher Marshalizer marshal.Marshalizer - BlsPrivateKey crypto.PrivateKey - BlsSingleSigner crypto.SingleSigner MultiSignerContainer cryptoCommon.MultiSignerContainer RoundHandler consensus.RoundHandler ShardCoordinator sharding.Coordinator @@ -79,8 +74,6 @@ func NewConsensusCore( chronologyHandler: args.ChronologyHandler, hasher: args.Hasher, marshalizer: args.Marshalizer, - blsPrivateKey: args.BlsPrivateKey, - blsSingleSigner: args.BlsSingleSigner, multiSignerContainer: args.MultiSignerContainer, roundHandler: args.RoundHandler, shardCoordinator: args.ShardCoordinator, @@ -174,16 +167,6 @@ func (cc *ConsensusCore) EpochStartRegistrationHandler() epochStart.Registration return cc.epochStartRegistrationHandler } -// PrivateKey returns the BLS private key stored in the ConsensusStore -func (cc *ConsensusCore) PrivateKey() crypto.PrivateKey { - return cc.blsPrivateKey -} - -// SingleSigner returns the bls single signer stored in the ConsensusStore -func (cc *ConsensusCore) SingleSigner() crypto.SingleSigner { - return cc.blsSingleSigner -} - // PeerHonestyHandler will return the peer honesty handler which will be used in subrounds func (cc *ConsensusCore) PeerHonestyHandler() consensus.PeerHonestyHandler { return cc.peerHonestyHandler diff --git a/consensus/spos/consensusCoreValidator.go b/consensus/spos/consensusCoreValidator.go index da0a7f41438..5583450f11f 100644 --- a/consensus/spos/consensusCoreValidator.go +++ b/consensus/spos/consensusCoreValidator.go @@ -47,12 +47,6 @@ func ValidateConsensusCore(container ConsensusCoreHandler) error { if check.IfNil(container.NodesCoordinator()) { return ErrNilNodesCoordinator } - if check.IfNil(container.PrivateKey()) { - return ErrNilBlsPrivateKey - } - if check.IfNil(container.SingleSigner()) { - return ErrNilBlsSingleSigner - } if check.IfNil(container.GetAntiFloodHandler()) { return ErrNilAntifloodHandler } diff --git a/consensus/spos/consensusCoreValidator_test.go b/consensus/spos/consensusCoreValidator_test.go index e5e2b69d096..96ae2e32e77 100644 --- a/consensus/spos/consensusCoreValidator_test.go +++ b/consensus/spos/consensusCoreValidator_test.go @@ -17,8 +17,6 @@ func initConsensusDataContainer() *ConsensusCore { bootstrapperMock := &mock.BootstrapperStub{} broadcastMessengerMock := &mock.BroadcastMessengerMock{} chronologyHandlerMock := mock.InitChronologyHandlerMock() - blsPrivateKeyMock := &mock.PrivateKeyMock{} - blsSingleSignerMock := &mock.SingleSignerMock{} multiSignerMock := cryptoMocks.NewMultiSigner() hasherMock := &hashingMocks.HasherMock{} marshalizerMock := mock.MarshalizerMock{} @@ -42,8 +40,6 @@ func initConsensusDataContainer() *ConsensusCore { chronologyHandler: chronologyHandlerMock, hasher: hasherMock, marshalizer: marshalizerMock, - blsPrivateKey: blsPrivateKeyMock, - blsSingleSigner: blsSingleSignerMock, multiSignerContainer: multiSignerContainer, roundHandler: roundHandlerMock, shardCoordinator: shardCoordinatorMock, diff --git a/consensus/spos/consensusCore_test.go b/consensus/spos/consensusCore_test.go index 9f56b3adefc..edfc80add95 100644 --- a/consensus/spos/consensusCore_test.go +++ b/consensus/spos/consensusCore_test.go @@ -23,8 +23,6 @@ func createDefaultConsensusCoreArgs() *spos.ConsensusCoreArgs { ChronologyHandler: consensusCoreMock.Chronology(), Hasher: consensusCoreMock.Hasher(), Marshalizer: consensusCoreMock.Marshalizer(), - BlsPrivateKey: consensusCoreMock.PrivateKey(), - BlsSingleSigner: consensusCoreMock.SingleSigner(), MultiSignerContainer: consensusCoreMock.MultiSignerContainer(), RoundHandler: consensusCoreMock.RoundHandler(), ShardCoordinator: consensusCoreMock.ShardCoordinator(), @@ -138,34 +136,6 @@ func TestConsensusCore_WithNilMarshalizerShouldFail(t *testing.T) { assert.Equal(t, spos.ErrNilMarshalizer, err) } -func TestConsensusCore_WithNilBlsPrivateKeyShouldFail(t *testing.T) { - t.Parallel() - - args := createDefaultConsensusCoreArgs() - args.BlsPrivateKey = nil - - consensusCore, err := spos.NewConsensusCore( - args, - ) - - assert.Nil(t, consensusCore) - assert.Equal(t, spos.ErrNilBlsPrivateKey, err) -} - -func TestConsensusCore_WithNilBlsSingleSignerShouldFail(t *testing.T) { - t.Parallel() - - args := createDefaultConsensusCoreArgs() - args.BlsSingleSigner = nil - - consensusCore, err := spos.NewConsensusCore( - args, - ) - - assert.Nil(t, consensusCore) - assert.Equal(t, spos.ErrNilBlsSingleSigner, err) -} - func TestConsensusCore_WithNilMultiSignerContainerShouldFail(t *testing.T) { t.Parallel() diff --git a/consensus/spos/interface.go b/consensus/spos/interface.go index 5720a82fffa..49c552df24b 100644 --- a/consensus/spos/interface.go +++ b/consensus/spos/interface.go @@ -8,7 +8,6 @@ import ( "github.com/ElrondNetwork/elrond-go-core/data/outport" "github.com/ElrondNetwork/elrond-go-core/hashing" "github.com/ElrondNetwork/elrond-go-core/marshal" - crypto "github.com/ElrondNetwork/elrond-go-crypto" cryptoCommon "github.com/ElrondNetwork/elrond-go/common/crypto" "github.com/ElrondNetwork/elrond-go/consensus" "github.com/ElrondNetwork/elrond-go/epochStart" @@ -49,10 +48,6 @@ type ConsensusCoreHandler interface { NodesCoordinator() nodesCoordinator.NodesCoordinator // EpochStartRegistrationHandler gets the RegistrationHandler stored in the ConsensusCore EpochStartRegistrationHandler() epochStart.RegistrationHandler - // PrivateKey returns the private key stored in the ConsensusStore used for randomness and leader's signature generation - PrivateKey() crypto.PrivateKey - // SingleSigner returns the single signer stored in the ConsensusStore used for randomness and leader's signature generation - SingleSigner() crypto.SingleSigner // PeerHonestyHandler returns the peer honesty handler which will be used in subrounds PeerHonestyHandler() consensus.PeerHonestyHandler // HeaderSigVerifier returns the sig verifier handler which will be used in subrounds diff --git a/consensus/spos/subround.go b/consensus/spos/subround.go index 2a70885ddd0..77ada21d51b 100644 --- a/consensus/spos/subround.go +++ b/consensus/spos/subround.go @@ -6,7 +6,6 @@ import ( "github.com/ElrondNetwork/elrond-go-core/core" "github.com/ElrondNetwork/elrond-go-core/core/check" - crypto "github.com/ElrondNetwork/elrond-go-crypto" "github.com/ElrondNetwork/elrond-go/consensus" ) @@ -210,11 +209,6 @@ func (sr *Subround) GetAssociatedPid(pkBytes []byte) core.PeerID { return sr.keysHandler.GetAssociatedPid(pkBytes) } -// GetMessageSigningPrivateKey returns the correct private key based on the provided pkBytes -func (sr *Subround) GetMessageSigningPrivateKey(pkBytes []byte) crypto.PrivateKey { - return sr.keysHandler.GetHandledPrivateKey(pkBytes) -} - // IsInterfaceNil returns true if there is no value under the interface func (sr *Subround) IsInterfaceNil() bool { return sr == nil diff --git a/factory/consensus/consensusComponents.go b/factory/consensus/consensusComponents.go index 95afb0155c1..417e01f8055 100644 --- a/factory/consensus/consensusComponents.go +++ b/factory/consensus/consensusComponents.go @@ -247,8 +247,6 @@ func (ccf *consensusComponentsFactory) Create() (*consensusComponents, error) { ChronologyHandler: cc.chronology, Hasher: ccf.coreComponents.Hasher(), Marshalizer: ccf.coreComponents.InternalMarshalizer(), - BlsPrivateKey: ccf.cryptoComponents.PrivateKey(), - BlsSingleSigner: ccf.cryptoComponents.BlockSigner(), MultiSignerContainer: ccf.cryptoComponents.MultiSignerContainer(), RoundHandler: ccf.processComponents.RoundHandler(), ShardCoordinator: ccf.processComponents.ShardCoordinator(), @@ -662,16 +660,12 @@ func (ccf *consensusComponentsFactory) createConsensusTopic(cc *consensusCompone } func (ccf *consensusComponentsFactory) createBlsSignatureHandler() (consensus.SignatureHandler, error) { - privKeyBytes, err := ccf.cryptoComponents.PrivateKey().ToByteArray() - if err != nil { - return nil, err - } - signatureHolderArgs := signing.ArgsSignatureHolder{ PubKeys: []string{ccf.cryptoComponents.PublicKeyString()}, - PrivKeyBytes: privKeyBytes, MultiSignerContainer: ccf.cryptoComponents.MultiSignerContainer(), + SingleSigner: ccf.cryptoComponents.BlockSigner(), KeyGenerator: ccf.cryptoComponents.BlockSignKeyGen(), + KeysHandler: ccf.cryptoComponents.KeysHandler(), } return signing.NewSignatureHolder(signatureHolderArgs)