From 88453d1418e61a393ddd445251077723ac93732d Mon Sep 17 00:00:00 2001 From: freehere107 Date: Thu, 2 Jan 2025 17:32:02 +0800 Subject: [PATCH] Metadata add PortableRegistry --- metadata.go | 28 +++++++++++++++++++++++++--- metadata_test.go | 9 +++++++++ types/scaleInfo.go | 25 +++++++++++++++++++++---- types/v14.go | 2 +- types/v15.go | 2 +- 5 files changed, 57 insertions(+), 9 deletions(-) diff --git a/metadata.go b/metadata.go index da5d9f7..8fb5ce8 100644 --- a/metadata.go +++ b/metadata.go @@ -23,9 +23,7 @@ func (m *MetadataDecoder) Process() error { magicBytes := m.NextBytes(4) if string(magicBytes) != "meta" { m.Data.Reset() - // option> version > v15 - m.NextBytes(1) // 0x01 - m.ProcessAndUpdateData("Compact") // Compact + m.ProcessAndUpdateData("Option>") // Option> magicBytes = m.NextBytes(4) } if string(magicBytes) == "meta" { @@ -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>") + 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") +} diff --git a/metadata_test.go b/metadata_test.go index 0624116..647d2c4 100644 --- a/metadata_test.go +++ b/metadata_test.go @@ -1,6 +1,7 @@ package scalecodec_test import ( + "github.com/stretchr/testify/assert" "testing" scalecodec "github.com/itering/scale.go" @@ -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) +} diff --git a/types/scaleInfo.go b/types/scaleInfo.go index 5ecff4e..c227185 100644 --- a/types/scaleInfo.go +++ b/types/scaleInfo.go @@ -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 { @@ -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"` @@ -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 { @@ -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 { @@ -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 { diff --git a/types/v14.go b/types/v14.go index 630decb..f149c00 100644 --- a/types/v14.go +++ b/types/v14.go @@ -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} diff --git a/types/v15.go b/types/v15.go index e289ff5..34ef1ce 100644 --- a/types/v15.go +++ b/types/v15.go @@ -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}