-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Collection] Rate limiting transaction by payer #5218
Conversation
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## master #5218 +/- ##
=======================================
Coverage 55.47% 55.47%
=======================================
Files 996 997 +1
Lines 95787 95866 +79
=======================================
+ Hits 53136 53181 +45
- Misses 38661 38689 +28
- Partials 3990 3996 +6
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
engine/collection/ingest/engine.go
Outdated
@@ -70,6 +70,7 @@ func New( | |||
MaxTransactionByteSize: config.MaxTransactionByteSize, | |||
MaxCollectionByteSize: config.MaxCollectionByteSize, | |||
}, | |||
access.NewNoopLimit(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it intended?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a placeholder. The actual rate limiter is not implemented, which needs some discussion.
This PR implemented the check for rate limiting transactions when they are received and before being added to the mempool.
b658972
to
62dec77
Compare
} | ||
} | ||
|
||
func NewTransactionValidatorWithLimiter( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a new method, so that the emulator can still depend on the existing NewTransactionValidator
method
access/errors.go
Outdated
} | ||
|
||
func (e InvalidTxRateLimittedError) Error() string { | ||
return fmt.Sprintf("transaction rate limitted for payer (%s)", e.Payer) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the error message returned to the client which sends the transaction to the AN.
Is it possible to alter the error message?
} | ||
|
||
// AddressRateLimiter limits the rate of messages sent to a given address | ||
// It allows the given "limit" amount messages per second with a "burst" amount of messages to be sent at once |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kc1116 could you review the rate limiter implementation? Please check if it's implementing the correct behavior explained here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good this is the expected behavior, you can also cross reference with the godoc for the rate package
access/errors.go
Outdated
@@ -90,3 +90,11 @@ type InvalidTxByteSizeError struct { | |||
func (e InvalidTxByteSizeError) Error() string { | |||
return fmt.Sprintf("transaction byte size (%d) exceeds the maximum byte size allowed for a transaction (%d)", e.Actual, e.Maximum) | |||
} | |||
|
|||
type InvalidTxRateLimittedError struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type InvalidTxRateLimittedError struct { | |
type InvalidTxRateLimitedError struct { |
A few other similar typos in the PR, could do a find+replace limitted->limited
.
limit: limit, | ||
burst: burst, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is worth documenting in this new component how these rate limiter configs set on individual nodes will translate to network-wide effective rate limits. I think the assumption of someone setting these configs would otherwise reasonably be that the limit value they set will translate more or less directly to the network-wide effect limit, but this isn't the case.
For simplicity, let's assume that all Collection Nodes in the network are configured with the same limit
, burst
, and payerAddresses
config.
The question is, if I configure all Collection Nodes with this common individual config, with limit
There are two parameters I can think of that will impact this:
- The number of clusters
- The propagation redundancy factor - how many nodes we replicate a transaction to upon receiving it
The cluster count (
The impact of the propagation redundancy factor is less straightforward, but will further increase
There's some uncertainty in the second term, but overall, my gut feeling is that setting a rate limit of for example 1TPS in this config would result in an effective rate limit of somewhere in the range of 5-10TPS. That's fairly unintuitive, so we should be aware of it when using this, and document it.
good1 := unittest.RandomAddressFixture() | ||
limited1 := unittest.RandomAddressFixture() | ||
limited2 := unittest.RandomAddressFixture() | ||
fmt.Println(limited1, limited2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should remove or change to t.Log
l.AddAddress(limited2) | ||
list := l.GetAddresses() | ||
require.Len(t, list, 2) | ||
require.Contains(t, list, limited1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
require.Contains(t, list, limited1) | |
require.Contains(t, list, limited2) |
Should this be limited2
since we just added it?
require.False(t, l.IsRateLimited(addr1)) | ||
require.True(t, l.IsRateLimited(addr1)) | ||
|
||
time.Sleep(time.Millisecond * 1100) // wait 1.1 second |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
time.Sleep(time.Millisecond * 1100) // wait 1.1 second | |
time.Sleep(time.Millisecond * 1100) // wait 1.1 second |
Could we set numPerSec
to eg. 0.1 and then wait 110ms?
Better to minimize sleeps in tests if possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use require.Eventually
require.Eventually(t, func() bool {
return l.IsRateLimited(addr1) == false && l.IsRateLimited(addr1) == true
}, 2*time.Second, 100*time.Millisecond)
@@ -84,17 +83,26 @@ func TestLimiterWaitLongEnough(t *testing.T) { | |||
|
|||
addr1 := unittest.RandomAddressFixture() | |||
|
|||
numPerSec := rate.Limit(1) | |||
// with limit set to 10, it means we allow 10 messages per second, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
1469927
to
657fe8b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work 🎸
- I added some code suggestions for documentation from the comment about the effective network-wide rate limit in my last review
- There is also this comment from the last review, which hasn't been addressed. Could you check to confirm that line is correct the way it is, if you haven't already? (If it is, feel free to resolve the comment)
c893dc7
to
680e815
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good other than comment about lock
Co-authored-by: Jordan Schalm <jordan@dapperlabs.com>
Co-authored-by: Jordan Schalm <jordan@dapperlabs.com>
Co-authored-by: Jordan Schalm <jordan@dapperlabs.com>
Co-authored-by: Khalil Claybon <khalil.claybon@dapperlabs.com>
Co-authored-by: Jordan Schalm <jordan@dapperlabs.com>
Co-authored-by: Peter Argue <89119817+peterargue@users.noreply.github.com>
4469cf0
to
e090e15
Compare
This PR adds a rate limiter to the collection node when adding to the transaction mempool as a short term solution for preventing from attacks.