Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Commit

Permalink
v2alpha1 node service (#5737)
Browse files Browse the repository at this point in the history
getting node status:
```
> grpcurl -plaintext -d '{}' 127.0.0.1:9092 spacemesh.v2alpha1.NodeService.Status

{
  "connectedPeers": "111",
  "status": "SYNC_STATUS_SYNCING",
  "latestLayer": 50373,
  "appliedLayer": 50372,
  "processedLayer": 50372,
  "currentLayer": 54315
}

```
  • Loading branch information
kacpersaw committed Apr 7, 2024
1 parent 5ec8b55 commit 2ec7aee
Show file tree
Hide file tree
Showing 7 changed files with 423 additions and 4 deletions.
3 changes: 2 additions & 1 deletion api/grpcserver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ const (
RewardV2Alpha1 Service = "reward_v2alpha1"
RewardStreamV2Alpha1 Service = "reward_stream_v2alpha1"
NetworkV2Alpha1 Service = "network_v2alpha1"
NodeV2Alpha1 Service = "node_v2alpha1"
)

// DefaultConfig defines the default configuration options for api.
func DefaultConfig() Config {
return Config{
PublicServices: []Service{
GlobalState, Mesh, Transaction, Node, Activation, ActivationV2Alpha1,
NetworkV2Alpha1,
RewardV2Alpha1, NetworkV2Alpha1, NodeV2Alpha1,
},
PublicListener: "0.0.0.0:9092",
PrivateServices: []Service{
Expand Down
83 changes: 83 additions & 0 deletions api/grpcserver/v2alpha1/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package v2alpha1

import (
"context"

"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
spacemeshv2alpha1 "github.com/spacemeshos/api/release/go/spacemesh/v2alpha1"
"google.golang.org/grpc"

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

const (
Node = "node_v2alpha1"
)

// nodePeerCounter is an api to get current peer count.
type nodePeerCounter interface {
PeerCount() uint64
}

// nodeMeshAPI is an api for getting mesh status.
type nodeMeshAPI interface {
LatestLayer() types.LayerID
LatestLayerInState() types.LayerID
ProcessedLayer() types.LayerID
}

// nodeSyncer is an API to get sync status.
type nodeSyncer interface {
IsSynced(context.Context) bool
}

func NewNodeService(peers nodePeerCounter, msh nodeMeshAPI, clock *timesync.NodeClock, syncer nodeSyncer) *NodeService {
return &NodeService{
mesh: msh,
clock: clock,
peerCounter: peers,
syncer: syncer,
}
}

type NodeService struct {
mesh nodeMeshAPI
clock *timesync.NodeClock
peerCounter nodePeerCounter
syncer nodeSyncer
}

func (s *NodeService) RegisterService(server *grpc.Server) {
spacemeshv2alpha1.RegisterNodeServiceServer(server, s)
}

func (s *NodeService) RegisterHandlerService(mux *runtime.ServeMux) error {
return spacemeshv2alpha1.RegisterNodeServiceHandlerServer(context.Background(), mux, s)
}

// String returns the service name.
func (s *NodeService) String() string {
return "NodeService"
}

func (s *NodeService) Status(ctx context.Context, _ *spacemeshv2alpha1.NodeStatusRequest) (
*spacemeshv2alpha1.NodeStatusResponse, error,
) {
var status spacemeshv2alpha1.NodeStatusResponse_SyncStatus

if s.syncer.IsSynced(ctx) {
status = spacemeshv2alpha1.NodeStatusResponse_SYNC_STATUS_SYNCED
} else {
status = spacemeshv2alpha1.NodeStatusResponse_SYNC_STATUS_SYNCING
}

return &spacemeshv2alpha1.NodeStatusResponse{
ConnectedPeers: s.peerCounter.PeerCount(),
Status: status,
LatestLayer: s.mesh.LatestLayer().Uint32(), // latest layer node has seen from blocks
AppliedLayer: s.mesh.LatestLayerInState().Uint32(), // last layer node has applied to the state
ProcessedLayer: s.mesh.ProcessedLayer().Uint32(), // last layer whose votes have been processed
CurrentLayer: s.clock.CurrentLayer().Uint32(), // current layer, based on clock time
}, nil
}
277 changes: 277 additions & 0 deletions api/grpcserver/v2alpha1/node_mocks.go

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

Loading

0 comments on commit 2ec7aee

Please sign in to comment.