Skip to content

Commit

Permalink
Merge pull request #507 from ElrondNetwork/EN-4217-bugfixing-tx-flood
Browse files Browse the repository at this point in the history
En 4217 bugfixing tx flood
  • Loading branch information
AdoAdoAdo authored Oct 4, 2019
2 parents dbe3bfb + b6e35c5 commit e35fd4b
Show file tree
Hide file tree
Showing 38 changed files with 984 additions and 257 deletions.
2 changes: 1 addition & 1 deletion cmd/node/config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
Type = "LRU"

[TxBlockBodyDataPool]
Size = 100
Size = 300
Type = "LRU"

[StateBlockBodyDataPool]
Expand Down
22 changes: 18 additions & 4 deletions cmd/node/factory/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ const (

var log = logger.DefaultLogger()

//TODO: Extract all others error messages from this file in some defined errors
const maxTxNonceDeltaAllowed = 100

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

// Network struct holds the network components of the Elrond protocol
Expand Down Expand Up @@ -457,7 +458,14 @@ func NewProcessComponentsFactoryArgs(
// ProcessComponentsFactory creates the process components
func ProcessComponentsFactory(args *processComponentsFactoryArgs) (*Process, error) {
interceptorContainerFactory, resolversContainerFactory, err := newInterceptorAndResolverContainerFactory(
args.shardCoordinator, args.nodesCoordinator, args.data, args.core, args.crypto, args.state, args.network)
args.shardCoordinator,
args.nodesCoordinator,
args.data, args.core,
args.crypto,
args.state,
args.network,
args.economicsData,
)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1179,7 +1187,9 @@ func newInterceptorAndResolverContainerFactory(
crypto *Crypto,
state *State,
network *Network,
economics *economics.EconomicsData,
) (process.InterceptorsContainerFactory, dataRetriever.ResolversContainerFactory, error) {

if shardCoordinator.SelfId() < shardCoordinator.NumberOfShards() {
return newShardInterceptorAndResolverContainerFactory(
shardCoordinator,
Expand All @@ -1189,6 +1199,7 @@ func newInterceptorAndResolverContainerFactory(
crypto,
state,
network,
economics,
)
}
if shardCoordinator.SelfId() == sharding.MetachainShardId {
Expand All @@ -1213,8 +1224,9 @@ func newShardInterceptorAndResolverContainerFactory(
crypto *Crypto,
state *State,
network *Network,
economics *economics.EconomicsData,
) (process.InterceptorsContainerFactory, dataRetriever.ResolversContainerFactory, error) {
//TODO add a real chronology validator and remove null chronology validator

interceptorContainerFactory, err := shard.NewInterceptorsContainerFactory(
state.AccountsAdapter,
shardCoordinator,
Expand All @@ -1228,6 +1240,8 @@ func newShardInterceptorAndResolverContainerFactory(
crypto.MultiSigner,
data.Datapool,
state.AddressConverter,
maxTxNonceDeltaAllowed,
economics,
)
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -1262,7 +1276,7 @@ func newMetaInterceptorAndResolverContainerFactory(
crypto *Crypto,
network *Network,
) (process.InterceptorsContainerFactory, dataRetriever.ResolversContainerFactory, error) {
//TODO add a real chronology validator and remove null chronology validator

interceptorContainerFactory, err := metachain.NewInterceptorsContainerFactory(
shardCoordinator,
nodesCoordinator,
Expand Down
3 changes: 3 additions & 0 deletions core/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ var ErrEmptyFile = errors.New("empty file provided")

// ErrInvalidIndex signals that an invalid private key index has been provided
var ErrInvalidIndex = errors.New("invalid private key index")

// ErrNotPositiveValue signals that a 0 or negative value has been provided
var ErrNotPositiveValue = errors.New("the provided value is not positive")
49 changes: 49 additions & 0 deletions core/throttler/numGoRoutineThrottler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package throttler

import (
"sync/atomic"

"github.com/ElrondNetwork/elrond-go/core"
)

// NumGoRoutineThrottler can limit the number of go routines launched
type NumGoRoutineThrottler struct {
max int32
counter int32
}

// NewNumGoRoutineThrottler creates a new num go routine throttler instance
func NewNumGoRoutineThrottler(max int32) (*NumGoRoutineThrottler, error) {
if max <= 0 {
return nil, core.ErrNotPositiveValue
}

return &NumGoRoutineThrottler{
max: max,
}, nil
}

// CanProcess returns true if current counter is less than max
func (ngrt *NumGoRoutineThrottler) CanProcess() bool {
valCounter := atomic.LoadInt32(&ngrt.counter)

return valCounter < ngrt.max
}

// StartProcessing will increment current counter
func (ngrt *NumGoRoutineThrottler) StartProcessing() {
atomic.AddInt32(&ngrt.counter, 1)
}

// EndProcessing will decrement current counter
func (ngrt *NumGoRoutineThrottler) EndProcessing() {
atomic.AddInt32(&ngrt.counter, -1)
}

// IsInterfaceNil returns true if there is no value under the interface
func (ngrt *NumGoRoutineThrottler) IsInterfaceNil() bool {
if ngrt == nil {
return true
}
return false
}
93 changes: 93 additions & 0 deletions core/throttler/numGoRoutineThrottler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package throttler_test

import (
"testing"

"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/core/throttler"
"github.com/stretchr/testify/assert"
)

func TestNewNumGoRoutineThrottler_WithNegativeShouldError(t *testing.T) {
t.Parallel()

nt, err := throttler.NewNumGoRoutineThrottler(-1)

assert.Nil(t, nt)
assert.Equal(t, core.ErrNotPositiveValue, err)
}

func TestNewNumGoRoutineThrottler_WithZeroShouldError(t *testing.T) {
t.Parallel()

nt, err := throttler.NewNumGoRoutineThrottler(0)

assert.Nil(t, nt)
assert.Equal(t, core.ErrNotPositiveValue, err)
}

func TestNewNumGoRoutineThrottler_ShouldWork(t *testing.T) {
t.Parallel()

nt, err := throttler.NewNumGoRoutineThrottler(1)

assert.NotNil(t, nt)
assert.Nil(t, err)
}

func TestNumGoRoutineThrottler_CanProcessMessageWithZeroCounter(t *testing.T) {
t.Parallel()

nt, _ := throttler.NewNumGoRoutineThrottler(1)

assert.True(t, nt.CanProcess())
}

func TestNumGoRoutineThrottler_CanProcessMessageCounterEqualsMax(t *testing.T) {
t.Parallel()

nt, _ := throttler.NewNumGoRoutineThrottler(1)
nt.StartProcessing()

assert.False(t, nt.CanProcess())
}

func TestNumGoRoutineThrottler_CanProcessMessageCounterIsMaxLessThanOne(t *testing.T) {
t.Parallel()

max := int32(45)
nt, _ := throttler.NewNumGoRoutineThrottler(max)

for i := int32(0); i < max-1; i++ {
nt.StartProcessing()
}

assert.True(t, nt.CanProcess())
}

func TestNumGoRoutineThrottler_CanProcessMessageCounterIsMax(t *testing.T) {
t.Parallel()

max := int32(45)
nt, _ := throttler.NewNumGoRoutineThrottler(max)

for i := int32(0); i < max; i++ {
nt.StartProcessing()
}

assert.False(t, nt.CanProcess())
}

func TestNumGoRoutineThrottler_CanProcessMessageCounterIsMaxLessOneFromEndProcessMessage(t *testing.T) {
t.Parallel()

max := int32(45)
nt, _ := throttler.NewNumGoRoutineThrottler(max)

for i := int32(0); i < max; i++ {
nt.StartProcessing()
}
nt.EndProcessing()

assert.True(t, nt.CanProcess())
}
28 changes: 0 additions & 28 deletions integrationTests/frontend/wallet/txInterception_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,6 @@ import (
"github.com/stretchr/testify/assert"
)

func TestInterceptedTxFromFrontendGeneratedParamsWithoutData(t *testing.T) {
testInterceptedTxFromFrontendGeneratedParams(
t,
0,
big.NewInt(10),
"53669be65aac358a6add8e8a8b1251bb994dc1e4a0cc885956f5ecd53396f0d8",
"fe73b8960894941bcf100f7378dba2a6fa2591343413710073c2515817b27dc5",
"f2ae2ad6585f3b44bbbe84f93c3c5ec04a53799d24c04a1dd519666f2cd3dc3d7fbe6c75550b0eb3567fdc0708a8534ae3e5393d0dd9e03c70972f2e716a7007",
0,
0,
"",
)
}

func TestInterceptedTxFromFrontendGeneratedParams(t *testing.T) {
testInterceptedTxFromFrontendGeneratedParams(
t,
0,
big.NewInt(10),
"53669be65aac358a6add8e8a8b1251bb994dc1e4a0cc885956f5ecd53396f0d8",
"6c9f95220912dfe4d7be57c26f8f4d1594fee53fc6d958fb9009ed744a681a5a",
"e0e5d089dd7d47abfeabf17f4d4ab0022c32b844dfd8124e45c20370d1a1049202c50d8e9c4e8841ce65848b5f0503212e9879f0556706dc6a849d789dfdcb01",
0,
0,
"aa@bbbb@cccc",
)
}

func TestInterceptedTxFromFrontendGeneratedParamsAllParams(t *testing.T) {
testInterceptedTxFromFrontendGeneratedParams(
t,
Expand Down
27 changes: 0 additions & 27 deletions integrationTests/mock/feeHandlerMock.go

This file was deleted.

27 changes: 27 additions & 0 deletions integrationTests/mock/feeHandlerStub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package mock

type FeeHandlerStub struct {
MinGasPriceCalled func() uint64
MinGasLimitForTxCalled func() uint64
MinTxFeeCalled func() uint64
}

func (fhs *FeeHandlerStub) MinGasPrice() uint64 {
return fhs.MinGasPriceCalled()
}

func (fhs *FeeHandlerStub) MinGasLimitForTx() uint64 {
return fhs.MinGasLimitForTxCalled()
}

func (fhs *FeeHandlerStub) MinTxFee() uint64 {
return fhs.MinTxFeeCalled()
}

// IsInterfaceNil returns true if there is no value under the interface
func (fhs *FeeHandlerStub) IsInterfaceNil() bool {
if fhs == nil {
return true
}
return false
}
10 changes: 4 additions & 6 deletions integrationTests/multiShard/block/executingMiniblocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ func TestShouldProcessBlocksInMultiShardArchitecture(t *testing.T) {

valMinting := big.NewInt(100)
valToTransferPerTx := big.NewInt(2)
gasPricePerTx := uint64(2)
gasLimitPerTx := uint64(2)

advertiser := integrationTests.CreateMessengerWithKadDht(context.Background(), "")
_ = advertiser.Bootstrap()
Expand Down Expand Up @@ -81,8 +79,8 @@ func TestShouldProcessBlocksInMultiShardArchitecture(t *testing.T) {
sendersPrivateKeys,
receiversPublicKeys,
valToTransferPerTx,
gasPricePerTx,
gasLimitPerTx,
integrationTests.MinTxGasPrice,
integrationTests.MinTxGasLimit,
)
fmt.Println("Delaying for disseminating transactions...")
time.Sleep(time.Second * 5)
Expand All @@ -94,8 +92,8 @@ func TestShouldProcessBlocksInMultiShardArchitecture(t *testing.T) {
round, nonce = integrationTests.ProposeAndSyncOneBlock(t, nodes, idxProposers, round, nonce)
}

gasPricePerTxBigInt := big.NewInt(int64(gasPricePerTx))
gasLimitPerTxBigInt := big.NewInt(int64(gasLimitPerTx))
gasPricePerTxBigInt := big.NewInt(0).SetUint64(integrationTests.MinTxGasPrice)
gasLimitPerTxBigInt := big.NewInt(0).SetUint64(integrationTests.MinTxGasLimit)
gasValue := big.NewInt(0).Mul(gasPricePerTxBigInt, gasLimitPerTxBigInt)
totalValuePerTx := big.NewInt(0).Add(gasValue, valToTransferPerTx)
fmt.Println("Test nodes from proposer shard to have the correct balances...")
Expand Down
Loading

0 comments on commit e35fd4b

Please sign in to comment.