Skip to content

fix(dot/sync, dot/rpc): implement HighestBlock #2195

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

Merged
merged 17 commits into from
Jan 28, 2022
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
13 changes: 12 additions & 1 deletion dot/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,18 @@ func NewNode(cfg *Config, ks *keystore.GlobalKeystore) (*Node, error) {
// check if rpc service is enabled
if enabled := cfg.RPC.isRPCEnabled() || cfg.RPC.isWSEnabled(); enabled {
var rpcSrvc *rpc.HTTPServer
rpcSrvc, err = createRPCService(cfg, ns, stateSrvc, coreSrvc, networkSrvc, bp, sysSrvc, fg)
cRPCParams := rpcServiceSettings{
config: cfg,
nodeStorage: ns,
state: stateSrvc,
core: coreSrvc,
network: networkSrvc,
blockProducer: bp,
system: sysSrvc,
blockFinality: fg,
syncer: syncer,
}
rpcSrvc, err = createRPCService(cRPCParams)
if err != nil {
return nil, fmt.Errorf("failed to create rpc service: %s", err)
}
Expand Down
4 changes: 3 additions & 1 deletion dot/rpc/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type HTTPServerConfig struct {
RPCAPI modules.RPCAPI
SystemAPI modules.SystemAPI
SyncStateAPI modules.SyncStateAPI
SyncAPI modules.SyncAPI
NodeStorage *runtime.NodeStorage
RPC bool
RPCExternal bool
Expand Down Expand Up @@ -97,7 +98,8 @@ func (h *HTTPServer) RegisterModules(mods []string) {
switch mod {
case "system":
srvc = modules.NewSystemModule(h.serverConfig.NetworkAPI, h.serverConfig.SystemAPI,
h.serverConfig.CoreAPI, h.serverConfig.StorageAPI, h.serverConfig.TransactionQueueAPI, h.serverConfig.BlockAPI)
h.serverConfig.CoreAPI, h.serverConfig.StorageAPI, h.serverConfig.TransactionQueueAPI,
h.serverConfig.BlockAPI, h.serverConfig.SyncAPI)
case "author":
srvc = modules.NewAuthorModule(h.logger, h.serverConfig.CoreAPI, h.serverConfig.TransactionQueueAPI)
case "chain":
Expand Down
8 changes: 7 additions & 1 deletion dot/rpc/modules/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ type NetworkAPI interface {
Stop() error
Start() error
IsStopped() bool
HighestBlock() int64
StartingBlock() int64
AddReservedPeers(addrs ...string) error
RemoveReservedPeers(addrs ...string) error
Expand Down Expand Up @@ -153,3 +152,10 @@ type RuntimeStorageAPI interface {
type SyncStateAPI interface {
GenSyncSpec(raw bool) (*genesis.Genesis, error)
}

//go:generate mockgen -destination=mock_sync_api_test.go -package $GOPACKAGE . SyncAPI

// SyncAPI is the interface to interact with the sync service
type SyncAPI interface {
HighestBlock() int64
}
48 changes: 48 additions & 0 deletions dot/rpc/modules/mock_sync_api_test.go

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

14 changes: 0 additions & 14 deletions dot/rpc/modules/mocks/network_api.go

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

7 changes: 5 additions & 2 deletions dot/rpc/modules/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type SystemModule struct {
storageAPI StorageAPI
txStateAPI TransactionStateAPI
blockAPI BlockAPI
syncAPI SyncAPI
}

// EmptyRequest represents an RPC request with no fields
Expand Down Expand Up @@ -67,14 +68,16 @@ type SyncStateResponse struct {

// NewSystemModule creates a new API instance
func NewSystemModule(net NetworkAPI, sys SystemAPI, core CoreAPI,
storage StorageAPI, txAPI TransactionStateAPI, blockAPI BlockAPI) *SystemModule {
storage StorageAPI, txAPI TransactionStateAPI, blockAPI BlockAPI,
syncAPI SyncAPI) *SystemModule {
return &SystemModule{
networkAPI: net,
systemAPI: sys,
coreAPI: core,
storageAPI: storage,
txStateAPI: txAPI,
blockAPI: blockAPI,
syncAPI: syncAPI,
}
}

Expand Down Expand Up @@ -233,7 +236,7 @@ func (sm *SystemModule) SyncState(r *http.Request, req *EmptyRequest, res *SyncS

*res = SyncStateResponse{
CurrentBlock: uint32(h.Number.Int64()),
HighestBlock: uint32(sm.networkAPI.HighestBlock()),
HighestBlock: uint32(sm.syncAPI.HighestBlock()),
StartingBlock: uint32(sm.networkAPI.StartingBlock()),
}
return nil
Expand Down
26 changes: 15 additions & 11 deletions dot/rpc/modules/system_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestSystemModule_Health(t *testing.T) {
networkMock := new(mocks.NetworkAPI)
networkMock.On("Health").Return(testHealth)

sys := NewSystemModule(networkMock, nil, nil, nil, nil, nil)
sys := NewSystemModule(networkMock, nil, nil, nil, nil, nil, nil)

res := &SystemHealthResponse{}
err := sys.Health(nil, nil, res)
Expand All @@ -112,7 +112,7 @@ func TestSystemModule_Health(t *testing.T) {
// Test RPC's System.NetworkState() response
func TestSystemModule_NetworkState(t *testing.T) {
net := newNetworkService(t)
sys := NewSystemModule(net, nil, nil, nil, nil, nil)
sys := NewSystemModule(net, nil, nil, nil, nil, nil, nil)

res := &SystemNetworkStateResponse{}
err := sys.NetworkState(nil, nil, res)
Expand All @@ -129,7 +129,7 @@ func TestSystemModule_NetworkState(t *testing.T) {
func TestSystemModule_Peers(t *testing.T) {
net := newNetworkService(t)
net.Stop()
sys := NewSystemModule(net, nil, nil, nil, nil, nil)
sys := NewSystemModule(net, nil, nil, nil, nil, nil, nil)

res := &SystemPeersResponse{}
err := sys.Peers(nil, nil, res)
Expand All @@ -142,7 +142,7 @@ func TestSystemModule_Peers(t *testing.T) {

func TestSystemModule_NodeRoles(t *testing.T) {
net := newNetworkService(t)
sys := NewSystemModule(net, nil, nil, nil, nil, nil)
sys := NewSystemModule(net, nil, nil, nil, nil, nil, nil)
expected := []interface{}{"Full"}

var res []interface{}
Expand Down Expand Up @@ -174,7 +174,7 @@ func newMockSystemAPI() *mocks.SystemAPI {
}

func TestSystemModule_Chain(t *testing.T) {
sys := NewSystemModule(nil, newMockSystemAPI(), nil, nil, nil, nil)
sys := NewSystemModule(nil, newMockSystemAPI(), nil, nil, nil, nil, nil)

res := new(string)
err := sys.Chain(nil, nil, res)
Expand All @@ -185,14 +185,14 @@ func TestSystemModule_Chain(t *testing.T) {
func TestSystemModule_ChainType(t *testing.T) {
api := newMockSystemAPI()

sys := NewSystemModule(nil, api, nil, nil, nil, nil)
sys := NewSystemModule(nil, api, nil, nil, nil, nil, nil)

res := new(string)
sys.ChainType(nil, nil, res)
require.Equal(t, testGenesisData.ChainType, *res)
}
func TestSystemModule_Name(t *testing.T) {
sys := NewSystemModule(nil, newMockSystemAPI(), nil, nil, nil, nil)
sys := NewSystemModule(nil, newMockSystemAPI(), nil, nil, nil, nil, nil)

res := new(string)
err := sys.Name(nil, nil, res)
Expand All @@ -201,7 +201,7 @@ func TestSystemModule_Name(t *testing.T) {
}

func TestSystemModule_Version(t *testing.T) {
sys := NewSystemModule(nil, newMockSystemAPI(), nil, nil, nil, nil)
sys := NewSystemModule(nil, newMockSystemAPI(), nil, nil, nil, nil, nil)

res := new(string)
err := sys.Version(nil, nil, res)
Expand All @@ -210,7 +210,7 @@ func TestSystemModule_Version(t *testing.T) {
}

func TestSystemModule_Properties(t *testing.T) {
sys := NewSystemModule(nil, newMockSystemAPI(), nil, nil, nil, nil)
sys := NewSystemModule(nil, newMockSystemAPI(), nil, nil, nil, nil, nil)

expected := map[string]interface{}(nil)

Expand Down Expand Up @@ -340,7 +340,7 @@ func setupSystemModule(t *testing.T) *SystemModule {
AnyTimes()

txQueue := state.NewTransactionState(telemetryMock)
return NewSystemModule(net, nil, core, chain.Storage, txQueue, nil)
return NewSystemModule(net, nil, core, chain.Storage, txQueue, nil, nil)
}

func newCoreService(t *testing.T, srvc *state.Service) *core.Service {
Expand Down Expand Up @@ -399,12 +399,16 @@ func TestSyncState(t *testing.T) {
blockapiMock.On("GetHeader", fakeCommonHash).Return(fakeHeader, nil).Once()

netapiMock := new(mocks.NetworkAPI)
netapiMock.On("HighestBlock").Return(int64(90))
netapiMock.On("StartingBlock").Return(int64(10))

syncapiCtrl := gomock.NewController(t)
syncapiMock := NewMockSyncAPI(syncapiCtrl)
syncapiMock.EXPECT().HighestBlock().Return(int64(90))

sysmodule := new(SystemModule)
sysmodule.blockAPI = blockapiMock
sysmodule.networkAPI = netapiMock
sysmodule.syncAPI = syncapiMock

var res SyncStateResponse
err := sysmodule.SyncState(nil, nil, &res)
Expand Down
Loading