Skip to content

Commit

Permalink
Merge branch 'EN-4555-bad-blocks-black-list' into rc-BoN-v1038
Browse files Browse the repository at this point in the history
  • Loading branch information
iulianpascalau committed Nov 2, 2019
2 parents 04fa2d5 + 5d3fd81 commit c817ab3
Show file tree
Hide file tree
Showing 41 changed files with 1,193 additions and 443 deletions.
43 changes: 27 additions & 16 deletions cmd/node/factory/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import (
"github.com/ElrondNetwork/elrond-go/storage"
"github.com/ElrondNetwork/elrond-go/storage/memorydb"
"github.com/ElrondNetwork/elrond-go/storage/storageUnit"
"github.com/ElrondNetwork/elrond-go/storage/timecache"
"github.com/btcsuite/btcd/btcec"
libp2pCrypto "github.com/libp2p/go-libp2p-core/crypto"
"github.com/urfave/cli"
Expand Down Expand Up @@ -101,6 +102,9 @@ const maxTxNonceDeltaAllowed = 15000
//TODO: Extract all others error messages from this file in some defined errors
var ErrCreateForkDetector = errors.New("could not create fork detector")

// timeSpanForBadHeaders is the expiry time for an added block header hash
var timeSpanForBadHeaders = time.Minute * 2

// Network struct holds the network components of the Elrond protocol
type Network struct {
NetMessenger p2p.Messenger
Expand Down Expand Up @@ -148,6 +152,7 @@ type Process struct {
Rounder consensus.Rounder
ForkDetector process.ForkDetector
BlockProcessor process.BlockProcessor
BlackListHandler process.BlackListHandler
}

type coreComponentsFactoryArgs struct {
Expand Down Expand Up @@ -460,7 +465,7 @@ func NewProcessComponentsFactoryArgs(

// ProcessComponentsFactory creates the process components
func ProcessComponentsFactory(args *processComponentsFactoryArgs) (*Process, error) {
interceptorContainerFactory, resolversContainerFactory, err := newInterceptorAndResolverContainerFactory(
interceptorContainerFactory, resolversContainerFactory, blackListHandler, err := newInterceptorAndResolverContainerFactory(
args.shardCoordinator,
args.nodesCoordinator,
args.data, args.core,
Expand Down Expand Up @@ -498,7 +503,7 @@ func ProcessComponentsFactory(args *processComponentsFactoryArgs) (*Process, err
return nil, err
}

forkDetector, err := newForkDetector(rounder, args.shardCoordinator)
forkDetector, err := newForkDetector(rounder, args.shardCoordinator, blackListHandler)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -542,6 +547,7 @@ func ProcessComponentsFactory(args *processComponentsFactoryArgs) (*Process, err
Rounder: rounder,
ForkDetector: forkDetector,
BlockProcessor: blockProcessor,
BlackListHandler: blackListHandler,
}, nil
}

Expand Down Expand Up @@ -1276,7 +1282,7 @@ func newInterceptorAndResolverContainerFactory(
state *State,
network *Network,
economics *economics.EconomicsData,
) (process.InterceptorsContainerFactory, dataRetriever.ResolversContainerFactory, error) {
) (process.InterceptorsContainerFactory, dataRetriever.ResolversContainerFactory, process.BlackListHandler, error) {

if shardCoordinator.SelfId() < shardCoordinator.NumberOfShards() {
return newShardInterceptorAndResolverContainerFactory(
Expand All @@ -1303,7 +1309,7 @@ func newInterceptorAndResolverContainerFactory(
)
}

return nil, nil, errors.New("could not create interceptor and resolver container factory")
return nil, nil, nil, errors.New("could not create interceptor and resolver container factory")
}

func newShardInterceptorAndResolverContainerFactory(
Expand All @@ -1315,8 +1321,9 @@ func newShardInterceptorAndResolverContainerFactory(
state *State,
network *Network,
economics *economics.EconomicsData,
) (process.InterceptorsContainerFactory, dataRetriever.ResolversContainerFactory, error) {
) (process.InterceptorsContainerFactory, dataRetriever.ResolversContainerFactory, process.BlackListHandler, error) {

headerBlackList := timecache.NewTimeCache(timeSpanForBadHeaders)
interceptorContainerFactory, err := shard.NewInterceptorsContainerFactory(
state.AccountsAdapter,
shardCoordinator,
Expand All @@ -1332,14 +1339,15 @@ func newShardInterceptorAndResolverContainerFactory(
state.AddressConverter,
maxTxNonceDeltaAllowed,
economics,
headerBlackList,
)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}

dataPacker, err := partitioning.NewSimpleDataPacker(core.Marshalizer)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}

resolversContainerFactory, err := shardfactoryDataRetriever.NewResolversContainerFactory(
Expand All @@ -1352,10 +1360,10 @@ func newShardInterceptorAndResolverContainerFactory(
dataPacker,
)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}

return interceptorContainerFactory, resolversContainerFactory, nil
return interceptorContainerFactory, resolversContainerFactory, headerBlackList, nil
}

func newMetaInterceptorAndResolverContainerFactory(
Expand All @@ -1367,8 +1375,9 @@ func newMetaInterceptorAndResolverContainerFactory(
network *Network,
state *State,
economics *economics.EconomicsData,
) (process.InterceptorsContainerFactory, dataRetriever.ResolversContainerFactory, error) {
) (process.InterceptorsContainerFactory, dataRetriever.ResolversContainerFactory, process.BlackListHandler, error) {

headerBlackList := timecache.NewTimeCache(timeSpanForBadHeaders)
interceptorContainerFactory, err := metachain.NewInterceptorsContainerFactory(
shardCoordinator,
nodesCoordinator,
Expand All @@ -1384,14 +1393,15 @@ func newMetaInterceptorAndResolverContainerFactory(
crypto.TxSignKeyGen,
maxTxNonceDeltaAllowed,
economics,
headerBlackList,
)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}

dataPacker, err := partitioning.NewSimpleDataPacker(core.Marshalizer)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}

resolversContainerFactory, err := metafactoryDataRetriever.NewResolversContainerFactory(
Expand All @@ -1404,9 +1414,9 @@ func newMetaInterceptorAndResolverContainerFactory(
dataPacker,
)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}
return interceptorContainerFactory, resolversContainerFactory, nil
return interceptorContainerFactory, resolversContainerFactory, headerBlackList, nil
}

func generateGenesisHeadersAndApplyInitialBalances(
Expand Down Expand Up @@ -1537,12 +1547,13 @@ func createInMemoryShardCoordinatorAndAccount(
func newForkDetector(
rounder consensus.Rounder,
shardCoordinator sharding.Coordinator,
headerBlackList process.BlackListHandler,
) (process.ForkDetector, error) {
if shardCoordinator.SelfId() < shardCoordinator.NumberOfShards() {
return processSync.NewShardForkDetector(rounder)
return processSync.NewShardForkDetector(rounder, headerBlackList)
}
if shardCoordinator.SelfId() == sharding.MetachainShardId {
return processSync.NewMetaForkDetector(rounder)
return processSync.NewMetaForkDetector(rounder, headerBlackList)
}

return nil, ErrCreateForkDetector
Expand Down
1 change: 1 addition & 0 deletions cmd/node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,7 @@ func createNode(
node.WithBootstrapRoundIndex(bootstrapRoundIndex),
node.WithAppStatusHandler(core.StatusHandler),
node.WithIndexer(indexer),
node.WithBlackListHandler(process.BlackListHandler),
)
if err != nil {
return nil, errors.New("error creating node: " + err.Error())
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ require (
github.com/pkg/errors v0.8.1
github.com/pkg/profile v1.3.0
github.com/prometheus/client_golang v1.0.0
github.com/prometheus/common v0.4.1
github.com/satori/go.uuid v1.2.0
github.com/shirou/gopsutil v0.0.0-20190731134726-d80c43f9c984
github.com/sirupsen/logrus v1.4.0
Expand Down
4 changes: 3 additions & 1 deletion integrationTests/consensus/testInitializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"github.com/ElrondNetwork/elrond-go/storage"
"github.com/ElrondNetwork/elrond-go/storage/memorydb"
"github.com/ElrondNetwork/elrond-go/storage/storageUnit"
"github.com/ElrondNetwork/elrond-go/storage/timecache"
"github.com/btcsuite/btcd/btcec"
libp2pCrypto "github.com/libp2p/go-libp2p-core/crypto"
)
Expand Down Expand Up @@ -361,7 +362,7 @@ func createConsensusOnlyNode(
time.Millisecond*time.Duration(uint64(roundTime)),
syncer)

forkDetector, _ := syncFork.NewShardForkDetector(rounder)
forkDetector, _ := syncFork.NewShardForkDetector(rounder, timecache.NewTimeCache(time.Second))

hdrResolver := &mock.HeaderResolverMock{}
mbResolver := &mock.MiniBlocksResolverMock{}
Expand Down Expand Up @@ -416,6 +417,7 @@ func createConsensusOnlyNode(
node.WithDataStore(createTestStore()),
node.WithResolversFinder(resolverFinder),
node.WithConsensusType(consensusType),
node.WithBlackListHandler(&mock.BlackListHandlerStub{}),
)

if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions integrationTests/mock/blackListHandleStub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package mock

type BlackListHandlerStub struct {
AddCalled func(key string) error
HasCalled func(key string) bool
}

func (blhs *BlackListHandlerStub) Add(key string) error {
return blhs.AddCalled(key)
}

func (blhs *BlackListHandlerStub) Has(key string) bool {
return blhs.HasCalled(key)
}

func (blhs *BlackListHandlerStub) IsInterfaceNil() bool {
return blhs == nil
}
3 changes: 3 additions & 0 deletions integrationTests/multiShard/smartContract/testInitializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import (
"github.com/ElrondNetwork/elrond-go/storage"
"github.com/ElrondNetwork/elrond-go/storage/memorydb"
"github.com/ElrondNetwork/elrond-go/storage/storageUnit"
"github.com/ElrondNetwork/elrond-go/storage/timecache"
vmcommon "github.com/ElrondNetwork/elrond-vm-common"
"github.com/btcsuite/btcd/btcec"
libp2pCrypto "github.com/libp2p/go-libp2p-core/crypto"
Expand Down Expand Up @@ -334,6 +335,7 @@ func createNetNode(
testAddressConverter,
maxTxNonceDeltaAllowed,
createMockTxFeeHandler(),
timecache.NewTimeCache(time.Second),
)
interceptorsContainer, err := interceptorContainerFactory.Create()
if err != nil {
Expand Down Expand Up @@ -797,6 +799,7 @@ func createMetaNetNode(
params.keyGen,
maxTxNonceDeltaAllowed,
feeHandler,
timecache.NewTimeCache(time.Second),
)
interceptorsContainer, err := interceptorContainerFactory.Create()
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions integrationTests/testProcessorNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"github.com/ElrondNetwork/elrond-go/process/smartContract/hooks"
"github.com/ElrondNetwork/elrond-go/process/transaction"
"github.com/ElrondNetwork/elrond-go/sharding"
"github.com/ElrondNetwork/elrond-go/storage/timecache"
"github.com/ElrondNetwork/elrond-vm-common"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -72,6 +73,9 @@ var MinTxGasLimit = uint64(4)

const maxTxNonceDeltaAllowed = 8000

// TimeSpanForBadHeaders is the expiry time for an added block header hash
var TimeSpanForBadHeaders = time.Second * 30

// TestKeyPair holds a pair of private/public Keys
type TestKeyPair struct {
Sk crypto.PrivateKey
Expand Down Expand Up @@ -105,6 +109,7 @@ type TestProcessorNode struct {

EconomicsData *economics.TestEconomicsData

BlackListHandler process.BlackListHandler
InterceptorsContainer process.InterceptorsContainer
ResolversContainer dataRetriever.ResolversContainer
ResolverFinder dataRetriever.ResolversFinder
Expand Down Expand Up @@ -286,6 +291,8 @@ func (tpn *TestProcessorNode) initEconomicsData() {

func (tpn *TestProcessorNode) initInterceptors() {
var err error
tpn.BlackListHandler = timecache.NewTimeCache(TimeSpanForBadHeaders)

if tpn.ShardCoordinator.SelfId() == sharding.MetachainShardId {
interceptorContainerFactory, _ := metaProcess.NewInterceptorsContainerFactory(
tpn.ShardCoordinator,
Expand All @@ -302,6 +309,7 @@ func (tpn *TestProcessorNode) initInterceptors() {
tpn.OwnAccount.KeygenTxSign,
maxTxNonceDeltaAllowed,
tpn.EconomicsData,
tpn.BlackListHandler,
)

tpn.InterceptorsContainer, err = interceptorContainerFactory.Create()
Expand All @@ -324,6 +332,7 @@ func (tpn *TestProcessorNode) initInterceptors() {
TestAddressConverter,
maxTxNonceDeltaAllowed,
tpn.EconomicsData,
tpn.BlackListHandler,
)

tpn.InterceptorsContainer, err = interceptorContainerFactory.Create()
Expand Down Expand Up @@ -567,6 +576,7 @@ func (tpn *TestProcessorNode) initNode() {
node.WithTxSingleSigner(tpn.OwnAccount.SingleSigner),
node.WithDataStore(tpn.Storage),
node.WithSyncer(&mock.SyncTimerMock{}),
node.WithBlackListHandler(tpn.BlackListHandler),
)
if err != nil {
fmt.Printf("Error creating node: %s\n", err.Error())
Expand Down
6 changes: 4 additions & 2 deletions integrationTests/testSyncNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (tpn *TestProcessorNode) initBlockProcessorWithSync() {
}

if tpn.ShardCoordinator.SelfId() == sharding.MetachainShardId {
tpn.ForkDetector, _ = sync.NewMetaForkDetector(tpn.Rounder)
tpn.ForkDetector, _ = sync.NewMetaForkDetector(tpn.Rounder, tpn.BlackListHandler)
argumentsBase.Core = &mock.ServiceContainerMock{}
argumentsBase.ForkDetector = tpn.ForkDetector
arguments := block.ArgMetaProcessor{
Expand All @@ -106,7 +106,7 @@ func (tpn *TestProcessorNode) initBlockProcessorWithSync() {
tpn.BlockProcessor, err = block.NewMetaProcessor(arguments)

} else {
tpn.ForkDetector, _ = sync.NewShardForkDetector(tpn.Rounder)
tpn.ForkDetector, _ = sync.NewShardForkDetector(tpn.Rounder, tpn.BlackListHandler)
argumentsBase.ForkDetector = tpn.ForkDetector
arguments := block.ArgShardProcessor{
ArgBaseProcessor: argumentsBase,
Expand Down Expand Up @@ -138,6 +138,7 @@ func (tpn *TestProcessorNode) createShardBootstrapper() (TestBootstrapper, error
tpn.ShardCoordinator,
tpn.AccntState,
1,
tpn.BlackListHandler,
)
if err != nil {
return nil, err
Expand All @@ -163,6 +164,7 @@ func (tpn *TestProcessorNode) createMetaChainBootstrapper() (TestBootstrapper, e
tpn.ShardCoordinator,
tpn.AccntState,
1,
tpn.BlackListHandler,
)

if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions node/defineOptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,11 @@ func WithIndexer(indexer indexer.Indexer) Option {
return nil
}
}

// WithBlackListHandler sets up a black list handler for the Node
func WithBlackListHandler(blackListHandler process.BlackListHandler) Option {
return func(n *Node) error {
n.blackListHandler = blackListHandler
return nil
}
}
5 changes: 4 additions & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ type Node struct {
currentSendingGoRoutines int32
bootstrapRoundIndex uint64

indexer indexer.Indexer
indexer indexer.Indexer
blackListHandler process.BlackListHandler
}

// ApplyOptions can set up different configurable options of a Node instance
Expand Down Expand Up @@ -389,6 +390,7 @@ func (n *Node) createShardBootstrapper(rounder consensus.Rounder) (process.Boots
n.shardCoordinator,
n.accounts,
n.bootstrapRoundIndex,
n.blackListHandler,
)
if err != nil {
return nil, err
Expand All @@ -412,6 +414,7 @@ func (n *Node) createMetaChainBootstrapper(rounder consensus.Rounder) (process.B
n.shardCoordinator,
n.accounts,
n.bootstrapRoundIndex,
n.blackListHandler,
)

if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions process/block/metablock.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,10 @@ func (mp *metaProcessor) RestoreBlockIntoPools(headerHandler data.HeaderHandler,
syncMap.Store(hdr.ShardId, hdrHash)
headerNoncesPool.Merge(hdr.Nonce, syncMap)

err = mp.store.GetStorer(dataRetriever.BlockHeaderUnit).Remove(hdrHash)
if err != nil {
return err
}
//err = mp.store.GetStorer(dataRetriever.BlockHeaderUnit).Remove(hdrHash)
//if err != nil {
// return err
//}

nonceToByteSlice := mp.uint64Converter.ToByteSlice(hdr.Nonce)
err = mp.store.GetStorer(dataRetriever.ShardHdrNonceHashDataUnit).Remove(nonceToByteSlice)
Expand Down
Loading

0 comments on commit c817ab3

Please sign in to comment.