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 10 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Unreleased
### Changes
#### Blocks
- ([\#72](https://github.com/forbole/juno/pull/72)) Updated missing blocks parsing


## v3.3.0
### Changes
- ([\#67](https://github.com/forbole/juno/pull/67)) Added support for concurrent transaction handling
Expand Down
10 changes: 10 additions & 0 deletions cmd/parse/blocks/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,18 @@ will be replaced with the data downloaded from the node.
end, _ := cmd.Flags().GetInt64(flagEnd)
force, _ := cmd.Flags().GetBool(flagForce)

lastBlockHeight, err := parseCtx.Database.GetLastBlockHeight()
if err != nil {
return err
}

// Get the start height, default to the config's height; use flagStart if set
startHeight := config.Cfg.Parser.StartHeight

if lastBlockHeight > startHeight {
startHeight = lastBlockHeight
}

MonikaCat marked this conversation as resolved.
Show resolved Hide resolved
if start > 0 {
startHeight = start
}
Expand Down
24 changes: 23 additions & 1 deletion cmd/start/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,28 @@ func enqueueMissingBlocks(exportQueue types.HeightQueue, ctx *parser.Context) {
panic(fmt.Errorf("failed to get last block from RPCConfig client: %s", err))
}

lastBlockHeightInDB, err := ctx.Database.GetLastBlockHeight()
if err != nil {
fmt.Errorf("failed to get last block height from database: %s", err)
MonikaCat marked this conversation as resolved.
Show resolved Hide resolved
}

// 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,
MonikaCat marked this conversation as resolved.
Show resolved Hide resolved
if startHeight == 0 {
if lastBlockHeightInDB > 0 {
startHeight = lastBlockHeightInDB
} else {
startHeight = 1
}
}
MonikaCat marked this conversation as resolved.
Show resolved Hide resolved

if lastBlockHeightInDB > startHeight {
startHeight = lastBlockHeightInDB
}
MonikaCat marked this conversation as resolved.
Show resolved Hide resolved

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 @@ -146,7 +168,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