forked from relayooor/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rate limit blocks submitted to the relay and DB (ethereum#25)
- Loading branch information
Showing
6 changed files
with
225 additions
and
19 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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package builder | ||
|
||
import ( | ||
"context" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/ethereum/go-ethereum/core/types" | ||
) | ||
|
||
type blockRateLimitSubmission struct { | ||
resultCh chan bool | ||
block *types.Block | ||
} | ||
|
||
type BlockSubmissionRateLimiter struct { | ||
submissionsCh chan blockRateLimitSubmission | ||
started uint32 | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
} | ||
|
||
func NewBlockSubmissionRateLimiter() *BlockSubmissionRateLimiter { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
r := &BlockSubmissionRateLimiter{ | ||
submissionsCh: make(chan blockRateLimitSubmission), | ||
started: uint32(0), | ||
ctx: ctx, | ||
cancel: cancel, | ||
} | ||
|
||
return r | ||
} | ||
func (r *BlockSubmissionRateLimiter) Limit(block *types.Block) chan bool { | ||
resultCh := make(chan bool, 1) | ||
if atomic.LoadUint32(&r.started) != 1 { | ||
resultCh <- true | ||
return resultCh | ||
} | ||
|
||
select { | ||
case r.submissionsCh <- blockRateLimitSubmission{ | ||
resultCh: resultCh, | ||
block: block, | ||
}: | ||
case <-r.ctx.Done(): | ||
resultCh <- true | ||
} | ||
return resultCh | ||
} | ||
|
||
func (r *BlockSubmissionRateLimiter) Start() { | ||
if !atomic.CompareAndSwapUint32(&r.started, 0, 1) { | ||
return | ||
} | ||
|
||
go r.rateLimit() | ||
} | ||
|
||
func (r *BlockSubmissionRateLimiter) rateLimit() { | ||
for r.ctx.Err() == nil { | ||
// Beginning of the rate limit bucket | ||
bestSubmission := <-r.submissionsCh | ||
|
||
bucketCutoffCh := time.After(100 * time.Millisecond) | ||
|
||
bucketClosed := false | ||
for !bucketClosed { | ||
select { | ||
case <-r.ctx.Done(): | ||
bucketClosed = true | ||
break | ||
case <-bucketCutoffCh: | ||
bucketClosed = true | ||
break | ||
case newSubmission := <-r.submissionsCh: | ||
if bestSubmission.block.Profit.Cmp(newSubmission.block.Profit) < 0 { | ||
bestSubmission.resultCh <- false | ||
bestSubmission = newSubmission | ||
} else { | ||
newSubmission.resultCh <- false | ||
} | ||
} | ||
} | ||
|
||
bestSubmission.resultCh <- true | ||
} | ||
} | ||
|
||
func (r *BlockSubmissionRateLimiter) Stop() { | ||
r.cancel() | ||
} |
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,72 @@ | ||
package builder | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
"time" | ||
|
||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLimit(t *testing.T) { | ||
rl := NewBlockSubmissionRateLimiter() | ||
|
||
// Check that before starting requests are passed through | ||
ch1 := rl.Limit(&types.Block{Profit: new(big.Int)}) | ||
ch2 := rl.Limit(&types.Block{Profit: new(big.Int)}) | ||
ch3 := rl.Limit(&types.Block{Profit: new(big.Int)}) | ||
|
||
time.Sleep(200 * time.Millisecond) | ||
|
||
for _, ch := range []chan bool{ch1, ch2, ch3} { | ||
select { | ||
case shouldSubmit := <-ch: | ||
require.True(t, shouldSubmit) | ||
default: | ||
t.Error("chan was not ready") | ||
} | ||
} | ||
|
||
// Check that after starting requests are rate limited | ||
rl.Start() | ||
|
||
// Check that before starting requests are passed through | ||
ch1 = rl.Limit(&types.Block{Profit: new(big.Int)}) | ||
ch2 = rl.Limit(&types.Block{Profit: new(big.Int)}) | ||
ch3 = rl.Limit(&types.Block{Profit: big.NewInt(1)}) | ||
|
||
time.Sleep(200 * time.Millisecond) | ||
|
||
for _, ch := range []chan bool{ch1, ch2, ch3} { | ||
select { | ||
case shouldSubmit := <-ch: | ||
if ch == ch3 { | ||
require.True(t, shouldSubmit) | ||
} else { | ||
require.False(t, shouldSubmit) | ||
} | ||
default: | ||
t.Error("chan was not ready") | ||
} | ||
} | ||
|
||
// Check that after stopping requests are passed through | ||
rl.Stop() | ||
|
||
ch1 = rl.Limit(&types.Block{Profit: new(big.Int)}) | ||
ch2 = rl.Limit(&types.Block{Profit: new(big.Int)}) | ||
ch3 = rl.Limit(&types.Block{Profit: new(big.Int)}) | ||
|
||
time.Sleep(200 * time.Millisecond) | ||
|
||
for _, ch := range []chan bool{ch1, ch2, ch3} { | ||
select { | ||
case shouldSubmit := <-ch: | ||
require.True(t, shouldSubmit) | ||
default: | ||
t.Error("chan was not ready") | ||
} | ||
} | ||
|
||
} |
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
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