Skip to content

Commit

Permalink
Merge branch 'EN-3563-update-term-ui' of https://github.com/ElrondNet…
Browse files Browse the repository at this point in the history
…work/elrond-go into EN-3563-update-term-ui
  • Loading branch information
iulianpascalau committed Aug 14, 2019
2 parents c45c840 + 1f15605 commit 0295f50
Show file tree
Hide file tree
Showing 7 changed files with 438 additions and 788 deletions.
98 changes: 77 additions & 21 deletions process/block/interceptors/headerInterceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ import (

// HeaderInterceptor represents an interceptor used for block headers
type HeaderInterceptor struct {
hdrInterceptorBase *HeaderInterceptorBase
headers storage.Cacher
headersNonces dataRetriever.Uint64SyncMapCacher
shardCoordinator sharding.Coordinator
marshalizer marshal.Marshalizer
storer storage.Storer
multiSigVerifier crypto.MultiSigVerifier
hasher hashing.Hasher
chronologyValidator process.ChronologyValidator
headers storage.Cacher
headersNonces dataRetriever.Uint64SyncMapCacher
headerValidator process.HeaderValidator
shardCoordinator sharding.Coordinator
}

// NewHeaderInterceptor hooks a new interceptor for block headers
Expand All @@ -34,38 +39,81 @@ func NewHeaderInterceptor(
chronologyValidator process.ChronologyValidator,
) (*HeaderInterceptor, error) {

if marshalizer == nil {
return nil, process.ErrNilMarshalizer
}
if headersNonces == nil {
return nil, process.ErrNilHeadersNoncesDataPool
}
if headers == nil {
return nil, process.ErrNilHeadersDataPool
}
hdrBaseInterceptor, err := NewHeaderInterceptorBase(
marshalizer,
headerValidator,
multiSigVerifier,
hasher,
shardCoordinator,
chronologyValidator,
)
if headerValidator == nil {
return nil, process.ErrNilHeaderHandlerValidator
}
if multiSigVerifier == nil {
return nil, process.ErrNilMultiSigVerifier
}
if hasher == nil {
return nil, process.ErrNilHasher
}
if shardCoordinator == nil {
return nil, process.ErrNilShardCoordinator
}
if chronologyValidator == nil {
return nil, process.ErrNilChronologyValidator
}

hdrInterceptor := &HeaderInterceptor{
marshalizer: marshalizer,
multiSigVerifier: multiSigVerifier,
hasher: hasher,
shardCoordinator: shardCoordinator,
chronologyValidator: chronologyValidator,
headers: headers,
headersNonces: headersNonces,
headerValidator: headerValidator,
}

return hdrInterceptor, nil
}

// ParseReceivedMessage will transform the received p2p.Message in an InterceptedHeader.
// If the header hash is present in storage it will output an error
func (hi *HeaderInterceptor) ParseReceivedMessage(message p2p.MessageP2P) (*block.InterceptedHeader, error) {
if message == nil {
return nil, process.ErrNilMessage
}
if message.Data() == nil {
return nil, process.ErrNilDataToProcess
}

hdrIntercepted := block.NewInterceptedHeader(hi.multiSigVerifier, hi.chronologyValidator)
err := hi.marshalizer.Unmarshal(hdrIntercepted, message.Data())
if err != nil {
return nil, err
}

hashWithSig := hi.hasher.Compute(string(message.Data()))
hdrIntercepted.SetHash(hashWithSig)

err = hdrIntercepted.IntegrityAndValidity(hi.shardCoordinator)
if err != nil {
return nil, err
}

hdrIntercept := &HeaderInterceptor{
hdrInterceptorBase: hdrBaseInterceptor,
headers: headers,
headersNonces: headersNonces,
shardCoordinator: shardCoordinator,
err = hdrIntercepted.VerifySig()
if err != nil {
return nil, err
}

return hdrIntercept, nil
return hdrIntercepted, nil
}

// ProcessReceivedMessage will be the callback func from the p2p.Messenger and will be called each time a new message was received
// (for the topic this validator was registered to)
func (hi *HeaderInterceptor) ProcessReceivedMessage(message p2p.MessageP2P) error {
hdrIntercepted, err := hi.hdrInterceptorBase.ParseReceivedMessage(message)
hdrIntercepted, err := hi.ParseReceivedMessage(message)
if err != nil {
return err
}
Expand All @@ -75,12 +123,20 @@ func (hi *HeaderInterceptor) ProcessReceivedMessage(message p2p.MessageP2P) erro
return nil
}

// checkHeaderForCurrentShard checks if the header is for current shard
func (hi *HeaderInterceptor) checkHeaderForCurrentShard(interceptedHdr *block.InterceptedHeader) bool {
isHeaderForCurrentShard := hi.shardCoordinator.SelfId() == interceptedHdr.GetHeader().ShardId
isMetachainShardCoordinator := hi.shardCoordinator.SelfId() == sharding.MetachainShardId

return isHeaderForCurrentShard || isMetachainShardCoordinator
}

func (hi *HeaderInterceptor) processHeader(hdrIntercepted *block.InterceptedHeader) {
if !hi.hdrInterceptorBase.CheckHeaderForCurrentShard(hdrIntercepted) {
if !hi.checkHeaderForCurrentShard(hdrIntercepted) {
return
}

isHeaderOkForProcessing := hi.hdrInterceptorBase.headerValidator.IsHeaderValidForProcessing(hdrIntercepted.Header)
isHeaderOkForProcessing := hi.headerValidator.IsHeaderValidForProcessing(hdrIntercepted.Header)
if !isHeaderOkForProcessing {
log.Debug("intercepted block header can not be processed")
return
Expand Down
101 changes: 0 additions & 101 deletions process/block/interceptors/headerInterceptorBase.go

This file was deleted.

Loading

0 comments on commit 0295f50

Please sign in to comment.