diff --git a/lib/block_view_stake_test.go b/lib/block_view_stake_test.go index f1ff8576f..81b6f8a54 100644 --- a/lib/block_view_stake_test.go +++ b/lib/block_view_stake_test.go @@ -933,10 +933,6 @@ func TestStakingWithDerivedKey(t *testing.T) { return fees, nil } - // Seed a CurrentEpochEntry. - epochUtxoView := newUtxoView() - epochUtxoView._setCurrentEpochEntry(&EpochEntry{EpochNumber: 1, FinalBlockHeight: blockHeight + 10}) - require.NoError(t, epochUtxoView.FlushToDb(blockHeight)) currentEpochNumber, err := newUtxoView().GetCurrentEpochNumber() require.NoError(t, err) diff --git a/lib/block_view_validator_test.go b/lib/block_view_validator_test.go index 33a4d210d..3fe943f2b 100644 --- a/lib/block_view_validator_test.go +++ b/lib/block_view_validator_test.go @@ -65,12 +65,6 @@ func _testValidatorRegistration(t *testing.T, flushToDB bool) { m0PKID := DBGetPKIDEntryForPublicKey(db, chain.snapshot, m0PkBytes).PKID - // Seed a CurrentEpochEntry. - epochUtxoView, err := NewUtxoView(db, params, chain.postgres, chain.snapshot) - require.NoError(t, err) - epochUtxoView._setCurrentEpochEntry(&EpochEntry{EpochNumber: 1, FinalBlockHeight: blockHeight + 10}) - require.NoError(t, epochUtxoView.FlushToDb(blockHeight)) - { // ParamUpdater set min fee rate params.ExtraRegtestParamUpdaterKeys[MakePkMapKey(paramUpdaterPkBytes)] = true @@ -576,12 +570,6 @@ func TestValidatorRegistrationWithDerivedKey(t *testing.T) { return nil } - // Seed a CurrentEpochEntry. - epochUtxoView, err := NewUtxoView(db, params, chain.postgres, chain.snapshot) - require.NoError(t, err) - epochUtxoView._setCurrentEpochEntry(&EpochEntry{EpochNumber: 1, FinalBlockHeight: blockHeight + 10}) - require.NoError(t, epochUtxoView.FlushToDb(blockHeight)) - { // ParamUpdater set min fee rate params.ExtraRegtestParamUpdaterKeys[MakePkMapKey(paramUpdaterPkBytes)] = true @@ -750,12 +738,6 @@ func _testGetTopActiveValidatorsByStake(t *testing.T, flushToDB bool) { m1PKID := DBGetPKIDEntryForPublicKey(db, chain.snapshot, m1PkBytes).PKID m2PKID := DBGetPKIDEntryForPublicKey(db, chain.snapshot, m2PkBytes).PKID - // Seed a CurrentEpochEntry. - epochUtxoView, err := NewUtxoView(db, params, chain.postgres, chain.snapshot) - require.NoError(t, err) - epochUtxoView._setCurrentEpochEntry(&EpochEntry{EpochNumber: 1, FinalBlockHeight: blockHeight + 10}) - require.NoError(t, epochUtxoView.FlushToDb(blockHeight)) - { // ParamUpdater set min fee rate params.ExtraRegtestParamUpdaterKeys[MakePkMapKey(paramUpdaterPkBytes)] = true @@ -1188,12 +1170,6 @@ func _testUpdatingValidatorDisableDelegatedStake(t *testing.T, flushToDB bool) { m0PKID := DBGetPKIDEntryForPublicKey(db, chain.snapshot, m0PkBytes).PKID - // Seed a CurrentEpochEntry. - epochUtxoView, err := NewUtxoView(db, params, chain.postgres, chain.snapshot) - require.NoError(t, err) - epochUtxoView._setCurrentEpochEntry(&EpochEntry{EpochNumber: 1, FinalBlockHeight: blockHeight + 10}) - require.NoError(t, epochUtxoView.FlushToDb(blockHeight)) - { // ParamUpdater set min fee rate params.ExtraRegtestParamUpdaterKeys[MakePkMapKey(paramUpdaterPkBytes)] = true @@ -1384,11 +1360,6 @@ func _testUnregisterAsValidator(t *testing.T, flushToDB bool) { m0PKID := DBGetPKIDEntryForPublicKey(db, chain.snapshot, m0PkBytes).PKID m1PKID := DBGetPKIDEntryForPublicKey(db, chain.snapshot, m1PkBytes).PKID - // Seed a CurrentEpochEntry. - epochUtxoView, err := NewUtxoView(db, params, chain.postgres, chain.snapshot) - require.NoError(t, err) - epochUtxoView._setCurrentEpochEntry(&EpochEntry{EpochNumber: 1, FinalBlockHeight: blockHeight + 10}) - require.NoError(t, epochUtxoView.FlushToDb(blockHeight)) currentEpochNumber, err := utxoView().GetCurrentEpochNumber() require.NoError(t, err) diff --git a/lib/pos_epoch.go b/lib/pos_epoch.go index cb4e2c16e..6025b04cf 100644 --- a/lib/pos_epoch.go +++ b/lib/pos_epoch.go @@ -5,6 +5,7 @@ import ( "github.com/dgraph-io/badger/v3" "github.com/golang/glog" "github.com/pkg/errors" + "math" ) // @@ -78,8 +79,12 @@ func (bav *UtxoView) GetCurrentEpochEntry() (*EpochEntry, error) { if epochEntry != nil { // Cache in the UtxoView. bav._setCurrentEpochEntry(epochEntry) + return epochEntry, nil } - return epochEntry, nil + + // If still not found, return the GenesisEpochEntry. This will be the + // case prior to the first execution of the OnEpochCompleteHook. + return &EpochEntry{EpochNumber: 0, FinalBlockHeight: math.MaxUint64}, nil } func (bav *UtxoView) GetCurrentEpochNumber() (uint64, error) { diff --git a/lib/pos_epoch_test.go b/lib/pos_epoch_test.go index 3d84f1ec9..9d3f58ecb 100644 --- a/lib/pos_epoch_test.go +++ b/lib/pos_epoch_test.go @@ -2,6 +2,7 @@ package lib import ( "github.com/stretchr/testify/require" + "math" "testing" ) @@ -23,10 +24,12 @@ func TestCurrentEpoch(t *testing.T) { // Test that the CurrentEpoch is nil in the UtxoView. require.Nil(t, utxoView.CurrentEpochEntry) - // Test GetCurrentEpoch(). + // Test GetCurrentEpoch() returns the GenesisEpochEntry. epochEntry, err = utxoView.GetCurrentEpochEntry() require.NoError(t, err) - require.Nil(t, epochEntry) + require.NotNil(t, epochEntry) + require.Equal(t, epochEntry.EpochNumber, uint64(0)) + require.Equal(t, epochEntry.FinalBlockHeight, uint64(math.MaxUint64)) // Set the CurrentEpoch. epochEntry = &EpochEntry{