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

feat(dot/rpc): implement sync_state_genSyncSpec RPC call #1827

Merged
merged 14 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion chain/dev/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ enabled = true
ws = true
port = 8545
host = "localhost"
modules = ["system", "author", "chain", "state", "rpc", "grandpa", "offchain", "childstate"]
modules = ["system", "author", "chain", "state", "rpc", "grandpa", "offchain", "childstate", "syncstate"]
ws-port = 8546
2 changes: 1 addition & 1 deletion chain/dev/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ var (
// DefaultRPCHTTPPort rpc port
DefaultRPCHTTPPort = uint32(8545)
// DefaultRPCModules rpc modules
DefaultRPCModules = []string{"system", "author", "chain", "state", "rpc", "grandpa", "offchain", "childstate"}
DefaultRPCModules = []string{"system", "author", "chain", "state", "rpc", "grandpa", "offchain", "childstate", "syncstate"}
// DefaultRPCWSPort rpc websocket port
DefaultRPCWSPort = uint32(8546)
// DefaultRPCEnabled enables the RPC server
Expand Down
2 changes: 1 addition & 1 deletion chain/gssmr/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ discovery-interval = 10
enabled = false
port = 8545
host = "localhost"
modules = ["system", "author", "chain", "state", "rpc", "grandpa", "offchain", "childstate"]
modules = ["system", "author", "chain", "state", "rpc", "grandpa", "offchain", "childstate", "syncstate"]
ws-port = 8546
2 changes: 1 addition & 1 deletion chain/gssmr/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ var (
// DefaultRPCHTTPPort rpc port
DefaultRPCHTTPPort = uint32(8545)
// DefaultRPCModules rpc modules
DefaultRPCModules = []string{"system", "author", "chain", "state", "rpc", "grandpa", "offchain", "childstate"}
DefaultRPCModules = []string{"system", "author", "chain", "state", "rpc", "grandpa", "offchain", "childstate", "syncstate"}
// DefaultRPCWSPort rpc websocket port
DefaultRPCWSPort = uint32(8546)
)
2 changes: 1 addition & 1 deletion chain/kusama/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ enabled = false
external = false
port = 8545
host = "localhost"
modules = ["system", "author", "chain", "state", "rpc", "grandpa", "offchain", "childstate"]
modules = ["system", "author", "chain", "state", "rpc", "grandpa", "offchain", "childstate", "syncstate"]
ws-port = 8546
ws = false
ws-external = false
2 changes: 1 addition & 1 deletion chain/kusama/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ var (
// DefaultRPCHTTPPort rpc port
DefaultRPCHTTPPort = uint32(8545)
// DefaultRPCModules rpc modules
DefaultRPCModules = []string{"system", "author", "chain", "state", "rpc", "grandpa", "offchain", "childstate"}
DefaultRPCModules = []string{"system", "author", "chain", "state", "rpc", "grandpa", "offchain", "childstate", "syncstate"}
// DefaultRPCWSPort rpc websocket port
DefaultRPCWSPort = uint32(8546)
)
2 changes: 1 addition & 1 deletion chain/polkadot/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ nomdns = false
enabled = false
port = 8545
host = "localhost"
modules = ["system", "author", "chain", "state", "rpc", "grandpa", "offchain", "childstate"]
modules = ["system", "author", "chain", "state", "rpc", "grandpa", "offchain", "childstate", "syncstate"]
ws-port = 8546
2 changes: 1 addition & 1 deletion chain/polkadot/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ var (
// DefaultRPCHTTPPort rpc port
DefaultRPCHTTPPort = uint32(8545)
// DefaultRPCModules rpc modules
DefaultRPCModules = []string{"system", "author", "chain", "state", "rpc", "grandpa", "offchain", "childstate"}
DefaultRPCModules = []string{"system", "author", "chain", "state", "rpc", "grandpa", "offchain", "childstate", "syncstate"}
// DefaultRPCWSPort rpc websocket port
DefaultRPCWSPort = uint32(8546)
)
4 changes: 3 additions & 1 deletion dot/rpc/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type HTTPServerConfig struct {
TransactionQueueAPI modules.TransactionStateAPI
RPCAPI modules.RPCAPI
SystemAPI modules.SystemAPI
SyncStateAPI modules.SyncStateAPI
NodeStorage *runtime.NodeStorage
RPC bool
RPCExternal bool
Expand Down Expand Up @@ -129,13 +130,14 @@ func (h *HTTPServer) RegisterModules(mods []string) {
srvc = modules.NewOffchainModule(h.serverConfig.NodeStorage)
case "childstate":
srvc = modules.NewChildStateModule(h.serverConfig.StorageAPI, h.serverConfig.BlockAPI)
case "syncstate":
srvc = modules.NewSyncStateModule(h.serverConfig.SyncStateAPI)
default:
h.logger.Warn("Unrecognised module", "module", mod)
continue
}

err := h.rpcServer.RegisterService(srvc, mod)

if err != nil {
h.logger.Warn("Failed to register module", "mod", mod, "err", err)
}
Expand Down
4 changes: 2 additions & 2 deletions dot/rpc/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
func TestRegisterModules(t *testing.T) {
rpcapiMocks := new(mocks.MockRPCAPI)

mods := []string{"system", "author", "chain", "state", "rpc", "grandpa", "offchain", "childstate"}
mods := []string{"system", "author", "chain", "state", "rpc", "grandpa", "offchain", "childstate", "syncstate"}

for _, modName := range mods {
rpcapiMocks.On("BuildMethodNames", mock.Anything, modName).Once()
Expand Down Expand Up @@ -125,7 +125,7 @@ func TestNewHTTPServer(t *testing.T) {

func TestUnsafeRPCProtection(t *testing.T) {
cfg := &HTTPServerConfig{
Modules: []string{"system", "author", "chain", "state", "rpc", "grandpa", "dev"},
Modules: []string{"system", "author", "chain", "state", "rpc", "grandpa", "dev", "syncstate"},
RPCPort: 7878,
RPCAPI: NewService(),
RPCUnsafe: false,
Expand Down
5 changes: 5 additions & 0 deletions dot/rpc/modules/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto"
"github.com/ChainSafe/gossamer/lib/crypto/ed25519"
"github.com/ChainSafe/gossamer/lib/genesis"
"github.com/ChainSafe/gossamer/lib/grandpa"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/transaction"
Expand Down Expand Up @@ -121,3 +122,7 @@ type RuntimeStorageAPI interface {
GetLocal(k []byte) ([]byte, error)
GetPersistent(k []byte) ([]byte, error)
}

type SyncStateAPI interface {
GenSyncSpec(raw bool) (*genesis.Genesis, error)
}
80 changes: 80 additions & 0 deletions dot/rpc/modules/sync_state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2019 ChainSafe Systems (ON) Corp.
// This file is part of gossamer.
//
// The gossamer library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The gossamer library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the gossamer library. If not, see <http://www.gnu.org/licenses/>.

package modules

import (
"encoding/json"
"io/ioutil"
"net/http"
"path/filepath"

"github.com/ChainSafe/gossamer/lib/genesis"
)

type SyncStateModule struct {
SyncStateAPI SyncStateAPI
}

type BoolRequest struct {
Raw bool
}

func NewSyncStateModule(s SyncStateAPI) *SyncStateModule {
return &SyncStateModule{SyncStateAPI: s}
}

func (ss *SyncStateModule) GenSyncSpec(_ *http.Request, req *BoolRequest, res *genesis.Genesis) error {
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved
genesis, err := ss.SyncStateAPI.GenSyncSpec(req.Raw)
if err != nil {
return err
}

*res = *genesis
return nil
}

type SyncState struct {
GenesisFilePath string
}

// GenSyncSpec returns the JSON serialized chain specification running the node
// (i.e. the current state), with a sync state.
func (s SyncState) GenSyncSpec(raw bool) (*genesis.Genesis, error) {
fp, err := filepath.Abs(s.GenesisFilePath)
if err != nil {
return nil, err
}
data, err := ioutil.ReadFile(filepath.Clean(fp))
if err != nil {
return nil, err
}

g := new(genesis.Genesis)
err = json.Unmarshal(data, g)
if err != nil {
return nil, err
}

if raw {
err = g.ToRaw()
if err != nil {
return nil, err
}
}

return g, nil
}
22 changes: 22 additions & 0 deletions dot/rpc/modules/sync_state_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package modules

import (
"testing"

"github.com/ChainSafe/gossamer/lib/genesis"
"github.com/stretchr/testify/require"
)

const GssmrGenesisPath = "../../../chain/gssmr/genesis.json"

func TestSyncStateModule(t *testing.T) {
module := NewSyncStateModule(SyncState{GenesisFilePath: GssmrGenesisPath})

req := BoolRequest{
Raw: true,
}
var res genesis.Genesis

err := module.GenSyncSpec(nil, &req, &res)
require.NoError(t, err)
}
2 changes: 1 addition & 1 deletion dot/rpc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var (
)

// BuildMethodNames takes receiver interface and populates rpcMethods array with available
// method names
// method names
func (s *Service) BuildMethodNames(rcvr interface{}, name string) {
rcvrType := reflect.TypeOf(rcvr)
for i := 0; i < rcvrType.NumMethod(); i++ {
Expand Down
1 change: 1 addition & 0 deletions dot/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ func createRPCService(cfg *Config, ns *runtime.NodeStorage, stateSrvc *state.Ser
BlockFinalityAPI: finSrvc,
TransactionQueueAPI: stateSrvc.Transaction,
RPCAPI: rpcService,
SyncStateAPI: modules.SyncState{GenesisFilePath: cfg.Init.Genesis},
SystemAPI: sysSrvc,
RPC: cfg.RPC.Enabled,
RPCExternal: cfg.RPC.External,
Expand Down
2 changes: 1 addition & 1 deletion tests/data/db/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ nomdns = false
enabled = false
port = 8545
host = "localhost"
modules = ["system", "author", "chain", "state", "rpc", "grandpa"]
modules = ["system", "author", "chain", "state", "rpc", "grandpa", "syncstate"]
ws-port = 8546