-
Notifications
You must be signed in to change notification settings - Fork 180
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 #5218 from onflow/leo/rate-limit-transaction-by-payer
[Collection] Rate limiting transaction by payer
- Loading branch information
Showing
12 changed files
with
579 additions
and
6 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
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,122 @@ | ||
package collection | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/rs/zerolog/log" | ||
"golang.org/x/time/rate" | ||
|
||
"github.com/onflow/flow-go/admin" | ||
"github.com/onflow/flow-go/admin/commands" | ||
"github.com/onflow/flow-go/engine/collection/ingest" | ||
) | ||
|
||
var _ commands.AdminCommand = (*TxRateLimitCommand)(nil) | ||
|
||
// TxRateLimitCommand will adjust the transaction ingest rate limiter. | ||
type TxRateLimitCommand struct { | ||
limiter *ingest.AddressRateLimiter | ||
} | ||
|
||
type TxRateLimitCommandAddress struct { | ||
Addresses []string | ||
} | ||
|
||
func NewTxRateLimitCommand(limiter *ingest.AddressRateLimiter) *TxRateLimitCommand { | ||
return &TxRateLimitCommand{ | ||
limiter: limiter, | ||
} | ||
} | ||
|
||
func (s *TxRateLimitCommand) Handler(_ context.Context, req *admin.CommandRequest) (interface{}, error) { | ||
input, ok := req.Data.(map[string]interface{}) | ||
if !ok { | ||
return admin.NewInvalidAdminReqFormatError("expected { \"command\": \"add|remove|get|get_config|set_config\", \"addresses\": \"addresses\""), nil | ||
} | ||
|
||
command, ok := input["command"] | ||
if !ok { | ||
return admin.NewInvalidAdminReqErrorf("the \"command\" field is empty, must be one of add|remove|get|get_config|set_config"), nil | ||
} | ||
|
||
cmd, ok := command.(string) | ||
if !ok { | ||
return admin.NewInvalidAdminReqErrorf("the \"command\" field is not string, must be one of add|remove|get|get_config|set_config"), nil | ||
} | ||
|
||
if cmd == "get" { | ||
list := s.limiter.GetAddresses() | ||
return fmt.Sprintf("rate limited list contains a total of %d addresses: %v", len(list), list), nil | ||
} | ||
|
||
if cmd == "add" || cmd == "remove" { | ||
result, ok := input["addresses"] | ||
if !ok { | ||
return admin.NewInvalidAdminReqErrorf("the \"addresses\" field is empty, must be hex formated addresses, can be splitted by \",\""), nil | ||
} | ||
addresses, ok := result.(string) | ||
if !ok { | ||
return admin.NewInvalidAdminReqErrorf("the \"addresses\" field is not string, must be hex formated addresses, can be splitted by \",\""), nil | ||
} | ||
|
||
log.Info().Msgf("admintool %v addresses: %v", cmd, addresses) | ||
|
||
resp, err := s.AddOrRemove(cmd, addresses) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return resp, nil | ||
} | ||
|
||
if cmd == "get_config" { | ||
limit, burst := s.limiter.GetLimitConfig() | ||
return fmt.Sprintf("limit: %v, burst: %v", limit, burst), nil | ||
} | ||
|
||
if cmd == "set_config" { | ||
dataLimit, limit_ok := input["limit"] | ||
dataBurst, burst_ok := input["burst"] | ||
if !burst_ok || !limit_ok { | ||
return admin.NewInvalidAdminReqErrorf("the \"limit\" or \"burst\" field is empty, must be number"), nil | ||
} | ||
limit, ok := dataLimit.(float64) | ||
if !ok { | ||
return admin.NewInvalidAdminReqErrorf("the \"limit\" field is not number: %v", dataLimit), nil | ||
} | ||
|
||
burst, ok := dataBurst.(float64) | ||
if !ok { | ||
return admin.NewInvalidAdminReqErrorf("the \"burst\" field is not number: %v", dataBurst), nil | ||
} | ||
|
||
oldLimit, oldBurst := s.limiter.GetLimitConfig() | ||
log.Info().Msgf("admintool set_config limit: %v, burst: %v, old limit: %v, old burst: %v", limit, burst, oldLimit, oldBurst) | ||
s.limiter.SetLimitConfig(rate.Limit(limit), int(burst)) | ||
return fmt.Sprintf("succesfully set limit %v, burst %v", limit, burst), nil | ||
} | ||
|
||
return fmt.Sprintf( | ||
"invalid command field (%s), must be either \"add\" or \"remove\" or \"get\" or \"get_config\" or \"set_config\"", | ||
cmd), nil | ||
} | ||
|
||
func (s *TxRateLimitCommand) Validator(req *admin.CommandRequest) error { | ||
return nil | ||
} | ||
|
||
func (s *TxRateLimitCommand) AddOrRemove(command string, addresses string) (string, error) { | ||
addrList, err := ingest.ParseAddresses(addresses) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if command == "add" { | ||
ingest.AddAddresses(s.limiter, addrList) | ||
return fmt.Sprintf("added %d addresses", len(addrList)), nil | ||
} | ||
|
||
// command == "remove" | ||
ingest.RemoveAddresses(s.limiter, addrList) | ||
return fmt.Sprintf("removed %d addresses", len(addrList)), nil | ||
} |
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
Oops, something went wrong.