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

Sort babylon params in /v2/network-info according version field (#186) #199

Merged
merged 5 commits into from
Jan 16, 2025
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
9 changes: 3 additions & 6 deletions internal/indexer/db/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/babylonlabs-io/staking-api-service/internal/shared/db"
)

//go:generate mockery --name=IndexerDBClient --output=../../../../tests/mocks --outpkg=mocks --filename=mock_indexer_db_client.go
type IndexerDBClient interface {
Ping(ctx context.Context) error
// Params
Expand All @@ -19,10 +20,6 @@ type IndexerDBClient interface {
// Staker Delegations
GetDelegation(ctx context.Context, stakingTxHashHex string) (*indexerdbmodel.IndexerDelegationDetails, error)
GetDelegations(ctx context.Context, stakerPKHex string, paginationToken string) (*db.DbResultMap[indexerdbmodel.IndexerDelegationDetails], error)
/**
* GetLastProcessedBbnHeight retrieves the last processed BBN height.
* @param ctx The context
* @return The last processed height or an error
*/
GetLastProcessedBbnHeight(ctx context.Context) (uint64, error)
// GetLastProcessedBbnHeight retrieves the last processed BBN height.
GetLastProcessedBbnHeight(ctx context.Context) (lastProcessedHeight uint64, err error)
}
8 changes: 8 additions & 0 deletions internal/v2/service/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (

"github.com/babylonlabs-io/staking-api-service/internal/shared/types"
"github.com/rs/zerolog/log"
"slices"
indexertypes "github.com/babylonlabs-io/staking-api-service/internal/indexer/types"
"cmp"
)

type StakingStatusPublic struct {
Expand Down Expand Up @@ -39,6 +42,11 @@ func (s *V2Service) GetNetworkInfo(ctx context.Context) (*NetworkInfoPublic, *ty
status = bbnHeight >= s.Cfg.DelegationTransition.AllowListExpirationHeight
}

// sort (asc) babylon params according to their version
slices.SortFunc(babylonParams, func(a, b *indexertypes.BbnStakingParams) int {
return cmp.Compare(a.Version, b.Version)
})
jrwbabylonlab marked this conversation as resolved.
Show resolved Hide resolved

return &NetworkInfoPublic{
StakingStatus: StakingStatusPublic{
IsStakingOpen: status,
Expand Down
48 changes: 48 additions & 0 deletions internal/v2/service/network_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package v2service

import (
"testing"
"context"
"github.com/stretchr/testify/require"
dbclients "github.com/babylonlabs-io/staking-api-service/internal/shared/db/clients"
"github.com/babylonlabs-io/staking-api-service/tests/mocks"
"github.com/babylonlabs-io/staking-api-service/internal/shared/config"
indexertypes "github.com/babylonlabs-io/staking-api-service/internal/indexer/types"
"github.com/stretchr/testify/assert"
"slices"
)

func TestGetNetworkInfo(t *testing.T) {
ctx := context.Background() // todo(Kirill) replace with t.Context() after go 1.24 release
t.Run("BBN params are sorted", func(t *testing.T) {
indexerDB := &mocks.IndexerDBClient{}
defer indexerDB.AssertExpectations(t)

bbnStakingParams := []*indexertypes.BbnStakingParams{
// other values are not important in this test, focus only on version
{Version: 33},
{Version: 0},
{Version: 7},
{Version: 9},
}
indexerDB.On("GetBbnStakingParams", ctx).Return(bbnStakingParams, nil).Once()
indexerDB.On("GetBtcCheckpointParams", ctx).Return(nil, nil).Once()

service, err := New(ctx, &config.Config{}, nil, &dbclients.DbClients{
IndexerDBClient: indexerDB,
})
require.NoError(t, err)

resp, rpcErr := service.GetNetworkInfo(ctx)
require.Nil(t, rpcErr)

// todo(Kirill) use Map functional flow once it's added
var versions []uint32
for _, param := range resp.Params.Bbn {
versions = append(versions, param.Version)
}

assert.NotEmpty(t, versions)
assert.True(t, slices.IsSorted(versions))
})
}
260 changes: 260 additions & 0 deletions tests/mocks/mock_indexer_db_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading