Skip to content

Commit

Permalink
api: /chain/blocks/{height} and /chain/blocks/hash/{hash} now include…
Browse files Browse the repository at this point in the history
… txCount
  • Loading branch information
altergui committed Aug 20, 2024
1 parent d15010e commit bfe1ebd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
1 change: 1 addition & 0 deletions api/api_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
14 changes: 12 additions & 2 deletions api/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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 {
Expand Down Expand Up @@ -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{
Expand All @@ -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 {
Expand Down
35 changes: 35 additions & 0 deletions test/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit bfe1ebd

Please sign in to comment.