Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve block parsing starting height calculations #72

Merged
merged 21 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}