Skip to content

Commit

Permalink
indexer: CreateBlock during Commit instead of OnBeginBlock
Browse files Browse the repository at this point in the history
since the CreateBlock now fetches the whole block, we need app.NodeClient initialized
in all nodes (including seeds), else seeds panic on first Commit
  • Loading branch information
altergui committed Sep 2, 2024
1 parent 60b68ee commit 07143c2
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 44 deletions.
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
17 changes: 0 additions & 17 deletions vochain/indexer/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,11 @@ import (
"errors"
"fmt"
"time"

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

// 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.readOnlyQuery.GetBlock(context.TODO(), height)
Expand Down
11 changes: 11 additions & 0 deletions vochain/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,17 @@ func (idx *Indexer) Commit(height uint32) error {
queries := idx.blockTxQueries()
ctx := context.TODO()

// index the new block
bb := idx.App.GetBlockByHeight(int64(height))
if _, err := queries.CreateBlock(context.TODO(), indexerdb.CreateBlockParams{
Height: bb.Height,
Time: bb.Time,
DataHash: nonNullBytes(bb.DataHash),
// TODO: ProposerAddress, Hash, AppHash, ChainID, LastBlockHash?, ValidatorSignatures?
}); err != nil {
log.Errorw(err, "cannot index new block")
}

for _, pidStr := range updateProcs {
pid := types.ProcessID(pidStr)
if err := idx.updateProcess(ctx, queries, pid); err != nil {
Expand Down
3 changes: 0 additions & 3 deletions vochain/keykeeper/keykeeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,6 @@ func (*KeyKeeper) OnVote(_ *state.Vote, _ int32) {}
// OnNewTx is not used by the KeyKeeper
func (*KeyKeeper) OnNewTx(_ *vochaintx.Tx, _ uint32, _ int32) {}

// OnBeginBlock is not used by the KeyKeeper
func (*KeyKeeper) OnBeginBlock(_ state.BeginBlock) {}

// OnCensusUpdate is not used by the KeyKeeper
func (*KeyKeeper) OnCensusUpdate(_, _ []byte, _ string, _ uint64) {}

Expand Down
1 change: 0 additions & 1 deletion vochain/offchaindatahandler/offchaindatahandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ func (d *OffChainDataHandler) OnSetAccount(_ []byte, account *state.Account) {
func (*OffChainDataHandler) OnCancel(_ []byte, _ int32) {}
func (*OffChainDataHandler) OnVote(_ *state.Vote, _ int32) {}
func (*OffChainDataHandler) OnNewTx(_ *vochaintx.Tx, _ uint32, _ int32) {}
func (*OffChainDataHandler) OnBeginBlock(state.BeginBlock) {}
func (*OffChainDataHandler) OnProcessKeys(_ []byte, _ string, _ int32) {}
func (*OffChainDataHandler) OnRevealKeys(_ []byte, _ string, _ int32) {}
func (*OffChainDataHandler) OnProcessStatusChange(_ []byte, _ models.ProcessStatus, _ int32) {}
Expand Down
15 changes: 0 additions & 15 deletions vochain/state/eventlistener.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package state

import (
"time"

"go.vocdoni.io/dvote/vochain/transaction/vochaintx"
"go.vocdoni.io/proto/build/go/models"
)
Expand Down Expand Up @@ -32,7 +30,6 @@ type EventListener interface {
OnSpendTokens(addr []byte, txType models.TxType, cost uint64, reference string)
OnCensusUpdate(pid, censusRoot []byte, censusURI string, censusSize uint64)
Commit(height uint32) (err error)
OnBeginBlock(BeginBlock)
Rollback()
}

Expand All @@ -46,15 +43,3 @@ func (v *State) AddEventListener(l EventListener) {
func (v *State) CleanEventListeners() {
v.eventListeners = nil
}

type BeginBlock struct {
Height int64
Time time.Time
DataHash []byte
}

func (v *State) OnBeginBlock(bb BeginBlock) {
for _, l := range v.eventListeners {
l.OnBeginBlock(bb)
}
}
1 change: 0 additions & 1 deletion vochain/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ type Listener struct {

func (*Listener) OnVote(_ *Vote, _ int32) {}
func (*Listener) OnNewTx(_ *vochaintx.Tx, _ uint32, _ int32) {}
func (*Listener) OnBeginBlock(BeginBlock) {}
func (*Listener) OnProcess(_ *models.Process, _ int32) {}
func (*Listener) OnProcessStatusChange(_ []byte, _ models.ProcessStatus, _ int32) {}
func (*Listener) OnProcessDurationChange(_ []byte, _ uint32, _ int32) {}
Expand Down

0 comments on commit 07143c2

Please sign in to comment.