Skip to content

Commit

Permalink
Merge branch 'feat/index-blocks' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
altergui committed Jul 9, 2024
2 parents 44986f9 + 476f679 commit 6e6db14
Show file tree
Hide file tree
Showing 21 changed files with 271 additions and 136 deletions.
49 changes: 32 additions & 17 deletions api/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ 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"
Expand Down Expand Up @@ -821,18 +820,26 @@ 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)
}
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,
}
data, err := json.Marshal(block)
if err != nil {
Expand All @@ -856,18 +863,26 @@ 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)
}
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,
}
data, err := json.Marshal(block)
if err != nil {
Expand Down
4 changes: 0 additions & 4 deletions vochain/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,6 @@ func (app *BaseApplication) beginBlock(t time.Time, height uint32) {
app.State.SetHeight(height)

go app.State.CachePurge(height)
app.State.OnBeginBlock(vstate.BeginBlock{
Height: int64(height),
Time: t,
})
}

// endBlock is called at the end of every block.
Expand Down
3 changes: 0 additions & 3 deletions vochain/appsetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ func (app *BaseApplication) SetNode(vochaincfg *config.VochainCfg) error {
if app.Node, err = newTendermint(app, vochaincfg); err != nil {
return fmt.Errorf("could not set tendermint node service: %s", err)
}
if vochaincfg.IsSeedNode {
return nil
}
// Note that cometcli.New logs any error rather than returning it.
app.NodeClient = cometcli.New(app.Node)
return nil
Expand Down
7 changes: 6 additions & 1 deletion vochain/indexer/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func BenchmarkIndexer(b *testing.B) {
tx := &vochaintx.Tx{
TxID: rnd.Random32(),
TxModelType: "vote",
Tx: &models.Tx{Payload: &models.Tx_Vote{}},
}
idx.OnNewTx(tx, height, txBlockIndex)
curTxs = append(curTxs, tx)
Expand Down Expand Up @@ -138,7 +139,11 @@ func BenchmarkFetchTx(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < numTxs; j++ {
idx.OnNewTx(&vochaintx.Tx{TxID: util.Random32()}, uint32(i), int32(j))
idx.OnNewTx(&vochaintx.Tx{
TxID: util.Random32(),
TxModelType: "vote",
Tx: &models.Tx{Payload: &models.Tx_Vote{}},
}, uint32(i), int32(j))
}
err := idx.Commit(uint32(i))
qt.Assert(b, err, qt.IsNil)
Expand Down
44 changes: 25 additions & 19 deletions vochain/indexer/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,41 @@ import (
"fmt"
"time"

"go.vocdoni.io/dvote/log"
indexerdb "go.vocdoni.io/dvote/vochain/indexer/db"
"go.vocdoni.io/dvote/vochain/state"
"go.vocdoni.io/dvote/vochain/indexer/indexertypes"
)

// ErrBlockNotFound is returned if the block is not found in the indexer database.
var ErrBlockNotFound = fmt.Errorf("block not found")

func (idx *Indexer) OnBeginBlock(bb state.BeginBlock) {
idx.blockMu.Lock()
defer idx.blockMu.Unlock()
queries := idx.blockTxQueries()
if _, err := queries.CreateBlock(context.TODO(), indexerdb.CreateBlockParams{
Height: bb.Height,
Time: bb.Time,
DataHash: nonNullBytes(bb.DataHash),
}); err != nil {
log.Errorw(err, "cannot index new block")
// BlockTimestamp returns the timestamp of the block at the given height
func (idx *Indexer) BlockTimestamp(height int64) (time.Time, error) {
block, err := idx.BlockByHeight(height)
if err != nil {
return time.Time{}, err
}
return block.Time, nil
}

// BlockTimestamp returns the timestamp of the block at the given height
func (idx *Indexer) BlockTimestamp(height int64) (time.Time, error) {
block, err := idx.readOnlyQuery.GetBlock(context.TODO(), height)
// BlockByHeight returns the available information of the block at the given height
func (idx *Indexer) BlockByHeight(height int64) (*indexertypes.Block, error) {
block, err := idx.readOnlyQuery.GetBlockByHeight(context.TODO(), height)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return time.Time{}, ErrBlockNotFound
return nil, ErrBlockNotFound
}
return time.Time{}, err
return nil, err
}
return block.Time, nil
return indexertypes.BlockFromDB(&block), nil
}

// BlockByHeight returns the available information of the block with the given hash
func (idx *Indexer) BlockByHash(hash []byte) (*indexertypes.Block, error) {
block, err := idx.readOnlyQuery.GetBlockByHash(context.TODO(), hash)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrBlockNotFound
}
return nil, err
}
return indexertypes.BlockFromDB(&block), nil
}
59 changes: 48 additions & 11 deletions vochain/indexer/db/blocks.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 57 additions & 47 deletions vochain/indexer/db/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6e6db14

Please sign in to comment.