From 8fca1cacd81d4f15843fbfdcb4d51c89ca0f865c Mon Sep 17 00:00:00 2001 From: Victor Yves Crispim Date: Wed, 28 Aug 2024 14:57:33 -0300 Subject: [PATCH] fix(repository): get the previous epoch by index Since epochs with no inputs won't be stored in the database, the block range between epochs might be disjointed, breaking this function. Filtering results based on the epoch index alone will be enough. --- internal/repository/validator.go | 12 +++++------- internal/repository/validator_test.go | 2 +- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/internal/repository/validator.go b/internal/repository/validator.go index 2c95120ee..ca6748b74 100644 --- a/internal/repository/validator.go +++ b/internal/repository/validator.go @@ -198,11 +198,6 @@ func (pg *Database) GetLastInputOutputsHash( // GetPreviousEpoch returns the epoch that ended one block before the start // of the current epoch func (pg *Database) GetPreviousEpoch(ctx context.Context, currentEpoch Epoch) (*Epoch, error) { - if currentEpoch.FirstBlock == 0 { - // if this is the first epoch, there is nothing to return - return nil, nil - } - query := ` SELECT id, @@ -216,7 +211,7 @@ func (pg *Database) GetPreviousEpoch(ctx context.Context, currentEpoch Epoch) (* FROM epoch WHERE - application_address=@appAddress AND last_block=@lastBlock + application_address=@appAddress AND index < @index ORDER BY index DESC LIMIT 1 @@ -224,7 +219,7 @@ func (pg *Database) GetPreviousEpoch(ctx context.Context, currentEpoch Epoch) (* args := pgx.NamedArgs{ "appAddress": currentEpoch.AppAddress, - "lastBlock": currentEpoch.FirstBlock - 1, + "index": currentEpoch.Index, } var ( @@ -245,6 +240,9 @@ func (pg *Database) GetPreviousEpoch(ctx context.Context, currentEpoch Epoch) (* &status, ) if err != nil { + if errors.Is(err, pgx.ErrNoRows) { + return nil, nil + } return nil, fmt.Errorf("GetPreviousEpoch failed: %w", err) } diff --git a/internal/repository/validator_test.go b/internal/repository/validator_test.go index 20731405c..92cf98bf5 100644 --- a/internal/repository/validator_test.go +++ b/internal/repository/validator_test.go @@ -225,7 +225,7 @@ func (s *RepositorySuite) TestGetPreviousEpoch() { previousEpoch, err = s.database.GetPreviousEpoch(s.ctx, epoch2) s.Require().Nil(err) s.Require().NotNil(previousEpoch) - s.Require().Equal(*previousEpoch, epoch) + s.Require().Equal(previousEpoch.Id, epoch.Id) } func (s *RepositorySuite) TestSetEpochClaimAndInsertProofsTransaction() {