Skip to content

Commit

Permalink
Metadata add PortableRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
freehere107 committed Jan 2, 2025
1 parent ac8d979 commit 88453d1
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 9 deletions.
28 changes: 25 additions & 3 deletions metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ func (m *MetadataDecoder) Process() error {
magicBytes := m.NextBytes(4)
if string(magicBytes) != "meta" {
m.Data.Reset()
// option<Opaque<metadata>> version > v15
m.NextBytes(1) // 0x01
m.ProcessAndUpdateData("Compact<u32>") // Compact<u32>
m.ProcessAndUpdateData("Option<Compact<u32>>") // Option<Compact<u32>>
magicBytes = m.NextBytes(4)
}
if string(magicBytes) == "meta" {
Expand All @@ -38,3 +36,27 @@ func (m *MetadataDecoder) Process() error {
return errors.New("not metadata")

}

func (m *MetadataDecoder) PortableRegistry() (map[int]types.SiType, error) {
magicBytes := m.NextBytes(4)
if string(magicBytes) != "meta" { // metadata version >= v15
m.Data.Reset()
m.ProcessAndUpdateData("Option<Compact<u32>>")
magicBytes = m.NextBytes(4)
}
if string(magicBytes) == "meta" {
metadataVersion := utiles.U256(utiles.BytesToHex(m.Data.Data[m.Data.Offset : m.Data.Offset+1]))
m.Version = m.ProcessAndUpdateData("MetadataVersion").(string)
if metadataVersion.Int64() < 14 {
return nil, errors.New("PortableRegistry not support, only metadata version >= v14")
}
portable := types.InitPortableRaw(m.ProcessAndUpdateData("PortableRegistry").([]interface{}))
var registry = make(map[int]types.SiType)
for index, item := range portable {
item.RemoveDocs()
registry[index] = item
}
return registry, nil
}
return nil, errors.New("not metadata")
}
9 changes: 9 additions & 0 deletions metadata_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scalecodec_test

import (
"github.com/stretchr/testify/assert"
"testing"

scalecodec "github.com/itering/scale.go"
Expand Down Expand Up @@ -51,3 +52,11 @@ func TestMetadataV15DecoderProcess(t *testing.T) {
}
// utiles.Debug(m.Metadata)
}

func TestMetadataPortableRegistry(t *testing.T) {
m := scalecodec.MetadataDecoder{}
m.Init(utiles.HexToBytes(Kusama9370))
registry, err := m.PortableRegistry()
assert.NoError(t, err)
assert.Equal(t, len(registry), 856)
}
25 changes: 21 additions & 4 deletions types/scaleInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type SiType struct {
Path []string `json:"path"`
Params []SiTypeParameter `json:"params,omitempty"`
Def SiTypeDef `json:"def"`
Docs []string `json:"docs"`
Docs []string `json:"docs,omitempty"`
}

func (s *SiType) FindParameter(name string) *SiTypeParameter {
Expand All @@ -32,6 +32,23 @@ func (s *SiType) FindParameter(name string) *SiTypeParameter {
return nil
}

func (s *SiType) RemoveDocs() {
s.Docs = nil
switch {
case s.Def.Composite != nil:
for i := range s.Def.Composite.Fields {
s.Def.Composite.Fields[i].Docs = nil
}
case s.Def.Variant != nil:
for i := range s.Def.Variant.Variants {
s.Def.Variant.Variants[i].Docs = nil
for j := range s.Def.Variant.Variants[i].Fields {
s.Def.Variant.Variants[i].Fields[j].Docs = nil
}
}
}
}

type SiTypeParameter struct {
Name string `json:"name"`
Type int `json:"type"`
Expand Down Expand Up @@ -59,7 +76,7 @@ type SiField struct {
Name string `json:"name,omitempty"`
Type int `json:"type"`
TypeName string `json:"typeName"`
Docs []string `json:"docs"`
Docs []string `json:"docs,omitempty"`
}

type SiTypeDefRange struct {
Expand All @@ -76,7 +93,7 @@ type SiVariant struct {
Name string `json:"name"`
Fields []SiField `json:"fields"`
Index int `json:"index"`
Docs []string `json:"docs"`
Docs []string `json:"docs,omitempty"`
}

type SiTypeDefSequence struct {
Expand All @@ -101,7 +118,7 @@ type SiTypeDefBitSequence struct {
BitOrderType int `json:"bitOrderType"`
}

func initPortableRaw(raw []interface{}) map[int]SiType {
func InitPortableRaw(raw []interface{}) map[int]SiType {
var portables []PortableType
bm, _ := json.Marshal(raw)
if err := json.Unmarshal(bm, &portables); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion types/v14.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (m *MetadataV14Decoder) Process() {
}

// custom type lookup
portable := initPortableRaw(m.ProcessAndUpdateData("PortableRegistry").([]interface{}))
portable := InitPortableRaw(m.ProcessAndUpdateData("PortableRegistry").([]interface{}))
// utiles.Debug(portable)

scaleInfo := ScaleInfo{ScaleDecoder: &m.ScaleDecoder, V14: true}
Expand Down
2 changes: 1 addition & 1 deletion types/v15.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (m *MetadataV15Decoder) Process() {
}

// custom type lookup
portable := initPortableRaw(m.ProcessAndUpdateData("PortableRegistry").([]interface{}))
portable := InitPortableRaw(m.ProcessAndUpdateData("PortableRegistry").([]interface{}))
// utiles.Debug(portable)

scaleInfo := ScaleInfo{ScaleDecoder: &m.ScaleDecoder, V14: true}
Expand Down

0 comments on commit 88453d1

Please sign in to comment.