Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
See PR #80
  • Loading branch information
RiccardoM authored Jan 14, 2021
1 parent 6867871 commit 334c183
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 52 deletions.
21 changes: 15 additions & 6 deletions database/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,29 @@ func (db *BigDipperDb) SaveConsensus(event constypes.ConsensusEvent) error {
return err
}

// GetLastBlockHeight returns the last block height stored inside the database
func (db *BigDipperDb) GetLastBlockHeight() (int64, error) {
stmt := `SELECT height FROM block ORDER BY timestamp DESC LIMIT 1`
// GetLastBlock returns the last block stored inside the database based on the heights
func (db *BigDipperDb) GetLastBlock() (*dbtypes.BlockRow, error) {
stmt := `SELECT * FROM block ORDER BY height DESC LIMIT 1`

var blocks []dbtypes.BlockRow
if err := db.Sqlx.Select(&blocks, stmt); err != nil {
return 0, err
return nil, err
}

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

return blocks[0].Height, nil
return &blocks[0], nil
}

// GetLastBlockHeight returns the last block height stored inside the database
func (db *BigDipperDb) GetLastBlockHeight() (int64, error) {
block, err := db.GetLastBlock()
if err != nil {
return 0, err
}
return block.Height, nil
}

// getBlockHeightTime retrieves the block at the specific time
Expand Down
1 change: 1 addition & 0 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func Builder(cfg *config.Config, codec *codec.Codec) (db.Database, error) {
}, nil
}

// Cast allows to cast the given db to a BigDipperDb instance
func Cast(db db.Database) *BigDipperDb {
bdDatabase, ok := db.(*BigDipperDb)
if !ok {
Expand Down
9 changes: 7 additions & 2 deletions database/types/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func (r BlockTimeRow) Equal(s BlockTimeRow) bool {

//Container to return block needed in certain height
type BlockRow struct {
Height int64 `db:"height"`
Timestamp time.Time `db:"timestamp"`
Height int64 `db:"height"`
Hash string `db:"hash"`
TxNum int64 `db:"num_txs"`
TotalGas int64 `db:"total_gas"`
ProposerAddress string `db:"proposer_address"`
PreCommitsNum int64 `db:"pre_commits"`
Timestamp time.Time `db:"timestamp"`
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.15

require (
github.com/cosmos/cosmos-sdk v0.39.2
github.com/desmos-labs/juno v0.0.0-20210112080646-9335c7c12e7d
github.com/desmos-labs/juno v0.0.0-20210114090225-ead810b0ed1e
github.com/go-co-op/gocron v0.3.3
github.com/jmoiron/sqlx v1.2.1-0.20200324155115-ee514944af4b
github.com/lib/pq v1.3.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/desmos-labs/juno v0.0.0-20210112080646-9335c7c12e7d h1:5xHSxfe+/bzbmCbnrtauQS9p/aMTEAnKw71gOs/Byc8=
github.com/desmos-labs/juno v0.0.0-20210112080646-9335c7c12e7d/go.mod h1:j77T//iFUjtHLvRekkCsQj3984IaHzGKwcVk1hWsGp8=
github.com/desmos-labs/juno v0.0.0-20210114090225-ead810b0ed1e h1:jW0KEuLetk7nrKWokjG9WjufDBRkdNMnXmWY8EkCOy0=
github.com/desmos-labs/juno v0.0.0-20210114090225-ead810b0ed1e/go.mod h1:j77T//iFUjtHLvRekkCsQj3984IaHzGKwcVk1hWsGp8=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
Expand Down
1 change: 1 addition & 0 deletions schema/00-cosmos.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CREATE TABLE validator
CREATE TABLE pre_commit
(
id SERIAL PRIMARY KEY,
height BIGINT NOT NULL,
validator_address TEXT NOT NULL REFERENCES validator (consensus_address),
timestamp TIMESTAMP WITHOUT TIME ZONE NOT NULL,
voting_power BIGINT NOT NULL,
Expand Down
7 changes: 2 additions & 5 deletions x/auth/periodic_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/desmos-labs/juno/client"
"github.com/go-co-op/gocron"
"github.com/rs/zerolog/log"
tmctypes "github.com/tendermint/tendermint/rpc/core/types"

"github.com/forbole/bdjuno/database"
"github.com/forbole/bdjuno/x/utils"
Expand All @@ -29,10 +28,8 @@ func RegisterOps(scheduler *gocron.Scheduler, cp *client.Proxy, db *database.Big
// updateAccounts gets all the accounts stored inside the database, and refreshes their
// balances by fetching the LCD endpoint.
func updateAccounts(cp *client.Proxy, db *database.BigDipperDb) error {
var block tmctypes.ResultBlock
err := cp.QueryLCD("/blocks/latest", &block)
height, err := db.GetLastBlockHeight()
if err != nil {
log.Err(err).Str("module", "auth").Msg("error getting latest block")
return err
}

Expand All @@ -41,5 +38,5 @@ func updateAccounts(cp *client.Proxy, db *database.BigDipperDb) error {
return err
}

return RefreshAccounts(addresses, block.Block.Height, cp, db)
return RefreshAccounts(addresses, height, cp, db)
}
4 changes: 2 additions & 2 deletions x/consensus/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ func (m Module) Name() string {

// RegisterPeriodicOperations implements modules.Module
func (m Module) RegisterPeriodicOperations(
scheduler *gocron.Scheduler, _ *codec.Codec, cp *client.Proxy, db db.Database,
scheduler *gocron.Scheduler, _ *codec.Codec, _ *client.Proxy, db db.Database,
) error {
bdDatabase := database.Cast(db)
return Register(scheduler, cp, bdDatabase)
return Register(scheduler, bdDatabase)
}

// RunAdditionalOperations implements modules.Module
Expand Down
51 changes: 23 additions & 28 deletions x/consensus/periodic_operations.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
package consensus

import (
"github.com/desmos-labs/juno/client"
"github.com/go-co-op/gocron"
"github.com/rs/zerolog/log"
tmctypes "github.com/tendermint/tendermint/rpc/core/types"

"github.com/forbole/bdjuno/database"
"github.com/forbole/bdjuno/x/utils"
)

// PeriodicConcensusOperations returns the AdditionalOperation that periodically runs fetches from
// the LCD to make sure that constantly changing data are synced properly.
func Register(scheduler *gocron.Scheduler, cp *client.Proxy, db *database.BigDipperDb) error {
func Register(scheduler *gocron.Scheduler, db *database.BigDipperDb) error {
log.Debug().Str("module", "consensus").Msg("setting up periodic tasks")

if _, err := scheduler.Every(1).Minute().Do(func() {
utils.WatchMethod(func() error { return updateBlockTimeInMinute(cp, db) })
utils.WatchMethod(func() error { return updateBlockTimeInMinute(db) })
}); err != nil {
return err
}

if _, err := scheduler.Every(1).Hour().StartImmediately().Do(func() {
utils.WatchMethod(func() error { return updateBlockTimeInHour(cp, db) })
utils.WatchMethod(func() error { return updateBlockTimeInHour(db) })
}); err != nil {
return err
}

if _, err := scheduler.Every(1).Day().StartImmediately().Do(func() {
utils.WatchMethod(func() error { return updateBlockTimeInDay(cp, db) })
utils.WatchMethod(func() error { return updateBlockTimeInDay(db) })
}); err != nil {
return err
}
Expand All @@ -38,12 +36,11 @@ func Register(scheduler *gocron.Scheduler, cp *client.Proxy, db *database.BigDip
}

// updateBlockTimeInMinute insert average block time in the latest minute
func updateBlockTimeInMinute(cp *client.Proxy, db *database.BigDipperDb) error {
func updateBlockTimeInMinute(db *database.BigDipperDb) error {
log.Debug().Str("module", "consensus").Str("operation", "block time").
Msg("updating block time in minutes")

var block tmctypes.ResultBlock
err := cp.QueryLCD("/blocks/latest", &block)
block, err := db.GetLastBlock()
if err != nil {
return err
}
Expand All @@ -53,27 +50,26 @@ func updateBlockTimeInMinute(cp *client.Proxy, db *database.BigDipperDb) error {
return err
}

//check if chain is not created minutes ago
if block.Block.Time.Sub(genesis).Minutes() < 0 {
// check if chain is not created minutes ago
if block.Timestamp.Sub(genesis).Minutes() < 0 {
return nil
}

minute, err := db.GetBlockHeightTimeMinuteAgo(block.Block.Time)
minute, err := db.GetBlockHeightTimeMinuteAgo(block.Timestamp)
if err != nil {
return err
}
newBlockTime := block.Block.Time.Sub(minute.Timestamp).Seconds() / float64(block.Block.Height-minute.Height)
newBlockTime := block.Timestamp.Sub(minute.Timestamp).Seconds() / float64(block.Height-minute.Height)

return db.SaveAverageBlockTimePerMin(newBlockTime, block.Block.Height)
return db.SaveAverageBlockTimePerMin(newBlockTime, block.Height)
}

// updateBlockTimeInHour insert average block time in the latest hour
func updateBlockTimeInHour(cp *client.Proxy, db *database.BigDipperDb) error {
func updateBlockTimeInHour(db *database.BigDipperDb) error {
log.Debug().Str("module", "consensus").Str("operation", "block time").
Msg("updating block time in hours")

var block tmctypes.ResultBlock
err := cp.QueryLCD("/blocks/latest", &block)
block, err := db.GetLastBlock()
if err != nil {
return err
}
Expand All @@ -84,26 +80,25 @@ func updateBlockTimeInHour(cp *client.Proxy, db *database.BigDipperDb) error {
}

//check if chain is not created minutes ago
if block.Block.Time.Sub(genesis).Hours() < 0 {
if block.Timestamp.Sub(genesis).Hours() < 0 {
return nil
}

hour, err := db.GetBlockHeightTimeHourAgo(block.Block.Time)
hour, err := db.GetBlockHeightTimeHourAgo(block.Timestamp)
if err != nil {
return err
}
newBlockTime := block.Block.Time.Sub(hour.Timestamp).Seconds() / float64(block.Block.Height-hour.Height)
newBlockTime := block.Timestamp.Sub(hour.Timestamp).Seconds() / float64(block.Height-hour.Height)

return db.SaveAverageBlockTimePerHour(newBlockTime, block.Block.Height)
return db.SaveAverageBlockTimePerHour(newBlockTime, block.Height)
}

// updateBlockTimeInDay insert average block time in the latest minute
func updateBlockTimeInDay(cp *client.Proxy, db *database.BigDipperDb) error {
func updateBlockTimeInDay(db *database.BigDipperDb) error {
log.Debug().Str("module", "consensus").Str("operation", "block time").
Msg("updating block time in days")

var block tmctypes.ResultBlock
err := cp.QueryLCD("/blocks/latest", &block)
block, err := db.GetLastBlock()
if err != nil {
return err
}
Expand All @@ -114,15 +109,15 @@ func updateBlockTimeInDay(cp *client.Proxy, db *database.BigDipperDb) error {
}

//check if chain is not created days ago
if block.Block.Time.Sub(genesis).Hours() < 24 {
if block.Timestamp.Sub(genesis).Hours() < 24 {
return nil
}

day, err := db.GetBlockHeightTimeDayAgo(block.Block.Time)
day, err := db.GetBlockHeightTimeDayAgo(block.Timestamp)
if err != nil {
return err
}
newBlockTime := block.Block.Time.Sub(day.Timestamp).Seconds() / float64(block.Block.Height-day.Height)
newBlockTime := block.Timestamp.Sub(day.Timestamp).Seconds() / float64(block.Height-day.Height)

return db.SaveAverageBlockTimePerDay(newBlockTime, block.Block.Height)
return db.SaveAverageBlockTimePerDay(newBlockTime, block.Height)
}
9 changes: 3 additions & 6 deletions x/mint/periodic_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/desmos-labs/juno/client"
"github.com/go-co-op/gocron"
"github.com/rs/zerolog/log"
tmctypes "github.com/tendermint/tendermint/rpc/core/types"

"github.com/forbole/bdjuno/database"
"github.com/forbole/bdjuno/x/utils"
Expand Down Expand Up @@ -36,17 +35,15 @@ func updateInflation(cp *client.Proxy, db *database.BigDipperDb) error {
Str("operation", "inflation").
Msg("getting inflation data")

// Get the latest block height
var block tmctypes.ResultBlock
err := cp.QueryLCD("/blocks/latest", &block)
height, err := db.GetLastBlockHeight()
if err != nil {
return err
}

// Get the inflation
var inflation sdk.Dec
endpoint := fmt.Sprintf("/mint/inflation?height=%d", block.Block.Height)
height, err := cp.QueryLCDWithHeight(endpoint, &inflation)
endpoint := fmt.Sprintf("/mint/inflation?height=%d", height)
_, err = cp.QueryLCDWithHeight(endpoint, &inflation)
if err != nil {
return err
}
Expand Down

0 comments on commit 334c183

Please sign in to comment.