From f1b3c67c3fd4d1fc988302c3e219ed3fd8045ff7 Mon Sep 17 00:00:00 2001 From: Pedro Gomes Date: Thu, 11 Jul 2024 03:25:01 +0100 Subject: [PATCH] getsummary and getsummaryAndState decoupling (#786) * getsummary and getsummaryAndState decoupling * restart build --- api/accounts/accounts.go | 51 ++++++++++++++++++++----------------- api/blocks/blocks_test.go | 24 +++++++++-------- api/debug/debug.go | 4 +-- api/debug/debug_test.go | 1 - api/utils/revisions.go | 15 ++++++++--- api/utils/revisions_test.go | 16 ++++++------ 6 files changed, 63 insertions(+), 48 deletions(-) diff --git a/api/accounts/accounts.go b/api/accounts/accounts.go index 427694dd0..e8cd6cf90 100644 --- a/api/accounts/accounts.go +++ b/api/accounts/accounts.go @@ -50,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 } @@ -70,22 +68,23 @@ func (a *Accounts) handleGetCode(w http.ResponseWriter, req *http.Request) error 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 @@ -94,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 } @@ -106,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 } @@ -126,14 +122,16 @@ func (a *Accounts) handleGetAccount(w http.ResponseWriter, req *http.Request) er 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 } @@ -153,14 +151,16 @@ func (a *Accounts) handleGetStorage(w http.ResponseWriter, req *http.Request) er 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 } @@ -176,7 +176,7 @@ func (a *Accounts) handleCallContract(w http.ResponseWriter, req *http.Request) if err != nil { return utils.BadRequest(errors.WithMessage(err, "revision")) } - header, st, err := utils.GetHeaderAndState(revision, a.repo, a.bft, a.stater) + 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")) @@ -203,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, header, st) + results, err := a.batchCall(req.Context(), batchCallData, summary.Header, st) if err != nil { return err } @@ -219,21 +219,26 @@ func (a *Accounts) handleCallBatchCode(w http.ResponseWriter, req *http.Request) if err != nil { return utils.BadRequest(errors.WithMessage(err, "revision")) } - header, st, err := utils.GetHeaderAndState(revision, a.repo, a.bft, a.stater) + 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, header, st) + 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, header *block.Header, st *state.State) (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 diff --git a/api/blocks/blocks_test.go b/api/blocks/blocks_test.go index c2b676422..bf34e7b7a 100644 --- a/api/blocks/blocks_test.go +++ b/api/blocks/blocks_test.go @@ -42,16 +42,20 @@ 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) + for name, tt := range map[string]func(t2 *testing.T){ + "testBadQueryParams": testBadQueryParams, + "testInvalidBlockId": testInvalidBlockId, + "testInvalidBlockNumber": testInvalidBlockNumber, + "testGetBlockById": testGetBlockById, + "testGetBlockNotFound": testGetBlockNotFound, + "testGetExpandedBlockById": testGetExpandedBlockById, + "testGetBlockByHeight": testGetBlockByHeight, + "testGetBestBlock": testGetBestBlock, + "testGetFinalizedBlock": testGetFinalizedBlock, + "testGetBlockWithRevisionNumberTooHigh": testGetBlockWithRevisionNumberTooHigh, + } { + t.Run(name, tt) + } } func testBadQueryParams(t *testing.T) { diff --git a/api/debug/debug.go b/api/debug/debug.go index 8acc25644..646657c6f 100644 --- a/api/debug/debug.go +++ b/api/debug/debug.go @@ -182,7 +182,7 @@ func (d *Debug) handleTraceCall(w http.ResponseWriter, req *http.Request) error if err != nil { return utils.BadRequest(errors.WithMessage(err, "revision")) } - header, st, err := utils.GetHeaderAndState(revision, d.repo, d.bft, d.stater) + 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")) @@ -200,7 +200,7 @@ func (d *Debug) handleTraceCall(w http.ResponseWriter, req *http.Request) error return err } - res, err := d.traceCall(req.Context(), tracer, header, st, txCtx, gas, clause) + res, err := d.traceCall(req.Context(), tracer, summary.Header, st, txCtx, gas, clause) if err != nil { return err } diff --git a/api/debug/debug_test.go b/api/debug/debug_test.go index 6099262b6..8cc6204ca 100644 --- a/api/debug/debug_test.go +++ b/api/debug/debug_test.go @@ -103,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 diff --git a/api/utils/revisions.go b/api/utils/revisions.go index ba72074d6..c4cf668cb 100644 --- a/api/utils/revisions.go +++ b/api/utils/revisions.go @@ -95,9 +95,9 @@ func GetSummary(rev *Revision, repo *chain.Repository, bft bft.Finalizer) (sum * return summary, nil } -// GetHeaderAndState returns the block header and state for the given revision, +// GetSummaryAndState returns the block summary and state for the given revision, // this function supports the "next" revision. -func GetHeaderAndState(rev *Revision, repo *chain.Repository, bft bft.Finalizer, stater *state.Stater) (*block.Header, *state.State, error) { +func GetSummaryAndState(rev *Revision, repo *chain.Repository, bft bft.Finalizer, stater *state.Stater) (*chain.BlockSummary, *state.State, error) { if rev.IsNext() { best := repo.BestBlockSummary() @@ -128,7 +128,14 @@ func GetHeaderAndState(rev *Revision, repo *chain.Repository, bft bft.Finalizer, // state is also reused from the parent block st := stater.NewState(best.Header.StateRoot(), best.Header.Number(), best.Conflicts, best.SteadyNum) - return mocked.Header(), st, nil + // rebuild the block summary with the next header (mocked) AND the best block status + return &chain.BlockSummary{ + Header: mocked.Header(), + Txs: best.Txs, + Size: uint64(mocked.Size()), + Conflicts: best.Conflicts, + SteadyNum: best.SteadyNum, + }, st, nil } sum, err := GetSummary(rev, repo, bft) if err != nil { @@ -136,5 +143,5 @@ func GetHeaderAndState(rev *Revision, repo *chain.Repository, bft bft.Finalizer, } st := stater.NewState(sum.Header.StateRoot(), sum.Header.Number(), sum.Conflicts, sum.SteadyNum) - return sum.Header, st, nil + return sum, st, nil } diff --git a/api/utils/revisions_test.go b/api/utils/revisions_test.go index dc191853f..3af996693 100644 --- a/api/utils/revisions_test.go +++ b/api/utils/revisions_test.go @@ -152,7 +152,7 @@ func TestGetSummary(t *testing.T) { } } -func TestGetHeaderAndState(t *testing.T) { +func TestGetSummaryAndState(t *testing.T) { db := muxdb.NewMem() stater := state.NewStater(db) gene := genesis.NewDevnet() @@ -163,17 +163,17 @@ func TestGetHeaderAndState(t *testing.T) { repo, _ := chain.NewRepository(db, b) bft := solo.NewBFTEngine(repo) - header, _, err := GetHeaderAndState(&Revision{revBest}, repo, bft, stater) + summary, _, err := GetSummaryAndState(&Revision{revBest}, repo, bft, stater) assert.Nil(t, err) - assert.Equal(t, header.Number(), b.Header().Number()) - assert.Equal(t, header.Timestamp(), b.Header().Timestamp()) + assert.Equal(t, summary.Header.Number(), b.Header().Number()) + assert.Equal(t, summary.Header.Timestamp(), b.Header().Timestamp()) - header, _, err = GetHeaderAndState(&Revision{revNext}, repo, bft, stater) + summary, _, err = GetSummaryAndState(&Revision{revNext}, repo, bft, stater) assert.Nil(t, err) - assert.Equal(t, header.Number(), b.Header().Number()+1) - assert.Equal(t, header.Timestamp(), b.Header().Timestamp()+thor.BlockInterval) + assert.Equal(t, summary.Header.Number(), b.Header().Number()+1) + assert.Equal(t, summary.Header.Timestamp(), b.Header().Timestamp()+thor.BlockInterval) - signer, err := header.Signer() + signer, err := summary.Header.Signer() assert.NotNil(t, err) assert.True(t, signer.IsZero()) }