Skip to content

Commit

Permalink
Update to Substrate v2.0.0 (#100)
Browse files Browse the repository at this point in the history
* Add metadata V12

* FIX AccountInfo Refcount should be unsigned 32 (see paritytech/substrate#7164)

* FIX Use Module index when looking for Call index

* FIX Use released v2.0.0 substrate version

* FIX Goimports types/metadataV12.go

* FIX Add some unit tests

* Remove run-substrate-docker v1 makefile target.

Co-authored-by: Laurent Turek <[email protected]>
  • Loading branch information
LaurentTrk and LaurentTrk authored Oct 6, 2020
1 parent 305d63b commit 3e97443
Show file tree
Hide file tree
Showing 10 changed files with 389 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ FROM parity/subkey:2.0.0 as subkey
RUN subkey --version

## Second Phase - Build context for tests
FROM parity/substrate:v2.0.0-rc6
FROM parity/substrate:v2.0.0

USER root

Expand Down
9 changes: 3 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,10 @@ test-e2e-deployed: ## runs only end-to-end (e2e) tests against a deployed test
@docker build . -t gsrpc-test
@docker run --rm -e RPC_URL -e TEST_PRIV_KEY gsrpc-test go test -v github.com/centrifuge/go-substrate-rpc-client/teste2e

run-substrate-docker: ## runs the Substrate 1.0 Default Docker image, this can be used to run the tests
docker run -p 9933:9933 -p 9944:9944 -p 30333:30333 parity/substrate:latest-v1.0 --dev --rpc-external --ws-external

run-substrate-docker-v2: ## runs the Substrate 2.0 Default Docker image, this can be used to run the tests
docker run -p 9933:9933 -p 9944:9944 -p 30333:30333 parity/substrate:v2.0.0-rc6 --dev --rpc-external --ws-external
run-substrate-docker: ## runs the Substrate 2.0 Default Docker image, this can be used to run the tests
docker run -p 9933:9933 -p 9944:9944 -p 30333:30333 parity/substrate:v2.0.0 --dev --rpc-external --ws-external

help: ## shows this help
@sed -ne '/@sed/!s/## //p' $(MAKEFILE_LIST)

.PHONY: clean install lint lint-fix test test-dockerized run-substrate-docker run-substrate-docker-v2
.PHONY: clean install lint lint-fix test test-dockerized run-substrate-docker
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Please refer to https://godoc.org/github.com/centrifuge/go-substrate-rpc-client

## Run tests locally against the Substrate Default Docker image

1. Start the Substrate Default Docker image: `make run-substrate-docker-v2`
1. Start the Substrate Default Docker image: `make run-substrate-docker`
1. In another terminal, run the tests against that image: `make test`
1. Visit https://polkadot.js.org/apps for inspection

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3.7"
services:
substrate:
image: parity/substrate:v2.0.0-rc6
image: parity/substrate:v2.0.0
ports:
- 9933:9933
- 9944:9944
Expand Down
2 changes: 1 addition & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Example_simpleConnect() {

fmt.Printf("You are connected to chain %v using %v v%v\n", chain, nodeName, nodeVersion)

// Output: You are connected to chain Development using Substrate Node v2.0.0-rc6-be8bb186-x86_64-linux-gnu
// Output: You are connected to chain Development using Substrate Node v2.0.0-a200cdb9-x86_64-linux-gnu
}

func Example_listenToNewBlocks() {
Expand Down
2 changes: 1 addition & 1 deletion types/account_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package types
// AccountInfo contains information of an account
type AccountInfo struct {
Nonce U32
Refcount U8
Refcount U32
Data struct {
Free U128
Reserved U128
Expand Down
23 changes: 23 additions & 0 deletions types/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ type Metadata struct {
AsMetadataV10 MetadataV10
IsMetadataV11 bool
AsMetadataV11 MetadataV11
IsMetadataV12 bool
AsMetadataV12 MetadataV12
}

func NewMetadataV4() *Metadata {
Expand Down Expand Up @@ -71,6 +73,14 @@ func NewMetadataV11() *Metadata {
}
}

func NewMetadataV12() *Metadata {
return &Metadata{
Version: 12,
IsMetadataV12: true,
AsMetadataV12: MetadataV12{Modules: make([]ModuleMetadataV12, 0)},
}
}

func (m *Metadata) Decode(decoder scale.Decoder) error {
err := decoder.Decode(&m.MagicNumber)
if err != nil {
Expand Down Expand Up @@ -104,6 +114,9 @@ func (m *Metadata) Decode(decoder scale.Decoder) error {
case 11:
m.IsMetadataV11 = true
err = decoder.Decode(&m.AsMetadataV11)
case 12:
m.IsMetadataV12 = true
err = decoder.Decode(&m.AsMetadataV12)
default:
return fmt.Errorf("unsupported metadata version %v", m.Version)
}
Expand Down Expand Up @@ -135,6 +148,8 @@ func (m Metadata) Encode(encoder scale.Encoder) error {
err = encoder.Encode(m.AsMetadataV10)
case 11:
err = encoder.Encode(m.AsMetadataV11)
case 12:
err = encoder.Encode(m.AsMetadataV12)
default:
return fmt.Errorf("unsupported metadata version %v", m.Version)
}
Expand All @@ -156,6 +171,8 @@ func (m *Metadata) FindCallIndex(call string) (CallIndex, error) {
return m.AsMetadataV10.FindCallIndex(call)
case m.IsMetadataV11:
return m.AsMetadataV11.FindCallIndex(call)
case m.IsMetadataV12:
return m.AsMetadataV12.FindCallIndex(call)
default:
return CallIndex{}, fmt.Errorf("unsupported metadata version")
}
Expand All @@ -175,6 +192,8 @@ func (m *Metadata) FindEventNamesForEventID(eventID EventID) (Text, Text, error)
return m.AsMetadataV10.FindEventNamesForEventID(eventID)
case m.IsMetadataV11:
return m.AsMetadataV11.FindEventNamesForEventID(eventID)
case m.IsMetadataV12:
return m.AsMetadataV12.FindEventNamesForEventID(eventID)
default:
return "", "", fmt.Errorf("unsupported metadata version")
}
Expand All @@ -194,6 +213,8 @@ func (m *Metadata) FindStorageEntryMetadata(module string, fn string) (StorageEn
return m.AsMetadataV10.FindStorageEntryMetadata(module, fn)
case m.IsMetadataV11:
return m.AsMetadataV11.FindStorageEntryMetadata(module, fn)
case m.IsMetadataV12:
return m.AsMetadataV12.FindStorageEntryMetadata(module, fn)
default:
return nil, fmt.Errorf("unsupported metadata version")
}
Expand All @@ -213,6 +234,8 @@ func (m *Metadata) ExistsModuleMetadata(module string) bool {
return m.AsMetadataV10.ExistsModuleMetadata(module)
case m.IsMetadataV11:
return m.AsMetadataV11.ExistsModuleMetadata(module)
case m.IsMetadataV12:
return m.AsMetadataV12.ExistsModuleMetadata(module)
default:
return false
}
Expand Down
216 changes: 216 additions & 0 deletions types/metadataV12.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
package types

import (
"fmt"
"strings"

"github.com/centrifuge/go-substrate-rpc-client/scale"
)

// Modelled after packages/types/src/Metadata/v11/toV12.ts
type MetadataV12 struct {
Modules []ModuleMetadataV12
Extrinsic ExtrinsicV11
}

func (m *MetadataV12) Decode(decoder scale.Decoder) error {
err := decoder.Decode(&m.Modules)
if err != nil {
return err
}
return decoder.Decode(&m.Extrinsic)
}

func (m MetadataV12) Encode(encoder scale.Encoder) error {
err := encoder.Encode(m.Modules)
if err != nil {
return err
}
return encoder.Encode(m.Extrinsic)
}

func (m *MetadataV12) FindCallIndex(call string) (CallIndex, error) {
s := strings.Split(call, ".")
for _, mod := range m.Modules {
if !mod.HasCalls {
continue
}
if string(mod.Name) != s[0] {
continue
}
for ci, f := range mod.Calls {
if string(f.Name) == s[1] {
return CallIndex{mod.Index, uint8(ci)}, nil
}
}
return CallIndex{}, fmt.Errorf("method %v not found within module %v for call %v", s[1], mod.Name, call)
}
return CallIndex{}, fmt.Errorf("module %v not found in metadata for call %v", s[0], call)
}

func (m *MetadataV12) FindEventNamesForEventID(eventID EventID) (Text, Text, error) {
for _, mod := range m.Modules {
if !mod.HasEvents {
continue
}
if mod.Index != eventID[0] {
continue
}
if int(eventID[1]) >= len(mod.Events) {
return "", "", fmt.Errorf("event index %v for module %v out of range", eventID[1], mod.Name)
}
return mod.Name, mod.Events[eventID[1]].Name, nil
}
return "", "", fmt.Errorf("module index %v out of range", eventID[0])
}

func (m *MetadataV12) FindStorageEntryMetadata(module string, fn string) (StorageEntryMetadata, error) {
for _, mod := range m.Modules {
if !mod.HasStorage {
continue
}
if string(mod.Storage.Prefix) != module {
continue
}
for _, s := range mod.Storage.Items {
if string(s.Name) != fn {
continue
}
return s, nil
}
return nil, fmt.Errorf("storage %v not found within module %v", fn, module)
}
return nil, fmt.Errorf("module %v not found in metadata", module)
}

func (m *MetadataV12) ExistsModuleMetadata(module string) bool {
for _, mod := range m.Modules {
if string(mod.Name) == module {
return true
}
}
return false
}

type ModuleMetadataV12 struct {
Name Text
HasStorage bool
Storage StorageMetadataV10
HasCalls bool
Calls []FunctionMetadataV4
HasEvents bool
Events []EventMetadataV4
Constants []ModuleConstantMetadataV6
Errors []ErrorMetadataV8
Index uint8
}

func (m *ModuleMetadataV12) Decode(decoder scale.Decoder) error {
err := decoder.Decode(&m.Name)
if err != nil {
return err
}

err = decoder.Decode(&m.HasStorage)
if err != nil {
return err
}

if m.HasStorage {
err = decoder.Decode(&m.Storage)
if err != nil {
return err
}
}

err = decoder.Decode(&m.HasCalls)
if err != nil {
return err
}

if m.HasCalls {
err = decoder.Decode(&m.Calls)
if err != nil {
return err
}
}

err = decoder.Decode(&m.HasEvents)
if err != nil {
return err
}

if m.HasEvents {
err = decoder.Decode(&m.Events)
if err != nil {
return err
}
}

err = decoder.Decode(&m.Constants)
if err != nil {
return err
}

err = decoder.Decode(&m.Errors)
if err != nil {
return err
}

return decoder.Decode(&m.Index)
}

func (m ModuleMetadataV12) Encode(encoder scale.Encoder) error {
err := encoder.Encode(m.Name)
if err != nil {
return err
}

err = encoder.Encode(m.HasStorage)
if err != nil {
return err
}

if m.HasStorage {
err = encoder.Encode(m.Storage)
if err != nil {
return err
}
}

err = encoder.Encode(m.HasCalls)
if err != nil {
return err
}

if m.HasCalls {
err = encoder.Encode(m.Calls)
if err != nil {
return err
}
}

err = encoder.Encode(m.HasEvents)
if err != nil {
return err
}

if m.HasEvents {
err = encoder.Encode(m.Events)
if err != nil {
return err
}
}

err = encoder.Encode(m.Constants)
if err != nil {
return err
}

err = encoder.Encode(m.Errors)
if err != nil {
return err
}

return encoder.Encode(m.Index)
}
20 changes: 20 additions & 0 deletions types/metadataV12_examplary_string.go

Large diffs are not rendered by default.

Loading

0 comments on commit 3e97443

Please sign in to comment.