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

add api-logs-limit flag #777

Merged
merged 5 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test-e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ jobs:
uses: actions/checkout@v4
with:
repository: vechain/thor-e2e-tests
# https://github.com/vechain/thor-e2e-tests/tree/2cb22d804bb3cdf075917dbece42a182d42d7486
ref: 2cb22d804bb3cdf075917dbece42a182d42d7486
# https://github.com/vechain/thor-e2e-tests/tree/00bd3f1b949b05da94e82686e0089a11a136c34c
ref: 00bd3f1b949b05da94e82686e0089a11a136c34c

- name: Download artifact
uses: actions/download-artifact@v4
Expand Down
5 changes: 3 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func New(
allowCustomTracer bool,
enableReqLogger bool,
enableMetrics bool,
logsLimit uint64,
) (http.HandlerFunc, func()) {
origins := strings.Split(strings.TrimSpace(allowedOrigins), ",")
for i, o := range origins {
Expand All @@ -72,9 +73,9 @@ func New(
Mount(router, "/accounts")

if !skipLogs {
events.New(repo, logDB).
events.New(repo, logDB, logsLimit).
Mount(router, "/logs/event")
transfers.New(repo, logDB).
transfers.New(repo, logDB, logsLimit).
Mount(router, "/logs/transfer")
}
blocks.New(repo, bft).
Expand Down
18 changes: 15 additions & 3 deletions api/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ import (
)

type Events struct {
repo *chain.Repository
db *logdb.LogDB
repo *chain.Repository
db *logdb.LogDB
limit uint64
}

func New(repo *chain.Repository, db *logdb.LogDB) *Events {
func New(repo *chain.Repository, db *logdb.LogDB, logsLimit uint64) *Events {
return &Events{
repo,
db,
logsLimit,
}
}

Expand All @@ -51,6 +53,16 @@ func (e *Events) handleFilter(w http.ResponseWriter, req *http.Request) error {
if err := utils.ParseJSON(req.Body, &filter); err != nil {
return utils.BadRequest(errors.WithMessage(err, "body"))
}
if filter.Options != nil && filter.Options.Limit > e.limit {
return utils.Forbidden(errors.New("options.limit exceeds the maximum allowed value"))
}
if filter.Options == nil {
filter.Options = &logdb.Options{
Offset: 0,
Limit: e.limit,
}
}

fes, err := e.filter(req.Context(), &filter)
if err != nil {
return err
Expand Down
43 changes: 39 additions & 4 deletions api/events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/gorilla/mux"
Expand All @@ -26,6 +27,8 @@ import (
"github.com/vechain/thor/v2/tx"
)

const defaultLogLimit uint64 = 1000

var ts *httptest.Server

var (
Expand All @@ -35,7 +38,7 @@ var (

func TestEmptyEvents(t *testing.T) {
db := createDb(t)
initEventServer(t, db)
initEventServer(t, db, defaultLogLimit)
defer ts.Close()

testEventsBadRequest(t)
Expand All @@ -44,7 +47,7 @@ func TestEmptyEvents(t *testing.T) {

func TestEvents(t *testing.T) {
db := createDb(t)
initEventServer(t, db)
initEventServer(t, db, defaultLogLimit)
defer ts.Close()

blocksToInsert := 5
Expand All @@ -53,6 +56,38 @@ func TestEvents(t *testing.T) {
testEventWithBlocks(t, blocksToInsert)
}

func TestOption(t *testing.T) {
db := createDb(t)
initEventServer(t, db, 5)
defer ts.Close()
insertBlocks(t, db, 10)

filter := events.EventFilter{
CriteriaSet: make([]*events.EventCriteria, 0),
Range: nil,
Options: &logdb.Options{Limit: 6},
Order: logdb.DESC,
}

res, statusCode := httpPost(t, ts.URL+"/events", filter)
assert.Equal(t, "options.limit exceeds the maximum allowed value", strings.Trim(string(res), "\n"))
assert.Equal(t, http.StatusForbidden, statusCode)

filter.Options.Limit = 5
_, statusCode = httpPost(t, ts.URL+"/events", filter)
assert.Equal(t, http.StatusOK, statusCode)

// with nil options, should use default limit
filter.Options = nil
res, statusCode = httpPost(t, ts.URL+"/events", filter)
var tLogs []*events.FilteredEvent
if err := json.Unmarshal(res, &tLogs); err != nil {
t.Fatal(err)
}
assert.Equal(t, http.StatusOK, statusCode)
assert.Equal(t, 5, len(tLogs))
}

// Test functions
func testEventsBadRequest(t *testing.T) {
badBody := []byte{0x00, 0x01, 0x02}
Expand Down Expand Up @@ -128,7 +163,7 @@ func testEventWithBlocks(t *testing.T, expectedBlocks int) {
}

// Init functions
func initEventServer(t *testing.T, logDb *logdb.LogDB) {
func initEventServer(t *testing.T, logDb *logdb.LogDB, limit uint64) {
router := mux.NewRouter()

muxDb := muxdb.NewMem()
Expand All @@ -142,7 +177,7 @@ func initEventServer(t *testing.T, logDb *logdb.LogDB) {

repo, _ := chain.NewRepository(muxDb, b)

events.New(repo, logDb).Mount(router, "/events")
events.New(repo, logDb, limit).Mount(router, "/events")
ts = httptest.NewServer(router)
}

Expand Down
18 changes: 15 additions & 3 deletions api/transfers/transfers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ import (
)

type Transfers struct {
repo *chain.Repository
db *logdb.LogDB
repo *chain.Repository
db *logdb.LogDB
limit uint64
}

func New(repo *chain.Repository, db *logdb.LogDB) *Transfers {
func New(repo *chain.Repository, db *logdb.LogDB, logsLimit uint64) *Transfers {
return &Transfers{
repo,
db,
logsLimit,
}
}

Expand Down Expand Up @@ -57,6 +59,16 @@ func (t *Transfers) handleFilterTransferLogs(w http.ResponseWriter, req *http.Re
if err := utils.ParseJSON(req.Body, &filter); err != nil {
return utils.BadRequest(errors.WithMessage(err, "body"))
}
if filter.Options != nil && filter.Options.Limit > t.limit {
return utils.Forbidden(errors.New("options.limit exceeds the maximum allowed value"))
}
if filter.Options == nil {
filter.Options = &logdb.Options{
Offset: 0,
Limit: t.limit,
}
}

tLogs, err := t.filter(req.Context(), &filter)
if err != nil {
return err
Expand Down
44 changes: 40 additions & 4 deletions api/transfers/transfers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import (
"math/big"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/vechain/thor/v2/api/events"
"github.com/vechain/thor/v2/api/transfers"
"github.com/vechain/thor/v2/block"
"github.com/vechain/thor/v2/chain"
Expand All @@ -28,11 +30,13 @@ import (
"github.com/vechain/thor/v2/tx"
)

const defaultLogLimit uint64 = 1000

var ts *httptest.Server

func TestEmptyTransfers(t *testing.T) {
db := createDb(t)
initTransferServer(t, db)
initTransferServer(t, db, defaultLogLimit)
defer ts.Close()

testTransferBadRequest(t)
Expand All @@ -41,7 +45,7 @@ func TestEmptyTransfers(t *testing.T) {

func TestTransfers(t *testing.T) {
db := createDb(t)
initTransferServer(t, db)
initTransferServer(t, db, defaultLogLimit)
defer ts.Close()

blocksToInsert := 5
Expand All @@ -50,6 +54,38 @@ func TestTransfers(t *testing.T) {
testTransferWithBlocks(t, blocksToInsert)
}

func TestOption(t *testing.T) {
db := createDb(t)
initTransferServer(t, db, 5)
defer ts.Close()
insertBlocks(t, db, 10)

filter := transfers.TransferFilter{
CriteriaSet: make([]*logdb.TransferCriteria, 0),
Range: nil,
Options: &logdb.Options{Limit: 6},
Order: logdb.DESC,
}

res, statusCode := httpPost(t, ts.URL+"/transfers", filter)
assert.Equal(t, "options.limit exceeds the maximum allowed value", strings.Trim(string(res), "\n"))
assert.Equal(t, http.StatusForbidden, statusCode)

filter.Options.Limit = 5
_, statusCode = httpPost(t, ts.URL+"/transfers", filter)
assert.Equal(t, http.StatusOK, statusCode)

// with nil options, should use default limit
filter.Options = nil
res, statusCode = httpPost(t, ts.URL+"/transfers", filter)
var tLogs []*events.FilteredEvent
if err := json.Unmarshal(res, &tLogs); err != nil {
t.Fatal(err)
}
assert.Equal(t, http.StatusOK, statusCode)
assert.Equal(t, 5, len(tLogs))
}

// Test functions
func testTransferBadRequest(t *testing.T) {
badBody := []byte{0x00, 0x01, 0x02}
Expand Down Expand Up @@ -119,7 +155,7 @@ func insertBlocks(t *testing.T, db *logdb.LogDB, n int) {
}
}

func initTransferServer(t *testing.T, logDb *logdb.LogDB) {
func initTransferServer(t *testing.T, logDb *logdb.LogDB, limit uint64) {
router := mux.NewRouter()

muxDb := muxdb.NewMem()
Expand All @@ -133,7 +169,7 @@ func initTransferServer(t *testing.T, logDb *logdb.LogDB) {

repo, _ := chain.NewRepository(muxDb, b)

transfers.New(repo, logDb).Mount(router, "/transfers")
transfers.New(repo, logDb, limit).Mount(router, "/transfers")
ts = httptest.NewServer(router)
}

Expand Down
5 changes: 5 additions & 0 deletions cmd/thor/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ var (
Name: "api-allow-custom-tracer",
Usage: "allow custom JS tracer to be used tracer API",
}
apiLogsLimitFlag = cli.IntFlag{
libotony marked this conversation as resolved.
Show resolved Hide resolved
Name: "api-logs-limit",
Value: 1000,
Usage: "limit the number of logs returned by /logs API",
}
enableAPILogsFlag = cli.BoolFlag{
Name: "enable-api-logs",
Usage: "enables API requests logging",
Expand Down
4 changes: 4 additions & 0 deletions cmd/thor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func main() {
apiBacktraceLimitFlag,
apiAllowCustomTracerFlag,
enableAPILogsFlag,
apiLogsLimitFlag,
verbosityFlag,
maxPeersFlag,
p2pPortFlag,
Expand Down Expand Up @@ -109,6 +110,7 @@ func main() {
apiBacktraceLimitFlag,
apiAllowCustomTracerFlag,
enableAPILogsFlag,
apiLogsLimitFlag,
onDemandFlag,
blockInterval,
persistFlag,
Expand Down Expand Up @@ -234,6 +236,7 @@ func defaultAction(ctx *cli.Context) error {
ctx.Bool(apiAllowCustomTracerFlag.Name),
ctx.Bool(enableAPILogsFlag.Name),
ctx.Bool(enableMetricsFlag.Name),
uint64(ctx.Int(apiLogsLimitFlag.Name)),
)
defer func() { log.Info("closing API..."); apiCloser() }()

Expand Down Expand Up @@ -363,6 +366,7 @@ func soloAction(ctx *cli.Context) error {
ctx.Bool(apiAllowCustomTracerFlag.Name),
ctx.Bool(enableAPILogsFlag.Name),
ctx.Bool(enableMetricsFlag.Name),
uint64(ctx.Int(apiLogsLimitFlag.Name)),
)
defer func() { log.Info("closing API..."); apiCloser() }()

Expand Down
1 change: 1 addition & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ bin/thor -h
| `--api-backtrace-limit` | Limit the distance between 'position' and best block for subscriptions APIs (default: 1000) |
| `--api-allow-custom-tracer` | Allow custom JS tracer to be used for the tracer API |
| `--enable-api-logs` | Enables API requests logging |
| `--api-logs-limit` | Limit the number of logs returned by /logs API (default: 1000) |
| `--verbosity` | Log verbosity (0-9) (default: 3) |
| `--max-peers` | Maximum number of P2P network peers (P2P network disabled if set to 0) (default: 25) |
| `--p2p-port` | P2P network listening port (default: 11235) |
Expand Down
Loading
Loading