Skip to content

Commit

Permalink
api: fix max result in debug trace storage (#813)
Browse files Browse the repository at this point in the history
* api: fix max result in debug trace storage

* add tests

* minor improvement of dockerfile
  • Loading branch information
libotony authored Aug 9, 2024
1 parent 935b91b commit e142eee
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build thor in a stock Go builder container
FROM golang:1.22-alpine3.20 as builder
FROM golang:1.22-alpine3.20 AS builder

RUN apk add --no-cache make gcc musl-dev linux-headers git
WORKDIR /go/thor
Expand Down
11 changes: 11 additions & 0 deletions api/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (
"github.com/vechain/thor/v2/xenv"
)

const defaultMaxStorageResult = 1000

var devNetGenesisID = genesis.NewDevnet().ID()

type Debug struct {
Expand Down Expand Up @@ -296,6 +298,15 @@ func (d *Debug) handleDebugStorage(w http.ResponseWriter, req *http.Request) err
if err := utils.ParseJSON(req.Body, &opt); err != nil {
return utils.BadRequest(errors.WithMessage(err, "body"))
}

if opt.MaxResult > defaultMaxStorageResult {
return utils.BadRequest(errors.Errorf("maxResult: exceeds limit of %d", defaultMaxStorageResult))
}

if opt.MaxResult == 0 {
opt.MaxResult = defaultMaxStorageResult
}

blockID, txIndex, clauseIndex, err := d.parseTarget(opt.Target)
if err != nil {
return err
Expand Down
57 changes: 55 additions & 2 deletions api/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/vechain/thor/v2/block"
"github.com/vechain/thor/v2/builtin"
"github.com/vechain/thor/v2/chain"
"github.com/vechain/thor/v2/cmd/thor/solo"
"github.com/vechain/thor/v2/genesis"
Expand Down Expand Up @@ -84,8 +85,9 @@ func TestDebug(t *testing.T) {

// /storage/range endpoint
for name, tt := range map[string]func(*testing.T){
"testStorageRangeWithError": testStorageRangeWithError,
"testStorageRange": testStorageRange,
"testStorageRangeWithError": testStorageRangeWithError,
"testStorageRange": testStorageRange,
"testStorageRangeDefaultOption": testStorageRangeDefaultOption,
} {
t.Run(name, tt)
}
Expand Down Expand Up @@ -121,6 +123,39 @@ func TestStorageRangeFunc(t *testing.T) {
assert.Equal(t, 1, len(storage))
}

func TestStorageRangeMaxResult(t *testing.T) {
db := muxdb.NewMem()
state := state.New(db, thor.Bytes32{}, 0, 0, 0)

addr := thor.BytesToAddress([]byte("account1"))
for i := 0; i < 1001; i++ {
key := thor.BytesToBytes32([]byte(fmt.Sprintf("key%d", i)))
value := thor.BytesToBytes32([]byte(fmt.Sprintf("value%d", i)))
state.SetRawStorage(addr, key, value[:])
}

trie, err := state.BuildStorageTrie(addr)
if err != nil {
t.Fatal(err)
}
start, err := hexutil.Decode("0x00")
if err != nil {
t.Fatal(err)
}

storageRangeRes, err := storageRangeAt(trie, start, 1001)
assert.NoError(t, err)
assert.Equal(t, 1001, len(storageRangeRes.Storage))

storageRangeRes, err = storageRangeAt(trie, start, 1000)
assert.NoError(t, err)
assert.Equal(t, 1000, len(storageRangeRes.Storage))

storageRangeRes, err = storageRangeAt(trie, start, 10)
assert.NoError(t, err)
assert.Equal(t, 10, len(storageRangeRes.Storage))
}

func testTraceClauseWithEmptyTracerTarget(t *testing.T) {
res := httpPostAndCheckResponseStatus(t, ts.URL+"/debug/tracers", &TraceClauseOption{}, 400)
assert.Equal(t, "target: unsupported", strings.TrimSpace(res))
Expand Down Expand Up @@ -418,6 +453,9 @@ func testStorageRangeWithError(t *testing.T) {
// Error case 2: bad StorageRangeOption
badBodyRequest := 123
httpPostAndCheckResponseStatus(t, ts.URL+"/debug/storage-range", badBodyRequest, 400)

badMaxResult := &StorageRangeOption{MaxResult: 1001}
httpPostAndCheckResponseStatus(t, ts.URL+"/debug/storage-range", badMaxResult, 400)
}

func testStorageRange(t *testing.T) {
Expand All @@ -441,6 +479,21 @@ func testStorageRange(t *testing.T) {
assert.Equal(t, expectedStorageRangeResult, parsedExecutionRes)
}

func testStorageRangeDefaultOption(t *testing.T) {
opt := StorageRangeOption{
Address: builtin.Energy.Address,
Target: fmt.Sprintf("%s/%s/0", blk.Header().ID(), transaction.ID()),
}

res := httpPostAndCheckResponseStatus(t, ts.URL+"/debug/storage-range", &opt, 200)

var storageRangeRes *StorageRangeResult
if err := json.Unmarshal([]byte(res), &storageRangeRes); err != nil {
t.Fatal(err)
}
assert.NotZero(t, len(storageRangeRes.Storage))
}

func initDebugServer(t *testing.T) {
db := muxdb.NewMem()
stater := state.NewStater(db)
Expand Down

0 comments on commit e142eee

Please sign in to comment.