Skip to content

Commit

Permalink
api: fetch blocks and transactions from indexer rather than app Block…
Browse files Browse the repository at this point in the history
…Store

 * /chain/blocks/{height} and /chain/blocks/hash/{hash} now fetch blocks from indexer
 * /chain/blocks/{height} and /chain/blocks/hash/{hash} now include txCount
 * /chain/transactions/{height}/{index} now fetch full tx from indexex
  • Loading branch information
altergui committed Aug 20, 2024
1 parent fa960c6 commit 2b2f5f2
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 33 deletions.
7 changes: 4 additions & 3 deletions api/api_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,9 @@ type TransfersList struct {
}

type GenericTransactionWithInfo struct {
TxContent json.RawMessage `json:"tx"`
TxInfo indexertypes.Transaction `json:"txInfo"`
Signature types.HexBytes `json:"signature"`
TxContent json.RawMessage `json:"tx"`
TxInfo *indexertypes.Transaction `json:"txInfo"`
Signature types.HexBytes `json:"signature"`
}

type ChainInfo struct {
Expand Down 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"`
}
76 changes: 46 additions & 30 deletions api/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import (
"go.vocdoni.io/dvote/crypto/zk/circuit"
"go.vocdoni.io/dvote/httprouter"
"go.vocdoni.io/dvote/httprouter/apirest"
"go.vocdoni.io/dvote/types"
"go.vocdoni.io/dvote/util"
"go.vocdoni.io/dvote/vochain"
"go.vocdoni.io/dvote/vochain/genesis"
"go.vocdoni.io/dvote/vochain/indexer"
"go.vocdoni.io/dvote/vochain/state"
Expand Down Expand Up @@ -657,25 +655,17 @@ func (a *API) chainTxHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext) er
if err != nil {
return err
}
stx, err := a.vocapp.GetTx(uint32(height), int32(index))
if err != nil {
if errors.Is(err, vochain.ErrTransactionNotFound) {
return ErrTransactionNotFound
}
return ErrVochainGetTxFailed.WithErr(err)
}

ref, err := a.indexer.GetTxByBlockHeightAndBlockIndex(height, index)
ref, err := a.indexer.GetTransactionByHeightAndIndex(height, index)
if err != nil {
if errors.Is(err, indexer.ErrTransactionNotFound) {
return ErrTransactionNotFound
}
return ErrVochainGetTxFailed.WithErr(err)
}
tx := &GenericTransactionWithInfo{
TxContent: []byte(protoFormat(stx.Tx)),
Signature: stx.Signature,
TxInfo: *ref,
TxContent: []byte(protoFormat(ref.RawTx)),
TxInfo: ref,
Signature: ref.Signature,
}
data, err := json.Marshal(tx)
if err != nil {
Expand Down Expand Up @@ -871,18 +861,31 @@ func (a *API) chainBlockHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext)
if err != nil {
return err
}
tmblock := a.vocapp.GetBlockByHeight(int64(height))
if tmblock == nil {
return ErrBlockNotFound
idxblock, err := a.indexer.BlockByHeight(int64(height))
if err != nil {
if errors.Is(err, indexer.ErrBlockNotFound) {
return ErrBlockNotFound
}
return ErrBlockNotFound.WithErr(err)
}
txcount, err := a.indexer.CountTransactionsByHeight(int64(height))
if err != nil {
return ErrIndexerQueryFailed.WithErr(err)
}
block := &Block{
Block: comettypes.Block{
Header: tmblock.Header,
Data: tmblock.Data,
Evidence: tmblock.Evidence,
LastCommit: tmblock.LastCommit,
Header: comettypes.Header{
ChainID: idxblock.ChainID,
Height: idxblock.Height,
Time: idxblock.Time,
ProposerAddress: []byte(idxblock.ProposerAddress),
LastBlockID: comettypes.BlockID{
Hash: []byte(idxblock.LastBlockHash),
},
},
},
Hash: types.HexBytes(tmblock.Hash()),
Hash: idxblock.Hash,
TxCount: txcount,
}
data, err := json.Marshal(block)
if err != nil {
Expand All @@ -906,18 +909,31 @@ func (a *API) chainBlockByHashHandler(_ *apirest.APIdata, ctx *httprouter.HTTPCo
if err != nil {
return err
}
tmblock := a.vocapp.GetBlockByHash(hash)
if tmblock == nil {
return ErrBlockNotFound
idxblock, err := a.indexer.BlockByHash(hash)
if err != nil {
if errors.Is(err, indexer.ErrBlockNotFound) {
return ErrBlockNotFound
}
return ErrBlockNotFound.WithErr(err)
}
txcount, err := a.indexer.CountTransactionsByHeight(idxblock.Height)
if err != nil {
return ErrIndexerQueryFailed.WithErr(err)
}
block := &Block{
Block: comettypes.Block{
Header: tmblock.Header,
Data: tmblock.Data,
Evidence: tmblock.Evidence,
LastCommit: tmblock.LastCommit,
Header: comettypes.Header{
ChainID: idxblock.ChainID,
Height: idxblock.Height,
Time: idxblock.Time,
ProposerAddress: []byte(idxblock.ProposerAddress),
LastBlockID: comettypes.BlockID{
Hash: []byte(idxblock.LastBlockHash),
},
},
},
Hash: types.HexBytes(tmblock.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 2b2f5f2

Please sign in to comment.