Skip to content

Commit

Permalink
fix(repository): get the previous epoch by index
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
torives committed Aug 30, 2024
1 parent e514801 commit 8fca1ca
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
12 changes: 5 additions & 7 deletions internal/repository/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -216,15 +211,15 @@ 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
`

args := pgx.NamedArgs{
"appAddress": currentEpoch.AppAddress,
"lastBlock": currentEpoch.FirstBlock - 1,
"index": currentEpoch.Index,
}

var (
Expand All @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/repository/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 8fca1ca

Please sign in to comment.