Skip to content

Commit

Permalink
feat: improve block parsing starting height calculations (#72)
Browse files Browse the repository at this point in the history
## Description
Fixes [BDU-489](https://forbole.atlassian.net/browse/BDU-489)
## Checklist
- [x] Targeted PR against correct branch.
- [x] Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
- [ ] Wrote unit tests.  
- [x] Re-reviewed `Files changed` in the Github PR explorer.
  • Loading branch information
MonikaCat authored and RiccardoM committed Sep 2, 2022
1 parent 41aea9c commit 3605a07
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Unreleased
### Changes
- ([\#71](https://github.com/forbole/juno/pull/71)) Retry RPC client connection upon failure instead of panic
- ([\#72](https://github.com/forbole/juno/pull/72)) Updated missing blocks parsing

## v3.3.0
### Changes
Expand Down
12 changes: 10 additions & 2 deletions cmd/parse/blocks/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

parsecmdtypes "github.com/forbole/juno/v3/cmd/parse/types"
"github.com/forbole/juno/v3/types/utils"

"github.com/rs/zerolog/log"

Expand Down Expand Up @@ -44,8 +45,15 @@ will be replaced with the data downloaded from the node.
end, _ := cmd.Flags().GetInt64(flagEnd)
force, _ := cmd.Flags().GetBool(flagForce)

// Get the start height, default to the config's height; use flagStart if set
startHeight := config.Cfg.Parser.StartHeight
lastDbBlockHeight, err := parseCtx.Database.GetLastBlockHeight()
if err != nil {
return err
}

// Compare start height from config file and last block height in database
// and set higher block as start height
startHeight := utils.MaxInt64(config.Cfg.Parser.StartHeight, lastDbBlockHeight)

if start > 0 {
startHeight = start
}
Expand Down
17 changes: 16 additions & 1 deletion cmd/start/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

parsecmdtypes "github.com/forbole/juno/v3/cmd/parse/types"
"github.com/forbole/juno/v3/types/utils"

"github.com/forbole/juno/v3/logging"

Expand Down Expand Up @@ -126,6 +127,20 @@ func enqueueMissingBlocks(exportQueue types.HeightQueue, ctx *parser.Context) {
// Get the latest height
latestBlockHeight := mustGetLatestHeight(ctx)

lastDbBlockHeight, err := ctx.Database.GetLastBlockHeight()
if err != nil {
ctx.Logger.Error("failed to get last block height from database", "error", err)
}

// Get the start height, default to the config's height
startHeight := cfg.StartHeight

// Set startHeight to the latest height in database
// if is not set inside config.yaml file
if startHeight == 0 {
startHeight = utils.MaxInt64(1, lastDbBlockHeight)
}

if cfg.FastSync {
ctx.Logger.Info("fast sync is enabled, ignoring all previous blocks", "latest_block_height", latestBlockHeight)
for _, module := range ctx.Modules {
Expand All @@ -142,7 +157,7 @@ func enqueueMissingBlocks(exportQueue types.HeightQueue, ctx *parser.Context) {
}
} else {
ctx.Logger.Info("syncing missing blocks...", "latest_block_height", latestBlockHeight)
for i := cfg.StartHeight; i <= latestBlockHeight; i++ {
for i := startHeight; i <= latestBlockHeight; i++ {
ctx.Logger.Debug("enqueueing missing block", "height", i)
exportQueue <- i
}
Expand Down
4 changes: 4 additions & 0 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ type Database interface {
// An error is returned if the operation fails.
HasBlock(height int64) (bool, error)

// GetLastBlockHeight returns the last block height stored in database..
// An error is returned if the operation fails.
GetLastBlockHeight() (int64, error)

// SaveBlock will be called when a new block is parsed, passing the block itself
// and the transactions contained inside that block.
// An error is returned if the operation fails.
Expand Down
16 changes: 16 additions & 0 deletions database/postgresql/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,22 @@ func (db *Database) HasBlock(height int64) (bool, error) {
return res, err
}

// GetLastBlockHeight returns the last block height stored inside the database
func (db *Database) GetLastBlockHeight() (int64, error) {
stmt := `SELECT height FROM block ORDER BY height DESC LIMIT 1;`

var height int64
if err := db.Sql.QueryRow(stmt).Scan(&height); err != nil {
return 0, fmt.Errorf("error while getting last block height, error: %s", err)
}

if height == 0 {
return 0, fmt.Errorf("cannot get block height, no blocks saved")
}

return height, nil
}

// SaveBlock implements database.Database
func (db *Database) SaveBlock(block *types.Block) error {
sqlStatement := `
Expand Down
7 changes: 7 additions & 0 deletions types/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,10 @@ func FindAttributeByKey(event abci.Event, attrKey string) (abci.EventAttribute,

return abci.EventAttribute{}, fmt.Errorf("no attribute with key %s found inside event with type %s", attrKey, event.Type)
}

func MaxInt64(a, b int64) int64 {
if a > b {
return a
}
return b
}

0 comments on commit 3605a07

Please sign in to comment.