Skip to content

Commit

Permalink
Merge pull request #77 from itering/feat/compare
Browse files Browse the repository at this point in the history
Add runtime metadata compare
  • Loading branch information
freehere107 authored Apr 23, 2023
2 parents 98457ad + 23b7726 commit 66bc58a
Show file tree
Hide file tree
Showing 23 changed files with 396 additions and 27 deletions.
28 changes: 28 additions & 0 deletions compare_test.go

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ import (

type MetadataDecoder struct {
types.ScaleDecoder
Version string `json:"version"`
VersionNumber int `json:"version_number"`
Metadata types.MetadataStruct `json:"metadata"`
CodecTypes []string `json:"codec_types"`
Version string `json:"version"`
Metadata types.MetadataStruct `json:"metadata"`
}

func (m *MetadataDecoder) Init(data []byte) {
Expand All @@ -34,5 +32,6 @@ func (m *MetadataDecoder) Process() error {

}

// CheckRegistry deprecated, metadata v14 will auto register all types
// CheckRegistry
// Deprecated
func (m *MetadataDecoder) CheckRegistry() (notReg []string) { return }
7 changes: 7 additions & 0 deletions types/BTreeMap.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,10 @@ func (b *BTreeMap) Process() {
}
b.Value = result
}

func (b *BTreeMap) TypeStructString() string {
subType := strings.Split(b.SubType, ",")
type1 := getTypeStructString(subType[0], 0)
type2 := getTypeStructString(subType[1], 0)
return fmt.Sprintf("BTreeMap<%s,%s>", type1, type2)
}
4 changes: 4 additions & 0 deletions types/Bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ func (b *Bool) Encode(value bool) string {
}
return "00"
}

func (b *Bool) TypeStructString() string {
return "Bool"
}
12 changes: 12 additions & 0 deletions types/Bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func (b *Bytes) Encode(value string) string {
return Encode("Compact<u32>", len(bytes)) + value
}

func (b *Bytes) TypeStructString() string {
return "Bytes"
}

type HexBytes struct{ ScaleDecoder }

func (h *HexBytes) Process() {
Expand All @@ -39,9 +43,17 @@ func (h *HexBytes) Encode(value string) string {
return Encode("Compact<u32>", len(bytes)) + value
}

func (h *HexBytes) TypeStructString() string {
return "Bytes"
}

type String struct{ Bytes }

func (s *String) Encode(value string) string {
bytes := []byte(value)
return Encode("Compact<u32>", len(bytes)) + utiles.BytesToHex(bytes)
}

func (s *String) TypeStructString() string {
return "String"
}
4 changes: 4 additions & 0 deletions types/Call.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ func (s *Call) Encode(value interface{}) string {
return raw
}

func (s *Call) TypeStructString() string {
return "Call"
}

type BoxProposal struct {
Call
}
9 changes: 9 additions & 0 deletions types/Compact.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
"bytes"
"encoding/binary"
"fmt"
"io"

"github.com/itering/scale.go/types/scaleBytes"
Expand Down Expand Up @@ -66,6 +67,10 @@ func (c *Compact) Process() {
}
}

func (c *Compact) TypeStructString() string {
return fmt.Sprintf("Compact<%s>", c.SubType)
}

func (c *Compact) Encode(value interface{}) string {
var compactValue decimal.Decimal
switch v := value.(type) {
Expand Down Expand Up @@ -135,6 +140,10 @@ func (c *CompactU32) Process() {

}

func (c *CompactU32) TypeStructString() string {
return c.TypeString
}

func (c *CompactU32) Encode(value interface{}) string {
var i int
switch v := value.(type) {
Expand Down
14 changes: 11 additions & 3 deletions types/Enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,20 @@ func (e *Enum) Encode(data interface{}) string {
return ""
}

func (e *Enum) ToString() string {
func (e *Enum) TypeStructString() string {
if e.TypeMapping != nil {
return strings.Join(e.TypeMapping.Types, "")
var typeStrings []string
e.RecursiveTime++
for _, subType := range e.TypeMapping.Types {
if e.RecursiveTime > limitRecursiveTime {
return "Enum(...)"
}
typeStrings = append(typeStrings, getTypeStructString(subType, e.RecursiveTime))
}
return fmt.Sprintf("Enum(%s)", strings.Join(typeStrings, ","))
}
if e.ValueList != nil {
return strings.Join(e.ValueList, "")
return fmt.Sprintf("Enum(%s)", strings.Join(e.ValueList, ","))
}
return e.ScaleDecoder.TypeString
}
4 changes: 4 additions & 0 deletions types/FixedArray.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func (f *FixedArray) Process() {
}
}

func (f *FixedArray) TypeStructString() string {
return fmt.Sprintf("[%d;%s]", f.FixedLength, getTypeStructString(f.SubType, 0))
}

func (f *FixedArray) Encode(value interface{}) string {
var raw string
if reflect.TypeOf(value).Kind() == reflect.String && value.(string) == "" {
Expand Down
10 changes: 9 additions & 1 deletion types/FixedU8.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package types

import "github.com/itering/scale.go/utiles"
import (
"fmt"

"github.com/itering/scale.go/utiles"
)

type FixedU8 struct {
ScaleDecoder
Expand All @@ -19,3 +23,7 @@ func (s *FixedU8) Process() {
func (s *FixedU8) Encode(value string) string {
return utiles.TrimHex(value)
}

func (s *FixedU8) TypeStructString() string {
return fmt.Sprintf("[%d;U8]", s.FixedLength)
}
8 changes: 8 additions & 0 deletions types/Float.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func (f *Float64) Process() {
f.Value = f64
}

func (f *Float64) TypeStructString() string {
return "Float64"
}

type Float32 struct {
Float
}
Expand All @@ -52,3 +56,7 @@ func (f *Float32) Process() {
}
f.Value = f32
}

func (f *Float32) TypeStructString() string {
return "Float32"
}
4 changes: 4 additions & 0 deletions types/Int.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func (f *IntFixed) Encode(value interface{}) string {
}
}

func (f *IntFixed) TypeStructString() string {
return fmt.Sprintf("Int<%d>", f.FixedLength*8)
}

// BigIntToIntBytes ref https://github.com/centrifuge/go-substrate-rpc-client/blob/master/types/int.go#L218
func BigIntToIntBytes(i *big.Int, bytelen int) ([]byte, error) {
res := make([]byte, bytelen)
Expand Down
6 changes: 6 additions & 0 deletions types/Option.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
"fmt"

"github.com/itering/scale.go/utiles"
)

Expand Down Expand Up @@ -34,3 +36,7 @@ func (o *Option) Encode(value interface{}) string {
}
return EncodeWithOpt(o.SubType, value, &ScaleDecoderOption{Spec: o.Spec, Metadata: o.Metadata})
}

func (o *Option) TypeStructString() string {
return fmt.Sprintf("Option<%s>", getTypeStructString(o.SubType, o.RecursiveTime))
}
6 changes: 6 additions & 0 deletions types/Results.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"fmt"
"strings"

"github.com/itering/scale.go/utiles"
Expand Down Expand Up @@ -38,3 +39,8 @@ func (b *Result) Encode(value map[string]interface{}) string {
}
panic("illegal Result data")
}

func (b *Result) TypeStructString() string {
subType := strings.Split(b.SubType, ",")
return fmt.Sprintf("Results<%s,%s>", getTypeStructString(subType[0], 0), getTypeStructString(subType[1], 0))
}
17 changes: 14 additions & 3 deletions types/Struct.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package types

import "strings"
import (
"fmt"
"strings"
)

type Struct struct {
ScaleDecoder
Expand Down Expand Up @@ -31,9 +34,17 @@ func (s *Struct) Encode(value map[string]interface{}) string {
return raw
}

func (s *Struct) ToString() string {
func (s *Struct) TypeStructString() string {
if s.TypeMapping != nil {
return strings.Join(s.TypeMapping.Types, "")
var typeStrings []string
s.RecursiveTime++
for _, subType := range s.TypeMapping.Types {
if s.RecursiveTime > limitRecursiveTime {
return "Struct(...)"
}
typeStrings = append(typeStrings, getTypeStructString(subType, s.RecursiveTime))
}
return fmt.Sprintf("Struct(%s)", strings.Join(typeStrings, ","))
}
return s.ScaleDecoder.TypeString
}
20 changes: 20 additions & 0 deletions types/Uint.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func (u *U8) Encode(value interface{}) string {
return utiles.U8Encode(i)
}

func (u *U8) TypeStructString() string {
return "U8"
}

type U16 struct {
Reader io.Reader
ScaleDecoder
Expand Down Expand Up @@ -74,6 +78,10 @@ func (u *U16) Encode(value interface{}) string {
return utiles.BytesToHex(bs)
}

func (u *U16) TypeStructString() string {
return "U16"
}

type U32 struct {
Reader io.Reader
ScaleDecoder
Expand Down Expand Up @@ -105,6 +113,10 @@ func (u *U32) Encode(value interface{}) string {
return utiles.BytesToHex(bs)
}

func (u *U32) TypeStructString() string {
return "U32"
}

type U64 struct {
ScaleDecoder
Reader io.Reader
Expand Down Expand Up @@ -138,6 +150,10 @@ func (u *U64) Encode(value interface{}) string {
return utiles.BytesToHex(bs)
}

func (u *U64) TypeStructString() string {
return "U64"
}

type U128 struct {
ScaleDecoder
}
Expand Down Expand Up @@ -171,3 +187,7 @@ func (u *U128) Encode(value interface{}) string {
u128.PutBytes(bs)
return utiles.BytesToHex(bs)
}

func (u *U128) TypeStructString() string {
return "U128"
}
4 changes: 4 additions & 0 deletions types/Vectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func (v *Vec) Encode(value interface{}) string {
}
}

func (v *Vec) TypeStructString() string {
return fmt.Sprintf("Vec<%s>", getTypeStructString(v.SubType, v.RecursiveTime))
}

type BoundedVec struct {
Vec
}
Expand Down
Loading

0 comments on commit 66bc58a

Please sign in to comment.