Skip to content

Commit

Permalink
getsummary and getsummaryAndState decoupling (vechain#786)
Browse files Browse the repository at this point in the history
* getsummary and getsummaryAndState decoupling

* restart build
  • Loading branch information
otherview authored Jul 11, 2024
1 parent 448ab50 commit f1b3c67
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 48 deletions.
51 changes: 28 additions & 23 deletions api/accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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"))
Expand All @@ -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
}
Expand All @@ -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
Expand Down
24 changes: 14 additions & 10 deletions api/blocks/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions api/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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
}
Expand Down
1 change: 0 additions & 1 deletion api/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions api/utils/revisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -128,13 +128,20 @@ 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 {
return nil, nil, err
}

st := stater.NewState(sum.Header.StateRoot(), sum.Header.Number(), sum.Conflicts, sum.SteadyNum)
return sum.Header, st, nil
return sum, st, nil
}
16 changes: 8 additions & 8 deletions api/utils/revisions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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())
}

0 comments on commit f1b3c67

Please sign in to comment.