Skip to content

Commit

Permalink
Apply review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
kacpersaw committed Dec 18, 2024
1 parent af69bba commit 34a2545
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 166 deletions.
21 changes: 7 additions & 14 deletions api/grpcserver/v2alpha1/activation.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ const (
ActivationStream = "activation_stream_v2alpha1"
)

type activationProvider interface {
GetAtx(id types.ATXID) (*types.ActivationTx, error)
MaxHeightAtx() (types.ATXID, error)
}

func NewActivationStreamService(db sql.Executor) *ActivationStreamService {
return &ActivationStreamService{db: db}
}
Expand Down Expand Up @@ -172,18 +167,16 @@ func toAtx(atx *types.ActivationTx) *spacemeshv2alpha1.Activation {
}
}

func NewActivationService(db sql.Executor, atxProvider activationProvider, goldenAtx types.ATXID) *ActivationService {
func NewActivationService(db sql.Executor, goldenAtx types.ATXID) *ActivationService {
return &ActivationService{
db: db,
atxProvider: atxProvider,
goldenAtx: goldenAtx,
db: db,
goldenAtx: goldenAtx,
}
}

type ActivationService struct {
goldenAtx types.ATXID
db sql.Executor
atxProvider activationProvider
goldenAtx types.ATXID
db sql.Executor
}

func (s *ActivationService) RegisterService(server *grpc.Server) {
Expand Down Expand Up @@ -252,7 +245,7 @@ func (s *ActivationService) Highest(
_ context.Context,
_ *spacemeshv2alpha1.HighestRequest,
) (*spacemeshv2alpha1.HighestResponse, error) {
highest, err := s.atxProvider.MaxHeightAtx()
highest, err := atxs.GetIDWithMaxHeight(s.db, types.EmptyNodeID, atxs.FilterAll)
if err != nil {
return &spacemeshv2alpha1.HighestResponse{
Activation: &spacemeshv2alpha1.Activation{
Expand All @@ -261,7 +254,7 @@ func (s *ActivationService) Highest(
}, nil
}

atx, err := s.atxProvider.GetAtx(highest)
atx, err := atxs.Get(s.db, highest)
if err != nil || atx == nil {
return nil, status.Error(codes.NotFound, fmt.Sprintf("atx id %v not found: %s", highest, err))
}

Check warning on line 260 in api/grpcserver/v2alpha1/activation.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/activation.go#L259-L260

Added lines #L259 - L260 were not covered by tests
Expand Down
119 changes: 0 additions & 119 deletions api/grpcserver/v2alpha1/activation_mocks.go

This file was deleted.

62 changes: 30 additions & 32 deletions api/grpcserver/v2alpha1/activation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
spacemeshv2alpha1 "github.com/spacemeshos/api/release/go/spacemesh/v2alpha1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

Expand All @@ -26,8 +25,6 @@ import (
func TestActivationService_List(t *testing.T) {
setup := func(t *testing.T) (spacemeshv2alpha1.ActivationServiceClient, []types.ActivationTx) {
db := statesql.InMemoryTest(t)
ctrl, _ := gomock.WithContext(context.Background(), t)
atxProvider := NewMockactivationProvider(ctrl)

gen := fixture.NewAtxsGenerator()
activations := make([]types.ActivationTx, 100)
Expand All @@ -38,7 +35,7 @@ func TestActivationService_List(t *testing.T) {
}

goldenAtx := types.ATXID{2, 3, 4}
svc := NewActivationService(db, atxProvider, goldenAtx)
svc := NewActivationService(db, goldenAtx)
cfg, cleanup := launchServer(t, svc)
t.Cleanup(cleanup)

Expand Down Expand Up @@ -232,9 +229,6 @@ func TestActivationService_ActivationsCount(t *testing.T) {
db := statesql.InMemoryTest(t)
ctx := context.Background()

ctrl, ctx := gomock.WithContext(ctx, t)
atxProvider := NewMockactivationProvider(ctrl)

genEpoch3 := fixture.NewAtxsGenerator().WithEpochs(3, 1)
epoch3ATXs := make([]types.ActivationTx, 30)
for i := range epoch3ATXs {
Expand All @@ -253,7 +247,7 @@ func TestActivationService_ActivationsCount(t *testing.T) {
}

goldenAtx := types.ATXID{2, 3, 4}
svc := NewActivationService(db, atxProvider, goldenAtx)
svc := NewActivationService(db, goldenAtx)
cfg, cleanup := launchServer(t, svc)
t.Cleanup(cleanup)

Expand Down Expand Up @@ -286,32 +280,27 @@ func TestActivationService_ActivationsCount(t *testing.T) {
}

func TestActivationService_Highest(t *testing.T) {
db := statesql.InMemoryTest(t)
ctx := context.Background()

ctrl, ctx := gomock.WithContext(ctx, t)
atxProvider := NewMockactivationProvider(ctrl)
atx := types.ActivationTx{
Sequence: rand.Uint64(),
PublishEpoch: 0,
Coinbase: types.GenerateAddress(types.RandomBytes(32)),
NumUnits: rand.Uint32(),
}
id := types.RandomATXID()
atx.SetID(id)

goldenAtx := types.ATXID{2, 3, 4}
t.Run("max tick height", func(t *testing.T) {
db := statesql.InMemoryTest(t)
ctx := context.Background()

svc := NewActivationService(db, atxProvider, goldenAtx)
cfg, cleanup := launchServer(t, svc)
t.Cleanup(cleanup)
goldenAtx := types.ATXID{2, 3, 4}
svc := NewActivationService(db, goldenAtx)
cfg, cleanup := launchServer(t, svc)
t.Cleanup(cleanup)

conn := dialGrpc(t, cfg)
client := spacemeshv2alpha1.NewActivationServiceClient(conn)
conn := dialGrpc(t, cfg)
client := spacemeshv2alpha1.NewActivationServiceClient(conn)

t.Run("max tick height", func(t *testing.T) {
atxProvider.EXPECT().MaxHeightAtx().Return(id, nil)
atxProvider.EXPECT().GetAtx(id).Return(&atx, nil)
atx := &types.ActivationTx{
Sequence: rand.Uint64(),
PublishEpoch: 0,
Coinbase: types.GenerateAddress(types.RandomBytes(32)),
NumUnits: rand.Uint32(),
}
id := types.RandomATXID()
atx.SetID(id)
require.NoError(t, atxs.Add(db, atx, types.AtxBlob{}))

res, err := client.Highest(ctx, &spacemeshv2alpha1.HighestRequest{})
require.NoError(t, err)
Expand All @@ -324,7 +313,16 @@ func TestActivationService_Highest(t *testing.T) {
require.Equal(t, atx.TickHeight(), res.Activation.Height)
})
t.Run("returns golden atx on error", func(t *testing.T) {
atxProvider.EXPECT().MaxHeightAtx().Return(types.ATXID{}, errors.New("empty"))
db := statesql.InMemoryTest(t)
ctx := context.Background()

goldenAtx := types.ATXID{2, 3, 4}
svc := NewActivationService(db, goldenAtx)
cfg, cleanup := launchServer(t, svc)
t.Cleanup(cleanup)

conn := dialGrpc(t, cfg)
client := spacemeshv2alpha1.NewActivationServiceClient(conn)

res, err := client.Highest(ctx, &spacemeshv2alpha1.HighestRequest{})
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -1554,7 +1554,7 @@ func (app *App) grpcService(svc grpcserver.Service, lg log.Log) (grpcserver.Serv
app.grpcServices[svc] = service
return service, nil
case v2alpha1.Activation:
service := v2alpha1.NewActivationService(app.apiDB, app.cachedDB, types.ATXID(app.Config.Genesis.GoldenATX()))
service := v2alpha1.NewActivationService(app.apiDB, types.ATXID(app.Config.Genesis.GoldenATX()))
app.grpcServices[svc] = service
return service, nil
case v2alpha1.ActivationStream:
Expand Down

0 comments on commit 34a2545

Please sign in to comment.