Skip to content

Commit

Permalink
implement state_subscribeRuntimeVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardmack committed Mar 1, 2021
1 parent 42c3eb6 commit a066327
Show file tree
Hide file tree
Showing 12 changed files with 1,060 additions and 186 deletions.
1 change: 1 addition & 0 deletions dot/rpc/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type WSConn struct {
storageAPI modules.StorageAPI
blockAPI modules.BlockAPI
runtimeAPI modules.RuntimeAPI
coreAPI modules.CoreAPI
}

var logger log.Logger
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/modules/author.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (cm *AuthorModule) SubmitExtrinsic(r *http.Request, req *Extrinsic, res *Ex
return err
}
cm.logger.Trace("[rpc]", "extrinsic", extBytes)

fmt.Printf("SubExt ext %v\n", extBytes)
// For RPC request the transaction source is External
ext := types.Extrinsic(append([]byte{byte(types.TxnExternal)}, extBytes...))

Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/modules/author_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestAuthorModule_Pending(t *testing.T) {
}

func TestAuthorModule_SubmitExtrinsic(t *testing.T) {
t.Skip()
//t.Skip()
// setup auth module
txQueue := state.NewTransactionState()

Expand Down
4 changes: 2 additions & 2 deletions dot/rpc/modules/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func (sm *StateModule) GetRuntimeVersion(r *http.Request, req *StateRuntimeVersi
res.SpecVersion = rtVersion.SpecVersion()
res.ImplVersion = rtVersion.ImplVersion()
res.TransactionVersion = rtVersion.TransactionVersion()
res.Apis = convertAPIs(rtVersion.APIItems())
res.Apis = ConvertAPIs(rtVersion.APIItems())

return nil
}
Expand Down Expand Up @@ -404,7 +404,7 @@ func (sm *StateModule) SubscribeStorage(r *http.Request, req *StateStorageQueryR
return nil
}

func convertAPIs(in []*runtime.APIItem) []interface{} {
func ConvertAPIs(in []*runtime.APIItem) []interface{} {
ret := make([]interface{}, 0)
for _, item := range in {
encStr := hex.EncodeToString(item.Name[:])
Expand Down
68 changes: 66 additions & 2 deletions dot/rpc/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func NewWSConn(conn *websocket.Conn, cfg *HTTPServerConfig) *WSConn {
storageAPI: cfg.StorageAPI,
blockAPI: cfg.BlockAPI,
runtimeAPI: cfg.RuntimeAPI,
coreAPI: cfg.CoreAPI,
}
return c
}
Expand Down Expand Up @@ -190,10 +191,17 @@ func (c *WSConn) handleComm() {
case "chain_subscribeFinalizedHeads":
bfl, err3 := c.initBlockFinalizedListener(reqid)
if err3 != nil {
logger.Warn("failed to create block finalized", "error", err)
logger.Warn("failed to create block finalized", "error", err3)
continue
}
c.startListener(bfl)
case "state_subscribeRuntimeVersion":
rvl, err4 := c.initRuntimeVersionListener(reqid)
if err4 != nil {
logger.Warn("failed to create runtime version listener", "error", err4)
continue
}
c.startListener(rvl)
}
continue
}
Expand Down Expand Up @@ -551,6 +559,8 @@ func (l *ExtrinsicWatchListener) Listen() {
if block == nil {
continue
}
exts, err := block.Body.AsEncodedExtrinsics()
fmt.Printf("EXts %v\n", exts)
// TODO determine how if extrinsic is in this block
headM := make(map[string]interface{})
resM := make(map[string]interface{})
Expand All @@ -560,10 +570,64 @@ func (l *ExtrinsicWatchListener) Listen() {
res := newSubcriptionBaseResponseJSON()
res.Method = "author_extrinsicUpdate"
res.Params = headM
err := l.wsconn.safeSend(res)
err = l.wsconn.safeSend(res)
if err != nil {
logger.Error("error sending websocket message", "error", err)
}

}
}

// RuntimeVersionListener to handle listening for Runtime Version
type RuntimeVersionListener struct {
wsconn *WSConn
subID int
}

func (c *WSConn) initRuntimeVersionListener(reqID float64) (int, error) {
rvl := &RuntimeVersionListener{
wsconn: c,
}
if c.coreAPI == nil {
e := c.safeSendError(reqID, nil, "error CoreAPI not set")
if e != nil {
logger.Warn("error sending error message", "error", e)
}
return 0, fmt.Errorf("error CoreAPI not set")
}
c.qtyListeners++
rvl.subID = c.qtyListeners
c.subscriptions[rvl.subID] = rvl
initRes := newSubscriptionResponseJSON(rvl.subID, reqID)
err := c.safeSend(initRes)
if err != nil {
return 0, err
}
return rvl.subID, nil
}

func (l *RuntimeVersionListener) Listen() {
rtVersion, err := l.wsconn.coreAPI.GetRuntimeVersion(nil)
if err != nil {
return
}
result := make(map[string]interface{})
ver := modules.StateRuntimeVersionResponse{}

ver.SpecName = string(rtVersion.SpecName())
ver.ImplName = string(rtVersion.ImplName())
ver.AuthoringVersion = rtVersion.AuthoringVersion()
ver.SpecVersion = rtVersion.SpecVersion()
ver.ImplVersion = rtVersion.ImplVersion()
ver.TransactionVersion = rtVersion.TransactionVersion()
ver.Apis = modules.ConvertAPIs(rtVersion.APIItems())
result["result"] = ver
result["subscription"] = l.subID
res := newSubcriptionBaseResponseJSON()
res.Method = "state_runtimeVersion"
res.Params = result
err = l.wsconn.safeSend(res)
if err != nil {
logger.Error("error sending websocket message", "error", err)
}
}
4 changes: 2 additions & 2 deletions dot/state/block_notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package state

import (
"errors"
"fmt"
"math/rand"

"github.com/ChainSafe/gossamer/dot/types"
Expand Down Expand Up @@ -95,13 +96,12 @@ func (bs *BlockState) UnregisterFinalizedChannel(id byte) {
func (bs *BlockState) notifyImported(block *types.Block) {
bs.importedLock.RLock()
defer bs.importedLock.RUnlock()

fmt.Printf("IMPORTED %v, %v\n", block.Header.Number, block.Body)
if len(bs.imported) == 0 {
return
}

logger.Trace("notifying imported block chans...", "chans", bs.imported)

for _, ch := range bs.imported {
go func(ch chan<- *types.Block) {
select {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ require (
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 // indirect
golang.org/x/sys v0.0.0-20200824131525-c12d262b63d8 // indirect
golang.org/x/text v0.3.3 // indirect
golang.org/x/tools v0.0.0-20200221224223-e1da425f72fd // indirect
golang.org/x/tools v0.0.0-20200221224223-e1da425f72fd
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/protobuf v1.25.0
)
Expand Down
Loading

0 comments on commit a066327

Please sign in to comment.