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

[Access] Add spork and node root block heights to GetNodeVersionInfo #4690

Merged
merged 5 commits into from
Sep 15, 2023
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
10 changes: 6 additions & 4 deletions access/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ type NetworkParameters struct {

// NodeVersionInfo contains information about node, such as semver, commit, sporkID, protocolVersion, etc
type NodeVersionInfo struct {
Semver string
Commit string
SporkId flow.Identifier
ProtocolVersion uint64
Semver string
Commit string
SporkId flow.Identifier
ProtocolVersion uint64
SporkRootBlockHeight uint64
NodeRootBlockHeight uint64
}
10 changes: 6 additions & 4 deletions access/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ func (h *Handler) GetNodeVersionInfo(

return &access.GetNodeVersionInfoResponse{
Info: &entities.NodeVersionInfo{
Semver: nodeVersionInfo.Semver,
Commit: nodeVersionInfo.Commit,
SporkId: nodeVersionInfo.SporkId[:],
ProtocolVersion: nodeVersionInfo.ProtocolVersion,
Semver: nodeVersionInfo.Semver,
Commit: nodeVersionInfo.Commit,
SporkId: nodeVersionInfo.SporkId[:],
ProtocolVersion: nodeVersionInfo.ProtocolVersion,
SporkRootBlockHeight: nodeVersionInfo.SporkRootBlockHeight,
NodeRootBlockHeight: nodeVersionInfo.NodeRootBlockHeight,
},
}, nil
}
Expand Down
17 changes: 9 additions & 8 deletions engine/access/access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ type Suite struct {
metrics *metrics.NoopCollector
finalizedHeaderCache module.FinalizedHeaderCache
backend *backend.Backend
sporkID flow.Identifier
protocolVersion uint
}

// TestAccess tests scenarios which exercise multiple API calls using both the RPC handler and the ingest engine
Expand All @@ -84,6 +86,8 @@ func (suite *Suite) SetupTest() {
suite.state = new(protocol.State)
suite.finalSnapshot = new(protocol.Snapshot)
suite.sealedSnapshot = new(protocol.Snapshot)
suite.sporkID = unittest.IdentifierFixture()
suite.protocolVersion = uint(unittest.Uint64InRange(10, 30))

suite.rootBlock = unittest.BlockHeaderFixture(unittest.WithHeaderHeight(0))
suite.sealedBlock = suite.rootBlock
Expand All @@ -108,7 +112,10 @@ func (suite *Suite) SetupTest() {

suite.params = new(protocol.Params)
suite.params.On("FinalizedRoot").Return(suite.rootBlock, nil)
suite.params.On("SporkID").Return(suite.sporkID, nil)
suite.params.On("ProtocolVersion").Return(suite.protocolVersion, nil)
suite.params.On("SporkRootBlockHeight").Return(suite.rootBlock.Height, nil)
suite.params.On("SealedRoot").Return(suite.rootBlock, nil)
suite.state.On("Params").Return(suite.params).Maybe()
suite.collClient = new(accessmock.AccessAPIClient)
suite.execClient = new(accessmock.ExecutionAPIClient)
Expand Down Expand Up @@ -1110,12 +1117,6 @@ func (suite *Suite) TestExecuteScript() {
// information
func (suite *Suite) TestAPICallNodeVersionInfo() {
suite.RunTest(func(handler *access.Handler, db *badger.DB, all *storage.All) {
sporkId := unittest.IdentifierFixture()
protocolVersion := uint(unittest.Uint64InRange(10, 30))

suite.params.On("SporkID").Return(sporkId, nil)
suite.params.On("ProtocolVersion").Return(protocolVersion, nil)

req := &accessproto.GetNodeVersionInfoRequest{}
resp, err := handler.GetNodeVersionInfo(context.Background(), req)
require.NoError(suite.T(), err)
Expand All @@ -1125,8 +1126,8 @@ func (suite *Suite) TestAPICallNodeVersionInfo() {
suite.Require().Equal(respNodeVersionInfo, &entitiesproto.NodeVersionInfo{
Semver: build.Version(),
Commit: build.Commit(),
SporkId: sporkId[:],
ProtocolVersion: uint64(protocolVersion),
SporkId: suite.sporkID[:],
ProtocolVersion: uint64(suite.protocolVersion),
})
})
}
Expand Down
7 changes: 7 additions & 0 deletions engine/access/integration_unsecure_grpc_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ func (suite *SameGRPCPortTestSuite) SetupTest() {
suite.net = new(network.EngineRegistry)
suite.state = new(protocol.State)
suite.snapshot = new(protocol.Snapshot)
params := new(protocol.Params)

suite.epochQuery = new(protocol.EpochQuery)
suite.state.On("Sealed").Return(suite.snapshot, nil).Maybe()
suite.state.On("Final").Return(suite.snapshot, nil).Maybe()
suite.state.On("Params").Return(params)
suite.snapshot.On("Epochs").Return(suite.epochQuery).Maybe()
suite.blocks = new(storagemock.Blocks)
suite.headers = new(storagemock.Headers)
Expand Down Expand Up @@ -141,6 +143,11 @@ func (suite *SameGRPCPortTestSuite) SetupTest() {
suite.blockMap[block.Header.Height] = block
}

params.On("SporkID").Return(unittest.IdentifierFixture(), nil)
params.On("ProtocolVersion").Return(uint(unittest.Uint64InRange(10, 30)), nil)
params.On("SporkRootBlockHeight").Return(rootBlock.Header.Height, nil)
params.On("SealedRoot").Return(rootBlock.Header, nil)

// generate a server certificate that will be served by the GRPC server
networkingKey := unittest.NetworkingPrivKeyFixture()
x509Certificate, err := grpcutils.X509Certificate(networkingKey)
Expand Down
10 changes: 6 additions & 4 deletions engine/access/rest/models/model_node_version_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
package models

type NodeVersionInfo struct {
Semver string `json:"semver"`
Commit string `json:"commit"`
SporkId string `json:"spork_id"`
ProtocolVersion string `json:"protocol_version"`
Semver string `json:"semver"`
Commit string `json:"commit"`
SporkId string `json:"spork_id"`
ProtocolVersion string `json:"protocol_version"`
SporkRootBlockHeight string `json:"spork_root_block_height"`
NodeRootBlockHeight string `json:"node_root_block_height"`
}
2 changes: 2 additions & 0 deletions engine/access/rest/models/node_version_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ func (t *NodeVersionInfo) Build(params *access.NodeVersionInfo) {
t.Commit = params.Commit
t.SporkId = params.SporkId.String()
t.ProtocolVersion = util.FromUint64(params.ProtocolVersion)
t.SporkRootBlockHeight = util.FromUint64(params.SporkRootBlockHeight)
t.NodeRootBlockHeight = util.FromUint64(params.NodeRootBlockHeight)
}
23 changes: 17 additions & 6 deletions engine/access/rest/routes/node_version_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ func TestGetNodeVersionInfo(t *testing.T) {
req := getNodeVersionInfoRequest(t)

params := &access.NodeVersionInfo{
Semver: build.Version(),
Commit: build.Commit(),
SporkId: unittest.IdentifierFixture(),
ProtocolVersion: unittest.Uint64InRange(10, 30),
Semver: build.Version(),
Commit: build.Commit(),
SporkId: unittest.IdentifierFixture(),
ProtocolVersion: unittest.Uint64InRange(10, 30),
SporkRootBlockHeight: unittest.Uint64InRange(1000, 10_000),
NodeRootBlockHeight: unittest.Uint64InRange(10_000, 100_000),
}

backend.Mock.
Expand All @@ -51,8 +53,17 @@ func nodeVersionInfoExpectedStr(nodeVersionInfo *access.NodeVersionInfo) string
"semver": "%s",
"commit": "%s",
"spork_id": "%s",
"protocol_version": "%d"
}`, nodeVersionInfo.Semver, nodeVersionInfo.Commit, nodeVersionInfo.SporkId.String(), nodeVersionInfo.ProtocolVersion)
"protocol_version": "%d",
"spork_root_block_height": "%d",
"node_root_block_height": "%d"
}`,
nodeVersionInfo.Semver,
nodeVersionInfo.Commit,
nodeVersionInfo.SporkId.String(),
nodeVersionInfo.ProtocolVersion,
nodeVersionInfo.SporkRootBlockHeight,
nodeVersionInfo.NodeRootBlockHeight,
)
}

func getNodeVersionInfoRequest(t *testing.T) *http.Request {
Expand Down
18 changes: 14 additions & 4 deletions engine/access/rest_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,16 @@ func (suite *RestAPITestSuite) SetupTest() {
suite.sealedBlock = unittest.BlockHeaderFixture(unittest.WithHeaderHeight(0))
suite.finalizedBlock = unittest.BlockHeaderWithParentFixture(suite.sealedBlock)

rootHeader := unittest.BlockHeaderFixture()
params := new(protocol.Params)
params.On("SporkID").Return(unittest.IdentifierFixture(), nil)
params.On("ProtocolVersion").Return(uint(unittest.Uint64InRange(10, 30)), nil)
params.On("SporkRootBlockHeight").Return(rootHeader.Height, nil)
params.On("SealedRoot").Return(rootHeader, nil)

suite.state.On("Sealed").Return(suite.sealedSnaphost, nil)
suite.state.On("Final").Return(suite.finalizedSnapshot, nil)
suite.state.On("Params").Return(params)
suite.sealedSnaphost.On("Head").Return(
func() *flow.Header {
return suite.sealedBlock
Expand Down Expand Up @@ -202,10 +210,12 @@ func (suite *RestAPITestSuite) SetupTest() {
}

func (suite *RestAPITestSuite) TearDownTest() {
suite.cancel()
unittest.AssertClosesBefore(suite.T(), suite.secureGrpcServer.Done(), 2*time.Second)
unittest.AssertClosesBefore(suite.T(), suite.unsecureGrpcServer.Done(), 2*time.Second)
unittest.AssertClosesBefore(suite.T(), suite.rpcEng.Done(), 2*time.Second)
if suite.cancel != nil {
suite.cancel()
unittest.AssertClosesBefore(suite.T(), suite.secureGrpcServer.Done(), 2*time.Second)
unittest.AssertClosesBefore(suite.T(), suite.unsecureGrpcServer.Done(), 2*time.Second)
unittest.AssertClosesBefore(suite.T(), suite.rpcEng.Done(), 2*time.Second)
}
}

func TestRestAPI(t *testing.T) {
Expand Down
53 changes: 40 additions & 13 deletions engine/access/rpc/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
lru2 "github.com/hashicorp/golang-lru/v2"
accessproto "github.com/onflow/flow/protobuf/go/flow/access"
"github.com/rs/zerolog"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/onflow/flow-go/access"
"github.com/onflow/flow-go/cmd/build"
Expand Down Expand Up @@ -76,6 +74,9 @@ type Backend struct {
collections storage.Collections
executionReceipts storage.ExecutionReceipts
connFactory connection.ConnectionFactory

// cache the response to GetNodeVersionInfo since it doesn't change
nodeInfo *access.NodeVersionInfo
}

// Config defines the configurable options for creating Backend
Expand Down Expand Up @@ -145,6 +146,12 @@ func New(params Params) (*Backend, error) {
}
}

// initialize node version info
nodeInfo, err := getNodeVersionInfo(params.State.Params())
if err != nil {
return nil, fmt.Errorf("failed to initialize node version info: %w", err)
}

b := &Backend{
state: params.State,
// create the sub-backends
Expand Down Expand Up @@ -215,6 +222,7 @@ func New(params Params) (*Backend, error) {
executionReceipts: params.ExecutionReceipts,
connFactory: params.ConnFactory,
chainID: params.ChainID,
nodeInfo: nodeInfo,
}

retry.SetBackend(b)
Expand Down Expand Up @@ -303,24 +311,43 @@ func (b *Backend) Ping(ctx context.Context) error {
}

// GetNodeVersionInfo returns node version information such as semver, commit, sporkID, protocolVersion, etc
func (b *Backend) GetNodeVersionInfo(ctx context.Context) (*access.NodeVersionInfo, error) {
stateParams := b.state.Params()
sporkId, err := stateParams.SporkID()
func (b *Backend) GetNodeVersionInfo(_ context.Context) (*access.NodeVersionInfo, error) {
return b.nodeInfo, nil
}

// getNodeVersionInfo returns the NodeVersionInfo for the node.
// Since these values are static while the node is running, it is safe to cache.
func getNodeVersionInfo(stateParams protocol.Params) (*access.NodeVersionInfo, error) {
sporkID, err := stateParams.SporkID()
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to read spork ID: %v", err)
return nil, fmt.Errorf("failed to read spork ID: %v", err)
}

protocolVersion, err := stateParams.ProtocolVersion()
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to read protocol version: %v", err)
return nil, fmt.Errorf("failed to read protocol version: %v", err)
}

sporkRootBlockHeight, err := stateParams.SporkRootBlockHeight()
if err != nil {
return nil, fmt.Errorf("failed to read spork root block height: %w", err)
}

nodeRootBlockHeader, err := stateParams.SealedRoot()
if err != nil {
return nil, fmt.Errorf("failed to read node root block: %w", err)
}

nodeInfo := &access.NodeVersionInfo{
Semver: build.Version(),
Commit: build.Commit(),
SporkId: sporkID,
ProtocolVersion: uint64(protocolVersion),
SporkRootBlockHeight: sporkRootBlockHeight,
NodeRootBlockHeight: nodeRootBlockHeader.Height,
}

return &access.NodeVersionInfo{
Semver: build.Version(),
Commit: build.Commit(),
SporkId: sporkId,
ProtocolVersion: uint64(protocolVersion),
}, nil
return nodeInfo, nil
}

func (b *Backend) GetCollectionByID(_ context.Context, colID flow.Identifier) (*flow.LightCollection, error) {
Expand Down
Loading
Loading