From a43633d74f2e016cf2561cf6058f32288598a141 Mon Sep 17 00:00:00 2001 From: lazynina Date: Thu, 18 May 2023 21:47:40 -0400 Subject: [PATCH 1/6] Make Postgres behave the same as badger for immature block rewards --- lib/block_view.go | 7 +++++-- lib/postgres.go | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/block_view.go b/lib/block_view.go index 6f0788701..8bc2c216e 100644 --- a/lib/block_view.go +++ b/lib/block_view.go @@ -3889,10 +3889,13 @@ 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 } outputs := bav.Postgres.GetBlockRewardsForPublicKey(NewPublicKey(pkBytes), startHeight, tipHeight) diff --git a/lib/postgres.go b/lib/postgres.go index 01df347a5..e77faf7e7 100644 --- a/lib/postgres.go +++ b/lib/postgres.go @@ -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 } From 5df82a283ef41f803b920152c5d86d242633cef7 Mon Sep 17 00:00:00 2001 From: lazynina Date: Thu, 18 May 2023 21:55:47 -0400 Subject: [PATCH 2/6] reset postgres between pg tests --- lib/block_view_association_test.go | 10 ++++++++++ lib/block_view_dao_coin_limit_order_test.go | 5 +++++ lib/block_view_post_test.go | 5 +++++ lib/postgres_test.go | 11 +++++++++++ 4 files changed, 31 insertions(+) diff --git a/lib/block_view_association_test.go b/lib/block_view_association_test.go index 63fc14453..4e2a1ed0e 100644 --- a/lib/block_view_association_test.go +++ b/lib/block_view_association_test.go @@ -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(¶ms.ForkHeights) @@ -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. diff --git a/lib/block_view_dao_coin_limit_order_test.go b/lib/block_view_dao_coin_limit_order_test.go index d0a3bb070..8f7d72085 100644 --- a/lib/block_view_dao_coin_limit_order_test.go +++ b/lib/block_view_dao_coin_limit_order_test.go @@ -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(t, ResetPostgres(chain.postgres)) + } + }() mempool, miner := NewTestMiner(t, chain, params, true) params.ForkHeights.DAOCoinBlockHeight = uint32(0) diff --git a/lib/block_view_post_test.go b/lib/block_view_post_test.go index 02c469da3..6853ddfcd 100644 --- a/lib/block_view_post_test.go +++ b/lib/block_view_post_test.go @@ -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(¶ms.ForkHeights) params.EncoderMigrationHeightsList = GetEncoderMigrationHeightsList(¶ms.ForkHeights) diff --git a/lib/postgres_test.go b/lib/postgres_test.go index 585c8f565..a13930d61 100644 --- a/lib/postgres_test.go +++ b/lib/postgres_test.go @@ -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 +} From 3213bee0d8a22fbe79f7fdd4351f913431e930c9 Mon Sep 17 00:00:00 2001 From: lazynina Date: Thu, 18 May 2023 21:58:20 -0400 Subject: [PATCH 3/6] fix dao coin limit order test --- lib/block_view_dao_coin_limit_order_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/block_view_dao_coin_limit_order_test.go b/lib/block_view_dao_coin_limit_order_test.go index 8f7d72085..d1b332383 100644 --- a/lib/block_view_dao_coin_limit_order_test.go +++ b/lib/block_view_dao_coin_limit_order_test.go @@ -619,7 +619,7 @@ func TestDAOCoinLimitOrder(t *testing.T) { chain, params, db := NewLowDifficultyBlockchain(t) defer func() { if chain.postgres != nil { - require.NoError(t, ResetPostgres(chain.postgres)) + require.NoError(ResetPostgres(chain.postgres)) } }() mempool, miner := NewTestMiner(t, chain, params, true) From 88892be1e5f8cdeae0902727cb0f288b69670a4e Mon Sep 17 00:00:00 2001 From: lazynina Date: Thu, 18 May 2023 22:04:08 -0400 Subject: [PATCH 4/6] don't set view tipHash if postgres has been closed --- lib/block_view.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/block_view.go b/lib/block_view.go index 8bc2c216e..363b38012 100644 --- a/lib/block_view.go +++ b/lib/block_view.go @@ -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 */) } From e20a99b84ed6ba572efab33d62028143b92541ab Mon Sep 17 00:00:00 2001 From: lazynina Date: Thu, 18 May 2023 22:12:08 -0400 Subject: [PATCH 5/6] set block reward maturity to 0 in dao coin limit order test --- lib/block_view_dao_coin_limit_order_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/block_view_dao_coin_limit_order_test.go b/lib/block_view_dao_coin_limit_order_test.go index d1b332383..e35559537 100644 --- a/lib/block_view_dao_coin_limit_order_test.go +++ b/lib/block_view_dao_coin_limit_order_test.go @@ -627,7 +627,7 @@ func TestDAOCoinLimitOrder(t *testing.T) { params.ForkHeights.DAOCoinBlockHeight = uint32(0) params.ForkHeights.DAOCoinLimitOrderBlockHeight = uint32(0) params.ForkHeights.OrderBookDBFetchOptimizationBlockHeight = uint32(0) - params.BlockRewardMaturity = time.Second + params.BlockRewardMaturity = 0 * time.Second utxoView, err := NewUtxoView(db, params, chain.postgres, chain.snapshot) require.NoError(err) From 4342e392dd8dcd2ebcd388a4d49741b01dbb3ae2 Mon Sep 17 00:00:00 2001 From: lazynina Date: Thu, 18 May 2023 22:54:49 -0400 Subject: [PATCH 6/6] fix pg case where num immature blocks is 0 --- lib/block_view.go | 4 ++++ lib/block_view_dao_coin_limit_order_test.go | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/block_view.go b/lib/block_view.go index 363b38012..f9d0c9357 100644 --- a/lib/block_view.go +++ b/lib/block_view.go @@ -3900,6 +3900,10 @@ func (bav *UtxoView) GetSpendableDeSoBalanceNanosForPublicKey(pkBytes []byte, 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) var err error diff --git a/lib/block_view_dao_coin_limit_order_test.go b/lib/block_view_dao_coin_limit_order_test.go index e35559537..d1b332383 100644 --- a/lib/block_view_dao_coin_limit_order_test.go +++ b/lib/block_view_dao_coin_limit_order_test.go @@ -627,7 +627,7 @@ func TestDAOCoinLimitOrder(t *testing.T) { params.ForkHeights.DAOCoinBlockHeight = uint32(0) params.ForkHeights.DAOCoinLimitOrderBlockHeight = uint32(0) params.ForkHeights.OrderBookDBFetchOptimizationBlockHeight = uint32(0) - params.BlockRewardMaturity = 0 * time.Second + params.BlockRewardMaturity = time.Second utxoView, err := NewUtxoView(db, params, chain.postgres, chain.snapshot) require.NoError(err)