Skip to content

Commit

Permalink
Add support for v13 + General improvments (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
ParthDesai authored Jun 15, 2021
1 parent 3db578f commit 35e1b61
Show file tree
Hide file tree
Showing 21 changed files with 863 additions and 154 deletions.
8 changes: 4 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func Example_listenToBalanceChange() {
}

alice := signature.TestKeyringPairAlice.PublicKey
key, err := types.CreateStorageKey(meta, "System", "Account", alice, nil)
key, err := types.CreateStorageKey(meta, "System", "Account", alice)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -227,7 +227,7 @@ func Example_makeASimpleTransfer() {
panic(err)
}

key, err := types.CreateStorageKey(meta, "System", "Account", signature.TestKeyringPairAlice.PublicKey, nil)
key, err := types.CreateStorageKey(meta, "System", "Account", signature.TestKeyringPairAlice.PublicKey)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -280,7 +280,7 @@ func Example_displaySystemEvents() {
}

// Subscribe to system events via storage
key, err := types.CreateStorageKey(meta, "System", "Events", nil, nil)
key, err := types.CreateStorageKey(meta, "System", "Events", nil)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -441,7 +441,7 @@ func Example_transactionWithEvents() {
}

// Get the nonce for Alice
key, err := types.CreateStorageKey(meta, "System", "Account", signature.TestKeyringPairAlice.PublicKey, nil)
key, err := types.CreateStorageKey(meta, "System", "Account", signature.TestKeyringPairAlice.PublicKey)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion rpc/author/submit_and_watch_extrinsic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestAuthor_SubmitAndWatchExtrinsic(t *testing.T) {
assert.NoError(t, err)

// Get the nonce for Alice
key, err := types.CreateStorageKey(meta, "System", "Account", signature.TestKeyringPairAlice.PublicKey, nil)
key, err := types.CreateStorageKey(meta, "System", "Account", signature.TestKeyringPairAlice.PublicKey)
assert.NoError(t, err)

var accountInfo types.AccountInfo
Expand Down
2 changes: 1 addition & 1 deletion rpc/author/submit_extrinsic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestAuthor_SubmitExtrinsic(t *testing.T) {
assert.NoError(t, err)

// Get the nonce for Alice
key, err := types.CreateStorageKey(meta, "System", "Account", signature.TestKeyringPairAlice.PublicKey, nil)
key, err := types.CreateStorageKey(meta, "System", "Account", signature.TestKeyringPairAlice.PublicKey)
assert.NoError(t, err)

var accountInfo types.AccountInfo
Expand Down
2 changes: 1 addition & 1 deletion teste2e/author_submit_and_watch_extrinsic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestAuthor_SubmitAndWatchExtrinsic(t *testing.T) {
rv, err := api.RPC.State.GetRuntimeVersionLatest()
assert.NoError(t, err)

key, err := types.CreateStorageKey(meta, "System", "Account", from.PublicKey, nil)
key, err := types.CreateStorageKey(meta, "System", "Account", from.PublicKey)
assert.NoError(t, err)

var sub *author.ExtrinsicStatusSubscription
Expand Down
4 changes: 2 additions & 2 deletions teste2e/author_submit_extrinsic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestChain_Events(t *testing.T) {
meta, err := api.RPC.State.GetMetadataLatest()
assert.NoError(t, err)

key, err := types.CreateStorageKey(meta, "System", "Events", nil, nil)
key, err := types.CreateStorageKey(meta, "System", "Events", nil)
assert.NoError(t, err)

blockNUmber := uint64(0) // Replace with desired block to parse events
Expand Down Expand Up @@ -80,7 +80,7 @@ func TestChain_SubmitExtrinsic(t *testing.T) {
rv, err := api.RPC.State.GetRuntimeVersionLatest()
assert.NoError(t, err)

key, err := types.CreateStorageKey(meta, "System", "Account", from.PublicKey, nil)
key, err := types.CreateStorageKey(meta, "System", "Account", from.PublicKey)
assert.NoError(t, err)

var accountInfo types.AccountInfo
Expand Down
2 changes: 1 addition & 1 deletion teste2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestEnd2end(t *testing.T) {
meta, err := api.RPC.State.GetMetadataLatest()
assert.NoError(t, err)

key, err := types.CreateStorageKey(meta, "Session", "Validators", nil, nil)
key, err := types.CreateStorageKey(meta, "Session", "Validators", nil)
assert.NoError(t, err)

var validators []types.AccountID
Expand Down
4 changes: 2 additions & 2 deletions teste2e/state_subscribe_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestState_SubscribeStorage_EventsRaw(t *testing.T) {
meta, err := api.RPC.State.GetMetadataLatest()
assert.NoError(t, err)

key, err := types.CreateStorageKey(meta, "System", "Events", nil, nil)
key, err := types.CreateStorageKey(meta, "System", "Events", nil)
assert.NoError(t, err)

sub, err := api.RPC.State.SubscribeStorageRaw([]types.StorageKey{key})
Expand Down Expand Up @@ -75,7 +75,7 @@ func TestState_SubscribeStorage_Events(t *testing.T) {
meta, err := api.RPC.State.GetMetadataLatest()
assert.NoError(t, err)

key, err := types.CreateStorageKey(meta, "System", "Events", nil, nil)
key, err := types.CreateStorageKey(meta, "System", "Events", nil)
assert.NoError(t, err)

sub, err := api.RPC.State.SubscribeStorageRaw([]types.StorageKey{key})
Expand Down
48 changes: 48 additions & 0 deletions types/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type Metadata struct {
AsMetadataV11 MetadataV11
IsMetadataV12 bool
AsMetadataV12 MetadataV12
IsMetadataV13 bool
AsMetadataV13 MetadataV13
}

func NewMetadataV4() *Metadata {
Expand Down Expand Up @@ -81,6 +83,14 @@ func NewMetadataV12() *Metadata {
}
}

func NewMetadataV13() *Metadata {
return &Metadata{
Version: 13,
IsMetadataV13: true,
AsMetadataV13: MetadataV13{Modules: make([]ModuleMetadataV13, 0)},
}
}

func (m *Metadata) Decode(decoder scale.Decoder) error {
err := decoder.Decode(&m.MagicNumber)
if err != nil {
Expand Down Expand Up @@ -117,6 +127,9 @@ func (m *Metadata) Decode(decoder scale.Decoder) error {
case 12:
m.IsMetadataV12 = true
err = decoder.Decode(&m.AsMetadataV12)
case 13:
m.IsMetadataV13 = true
err = decoder.Decode(&m.AsMetadataV13)
default:
return fmt.Errorf("unsupported metadata version %v", m.Version)
}
Expand Down Expand Up @@ -150,13 +163,40 @@ func (m Metadata) Encode(encoder scale.Encoder) error {
err = encoder.Encode(m.AsMetadataV11)
case 12:
err = encoder.Encode(m.AsMetadataV12)
case 13:
err = encoder.Encode(m.AsMetadataV13)
default:
return fmt.Errorf("unsupported metadata version %v", m.Version)
}

return err
}

func (m *Metadata) FindConstantValue(module string, constantName string) ([]byte, error) {
txtModule := Text(module)
txtConstantName := Text(constantName)
switch {
case m.IsMetadataV4:
return m.AsMetadataV4.FindConstantValue(txtModule, txtConstantName)
case m.IsMetadataV7:
return m.AsMetadataV7.FindConstantValue(txtModule, txtConstantName)
case m.IsMetadataV8:
return m.AsMetadataV8.FindConstantValue(txtModule, txtConstantName)
case m.IsMetadataV9:
return m.AsMetadataV9.FindConstantValue(txtModule, txtConstantName)
case m.IsMetadataV10:
return m.AsMetadataV10.FindConstantValue(txtModule, txtConstantName)
case m.IsMetadataV11:
return m.AsMetadataV11.FindConstantValue(txtModule, txtConstantName)
case m.IsMetadataV12:
return m.AsMetadataV12.FindConstantValue(txtModule, txtConstantName)
case m.IsMetadataV13:
return m.AsMetadataV13.FindConstantValue(txtModule, txtConstantName)
default:
return nil, fmt.Errorf("unsupported metadata version")
}
}

func (m *Metadata) FindCallIndex(call string) (CallIndex, error) {
switch {
case m.IsMetadataV4:
Expand All @@ -173,6 +213,8 @@ func (m *Metadata) FindCallIndex(call string) (CallIndex, error) {
return m.AsMetadataV11.FindCallIndex(call)
case m.IsMetadataV12:
return m.AsMetadataV12.FindCallIndex(call)
case m.IsMetadataV13:
return m.AsMetadataV13.FindCallIndex(call)
default:
return CallIndex{}, fmt.Errorf("unsupported metadata version")
}
Expand All @@ -194,6 +236,8 @@ func (m *Metadata) FindEventNamesForEventID(eventID EventID) (Text, Text, error)
return m.AsMetadataV11.FindEventNamesForEventID(eventID)
case m.IsMetadataV12:
return m.AsMetadataV12.FindEventNamesForEventID(eventID)
case m.IsMetadataV13:
return m.AsMetadataV13.FindEventNamesForEventID(eventID)
default:
return "", "", fmt.Errorf("unsupported metadata version")
}
Expand All @@ -215,6 +259,8 @@ func (m *Metadata) FindStorageEntryMetadata(module string, fn string) (StorageEn
return m.AsMetadataV11.FindStorageEntryMetadata(module, fn)
case m.IsMetadataV12:
return m.AsMetadataV12.FindStorageEntryMetadata(module, fn)
case m.IsMetadataV13:
return m.AsMetadataV13.FindStorageEntryMetadata(module, fn)
default:
return nil, fmt.Errorf("unsupported metadata version")
}
Expand All @@ -236,6 +282,8 @@ func (m *Metadata) ExistsModuleMetadata(module string) bool {
return m.AsMetadataV11.ExistsModuleMetadata(module)
case m.IsMetadataV12:
return m.AsMetadataV12.ExistsModuleMetadata(module)
case m.IsMetadataV13:
return m.AsMetadataV13.ExistsModuleMetadata(module)
default:
return false
}
Expand Down
29 changes: 29 additions & 0 deletions types/metadataV10.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ func (m *MetadataV10) FindEventNamesForEventID(eventID EventID) (Text, Text, err
return "", "", fmt.Errorf("module index %v out of range", eventID[0])
}

func (m *MetadataV10) FindConstantValue(module Text, constant Text) ([]byte, error) {
for _, mod := range m.Modules {
if mod.Name == module {
value, err := mod.FindConstantValue(constant)
if err == nil {
return value, nil
}
}
}
return nil, fmt.Errorf("could not find constant %s.%s", module, constant)
}

func (m *MetadataV10) FindStorageEntryMetadata(module string, fn string) (StorageEntryMetadata, error) {
for _, mod := range m.Modules {
if !mod.HasStorage {
Expand Down Expand Up @@ -227,6 +239,15 @@ func (m ModuleMetadataV10) Encode(encoder scale.Encoder) error {
return encoder.Encode(m.Errors)
}

func (m *ModuleMetadataV10) FindConstantValue(constant Text) ([]byte, error) {
for _, cons := range m.Constants {
if cons.Name == constant {
return cons.Value, nil
}
}
return nil, fmt.Errorf("could not find constant %s", constant)
}

type StorageMetadataV10 struct {
Prefix Text
Items []StorageFunctionMetadataV10
Expand All @@ -252,6 +273,14 @@ func (s StorageFunctionMetadataV10) IsDoubleMap() bool {
return s.Type.IsDoubleMap
}

func (s StorageFunctionMetadataV10) IsNMap() bool {
return false
}

func (s StorageFunctionMetadataV10) Hashers() ([]hash.Hash, error) {
return nil, fmt.Errorf("Hashers is not supported for metadata v10, please upgrade to use metadata v13 or newer")
}

func (s StorageFunctionMetadataV10) Hasher() (hash.Hash, error) {
if s.Type.IsMap {
return s.Type.AsMap.Hasher.HashFunc()
Expand Down
Loading

0 comments on commit 35e1b61

Please sign in to comment.