Skip to content

Commit 65380fd

Browse files
Revert "feat(dot/rpc) implement author_hasSessionKeys RPC call (ChainSafe#1704)" (ChainSafe#1714)
This reverts commit 86df957.
1 parent 86df957 commit 65380fd

File tree

21 files changed

+171
-748
lines changed

21 files changed

+171
-748
lines changed

dot/core/service.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -461,16 +461,6 @@ func (s *Service) HasKey(pubKeyStr, keyType string) (bool, error) {
461461
return keystore.HasKey(pubKeyStr, keyType, s.keys.Acco)
462462
}
463463

464-
// DecodeSessionKeys executes the runtime DecodeSessionKeys and return the scale encoded keys
465-
func (s *Service) DecodeSessionKeys(enc []byte) ([]byte, error) {
466-
rt, err := s.blockState.GetRuntime(nil)
467-
if err != nil {
468-
return nil, err
469-
}
470-
471-
return rt.DecodeSessionKeys(enc)
472-
}
473-
474464
// GetRuntimeVersion gets the current RuntimeVersion
475465
func (s *Service) GetRuntimeVersion(bhash *common.Hash) (runtime.Version, error) {
476466
var stateRootHash *common.Hash

dot/rpc/http.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,15 @@ func (h *HTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
233233
// NewWSConn to create new WebSocket Connection struct
234234
func NewWSConn(conn *websocket.Conn, cfg *HTTPServerConfig) *subscription.WSConn {
235235
c := &subscription.WSConn{
236-
Wsconn: conn,
237-
Subscriptions: make(map[uint32]subscription.Listener),
238-
StorageAPI: cfg.StorageAPI,
239-
BlockAPI: cfg.BlockAPI,
240-
CoreAPI: cfg.CoreAPI,
241-
TxStateAPI: cfg.TransactionQueueAPI,
242-
RPCHost: fmt.Sprintf("http://%s:%d/", cfg.Host, cfg.RPCPort),
236+
Wsconn: conn,
237+
Subscriptions: make(map[uint]subscription.Listener),
238+
BlockSubChannels: make(map[uint]byte),
239+
StorageSubChannels: make(map[int]byte),
240+
StorageAPI: cfg.StorageAPI,
241+
BlockAPI: cfg.BlockAPI,
242+
CoreAPI: cfg.CoreAPI,
243+
TxStateAPI: cfg.TransactionQueueAPI,
244+
RPCHost: fmt.Sprintf("http://%s:%d/", cfg.Host, cfg.RPCPort),
243245
HTTP: &http.Client{
244246
Timeout: time.Second * 30,
245247
},

dot/rpc/modules/api.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ type CoreAPI interface {
7676
GetRuntimeVersion(bhash *common.Hash) (runtime.Version, error)
7777
HandleSubmittedExtrinsic(types.Extrinsic) error
7878
GetMetadata(bhash *common.Hash) ([]byte, error)
79-
DecodeSessionKeys(enc []byte) ([]byte, error)
8079
}
8180

8281
// RPCAPI is the interface for methods related to RPC service

dot/rpc/modules/author.go

Lines changed: 11 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@
1717
package modules
1818

1919
import (
20-
"errors"
20+
"fmt"
2121
"net/http"
22-
"strings"
22+
"reflect"
2323

2424
"github.com/ChainSafe/gossamer/dot/types"
2525
"github.com/ChainSafe/gossamer/lib/common"
2626
"github.com/ChainSafe/gossamer/lib/keystore"
27-
"github.com/ChainSafe/gossamer/pkg/scale"
2827

2928
log "github.com/ChainSafe/log15"
3029
)
@@ -36,17 +35,8 @@ type AuthorModule struct {
3635
txStateAPI TransactionStateAPI
3736
}
3837

39-
// HasSessionKeyRequest is used to receive the rpc data
40-
type HasSessionKeyRequest struct {
41-
PublicKeys string
42-
}
43-
4438
// KeyInsertRequest is used as model for the JSON
45-
type KeyInsertRequest struct {
46-
Type string
47-
Seed string
48-
PublicKey string
49-
}
39+
type KeyInsertRequest []string
5040

5141
// Extrinsic represents a hex-encoded extrinsic
5242
type Extrinsic struct {
@@ -74,18 +64,6 @@ type RemoveExtrinsicsResponse []common.Hash
7464
// KeyRotateResponse is a byte array used to rotate
7565
type KeyRotateResponse []byte
7666

77-
// HasSessionKeyResponse is the response to the RPC call author_hasSessionKeys
78-
type HasSessionKeyResponse bool
79-
80-
// KeyTypeID represents the key type of a session key
81-
type keyTypeID [4]uint8
82-
83-
// DecodedKey is the representation of a scaled decoded public key
84-
type decodedKey struct {
85-
Data []uint8
86-
Type keyTypeID
87-
}
88-
8967
// ExtrinsicStatus holds the actual valid statuses
9068
type ExtrinsicStatus struct {
9169
IsFuture bool
@@ -116,66 +94,27 @@ func NewAuthorModule(logger log.Logger, coreAPI CoreAPI, txStateAPI TransactionS
11694
}
11795
}
11896

119-
// HasSessionKeys checks if the keystore has private keys for the given session public keys.
120-
func (am *AuthorModule) HasSessionKeys(r *http.Request, req *HasSessionKeyRequest, res *HasSessionKeyResponse) error {
121-
pubKeysBytes, err := common.HexToBytes(req.PublicKeys)
122-
if err != nil {
123-
return err
124-
}
125-
126-
pkeys, err := scale.Marshal(pubKeysBytes)
127-
if err != nil {
128-
return err
129-
}
130-
131-
data, err := am.coreAPI.DecodeSessionKeys(pkeys)
132-
if err != nil {
133-
*res = false
134-
return err
135-
}
97+
// InsertKey inserts a key into the keystore
98+
func (am *AuthorModule) InsertKey(r *http.Request, req *KeyInsertRequest, res *KeyInsertResponse) error {
99+
keyReq := *req
136100

137-
var decodedKeys *[]decodedKey
138-
err = scale.Unmarshal(data, &decodedKeys)
101+
pkDec, err := common.HexToBytes(keyReq[1])
139102
if err != nil {
140103
return err
141104
}
142105

143-
if decodedKeys == nil || len(*decodedKeys) < 1 {
144-
*res = false
145-
return nil
146-
}
147-
148-
for _, key := range *decodedKeys {
149-
encType := keystore.Name(key.Type[:])
150-
ok, err := am.coreAPI.HasKey(common.BytesToHex(key.Data), string(encType))
151-
152-
if err != nil || !ok {
153-
*res = false
154-
return err
155-
}
156-
}
157-
158-
*res = true
159-
return nil
160-
}
161-
162-
// InsertKey inserts a key into the keystore
163-
func (am *AuthorModule) InsertKey(r *http.Request, req *KeyInsertRequest, res *KeyInsertResponse) error {
164-
keyReq := *req
165-
166-
keyBytes, err := common.HexToBytes(req.Seed)
106+
privateKey, err := keystore.DecodePrivateKey(pkDec, keystore.DetermineKeyType(keyReq[0]))
167107
if err != nil {
168108
return err
169109
}
170110

171-
keyPair, err := keystore.DecodeKeyPairFromHex(keyBytes, keystore.DetermineKeyType(keyReq.Type))
111+
keyPair, err := keystore.PrivateKeyToKeypair(privateKey)
172112
if err != nil {
173113
return err
174114
}
175115

176-
//strings.EqualFold compare using case-insensitivity.
177-
if !strings.EqualFold(keyPair.Public().Hex(), keyReq.PublicKey) {
178-
return errors.New("generated public key does not equal provide public key")
116+
if !reflect.DeepEqual(keyPair.Public().Hex(), keyReq[2]) {
117+
return fmt.Errorf("generated public key does not equal provide public key")
179118
}
180119

181120
am.coreAPI.InsertKey(keyPair)

dot/rpc/modules/author_test.go

Lines changed: 4 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -8,78 +8,13 @@ import (
88
apimocks "github.com/ChainSafe/gossamer/dot/rpc/modules/mocks"
99
"github.com/ChainSafe/gossamer/dot/types"
1010
"github.com/ChainSafe/gossamer/lib/common"
11-
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
1211
"github.com/ChainSafe/gossamer/lib/keystore"
13-
"github.com/ChainSafe/gossamer/lib/runtime"
14-
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
1512
"github.com/ChainSafe/gossamer/lib/transaction"
1613
log "github.com/ChainSafe/log15"
1714
"github.com/google/go-cmp/cmp"
1815
"github.com/stretchr/testify/mock"
19-
"github.com/stretchr/testify/require"
2016
)
2117

22-
func TestAuthorModule_HasSessionKey(t *testing.T) {
23-
globalStore := keystore.NewGlobalKeystore()
24-
25-
coremockapi := new(apimocks.MockCoreAPI)
26-
mockInsertKey := coremockapi.On("InsertKey", mock.AnythingOfType("*sr25519.Keypair"))
27-
mockInsertKey.Run(func(args mock.Arguments) {
28-
kp := args.Get(0).(*sr25519.Keypair)
29-
globalStore.Acco.Insert(kp)
30-
})
31-
32-
mockHasKey := coremockapi.On("HasKey", mock.AnythingOfType("string"), mock.AnythingOfType("string"))
33-
mockHasKey.Run(func(args mock.Arguments) {
34-
pubKeyHex := args.Get(0).(string)
35-
keyType := args.Get(1).(string)
36-
37-
ok, err := keystore.HasKey(pubKeyHex, keyType, globalStore.Acco)
38-
mockHasKey.ReturnArguments = []interface{}{ok, err}
39-
})
40-
41-
keys := "0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d34309a9d2a24213896ff06895db16aade8b6502f3a71cf56374cc3852042602634309a9d2a24213896ff06895db16aade8b6502f3a71cf56374cc3852042602634309a9d2a24213896ff06895db16aade8b6502f3a71cf56374cc38520426026"
42-
runtimeInstance := wasmer.NewTestInstance(t, runtime.NODE_RUNTIME)
43-
44-
decodeSessionKeysMock := coremockapi.On("DecodeSessionKeys", mock.AnythingOfType("[]uint8"))
45-
decodeSessionKeysMock.Run(func(args mock.Arguments) {
46-
b := args.Get(0).([]byte)
47-
dec, err := runtimeInstance.DecodeSessionKeys(b)
48-
decodeSessionKeysMock.ReturnArguments = []interface{}{dec, err}
49-
})
50-
51-
module := &AuthorModule{
52-
coreAPI: coremockapi,
53-
logger: log.New("service", "RPC", "module", "author"),
54-
}
55-
56-
req := &HasSessionKeyRequest{
57-
PublicKeys: keys,
58-
}
59-
60-
err := module.InsertKey(nil, &KeyInsertRequest{
61-
Type: "babe",
62-
Seed: "0xfec0f475b818470af5caf1f3c1b1558729961161946d581d2755f9fb566534f8",
63-
PublicKey: "0x34309a9d2a24213896ff06895db16aade8b6502f3a71cf56374cc38520426026",
64-
}, nil)
65-
coremockapi.AssertCalled(t, "InsertKey", mock.AnythingOfType("*sr25519.Keypair"))
66-
require.NoError(t, err)
67-
require.Equal(t, 1, globalStore.Acco.Size())
68-
69-
err = module.InsertKey(nil, &KeyInsertRequest{
70-
Type: "babe",
71-
Seed: "0xe5be9a5092b81bca64be81d212e7f2f9eba183bb7a90954f7b76361f6edb5c0a",
72-
PublicKey: "0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d",
73-
}, nil)
74-
require.NoError(t, err)
75-
require.Equal(t, 2, globalStore.Acco.Size())
76-
77-
var res HasSessionKeyResponse
78-
err = module.HasSessionKeys(nil, req, &res)
79-
require.NoError(t, err)
80-
require.True(t, bool(res))
81-
}
82-
8318
func TestAuthorModule_SubmitExtrinsic(t *testing.T) {
8419
errMockCoreAPI := &apimocks.MockCoreAPI{}
8520
errMockCoreAPI.On("HandleSubmittedExtrinsic", mock.AnythingOfType("types.Extrinsic")).Return(fmt.Errorf("some error"))
@@ -267,8 +202,8 @@ func TestAuthorModule_InsertKey(t *testing.T) {
267202
args: args{
268203
req: &KeyInsertRequest{
269204
"babe",
205+
"0xb7e9185065667390d2ad952a5324e8c365c9bf503dcf97c67a5ce861afe97309",
270206
"0x6246ddf254e0b4b4e7dffefc8adf69d212b98ac2b579c362b473fec8c40b4c0a",
271-
"0xdad5131003242c37c227f744f82118dd59a24b949ae264a93d949100738c196c",
272207
},
273208
},
274209
},
@@ -279,10 +214,9 @@ func TestAuthorModule_InsertKey(t *testing.T) {
279214
coreAPI: mockCoreAPI,
280215
},
281216
args: args{
282-
req: &KeyInsertRequest{
283-
"gran",
284-
"0xb48004c6e1625282313b07d1c9950935e86894a2e4f21fb1ffee9854d180c781",
285-
"0xa7d6507d59f8871b8f1a0f2c32e219adfacff4c9fcb05b0b2d8ebd6a65c88ee6",
217+
req: &KeyInsertRequest{"gran",
218+
"0xb7e9185065667390d2ad952a5324e8c365c9bf503dcf97c67a5ce861afe97309b7e9185065667390d2ad952a5324e8c365c9bf503dcf97c67a5ce861afe97309",
219+
"0xb7e9185065667390d2ad952a5324e8c365c9bf503dcf97c67a5ce861afe97309",
286220
},
287221
},
288222
},

dot/rpc/modules/mocks/core_api.go

Lines changed: 14 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dot/rpc/service_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func TestNewService(t *testing.T) {
3535
func TestService_Methods(t *testing.T) {
3636
qtySystemMethods := 13
3737
qtyRPCMethods := 1
38-
qtyAuthorMethods := 8
38+
qtyAuthorMethods := 7
3939

4040
rpcService := NewService()
4141
sysMod := modules.NewSystemModule(nil, nil, nil, nil, nil, nil)

0 commit comments

Comments
 (0)