Skip to content

Commit

Permalink
Grandpa Prove Finality RPC (#1379)
Browse files Browse the repository at this point in the history
  • Loading branch information
RyRy79261 committed Feb 25, 2021
1 parent cae3e8c commit da8a240
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 6 deletions.
2 changes: 1 addition & 1 deletion chain/gssmr/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ nomdns = false
enabled = false
port = 8545
host = "localhost"
modules = ["system", "author", "chain", "state", "rpc"]
modules = ["system", "author", "chain", "state", "rpc", "grandpa"]
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 @@ -79,7 +79,7 @@ var (
// DefaultRPCHTTPPort rpc port
DefaultRPCHTTPPort = uint32(8545)
// DefaultRPCModules rpc modules
DefaultRPCModules = []string{"system", "author", "chain", "state", "rpc"}
DefaultRPCModules = []string{"system", "author", "chain", "state", "rpc", "grandpa"}
// DefaultRPCWSPort rpc websocket port
DefaultRPCWSPort = uint32(8546)
)
2 changes: 1 addition & 1 deletion chain/ksmcc/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ enabled = false
external = false
port = 8545
host = "localhost"
modules = ["system", "author", "chain", "state", "rpc"]
modules = ["system", "author", "chain", "state", "rpc", "grandpa"]
ws-port = 8546
ws = false
ws-external = false
2 changes: 1 addition & 1 deletion chain/ksmcc/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ var (
// DefaultRPCHTTPPort rpc port
DefaultRPCHTTPPort = uint32(8545)
// DefaultRPCModules rpc modules
DefaultRPCModules = []string{"system", "author", "chain", "state", "rpc"}
DefaultRPCModules = []string{"system", "author", "chain", "state", "rpc", "grandpa"}
// 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 @@ -33,5 +33,5 @@ nomdns = false
enabled = false
port = 8545
host = "localhost"
modules = ["system", "author", "chain", "state", "rpc"]
modules = ["system", "author", "chain", "state", "rpc", "grandpa"]
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 @@ -79,7 +79,7 @@ var (
// DefaultRPCHTTPPort rpc port
DefaultRPCHTTPPort = uint32(8545)
// DefaultRPCModules rpc modules
DefaultRPCModules = []string{"system", "author", "chain", "state", "rpc"}
DefaultRPCModules = []string{"system", "author", "chain", "state", "rpc", "grandpa"}
// DefaultRPCWSPort rpc websocket port
DefaultRPCWSPort = uint32(8546)
)
2 changes: 2 additions & 0 deletions dot/rpc/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ func (h *HTTPServer) RegisterModules(mods []string) {
srvc = modules.NewAuthorModule(h.logger, h.serverConfig.CoreAPI, h.serverConfig.RuntimeAPI, h.serverConfig.TransactionQueueAPI)
case "chain":
srvc = modules.NewChainModule(h.serverConfig.BlockAPI)
case "grandpa":
srvc = modules.NewGrandpaModule(h.serverConfig.BlockAPI)
case "state":
srvc = modules.NewStateModule(h.serverConfig.NetworkAPI, h.serverConfig.StorageAPI, h.serverConfig.CoreAPI)
case "rpc":
Expand Down
3 changes: 3 additions & 0 deletions dot/rpc/modules/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ type BlockAPI interface {
GetBlockByHash(hash common.Hash) (*types.Block, error)
GetBlockHash(blockNumber *big.Int) (*common.Hash, error)
GetFinalizedHash(uint64, uint64) (common.Hash, error)
HasJustification(hash common.Hash) (bool, error)
GetJustification(hash common.Hash) ([]byte, error)
RegisterImportedChannel(ch chan<- *types.Block) (byte, error)
UnregisterImportedChannel(id byte)
RegisterFinalizedChannel(ch chan<- *types.Header) (byte, error)
UnregisterFinalizedChannel(id byte)
SubChain(start, end common.Hash) ([]common.Hash, error)
}

// NetworkAPI interface for network state methods
Expand Down
1 change: 1 addition & 0 deletions dot/rpc/modules/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//
// 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 (
Expand Down
1 change: 1 addition & 0 deletions dot/rpc/modules/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//
// 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 "errors"
Expand Down
73 changes: 73 additions & 0 deletions dot/rpc/modules/grandpa.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2020 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 (
"net/http"

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

// GrandpaModule init parameters
type GrandpaModule struct {
blockAPI BlockAPI
}

// NewGrandpaModule creates a new Grandpa rpc module.
func NewGrandpaModule(api BlockAPI) *GrandpaModule {
return &GrandpaModule{
blockAPI: api,
}
}

// ProveFinalityRequest request struct
type ProveFinalityRequest struct {
blockHashStart common.Hash
blockHashEnd common.Hash
authorityID uint64
}

// ProveFinalityResponse is an optional SCALE encoded proof array
type ProveFinalityResponse [][]byte

// ProveFinality for the provided block range. Returns NULL if there are no known finalized blocks in the range. If no authorities set is provided, the current one will be attempted.
func (gm *GrandpaModule) ProveFinality(r *http.Request, req *ProveFinalityRequest, res *ProveFinalityResponse) error {
blocksToCheck, err := gm.blockAPI.SubChain(req.blockHashStart, req.blockHashEnd)
if err != nil {
return err
}

// Leaving check in for linter
if req.authorityID != uint64(0) {
// TODO: #1404 Check if functionality relevant
}

for _, block := range blocksToCheck {
hasJustification, _ := gm.blockAPI.HasJustification(block)
if !hasJustification {
continue
}

justification, err := gm.blockAPI.GetJustification(block)
if err != nil {
continue
}
*res = append(*res, justification)
}

return nil
}
57 changes: 57 additions & 0 deletions dot/rpc/modules/grandpa_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2020 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 (
"reflect"
"testing"

"github.com/ChainSafe/gossamer/dot/state"
)

func TestGrandpaProveFinality(t *testing.T) {
testStateService := newTestStateService(t)

state.AddBlocksToState(t, testStateService.Block, 3)
bestBlock, err := testStateService.Block.BestBlock()

if err != nil {
t.Errorf("Fail: bestblock failed")
}

gmSvc := NewGrandpaModule(testStateService.Block)

testStateService.Block.SetJustification(bestBlock.Header.ParentHash, make([]byte, 10))
testStateService.Block.SetJustification(bestBlock.Header.Hash(), make([]byte, 11))

var expectedResponse ProveFinalityResponse
expectedResponse = append(expectedResponse, make([]byte, 10), make([]byte, 11))

res := new(ProveFinalityResponse)
err = gmSvc.ProveFinality(nil, &ProveFinalityRequest{
blockHashStart: bestBlock.Header.ParentHash,
blockHashEnd: bestBlock.Header.Hash(),
}, res)

if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(*res, expectedResponse) {
t.Errorf("Fail: expected: %+v got: %+v\n", res, &expectedResponse)
}
}
1 change: 1 addition & 0 deletions dot/rpc/modules/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//
// 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 "net/http"
Expand Down
12 changes: 12 additions & 0 deletions dot/rpc/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ func (m *MockBlockAPI) RegisterFinalizedChannel(ch chan<- *types.Header) (byte,
}
func (m *MockBlockAPI) UnregisterFinalizedChannel(id byte) {}

func (m *MockBlockAPI) GetJustification(hash common.Hash) ([]byte, error) {
return make([]byte, 10), nil
}

func (m *MockBlockAPI) HasJustification(hash common.Hash) (bool, error) {
return true, nil
}

func (m *MockBlockAPI) SubChain(start, end common.Hash) ([]common.Hash, error) {
return make([]common.Hash, 0), nil
}

type MockStorageAPI struct{}

func (m *MockStorageAPI) GetStorage(_ *common.Hash, key []byte) ([]byte, error) {
Expand Down
3 changes: 3 additions & 0 deletions tests/utils/rpc_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ var (

// DEV METHODS
DevControl = "dev_control"

// GRANDPA
GrandpaProveFinality = "grandpa_proveFinality"
)

0 comments on commit da8a240

Please sign in to comment.