Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into fix-gas-estimation
Browse files Browse the repository at this point in the history
  • Loading branch information
otherview committed Jul 10, 2024
2 parents 9535b6d + bfa83f3 commit ee58f62
Show file tree
Hide file tree
Showing 19 changed files with 361 additions and 120 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/go-mod-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ jobs:
name: ensure go mod is tidy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.22'
cache: false

- name: go mod tidy
run: |
go mod tidy
- name: check go module
run: |
if ! git diff --exit-code --quiet; then
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/lint-go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ jobs:
name: golangci-lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.22'
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v6
with:
version: v1.55.2
# use the default if on main branch, otherwise use the pull request config
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/on-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
uses: actions/checkout@v4

- name: Setup Node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: '20'

Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/test-e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
uses: docker/setup-buildx-action@v3

- name: Build and export
uses: docker/build-push-action@v5
uses: docker/build-push-action@v6
with:
context: .
tags: vechain/thor:${{ github.sha }}
Expand All @@ -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
8 changes: 4 additions & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Install Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}

Expand All @@ -41,10 +41,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Install Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: 1.22.x

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
Loading

0 comments on commit ee58f62

Please sign in to comment.