Skip to content

Commit

Permalink
dot/rpc: Implement RPC author_insertKey (#765)
Browse files Browse the repository at this point in the history
* add InsertKey interface

* implement author_insertKey

* lint issues

* added tests for author_insertKey

* remove unnecessary logging

* fix imports merge conflict

* move newTestSerice to test_helper.go and make public
  • Loading branch information
edwardmack committed Apr 10, 2020
1 parent e3bdbdc commit 3f02bed
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 83 deletions.
8 changes: 4 additions & 4 deletions dot/core/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestService_ProcessBlockRequestMessage(t *testing.T) {
MsgSend: msgSend,
}

s := newTestService(t, cfg)
s := NewTestService(t, cfg)

addTestBlocksToState(t, 2, s.blockState)

Expand Down Expand Up @@ -267,7 +267,7 @@ func TestService_ProcessBlockResponseMessage(t *testing.T) {
MsgSend: msgSend,
}

s := newTestService(t, cfg)
s := NewTestService(t, cfg)

hash := common.NewHash([]byte{0})
body := optional.CoreBody{0xa, 0xb, 0xc, 0xd}
Expand Down Expand Up @@ -341,7 +341,7 @@ func TestService_ProcessBlockAnnounceMessage(t *testing.T) {
IsBabeAuthority: false,
}

s := newTestService(t, cfg)
s := NewTestService(t, cfg)
err := s.Start()
require.Nil(t, err)

Expand Down Expand Up @@ -396,7 +396,7 @@ func TestService_ProcessTransactionMessage(t *testing.T) {
IsBabeAuthority: true,
}

s := newTestService(t, cfg)
s := NewTestService(t, cfg)

// https://github.com/paritytech/substrate/blob/5420de3face1349a97eb954ae71c5b0b940c31de/core/transaction-pool/src/tests.rs#L95
ext := []byte{1, 212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159, 214, 130, 44, 133, 88, 133, 76, 205, 227, 154, 86, 132, 231, 165, 109, 162, 125, 142, 175, 4, 21, 22, 135, 115, 99, 38, 201, 254, 161, 126, 37, 252, 82, 135, 97, 54, 147, 201, 18, 144, 156, 178, 38, 170, 71, 148, 242, 106, 72, 69, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 216, 5, 113, 87, 87, 40, 221, 120, 247, 252, 137, 201, 74, 231, 222, 101, 85, 108, 102, 39, 31, 190, 210, 14, 215, 124, 19, 160, 180, 203, 54, 110, 167, 163, 149, 45, 12, 108, 80, 221, 65, 238, 57, 237, 199, 16, 10, 33, 185, 8, 244, 184, 243, 139, 5, 87, 252, 245, 24, 225, 37, 154, 163, 142}
Expand Down
4 changes: 2 additions & 2 deletions dot/core/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestRetrieveAuthorityData(t *testing.T) {
}

func TestValidateTransaction(t *testing.T) {
s := newTestService(t, nil)
s := NewTestService(t, nil)

// https://github.com/paritytech/substrate/blob/5420de3face1349a97eb954ae71c5b0b940c31de/core/transaction-pool/src/tests.rs#L95
tx := []byte{1, 212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159, 214, 130, 44, 133, 88, 133, 76, 205, 227, 154, 86, 132, 231, 165, 109, 162, 125, 142, 175, 4, 21, 22, 135, 115, 99, 38, 201, 254, 161, 126, 37, 252, 82, 135, 97, 54, 147, 201, 18, 144, 156, 178, 38, 170, 71, 148, 242, 106, 72, 69, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 216, 5, 113, 87, 87, 40, 221, 120, 247, 252, 137, 201, 74, 231, 222, 101, 85, 108, 102, 39, 31, 190, 210, 14, 215, 124, 19, 160, 180, 203, 54, 110, 167, 163, 149, 45, 12, 108, 80, 221, 65, 238, 57, 237, 199, 16, 10, 33, 185, 8, 244, 184, 243, 139, 5, 87, 252, 245, 24, 225, 37, 154, 163, 142}
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestCheckForRuntimeChanges(t *testing.T) {
IsBabeAuthority: false,
}

s := newTestService(t, cfg)
s := NewTestService(t, cfg)

_, err = runtime.GetRuntimeBlob(runtime.TESTS_FP, runtime.TEST_WASM_URL)
require.Nil(t, err)
Expand Down
6 changes: 6 additions & 0 deletions dot/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/ChainSafe/gossamer/lib/babe"
"github.com/ChainSafe/gossamer/lib/blocktree"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto"
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
"github.com/ChainSafe/gossamer/lib/database"
"github.com/ChainSafe/gossamer/lib/keystore"
Expand Down Expand Up @@ -500,3 +501,8 @@ func (s *Service) checkForRuntimeChanges() error {

return nil
}

// InsertKey inserts keypair into keystore
func (s *Service) InsertKey(kp crypto.Keypair) {
s.keys.Insert(kp)
}
71 changes: 4 additions & 67 deletions dot/core/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ import (
"time"

"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/babe"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
"github.com/ChainSafe/gossamer/lib/genesis"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/trie"
Expand All @@ -39,67 +37,6 @@ import (
// testMessageTimeout is the wait time for messages to be exchanged
var testMessageTimeout = time.Second

// testGenesisHeader is a test block header
var testGenesisHeader = &types.Header{
Number: big.NewInt(0),
StateRoot: trie.EmptyHash,
}

// newTestService creates a new test core service
func newTestService(t *testing.T, cfg *Config) *Service {
if cfg == nil {
rt := runtime.NewTestRuntime(t, runtime.POLKADOT_RUNTIME_c768a7e4c70e)
cfg = &Config{
Runtime: rt,
IsBabeAuthority: false,
}
}

if cfg.Keystore == nil {
cfg.Keystore = keystore.NewKeystore()
}

if cfg.NewBlocks == nil {
cfg.NewBlocks = make(chan types.Block)
}

if cfg.MsgRec == nil {
cfg.MsgRec = make(chan network.Message, 10)
}

if cfg.MsgSend == nil {
cfg.MsgSend = make(chan network.Message, 10)
}

if cfg.SyncChan == nil {
cfg.SyncChan = make(chan *big.Int, 10)
}

stateSrvc := state.NewService("")
stateSrvc.UseMemDB()

genesisData := new(genesis.Data)

err := stateSrvc.Initialize(genesisData, testGenesisHeader, trie.NewEmptyTrie())
require.Nil(t, err)

err = stateSrvc.Start()
require.Nil(t, err)

if cfg.BlockState == nil {
cfg.BlockState = stateSrvc.Block
}

if cfg.StorageState == nil {
cfg.StorageState = stateSrvc.Storage
}

s, err := NewService(cfg)
require.Nil(t, err)

return s
}

// newTestServiceWithFirstBlock creates a new test service with a test block
func newTestServiceWithFirstBlock(t *testing.T) *Service {
tt := trie.NewEmptyTrie()
Expand All @@ -120,7 +57,7 @@ func newTestServiceWithFirstBlock(t *testing.T) *Service {
IsBabeAuthority: true,
}

s := newTestService(t, cfg)
s := NewTestService(t, cfg)

preDigest, err := common.HexToBytes("0x014241424538e93dcef2efc275b72b4fa748332dc4c9f13be1125909cf90c8e9109c45da16b04bc5fdf9fe06a4f35e4ae4ed7e251ff9ee3d0d840c8237c9fb9057442dbf00f210d697a7b4959f792a81b948ff88937e30bf9709a8ab1314f71284da89a40000000000000000001100000000000000")
require.Nil(t, err)
Expand Down Expand Up @@ -177,7 +114,7 @@ func addTestBlocksToState(t *testing.T, depth int, blockState BlockState) {
}

func TestStartService(t *testing.T) {
s := newTestService(t, nil)
s := NewTestService(t, nil)

// TODO: improve dot tests #687
require.NotNil(t, s)
Expand All @@ -194,7 +131,7 @@ func TestNotAuthority(t *testing.T) {
IsBabeAuthority: false,
}

s := newTestService(t, cfg)
s := NewTestService(t, cfg)
if s.bs != nil {
t.Fatal("Fail: should not have babe session")
}
Expand All @@ -209,7 +146,7 @@ func TestAnnounceBlock(t *testing.T) {
MsgSend: msgSend,
}

s := newTestService(t, cfg)
s := NewTestService(t, cfg)
err := s.Start()
require.Nil(t, err)
defer s.Stop()
Expand Down
4 changes: 2 additions & 2 deletions dot/core/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func TestWatchForResponses(t *testing.T) {

syncer.highestSeenBlock = big.NewInt(16)

coreSrv := newTestService(t, nil)
coreSrv := NewTestService(t, nil)
addTestBlocksToState(t, 16, coreSrv.blockState)

startNum := 1
Expand Down Expand Up @@ -354,7 +354,7 @@ func TestWatchForResponses_MissingBlocks(t *testing.T) {

syncer.highestSeenBlock = big.NewInt(16)

coreSrv := newTestService(t, nil)
coreSrv := NewTestService(t, nil)
addTestBlocksToState(t, 16, coreSrv.blockState)

startNum := 16
Expand Down
92 changes: 92 additions & 0 deletions dot/core/test_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// 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 core

import (
"math/big"
"testing"

"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/genesis"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/stretchr/testify/require"
)

// testGenesisHeader is a test block header
var testGenesisHeader = &types.Header{
Number: big.NewInt(0),
StateRoot: trie.EmptyHash,
}

// NewTestService creates a new test core service
func NewTestService(t *testing.T, cfg *Config) *Service {
if cfg == nil {
rt := runtime.NewTestRuntime(t, runtime.POLKADOT_RUNTIME_c768a7e4c70e)
cfg = &Config{
Runtime: rt,
IsBabeAuthority: false,
}
}

if cfg.Keystore == nil {
cfg.Keystore = keystore.NewKeystore()
}

if cfg.NewBlocks == nil {
cfg.NewBlocks = make(chan types.Block)
}

if cfg.MsgRec == nil {
cfg.MsgRec = make(chan network.Message, 10)
}

if cfg.MsgSend == nil {
cfg.MsgSend = make(chan network.Message, 10)
}

if cfg.SyncChan == nil {
cfg.SyncChan = make(chan *big.Int, 10)
}

stateSrvc := state.NewService("")
stateSrvc.UseMemDB()

genesisData := new(genesis.Data)

err := stateSrvc.Initialize(genesisData, testGenesisHeader, trie.NewEmptyTrie())
require.Nil(t, err)

err = stateSrvc.Start()
require.Nil(t, err)

if cfg.BlockState == nil {
cfg.BlockState = stateSrvc.Block
}

if cfg.StorageState == nil {
cfg.StorageState = stateSrvc.Storage
}

s, err := NewService(cfg)
require.Nil(t, err)

return s
}
6 changes: 5 additions & 1 deletion dot/rpc/modules/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package modules
import (
"math/big"

"github.com/ChainSafe/gossamer/lib/crypto"

"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/transaction"
Expand Down Expand Up @@ -35,4 +37,6 @@ type TransactionQueueAPI interface {
}

// CoreAPI is the interface for the core methods
type CoreAPI interface{}
type CoreAPI interface {
InsertKey(kp crypto.Keypair)
}
Loading

0 comments on commit 3f02bed

Please sign in to comment.