Skip to content

Commit

Permalink
api: v2alpha1: Add labels_per_unit to NetworkService.Info endpoint (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kacpersaw committed Aug 5, 2024
1 parent 6e32527 commit b498ac8
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 14 deletions.
10 changes: 7 additions & 3 deletions api/grpcserver/v2alpha1/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,27 @@ import (
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/config"
)

const (
Network = "network_v2alpha1"
)

func NewNetworkService(genesisTime time.Time, genesisID types.Hash20, layerDuration time.Duration) *NetworkService {
func NewNetworkService(genesisTime time.Time, config *config.Config) *NetworkService {
return &NetworkService{
genesisTime: genesisTime,
genesisID: genesisID,
layerDuration: layerDuration,
genesisID: config.Genesis.GenesisID(),
layerDuration: config.LayerDuration,
labelsPerUnit: config.POST.LabelsPerUnit,
}
}

type NetworkService struct {
genesisTime time.Time
genesisID types.Hash20
layerDuration time.Duration
labelsPerUnit uint64
}

func (s *NetworkService) RegisterService(server *grpc.Server) {
Expand All @@ -54,5 +57,6 @@ func (s *NetworkService) Info(context.Context,
Hrp: types.NetworkHRP(),
EffectiveGenesisLayer: types.GetEffectiveGenesis().Uint32(),
LayersPerEpoch: types.GetLayersPerEpoch(),
LabelsPerUnit: s.labelsPerUnit,
}, nil
}
9 changes: 6 additions & 3 deletions api/grpcserver/v2alpha1/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
"github.com/stretchr/testify/require"

"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/config"
)

func TestNetworkService_Info(t *testing.T) {
ctx := context.Background()
genesis := time.Unix(genTimeUnix, 0)
c := config.DefaultTestConfig()

svc := NewNetworkService(genesis, genesisID, layerDuration)
svc := NewNetworkService(genesis, &c)
cfg, cleanup := launchServer(t, svc)
t.Cleanup(cleanup)

Expand All @@ -27,10 +29,11 @@ func TestNetworkService_Info(t *testing.T) {
require.NoError(t, err)

require.Equal(t, genesis.UTC(), info.GenesisTime.AsTime().UTC())
require.Equal(t, layerDuration, info.LayerDuration.AsDuration())
require.Equal(t, genesisID.Bytes(), info.GenesisId)
require.Equal(t, c.LayerDuration, info.LayerDuration.AsDuration())
require.Equal(t, c.Genesis.GenesisID().Bytes(), info.GenesisId)
require.Equal(t, types.NetworkHRP(), info.Hrp)
require.Equal(t, types.GetEffectiveGenesis().Uint32(), info.EffectiveGenesisLayer)
require.Equal(t, types.GetLayersPerEpoch(), info.LayersPerEpoch)
require.Equal(t, c.POST.LabelsPerUnit, info.LabelsPerUnit)
})
}
3 changes: 0 additions & 3 deletions api/grpcserver/v2alpha1/v2alpha1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ import (
"google.golang.org/grpc/credentials/insecure"

"github.com/spacemeshos/go-spacemesh/api/grpcserver"
"github.com/spacemeshos/go-spacemesh/common/types"
)

const (
genTimeUnix = 1000000
layerDuration = 10 * time.Second
)

var genesisID = types.Hash20{}

func launchServer(tb testing.TB, services ...grpcserver.ServiceAPI) (grpcserver.Config, func()) {
cfg := grpcserver.DefaultTestConfig()
grpc, err := grpcserver.NewWithServices(cfg.PublicListener, zaptest.NewLogger(tb).Named("grpc"), cfg, services)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ require (
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/seehuhn/mt19937 v1.0.0
github.com/slok/go-http-metrics v0.12.0
github.com/spacemeshos/api/release/go v1.50.0
github.com/spacemeshos/api/release/go v1.51.0
github.com/spacemeshos/economics v0.1.3
github.com/spacemeshos/fixed v0.1.1
github.com/spacemeshos/go-scale v1.2.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -602,8 +602,8 @@ github.com/sourcegraph/annotate v0.0.0-20160123013949-f4cad6c6324d/go.mod h1:Udh
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e/go.mod h1:HuIsMU8RRBOtsCgI77wP899iHVBQpCmg4ErYMZB+2IA=
github.com/spacemeshos/api/release/go v1.50.0 h1:M7Usg/LxymscwqYO7/Doyb+sU4lS1e+JIsSgqTDGk/0=
github.com/spacemeshos/api/release/go v1.50.0/go.mod h1:PvgDpjfwkZLVVNExYG7wDNzgMqT3p+ppfTU2UESSF9U=
github.com/spacemeshos/api/release/go v1.51.0 h1:MSKRIUiXBAoDrj2Lj24q9g52ZaSIC3I0UH/Y0Oaz95o=
github.com/spacemeshos/api/release/go v1.51.0/go.mod h1:Qr/pVPMmN5Q5qLHSXqVMDKDCu6LkHWzGPNflylE0u00=
github.com/spacemeshos/economics v0.1.3 h1:ACkq3mTebIky4Zwbs9SeSSRZrUCjU/Zk0wq9Z0BTh2A=
github.com/spacemeshos/economics v0.1.3/go.mod h1:FH7u0FzTIm6Kpk+X5HOZDvpkgNYBKclmH86rVwYaDAo=
github.com/spacemeshos/fixed v0.1.1 h1:N1y4SUpq1EV+IdJrWJwUCt1oBFzeru/VKVcBsvPc2Fk=
Expand Down
3 changes: 1 addition & 2 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -1558,8 +1558,7 @@ func (app *App) grpcService(svc grpcserver.Service, lg log.Log) (grpcserver.Serv
case v2alpha1.Network:
service := v2alpha1.NewNetworkService(
app.clock.GenesisTime(),
app.Config.Genesis.GenesisID(),
app.Config.LayerDuration)
app.Config)
app.grpcServices[svc] = service
return service, nil
case v2alpha1.Node:
Expand Down

0 comments on commit b498ac8

Please sign in to comment.