-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #507 from ElrondNetwork/EN-4217-bugfixing-tx-flood
En 4217 bugfixing tx flood
- Loading branch information
Showing
38 changed files
with
984 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -154,7 +154,7 @@ | |
Type = "LRU" | ||
|
||
[TxBlockBodyDataPool] | ||
Size = 100 | ||
Size = 300 | ||
Type = "LRU" | ||
|
||
[StateBlockBodyDataPool] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.