From bfe1ebd501661632205da7a7382a52b460dd57cd Mon Sep 17 00:00:00 2001 From: Gui Iribarren Date: Wed, 17 Jul 2024 09:56:04 +0200 Subject: [PATCH] api: /chain/blocks/{height} and /chain/blocks/hash/{hash} now include txCount --- api/api_types.go | 1 + api/chain.go | 14 ++++++++++++-- test/api_test.go | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/api/api_types.go b/api/api_types.go index 6f74d74fe..9521b9f48 100644 --- a/api/api_types.go +++ b/api/api_types.go @@ -434,4 +434,5 @@ func CensusTypeToOrigin(ctype CensusTypeDescription) (models.CensusOrigin, []byt type Block struct { comettypes.Block `json:",inline"` Hash types.HexBytes `json:"hash" ` + TxCount int64 `json:"txCount"` } diff --git a/api/chain.go b/api/chain.go index 070f4b42c..1f5ddc3d0 100644 --- a/api/chain.go +++ b/api/chain.go @@ -877,6 +877,10 @@ func (a *API) chainBlockHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext) } return ErrBlockNotFound.WithErr(err) } + txcount, err := a.indexer.CountTransactionsByHeight(int64(height)) + if err != nil { + return ErrIndexerQueryFailed.WithErr(err) + } block := &Block{ Block: comettypes.Block{ Header: comettypes.Header{ @@ -889,7 +893,8 @@ func (a *API) chainBlockHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext) }, }, }, - Hash: idxblock.Hash, + Hash: idxblock.Hash, + TxCount: txcount, } data, err := json.Marshal(block) if err != nil { @@ -920,6 +925,10 @@ func (a *API) chainBlockByHashHandler(_ *apirest.APIdata, ctx *httprouter.HTTPCo } return ErrBlockNotFound.WithErr(err) } + txcount, err := a.indexer.CountTransactionsByHeight(idxblock.Height) + if err != nil { + return ErrIndexerQueryFailed.WithErr(err) + } block := &Block{ Block: comettypes.Block{ Header: comettypes.Header{ @@ -932,7 +941,8 @@ func (a *API) chainBlockByHashHandler(_ *apirest.APIdata, ctx *httprouter.HTTPCo }, }, }, - Hash: idxblock.Hash, + Hash: idxblock.Hash, + TxCount: txcount, } data, err := json.Marshal(block) if err != nil { diff --git a/test/api_test.go b/test/api_test.go index 80e4f3699..267895636 100644 --- a/test/api_test.go +++ b/test/api_test.go @@ -461,6 +461,41 @@ func TestAPIAccountTokentxs(t *testing.T) { qt.Assert(t, gotAcct1.Balance, qt.Equals, initBalance+amountAcc2toAcct1-amountAcc1toAcct2-uint64(txBasePrice)) } +func TestAPIBlocks(t *testing.T) { + server := testcommon.APIserver{} + server.Start(t, + api.ChainHandler, + api.CensusHandler, + api.VoteHandler, + api.AccountHandler, + api.ElectionHandler, + api.WalletHandler, + ) + token1 := uuid.New() + c := testutil.NewTestHTTPclient(t, server.ListenAddr, &token1) + + // Block 1 + server.VochainAPP.AdvanceTestBlock() + waitUntilHeight(t, c, 1) + + // create a new account + initBalance := uint64(80) + _ = createAccount(t, c, server, initBalance) + + // Block 2 + server.VochainAPP.AdvanceTestBlock() + waitUntilHeight(t, c, 2) + + // check the txCount + resp, code := c.Request("GET", nil, "chain", "blocks", "1") + qt.Assert(t, code, qt.Equals, 200, qt.Commentf("response: %s", resp)) + + block := api.Block{} + err := json.Unmarshal(resp, &block) + qt.Assert(t, err, qt.IsNil) + qt.Assert(t, block.TxCount, qt.Equals, int64(1)) +} + func runAPIElectionCostWithParams(t *testing.T, electionParams electionprice.ElectionParameters, startBlock uint32, initialBalance,