Skip to content

Commit

Permalink
Merge pull request #4316 from ElrondNetwork/signatures-holder-component
Browse files Browse the repository at this point in the history
Signature handler component for consensus
  • Loading branch information
AdoAdoAdo authored Aug 2, 2022
2 parents 1e94967 + 5969f45 commit a7ab265
Show file tree
Hide file tree
Showing 22 changed files with 1,262 additions and 70 deletions.
15 changes: 14 additions & 1 deletion consensus/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/data"
"github.com/ElrondNetwork/elrond-go-crypto"
crypto "github.com/ElrondNetwork/elrond-go-crypto"
"github.com/ElrondNetwork/elrond-go/p2p"
)

Expand Down Expand Up @@ -147,3 +147,16 @@ type ScheduledProcessor interface {
IsProcessedOKWithTimeout() bool
IsInterfaceNil() bool
}

// 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)
StoreSignatureShare(index uint16, sig []byte) error
SignatureShare(index uint16) ([]byte, error)
VerifySignatureShare(index uint16, sig []byte, msg []byte, epoch uint32) error
AggregateSigs(bitmap []byte, epoch uint32) ([]byte, error)
SetAggregatedSig([]byte) error
Verify(msg []byte, bitmap []byte, epoch uint32) error
IsInterfaceNil() bool
}
11 changes: 11 additions & 0 deletions consensus/mock/consensusDataContainerMock.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type ConsensusCoreMock struct {
fallbackHeaderValidator consensus.FallbackHeaderValidator
nodeRedundancyHandler consensus.NodeRedundancyHandler
scheduledProcessor consensus.ScheduledProcessor
signatureHandler consensus.SignatureHandler
}

// GetAntiFloodHandler -
Expand Down Expand Up @@ -230,6 +231,16 @@ func (ccm *ConsensusCoreMock) SetNodeRedundancyHandler(nodeRedundancyHandler con
ccm.nodeRedundancyHandler = nodeRedundancyHandler
}

// SignatureHandler -
func (ccm *ConsensusCoreMock) SignatureHandler() consensus.SignatureHandler {
return ccm.signatureHandler
}

// SetSignatureHandler -
func (ccm *ConsensusCoreMock) SetSignatureHandler(signatureHandler consensus.SignatureHandler) {
ccm.signatureHandler = signatureHandler
}

// IsInterfaceNil returns true if there is no value under the interface
func (ccm *ConsensusCoreMock) IsInterfaceNil() bool {
return ccm == nil
Expand Down
2 changes: 2 additions & 0 deletions consensus/mock/mockTestInitializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ func InitConsensusCoreWithMultiSigner(multiSigner crypto.MultiSigner) *Consensus
nodeRedundancyHandler := &NodeRedundancyHandlerStub{}
scheduledProcessor := &consensusMocks.ScheduledProcessorStub{}
multiSignerContainer := cryptoMocks.NewMultiSignerContainerMock(multiSigner)
signatureHandler := &SignatureHandlerStub{}

container := &ConsensusCoreMock{
blockChain: blockChain,
Expand All @@ -229,6 +230,7 @@ func InitConsensusCoreWithMultiSigner(multiSigner crypto.MultiSigner) *Consensus
fallbackHeaderValidator: fallbackHeaderValidator,
nodeRedundancyHandler: nodeRedundancyHandler,
scheduledProcessor: scheduledProcessor,
signatureHandler: signatureHandler,
}

return container
Expand Down
90 changes: 90 additions & 0 deletions consensus/mock/signatureHandlerStub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package mock

// SignatureHandlerStub implements SignatureHandler interface
type SignatureHandlerStub struct {
ResetCalled func(pubKeys []string) error
CreateSignatureShareCalled func(msg []byte, index uint16, epoch uint32) ([]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 -
func (stub *SignatureHandlerStub) Reset(pubKeys []string) error {
if stub.ResetCalled != nil {
return stub.ResetCalled(pubKeys)
}

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)
}

return []byte("sigShare"), nil
}

// StoreSignatureShare -
func (stub *SignatureHandlerStub) StoreSignatureShare(index uint16, sig []byte) error {
if stub.StoreSignatureShareCalled != nil {
return stub.StoreSignatureShareCalled(index, sig)
}

return nil
}

// SignatureShare -
func (stub *SignatureHandlerStub) SignatureShare(index uint16) ([]byte, error) {
if stub.SignatureShareCalled != nil {
return stub.SignatureShareCalled(index)
}

return []byte("sigShare"), nil
}

// VerifySignatureShare -
func (stub *SignatureHandlerStub) VerifySignatureShare(index uint16, sig []byte, msg []byte, epoch uint32) error {
if stub.VerifySignatureShareCalled != nil {
return stub.VerifySignatureShareCalled(index, sig, msg, epoch)
}

return nil
}

// AggregateSigs -
func (stub *SignatureHandlerStub) AggregateSigs(bitmap []byte, epoch uint32) ([]byte, error) {
if stub.AggregateSigsCalled != nil {
return stub.AggregateSigsCalled(bitmap, epoch)
}

return []byte("aggSigs"), nil
}

// SetAggregatedSig -
func (stub *SignatureHandlerStub) SetAggregatedSig(sig []byte) error {
if stub.SetAggregatedSigCalled != nil {
return stub.SetAggregatedSigCalled(sig)
}

return nil
}

// Verify -
func (stub *SignatureHandlerStub) Verify(msg []byte, bitmap []byte, epoch uint32) error {
if stub.VerifyCalled != nil {
return stub.VerifyCalled(msg, bitmap, epoch)
}

return nil
}

// IsInterfaceNil -
func (stub *SignatureHandlerStub) IsInterfaceNil() bool {
return stub == nil
}
42 changes: 42 additions & 0 deletions consensus/signing/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package signing

import "errors"

// ErrInvalidSignature is raised for an invalid signature
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")

// ErrNoPublicKeySet is raised when no public key was set for a multisignature
var ErrNoPublicKeySet = errors.New("no public key was set")

// ErrNilKeyGenerator is raised when a valid key generator is expected but nil used
var ErrNilKeyGenerator = errors.New("key generator is nil")

// ErrNilPublicKeys is raised when public keys are expected but received nil
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")

// ErrIndexOutOfBounds is raised when an out of bound index is used
var ErrIndexOutOfBounds = errors.New("index is out of bounds")

// ErrEmptyPubKeyString is raised when an empty public key string is used
var ErrEmptyPubKeyString = errors.New("public key string is empty")

// ErrNilMessage is raised when trying to verify a nil signed message or trying to sign a nil message
var ErrNilMessage = errors.New("message to be signed or to be verified is nil")

// ErrBitmapMismatch is raised when an invalid bitmap is passed to the multisigner
var ErrBitmapMismatch = errors.New("multi signer reported a mismatch in used bitmap")
Loading

0 comments on commit a7ab265

Please sign in to comment.