Skip to content

Commit

Permalink
Merge branch 'development' into EN-4088-integration-test-block-double…
Browse files Browse the repository at this point in the history
…-sign
  • Loading branch information
iulianpascalau authored Sep 22, 2019
2 parents 7c77b3d + 223a27e commit 68452d5
Show file tree
Hide file tree
Showing 30 changed files with 872 additions and 474 deletions.
26 changes: 24 additions & 2 deletions cmd/node/factory/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ const (

var log = logger.DefaultLogger()

//TODO: Extract all others error messages from this file in some defined errors
// ErrCreateForkDetector signals that a fork detector could not be created
var ErrCreateForkDetector = errors.New("could not create fork detector")

// Network struct holds the network components of the Elrond protocol
type Network struct {
NetMessenger p2p.Messenger
Expand Down Expand Up @@ -465,7 +469,7 @@ func ProcessComponentsFactory(args *processComponentsFactoryArgs) (*Process, err
return nil, err
}

forkDetector, err := processSync.NewBasicForkDetector(rounder)
forkDetector, err := newForkDetector(rounder, args.shardCoordinator)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1354,6 +1358,20 @@ func createInMemoryShardCoordinatorAndAccount(
return newShardCoordinator, accounts, nil
}

func newForkDetector(
rounder consensus.Rounder,
shardCoordinator sharding.Coordinator,
) (process.ForkDetector, error) {
if shardCoordinator.SelfId() < shardCoordinator.NumberOfShards() {
return processSync.NewShardForkDetector(rounder)
}
if shardCoordinator.SelfId() == sharding.MetachainShardId {
return processSync.NewMetaForkDetector(rounder)
}

return nil, ErrCreateForkDetector
}

func newBlockProcessorAndTracker(
resolversFinder dataRetriever.ResolversFinder,
shardCoordinator sharding.Coordinator,
Expand Down Expand Up @@ -1440,6 +1458,7 @@ func newShardBlockProcessorAndTracker(
factory.TransactionTopic,
factory.UnsignedTransactionTopic,
factory.MiniBlocksTopic,
factory.HeadersTopic,
factory.MetachainBlocksTopic,
MaxTxsToRequest,
)
Expand Down Expand Up @@ -1545,7 +1564,10 @@ func newMetaBlockProcessorAndTracker(
shardsGenesisBlocks map[uint32]data.HeaderHandler,
coreServiceContainer serviceContainer.Core,
) (process.BlockProcessor, process.BlocksTracker, error) {
requestHandler, err := requestHandlers.NewMetaResolverRequestHandler(resolversFinder, factory.ShardHeadersForMetachainTopic)
requestHandler, err := requestHandlers.NewMetaResolverRequestHandler(
resolversFinder,
factory.ShardHeadersForMetachainTopic,
factory.MetachainBlocksTopic)
if err != nil {
return nil, nil, err
}
Expand Down
7 changes: 5 additions & 2 deletions dataRetriever/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,11 @@ var ErrEmptyScrRequestTopic = errors.New("empty smart contract result request to
// ErrEmptyMiniBlockRequestTopic signals that an empty miniblock topic has been provided
var ErrEmptyMiniBlockRequestTopic = errors.New("empty miniblock request topic")

// ErrEmptyHeaderRequestTopic signals that an empty transaction topic has been provided
var ErrEmptyHeaderRequestTopic = errors.New("empty header request topic")
// ErrEmptyShardHeaderRequestTopic signals that an empty shard header topic has been provided
var ErrEmptyShardHeaderRequestTopic = errors.New("empty shard header request topic")

// ErrEmptyMetaHeaderRequestTopic signals that an empty meta header topic has been provided
var ErrEmptyMetaHeaderRequestTopic = errors.New("empty meta header request topic")

// ErrInvalidMaxTxRequest signals that max tx request is too small
var ErrInvalidMaxTxRequest = errors.New("max tx request number is invalid")
Expand Down
78 changes: 50 additions & 28 deletions dataRetriever/requestHandlers/requestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import (
)

type ResolverRequestHandler struct {
resolversFinder dataRetriever.ResolversFinder
txRequestTopic string
scrRequestTopic string
mbRequestTopic string
hdrRequestTopic string
isMetaChain bool
maxTxsToRequest int
resolversFinder dataRetriever.ResolversFinder
txRequestTopic string
scrRequestTopic string
mbRequestTopic string
shardHdrRequestTopic string
metaHdrRequestTopic string
isMetaChain bool
maxTxsToRequest int
}

var log = logger.DefaultLogger()
Expand All @@ -28,7 +29,8 @@ func NewShardResolverRequestHandler(
txRequestTopic string,
scrRequestTopic string,
mbRequestTopic string,
hdrRequestTopic string,
shardHdrRequestTopic string,
metaHdrRequestTopic string,
maxTxsToRequest int,
) (*ResolverRequestHandler, error) {
if finder == nil || finder.IsInterfaceNil() {
Expand All @@ -43,21 +45,25 @@ func NewShardResolverRequestHandler(
if len(mbRequestTopic) == 0 {
return nil, dataRetriever.ErrEmptyMiniBlockRequestTopic
}
if len(hdrRequestTopic) == 0 {
return nil, dataRetriever.ErrEmptyHeaderRequestTopic
if len(shardHdrRequestTopic) == 0 {
return nil, dataRetriever.ErrEmptyShardHeaderRequestTopic
}
if len(metaHdrRequestTopic) == 0 {
return nil, dataRetriever.ErrEmptyMetaHeaderRequestTopic
}
if maxTxsToRequest < 1 {
return nil, dataRetriever.ErrInvalidMaxTxRequest
}

rrh := &ResolverRequestHandler{
resolversFinder: finder,
txRequestTopic: txRequestTopic,
mbRequestTopic: mbRequestTopic,
hdrRequestTopic: hdrRequestTopic,
scrRequestTopic: scrRequestTopic,
isMetaChain: false,
maxTxsToRequest: maxTxsToRequest,
resolversFinder: finder,
txRequestTopic: txRequestTopic,
mbRequestTopic: mbRequestTopic,
shardHdrRequestTopic: shardHdrRequestTopic,
metaHdrRequestTopic: metaHdrRequestTopic,
scrRequestTopic: scrRequestTopic,
isMetaChain: false,
maxTxsToRequest: maxTxsToRequest,
}

return rrh, nil
Expand All @@ -66,19 +72,24 @@ func NewShardResolverRequestHandler(
// NewMetaResolverRequestHandler creates a requestHandler interface implementation with request functions
func NewMetaResolverRequestHandler(
finder dataRetriever.ResolversFinder,
hdrRequestTopic string,
shardHdrRequestTopic string,
metaHdrRequestTopic string,
) (*ResolverRequestHandler, error) {
if finder == nil || finder.IsInterfaceNil() {
return nil, dataRetriever.ErrNilResolverFinder
}
if len(hdrRequestTopic) == 0 {
return nil, dataRetriever.ErrEmptyHeaderRequestTopic
if len(shardHdrRequestTopic) == 0 {
return nil, dataRetriever.ErrEmptyShardHeaderRequestTopic
}
if len(metaHdrRequestTopic) == 0 {
return nil, dataRetriever.ErrEmptyMetaHeaderRequestTopic
}

rrh := &ResolverRequestHandler{
resolversFinder: finder,
hdrRequestTopic: hdrRequestTopic,
isMetaChain: true,
resolversFinder: finder,
shardHdrRequestTopic: shardHdrRequestTopic,
metaHdrRequestTopic: metaHdrRequestTopic,
isMetaChain: true,
}

return rrh, nil
Expand Down Expand Up @@ -132,7 +143,15 @@ func (rrh *ResolverRequestHandler) RequestMiniBlock(shardId uint32, miniblockHas

// RequestHeader method asks for header from the connected peers
func (rrh *ResolverRequestHandler) RequestHeader(shardId uint32, hash []byte) {
rrh.requestByHash(shardId, hash, rrh.hdrRequestTopic)
//TODO: Refactor this class and create specific methods for requesting shard or meta data
var topic string
if shardId == sharding.MetachainShardId {
topic = rrh.metaHdrRequestTopic
} else {
topic = rrh.shardHdrRequestTopic
}

rrh.requestByHash(shardId, hash, topic)
}

func (rrh *ResolverRequestHandler) requestByHash(destShardID uint32, hash []byte, baseTopic string) {
Expand Down Expand Up @@ -162,20 +181,23 @@ func (rrh *ResolverRequestHandler) requestByHash(destShardID uint32, hash []byte
func (rrh *ResolverRequestHandler) RequestHeaderByNonce(destShardID uint32, nonce uint64) {
var err error
var resolver dataRetriever.Resolver
var topic string
if rrh.isMetaChain {
resolver, err = rrh.resolversFinder.CrossShardResolver(rrh.hdrRequestTopic, destShardID)
topic = rrh.shardHdrRequestTopic
resolver, err = rrh.resolversFinder.CrossShardResolver(topic, destShardID)
} else {
resolver, err = rrh.resolversFinder.MetaChainResolver(rrh.hdrRequestTopic)
topic = rrh.metaHdrRequestTopic
resolver, err = rrh.resolversFinder.MetaChainResolver(topic)
}

if err != nil {
log.Error(fmt.Sprintf("missing resolver to %s topic to shard %d", rrh.hdrRequestTopic, destShardID))
log.Error(fmt.Sprintf("missing resolver to %s topic to shard %d", topic, destShardID))
return
}

headerResolver, ok := resolver.(dataRetriever.HeaderResolver)
if !ok {
log.Error(fmt.Sprintf("resolver is not a header resolver to %s topic to shard %d", rrh.hdrRequestTopic, destShardID))
log.Error(fmt.Sprintf("resolver is not a header resolver to %s topic to shard %d", topic, destShardID))
return
}

Expand Down
Loading

0 comments on commit 68452d5

Please sign in to comment.