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

Added revision next for optimizing call/estimate related endpoints #776

Merged
merged 20 commits into from
Jul 11, 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
2 changes: 1 addition & 1 deletion .github/workflows/test-e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
with:
repository: vechain/thor-e2e-tests
# https://github.com/vechain/thor-e2e-tests/tree/00bd3f1b949b05da94e82686e0089a11a136c34c
ref: 00bd3f1b949b05da94e82686e0089a11a136c34c
ref: 48aa71d61ee6438cc270f541a6a369731717ac06

- name: Download artifact
uses: actions/download-artifact@v4
Expand Down
66 changes: 35 additions & 31 deletions api/accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/pkg/errors"
"github.com/vechain/thor/v2/api/utils"
"github.com/vechain/thor/v2/bft"
"github.com/vechain/thor/v2/block"
"github.com/vechain/thor/v2/chain"
"github.com/vechain/thor/v2/runtime"
"github.com/vechain/thor/v2/state"
Expand Down Expand Up @@ -49,10 +50,8 @@ func New(
}
}

func (a *Accounts) getCode(addr thor.Address, summary *chain.BlockSummary) ([]byte, error) {
code, err := a.stater.
NewState(summary.Header.StateRoot(), summary.Header.Number(), summary.Conflicts, summary.SteadyNum).
GetCode(addr)
func (a *Accounts) getCode(addr thor.Address, state *state.State) ([]byte, error) {
code, err := state.GetCode(addr)
if err != nil {
return nil, err
}
Expand All @@ -65,26 +64,27 @@ func (a *Accounts) handleGetCode(w http.ResponseWriter, req *http.Request) error
if err != nil {
return utils.BadRequest(errors.WithMessage(err, "address"))
}
revision, err := utils.ParseRevision(req.URL.Query().Get("revision"))
revision, err := utils.ParseRevision(req.URL.Query().Get("revision"), false)
libotony marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return utils.BadRequest(errors.WithMessage(err, "revision"))
}
summary, err := utils.GetSummary(revision, a.repo, a.bft)

_, st, err := utils.GetSummaryAndState(revision, a.repo, a.bft, a.stater)
if err != nil {
if a.repo.IsNotFound(err) {
return utils.BadRequest(errors.WithMessage(err, "revision"))
}
return err
}
code, err := a.getCode(addr, summary)
code, err := a.getCode(addr, st)
if err != nil {
return err
}

return utils.WriteJSON(w, map[string]string{"code": hexutil.Encode(code)})
}

func (a *Accounts) getAccount(addr thor.Address, summary *chain.BlockSummary) (*Account, error) {
state := a.stater.NewState(summary.Header.StateRoot(), summary.Header.Number(), summary.Conflicts, summary.SteadyNum)
func (a *Accounts) getAccount(addr thor.Address, header *block.Header, state *state.State) (*Account, error) {
b, err := state.GetBalance(addr)
if err != nil {
return nil, err
Expand All @@ -93,7 +93,7 @@ func (a *Accounts) getAccount(addr thor.Address, summary *chain.BlockSummary) (*
if err != nil {
return nil, err
}
energy, err := state.GetEnergy(addr, summary.Header.Timestamp())
energy, err := state.GetEnergy(addr, header.Timestamp())
if err != nil {
return nil, err
}
Expand All @@ -105,11 +105,8 @@ func (a *Accounts) getAccount(addr thor.Address, summary *chain.BlockSummary) (*
}, nil
}

func (a *Accounts) getStorage(addr thor.Address, key thor.Bytes32, summary *chain.BlockSummary) (thor.Bytes32, error) {
storage, err := a.stater.
NewState(summary.Header.StateRoot(), summary.Header.Number(), summary.Conflicts, summary.SteadyNum).
GetStorage(addr, key)

func (a *Accounts) getStorage(addr thor.Address, key thor.Bytes32, state *state.State) (thor.Bytes32, error) {
storage, err := state.GetStorage(addr, key)
if err != nil {
return thor.Bytes32{}, err
}
Expand All @@ -121,18 +118,20 @@ func (a *Accounts) handleGetAccount(w http.ResponseWriter, req *http.Request) er
if err != nil {
return utils.BadRequest(errors.WithMessage(err, "address"))
}
revision, err := utils.ParseRevision(req.URL.Query().Get("revision"))
revision, err := utils.ParseRevision(req.URL.Query().Get("revision"), false)
if err != nil {
return utils.BadRequest(errors.WithMessage(err, "revision"))
}
summary, err := utils.GetSummary(revision, a.repo, a.bft)

summary, st, err := utils.GetSummaryAndState(revision, a.repo, a.bft, a.stater)
if err != nil {
if a.repo.IsNotFound(err) {
return utils.BadRequest(errors.WithMessage(err, "revision"))
}
return err
}
acc, err := a.getAccount(addr, summary)

acc, err := a.getAccount(addr, summary.Header, st)
if err != nil {
return err
}
Expand All @@ -148,18 +147,20 @@ func (a *Accounts) handleGetStorage(w http.ResponseWriter, req *http.Request) er
if err != nil {
return utils.BadRequest(errors.WithMessage(err, "key"))
}
revision, err := utils.ParseRevision(req.URL.Query().Get("revision"))
revision, err := utils.ParseRevision(req.URL.Query().Get("revision"), false)
if err != nil {
return utils.BadRequest(errors.WithMessage(err, "revision"))
}
summary, err := utils.GetSummary(revision, a.repo, a.bft)

_, st, err := utils.GetSummaryAndState(revision, a.repo, a.bft, a.stater)
if err != nil {
if a.repo.IsNotFound(err) {
return utils.BadRequest(errors.WithMessage(err, "revision"))
}
return err
}
storage, err := a.getStorage(addr, key, summary)

storage, err := a.getStorage(addr, key, st)
if err != nil {
return err
}
Expand All @@ -171,11 +172,11 @@ func (a *Accounts) handleCallContract(w http.ResponseWriter, req *http.Request)
if err := utils.ParseJSON(req.Body, &callData); err != nil {
return utils.BadRequest(errors.WithMessage(err, "body"))
}
revision, err := utils.ParseRevision(req.URL.Query().Get("revision"))
revision, err := utils.ParseRevision(req.URL.Query().Get("revision"), true)
if err != nil {
return utils.BadRequest(errors.WithMessage(err, "revision"))
}
summary, err := utils.GetSummary(revision, a.repo, a.bft)
summary, st, err := utils.GetSummaryAndState(revision, a.repo, a.bft, a.stater)
if err != nil {
if a.repo.IsNotFound(err) {
return utils.BadRequest(errors.WithMessage(err, "revision"))
Expand All @@ -202,7 +203,7 @@ func (a *Accounts) handleCallContract(w http.ResponseWriter, req *http.Request)
GasPrice: callData.GasPrice,
Caller: callData.Caller,
}
results, err := a.batchCall(req.Context(), batchCallData, summary)
results, err := a.batchCall(req.Context(), batchCallData, summary.Header, st)
if err != nil {
return err
}
Expand All @@ -214,34 +215,37 @@ func (a *Accounts) handleCallBatchCode(w http.ResponseWriter, req *http.Request)
if err := utils.ParseJSON(req.Body, &batchCallData); err != nil {
return utils.BadRequest(errors.WithMessage(err, "body"))
}
revision, err := utils.ParseRevision(req.URL.Query().Get("revision"))
revision, err := utils.ParseRevision(req.URL.Query().Get("revision"), true)
if err != nil {
return utils.BadRequest(errors.WithMessage(err, "revision"))
}
summary, err := utils.GetSummary(revision, a.repo, a.bft)
summary, st, err := utils.GetSummaryAndState(revision, a.repo, a.bft, a.stater)
if err != nil {
if a.repo.IsNotFound(err) {
return utils.BadRequest(errors.WithMessage(err, "revision"))
}
return err
}
results, err := a.batchCall(req.Context(), batchCallData, summary)
results, err := a.batchCall(req.Context(), batchCallData, summary.Header, st)
if err != nil {
return err
}
return utils.WriteJSON(w, results)
}

func (a *Accounts) batchCall(ctx context.Context, batchCallData *BatchCallData, summary *chain.BlockSummary) (results BatchCallResults, err error) {
func (a *Accounts) batchCall(
ctx context.Context,
batchCallData *BatchCallData,
header *block.Header,
st *state.State,
) (results BatchCallResults, err error) {
txCtx, gas, clauses, err := a.handleBatchCallData(batchCallData)
if err != nil {
return nil, err
}
header := summary.Header
state := a.stater.NewState(header.StateRoot(), header.Number(), summary.Conflicts, summary.SteadyNum)

signer, _ := header.Signer()
rt := runtime.New(a.repo.NewChain(header.ParentID()), state,
rt := runtime.New(a.repo.NewChain(header.ParentID()), st,
&xenv.BlockContext{
Beneficiary: header.Beneficiary(),
Signer: signer,
Expand Down
11 changes: 11 additions & 0 deletions api/accounts/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,13 @@ func callContract(t *testing.T) {
reqBody := &accounts.CallData{
Data: hexutil.Encode(input),
}

// next revisoun should be valid
_, statusCode = httpPost(t, ts.URL+"/accounts/"+contractAddr.String()+"?revision=next", reqBody)
assert.Equal(t, http.StatusOK, statusCode, "next revision should be okay")
_, statusCode = httpPost(t, ts.URL+"/accounts?revision=next", reqBody)
assert.Equal(t, http.StatusOK, statusCode, "next revision should be okay")

res, statusCode := httpPost(t, ts.URL+"/accounts/"+contractAddr.String(), reqBody)
var output *accounts.CallResult
if err = json.Unmarshal(res, &output); err != nil {
Expand Down Expand Up @@ -464,6 +471,10 @@ func batchCall(t *testing.T) {
}},
}

// 'next' revisoun should be valid
_, statusCode = httpPost(t, ts.URL+"/accounts/*?revision=next", reqBody)
assert.Equal(t, http.StatusOK, statusCode, "next revision should be okay")

res, statusCode := httpPost(t, ts.URL+"/accounts/*", reqBody)
var results accounts.BatchCallResults
if err = json.Unmarshal(res, &results); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion api/blocks/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func New(repo *chain.Repository, bft bft.Finalizer) *Blocks {
}

func (b *Blocks) handleGetBlock(w http.ResponseWriter, req *http.Request) error {
revision, err := utils.ParseRevision(mux.Vars(req)["revision"])
revision, err := utils.ParseRevision(mux.Vars(req)["revision"], false)
if err != nil {
return utils.BadRequest(errors.WithMessage(err, "revision"))
}
Expand Down
29 changes: 19 additions & 10 deletions api/blocks/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,25 @@ func TestBlock(t *testing.T) {
initBlockServer(t)
defer ts.Close()

testBadQueryParams(t)
testInvalidBlockId(t)
testInvalidBlockNumber(t)
testGetBlockById(t)
testGetBlockNotFound(t)
testGetExpandedBlockById(t)
testGetBlockByHeight(t)
testGetBestBlock(t)
testGetFinalizedBlock(t)
testGetBlockWithRevisionNumberTooHigh(t)
tests := []struct {
name string
run func(*testing.T)
}{
{"testBadQueryParams", testBadQueryParams},
{"testInvalidBlockId", testInvalidBlockId},
{"testInvalidBlockNumber", testInvalidBlockNumber},
{"testGetBlockById", testGetBlockById},
{"testGetBlockNotFound", testGetBlockNotFound},
{"testGetExpandedBlockById", testGetExpandedBlockById},
{"testGetBlockByHeight", testGetBlockByHeight},
{"testGetBestBlock", testGetBestBlock},
{"testGetFinalizedBlock", testGetFinalizedBlock},
{"testGetBlockWithRevisionNumberTooHigh", testGetBlockWithRevisionNumberTooHigh},
}

for _, tt := range tests {
t.Run(tt.name, tt.run)
}
}

func testBadQueryParams(t *testing.T) {
Expand Down
22 changes: 11 additions & 11 deletions api/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/pkg/errors"
"github.com/vechain/thor/v2/api/utils"
"github.com/vechain/thor/v2/bft"
"github.com/vechain/thor/v2/block"
"github.com/vechain/thor/v2/chain"
"github.com/vechain/thor/v2/consensus"
"github.com/vechain/thor/v2/genesis"
Expand Down Expand Up @@ -177,11 +178,11 @@ func (d *Debug) handleTraceCall(w http.ResponseWriter, req *http.Request) error
if err := utils.ParseJSON(req.Body, &opt); err != nil {
return utils.BadRequest(errors.WithMessage(err, "body"))
}
revision, err := utils.ParseRevision(req.URL.Query().Get("revision"))
revision, err := utils.ParseRevision(req.URL.Query().Get("revision"), true)
if err != nil {
return utils.BadRequest(errors.WithMessage(err, "revision"))
}
summary, err := utils.GetSummary(revision, d.repo, d.bft)
summary, st, err := utils.GetSummaryAndState(revision, d.repo, d.bft, d.stater)
if err != nil {
if d.repo.IsNotFound(err) {
return utils.BadRequest(errors.WithMessage(err, "revision"))
Expand All @@ -199,7 +200,7 @@ func (d *Debug) handleTraceCall(w http.ResponseWriter, req *http.Request) error
return err
}

res, err := d.traceCall(req.Context(), tracer, summary, txCtx, gas, clause)
res, err := d.traceCall(req.Context(), tracer, summary.Header, st, txCtx, gas, clause)
if err != nil {
return err
}
Expand All @@ -214,13 +215,12 @@ func (d *Debug) createTracer(name string, config json.RawMessage) (tracers.Trace
return tracers.DefaultDirectory.New(name, config, d.allowCustomTracer)
}

func (d *Debug) traceCall(ctx context.Context, tracer tracers.Tracer, summary *chain.BlockSummary, txCtx *xenv.TransactionContext, gas uint64, clause *tx.Clause) (interface{}, error) {
header := summary.Header
state := d.stater.NewState(header.StateRoot(), header.Number(), summary.Conflicts, summary.SteadyNum)
func (d *Debug) traceCall(ctx context.Context, tracer tracers.Tracer, header *block.Header, st *state.State, txCtx *xenv.TransactionContext, gas uint64, clause *tx.Clause) (interface{}, error) {
signer, _ := header.Signer()

rt := runtime.New(
d.repo.NewChain(header.ID()),
state,
d.repo.NewChain(header.ParentID()),
st,
&xenv.BlockContext{
Beneficiary: header.Beneficiary(),
Signer: signer,
Expand All @@ -232,9 +232,9 @@ func (d *Debug) traceCall(ctx context.Context, tracer tracers.Tracer, summary *c
d.forkConfig)

tracer.SetContext(&tracers.Context{
BlockID: summary.Header.ID(),
BlockTime: summary.Header.Timestamp(),
State: state,
BlockID: header.ID(),
BlockTime: header.Timestamp(),
State: st,
})
rt.SetVMConfig(vm.Config{Tracer: tracer})

Expand Down
7 changes: 6 additions & 1 deletion api/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func TestDebug(t *testing.T) {
testHandleTraceCallWithMalformedBodyRequest(t)
testHandleTraceCallWithEmptyTraceCallOption(t)
testHandleTraceCall(t)
testTraceCallNextBlock(t)
testHandleTraceCallWithValidRevisions(t)
testHandleTraceCallWithRevisionAsNonExistingHeight(t)
testHandleTraceCallWithRevisionAsNonExistingId(t)
Expand Down Expand Up @@ -102,7 +103,6 @@ func TestStorageRangeFunc(t *testing.T) {
}

storageRangeRes, err := storageRangeAt(trie, start, 1)

assert.NoError(t, err)
assert.NotNil(t, storageRangeRes.NextKey)
storage := storageRangeRes.Storage
Expand Down Expand Up @@ -250,6 +250,11 @@ func testHandleTraceCallWithEmptyTraceCallOption(t *testing.T) {
assert.Equal(t, expectedExecutionResult, parsedExecutionRes)
}

func testTraceCallNextBlock(t *testing.T) {
traceCallOption := &TraceCallOption{}
httpPostAndCheckResponseStatus(t, ts.URL+"/debug/tracers/call?revision=next", traceCallOption, 200)
}

func testHandleTraceCall(t *testing.T) {
addr := randAddress()
provedWork := math.HexOrDecimal256(*big.NewInt(1000))
Expand Down
Loading
Loading