Skip to content
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

Merged
merged 32 commits into from
Jan 30, 2024

Conversation

zhangchiqing
Copy link
Member

@zhangchiqing zhangchiqing commented Jan 9, 2024

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.

@codecov-commenter
Copy link

codecov-commenter commented Jan 9, 2024

Codecov Report

Attention: 26 lines in your changes are missing coverage. Please review.

Comparison is base (690a250) 55.47% compared to head (74e0be0) 55.47%.

Files Patch % Lines
engine/collection/ingest/rate_limiter.go 68.05% 22 Missing and 1 partial ⚠️
model/flow/address.go 62.50% 2 Missing and 1 partial ⚠️
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     
Flag Coverage Δ
unittests 55.47% <68.29%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -70,6 +70,7 @@ func New(
MaxTransactionByteSize: config.MaxTransactionByteSize,
MaxCollectionByteSize: config.MaxCollectionByteSize,
},
access.NewNoopLimit(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it intended?

Copy link
Member Author

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.

@zhangchiqing zhangchiqing force-pushed the leo/rate-limit-transaction-by-payer branch from b658972 to 62dec77 Compare January 16, 2024 19:03
}
}

func NewTransactionValidatorWithLimiter(
Copy link
Member Author

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)
Copy link
Member Author

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
Copy link
Member Author

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.

Copy link
Contributor

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

@zhangchiqing zhangchiqing marked this pull request as ready for review January 16, 2024 23:12
@zhangchiqing zhangchiqing requested a review from kc1116 January 16, 2024 23:12
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type InvalidTxRateLimittedError struct {
type InvalidTxRateLimitedError struct {

A few other similar typos in the PR, could do a find+replace limitted->limited.

access/validator.go Show resolved Hide resolved
admin/commands/collection/tx_rate_limiter.go Outdated Show resolved Hide resolved
cmd/collection/main.go Outdated Show resolved Hide resolved
engine/collection/ingest/rate_limiter.go Outdated Show resolved Hide resolved
Comment on lines +24 to +41
limit: limit,
burst: burst,
Copy link
Member

@jordanschalm jordanschalm Jan 17, 2024

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 $L_i$ and burst $B_i$, what will the effective network-wide rate limit for a given payer be? Let's call the effective global rate limit $L_g$ and burst $B_g$

There are two parameters I can think of that will impact this:

The cluster count ($k$) will proportionally increase the effective $L_g$ and $B_g$, because each Collection Node only ingests at most $\frac{1}{k}$ transactions. For Mainnet, where we have 3 clusters, we would have $L_g=3L_i$ and $B_g=3B_i$.

The impact of the propagation redundancy factor is less straightforward, but will further increase $L_g$ and $B_g$. Roughly, I think it will increase the effective limits proportionally to $\frac{ClusterSize}{PropagationRedundancyFactor}$.

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)
Copy link
Member

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
require.Contains(t, list, limited1)
require.Contains(t, list, limited2)

Should this be limited2 since we just added it?

engine/collection/ingest/rate_limiter_test.go Outdated Show resolved Hide resolved
require.False(t, l.IsRateLimited(addr1))
require.True(t, l.IsRateLimited(addr1))

time.Sleep(time.Millisecond * 1100) // wait 1.1 second
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Contributor

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)

access/validator.go Outdated Show resolved Hide resolved
@@ -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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@zhangchiqing zhangchiqing force-pushed the leo/rate-limit-transaction-by-payer branch from 1469927 to 657fe8b Compare January 20, 2024 00:14
Copy link
Member

@jordanschalm jordanschalm left a 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)

access/validator.go Outdated Show resolved Hide resolved
cmd/collection/main.go Show resolved Hide resolved
engine/collection/ingest/rate_limiter.go Show resolved Hide resolved
@zhangchiqing zhangchiqing force-pushed the leo/rate-limit-transaction-by-payer branch 2 times, most recently from c893dc7 to 680e815 Compare January 24, 2024 19:19
Copy link
Contributor

@peterargue peterargue left a 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

engine/collection/ingest/rate_limiter.go Show resolved Hide resolved
zhangchiqing and others added 21 commits January 29, 2024 13:40
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>
@zhangchiqing zhangchiqing force-pushed the leo/rate-limit-transaction-by-payer branch from 4469cf0 to e090e15 Compare January 29, 2024 21:40
@zhangchiqing zhangchiqing added this pull request to the merge queue Jan 30, 2024
Merged via the queue into master with commit cc807f3 Jan 30, 2024
51 checks passed
@zhangchiqing zhangchiqing deleted the leo/rate-limit-transaction-by-payer branch January 30, 2024 19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants