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

Make Postgres behave the same as badger for immature block rewards #532

Merged
merged 6 commits into from
May 19, 2023
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
16 changes: 13 additions & 3 deletions lib/block_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,10 @@ func NewUtxoView(
// not concern itself with the header chain (see comment on GetBestHash for more
// info on that).
if view.Postgres != nil {
view.TipHash = view.Postgres.GetChain(MAIN_CHAIN).TipHash
pgChain := view.Postgres.GetChain(MAIN_CHAIN)
if pgChain != nil {
view.TipHash = view.Postgres.GetChain(MAIN_CHAIN).TipHash
}
} else {
view.TipHash = DbGetBestHash(view.Handle, view.Snapshot, ChainTypeDeSoBlock /* don't get the header chain */)
}
Expand Down Expand Up @@ -3889,10 +3892,17 @@ func (bav *UtxoView) GetSpendableDeSoBalanceNanosForPublicKey(pkBytes []byte,
immatureBlockRewards := uint64(0)

if bav.Postgres != nil {
// Note: badger is only getting the block reward for the previous block, so we make postgres
// do the same thing. This is not ideal, but it is the simplest way to get the same behavior
// and we will address the issue soon.
// Filter out immature block rewards in postgres. UtxoType needs to be set correctly when importing blocks
var startHeight uint32
if tipHeight > numImmatureBlocks {
startHeight = tipHeight - numImmatureBlocks
if tipHeight > 0 {
startHeight = tipHeight - 1
}
// This is a special case to support tests where the number of immature blocks is 0.
if numImmatureBlocks == 0 {
startHeight = tipHeight
}
outputs := bav.Postgres.GetBlockRewardsForPublicKey(NewPublicKey(pkBytes), startHeight, tipHeight)

Expand Down
10 changes: 10 additions & 0 deletions lib/block_view_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func _testAssociations(t *testing.T, flushToDB bool) {

// Initialize test chain and miner.
chain, params, db := NewLowDifficultyBlockchain(t)
defer func() {
if chain.postgres != nil {
require.NoError(t, ResetPostgres(chain.postgres))
}
}()
mempool, miner := NewTestMiner(t, chain, params, true)
params.ForkHeights.AssociationsAndAccessGroupsBlockHeight = uint32(0)
GlobalDeSoParams.EncoderMigrationHeights = GetEncoderMigrationHeights(&params.ForkHeights)
Expand Down Expand Up @@ -2204,6 +2209,11 @@ func _testAssociationsWithDerivedKey(t *testing.T) {

// Initialize test chain and miner.
chain, params, db := NewLowDifficultyBlockchain(t)
defer func() {
if chain.postgres != nil {
require.NoError(t, ResetPostgres(chain.postgres))
}
}()
mempool, miner := NewTestMiner(t, chain, params, true)

// Initialize fork heights.
Expand Down
5 changes: 5 additions & 0 deletions lib/block_view_dao_coin_limit_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,11 @@ func TestDAOCoinLimitOrder(t *testing.T) {
// Initialize test chain and miner.
require := require.New(t)
chain, params, db := NewLowDifficultyBlockchain(t)
defer func() {
if chain.postgres != nil {
require.NoError(ResetPostgres(chain.postgres))
}
}()
mempool, miner := NewTestMiner(t, chain, params, true)

params.ForkHeights.DAOCoinBlockHeight = uint32(0)
Expand Down
5 changes: 5 additions & 0 deletions lib/block_view_post_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2108,6 +2108,11 @@ func TestDeSoDiamondErrorCases(t *testing.T) {
func TestFreezingPosts(t *testing.T) {
// Initialize blockchain.
chain, params, db := NewLowDifficultyBlockchain(t)
defer func() {
if chain.postgres != nil {
require.NoError(t, ResetPostgres(chain.postgres))
}
}()
params.ForkHeights.AssociationsAndAccessGroupsBlockHeight = 1
params.EncoderMigrationHeights = GetEncoderMigrationHeights(&params.ForkHeights)
params.EncoderMigrationHeightsList = GetEncoderMigrationHeightsList(&params.ForkHeights)
Expand Down
2 changes: 1 addition & 1 deletion lib/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -3284,7 +3284,7 @@ func (postgres *Postgres) GetBlockRewardsForPublicKey(publicKey *PublicKey, star
Where("pg_transaction_output.public_key = ?", publicKey).
Where("pgt.Type = ?", TxnTypeBlockReward).
Where("pg_transaction_output.height > ?", startHeight).
Where("pg_transaction_output.height < ?", endHeight).Select()
Where("pg_transaction_output.height <= ?", endHeight).Select()
if err != nil {
return nil
}
Expand Down
11 changes: 11 additions & 0 deletions lib/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,14 @@ func StopTestEmbeddedPostgresDB(epg *embeddedpostgres.EmbeddedPostgres) error {
}
return nil
}

func ResetPostgres(postgres *Postgres) error {
migrate.LoadMigrations()
if err := migrations.Run(postgres.db, "migrate", []string{"", "rollback"}); err != nil {
return errors.Wrapf(err, "StopTestEmbeddedPostgresDB: Problem running rollback")
}
if err := migrations.Run(postgres.db, "migrate", []string{"", "migrate"}); err != nil {
return errors.Wrapf(err, "StopTestEmbeddedPostgresDB: Problem running migrations")
}
return nil
}