From b3eefa7090f4ab8f0509d3b3fb05de43895eeb09 Mon Sep 17 00:00:00 2001 From: Charles Billette Date: Wed, 24 Jul 2024 10:51:02 -0400 Subject: [PATCH] remove idToField --- generator/generator.go | 3 +- generator/generator_test.go | 2 +- pb/sf/gear/metadata/type/v1/output.pb.go | 5 +- proto/sf/gear/metadata/type/v1/output.proto | 38 ++--- rpc/convert_metadata.go | 3 - templates/gen_types.go | 162 +++++++++++++------- templates/gen_types.go.gotmpl | 22 --- 7 files changed, 131 insertions(+), 104 deletions(-) diff --git a/generator/generator.go b/generator/generator.go index d07b1d6..634a503 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -20,13 +20,12 @@ type Generator struct { IdToField map[int64]protobuf.Field } -func NewGenerator(templatePath string, messages []*protobuf.Message, metadata *types.Metadata, IdToField map[int64]protobuf.Field) *Generator { +func NewGenerator(templatePath string, messages []*protobuf.Message, metadata *types.Metadata) *Generator { return &Generator{ templatePath: templatePath, Messages: messages, Metadata: metadata, seenFields: make(map[string]bool), - IdToField: IdToField, } } diff --git a/generator/generator_test.go b/generator/generator_test.go index de185e8..65eb1f5 100644 --- a/generator/generator_test.go +++ b/generator/generator_test.go @@ -25,7 +25,7 @@ func Test_Generator(t *testing.T) { return messages[i].FullTypeName() < messages[j].FullTypeName() }) - gen := NewGenerator("../templates/gen_types.go.gotmpl", messages, metadata, mc.TypeConverter.IdToField) + gen := NewGenerator("../templates/gen_types.go.gotmpl", messages, metadata) err = gen.Generate() require.NoError(t, err) } diff --git a/pb/sf/gear/metadata/type/v1/output.pb.go b/pb/sf/gear/metadata/type/v1/output.pb.go index 189aa7c..ec63992 100644 --- a/pb/sf/gear/metadata/type/v1/output.pb.go +++ b/pb/sf/gear/metadata/type/v1/output.pb.go @@ -7,10 +7,11 @@ package pbgear import ( - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" + + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" ) const ( diff --git a/proto/sf/gear/metadata/type/v1/output.proto b/proto/sf/gear/metadata/type/v1/output.proto index 0bf492a..5fd3d2b 100644 --- a/proto/sf/gear/metadata/type/v1/output.proto +++ b/proto/sf/gear/metadata/type/v1/output.proto @@ -2,15 +2,30 @@ syntax = "proto3"; package sf.gear.metadata.type.v1; option go_package = "github.com/streamingfast/firehose-gear/pb/sf/gear/metadata/type/v1;pbgear"; +message Balances_Raw { + repeated uint32 value_0 = 1; +} +message Balances_Address20 { + repeated uint32 value_0 = 1; +} +message Balances_CompactString { + string value = 1; +} +message CompactTupleNull { + TupleNull value = 1; +} +message SpCoreCryptoAccountId32 { + repeated uint32 value_0 = 1; +} +message Balances_Id { + SpCoreCryptoAccountId32 value_0 = 1; +} message TupleNull { } message Balances_Index { CompactTupleNull value_0 = 1; } -message Balances_Raw { - repeated uint32 value_0 = 1; -} -message Balances_Address20 { +message Balances_Address32 { repeated uint32 value_0 = 1; } message Balances_Dest { @@ -22,22 +37,7 @@ message Balances_Dest { Balances_Address20 address20 = 5; } } -message Balances_CompactString { - string value = 1; -} -message SpCoreCryptoAccountId32 { - repeated uint32 value_0 = 1; -} -message Balances_Address32 { - repeated uint32 value_0 = 1; -} message Balances_TransferKeepAliveCall { Balances_Dest dest = 1; Balances_CompactString value = 2; } -message Balances_Id { - SpCoreCryptoAccountId32 value_0 = 1; -} -message CompactTupleNull { - TupleNull value = 1; -} diff --git a/rpc/convert_metadata.go b/rpc/convert_metadata.go index 5a88ac5..1c05757 100644 --- a/rpc/convert_metadata.go +++ b/rpc/convert_metadata.go @@ -60,7 +60,6 @@ func (mc *MetadataConverter) Convert(blockHash string) (map[string]types.IType, case 14: mc.TypeConverter = &TypeConverter{ messages: make(map[string]*protobuf.Message), - IdToField: map[int64]protobuf.Field{}, allMetadataTypes: []substrateTypes.PortableTypeV14{}, } return mc.TypeConverter.convertTypesFromv14(metadata.AsMetadataV14) @@ -89,7 +88,6 @@ func (mc *MetadataConverter) fetchStateMetadata(blockHash string) (*substrateTyp type TypeConverter struct { messages map[string]*protobuf.Message allMetadataTypes []substrateTypes.PortableTypeV14 - IdToField map[int64]protobuf.Field } func (c *TypeConverter) convertTypesFromv14(metadata substrateTypes.MetadataV14) (map[string]types.IType, error) { @@ -496,7 +494,6 @@ func (c *TypeConverter) MessageForVariantTypes(name string, variant substrateTyp field := c.FieldForType(fieldType, palletNameFromPath(fieldType.Type.Path), callName, fn) msg.Fields = append(msg.Fields, field) - c.IdToField[idx] = field } return msg diff --git a/templates/gen_types.go b/templates/gen_types.go index ec341cc..d4422c0 100644 --- a/templates/gen_types.go +++ b/templates/gen_types.go @@ -1,81 +1,133 @@ package gen_types import ( - "github.com/centrifuge/go-substrate-rpc-client/v4/registry" - "github.com/centrifuge/go-substrate-rpc-client/v4/types" - pbgear "github.com/streamingfast/firehose-gear/pb/sf/gear/metadata/type/v1" + "github.com/centrifuge/go-substrate-rpc-client/v4/registry" + pbgear "github.com/streamingfast/firehose-gear/pb/sf/gear/metadata/type/v1" ) -// type regEntry struct{ -// fn func(fields []*registry.DecodedField) any -// returnType reflect.Type -// } -// -// var reg map[int64]*regEntry -// -// func init() { -// reg = make(map[int64]*regEntry) -// reg[0] = ®Entry{ -// fn: to_SpCoreCryptoAccountId32, -// } -// } -func To_Balances_TransferKeepAliveCall(fields []*registry.DecodedField) any { - out := &pbgear.Balances_TransferKeepAliveCall{} - f := fields[0].Value.(registry.DecodedFields) - - out.Dest = to_Balances_Dest([]*registry.DecodedField(f)) - //out.Value = to_Balances_CompactString(fields[1].Value.([]*registry.DecodedField)) - return out -} -func to_Balances_Dest(fields []*registry.DecodedField) *pbgear.Balances_Dest { - out := &pbgear.Balances_Dest{} - //=========== - switch { +func to_Balances_Address20(fields []*registry.DecodedField) any { + out := &pbgear.Balances_Address20{} + out.Value_0 = to_repeated_uint32(fields[0].Value.([]*registry.DecodedField)) + return out +} - case matchFields(fields, []int64{0}): - out.Value = &pbgear.Balances_Dest_Id{ - Id: to_Balances_Id(fields), - } //Passthrough fields... +func to_repeated_uint32(fields []*registry.DecodedField) []uint32 { + out := make([]uint32, len(fields)) + for _, f := range fields { + out = append(out, f.Value.(uint32)) } - //============ - return out -} +} + -func to_Balances_Id(fields []*registry.DecodedField) *pbgear.Balances_Id { - out := &pbgear.Balances_Id{} - out.Value_0 = to_SpCoreCryptoAccountId32(fields) - return out +func to_Balances_Address32(fields []*registry.DecodedField) any { + out := &pbgear.Balances_Address32{} + out.Value_0 = to_repeated_uint32(fields[0].Value.([]*registry.DecodedField)) + return out +} + + +func to_Balances_CompactString(fields []*registry.DecodedField) any { + out := &pbgear.Balances_CompactString{} + out.Value = fields[0].Value.(string) + return out +} + + +func to_Balances_Dest(fields []*registry.DecodedField) any { + out := &pbgear.Balances_Dest{} + + + //=========== + switch { + + case matchFields(fields, []int64{0}): + out.Value = to_Balances_Id(fields) //Passthrough fields... + + case matchFields(fields, []int64{95}): + out.Value = to_Balances_Index(fields) //Passthrough fields... + + case matchFields(fields, []int64{13}): + out.Value = to_Balances_Raw(fields) //Passthrough fields... + + case matchFields(fields, []int64{1}): + out.Value = to_Balances_Address32(fields) //Passthrough fields... + + case matchFields(fields, []int64{96}): + out.Value = to_Balances_Address20(fields) //Passthrough fields... + } + //============ + return out } -func to_SpCoreCryptoAccountId32(fields []*registry.DecodedField) *pbgear.SpCoreCryptoAccountId32 { - out := &pbgear.SpCoreCryptoAccountId32{} - f := fields[0].Value.(registry.DecodedFields) - out.Value_0 = to_repeated_uint32(f) +func to_oneof_Balances_Value(fields []*registry.DecodedField) any {IdIndexRawAddress32Address20 + var out *&pbgear.Balances_Value + out.Value = to_oneof_Balances_Value(fields) //Passthrough fields... return out -} +} + + +func to_Balances_Id(fields []*registry.DecodedField) any { + out := &pbgear.Balances_Id{} + out.Value_0 = to_SpCoreCryptoAccountId32(fields[0].Value.([]*registry.DecodedField)) + return out +} + + +func to_Balances_Index(fields []*registry.DecodedField) any { + out := &pbgear.Balances_Index{} + out.Value_0 = to_CompactTupleNull(fields[0].Value.([]*registry.DecodedField)) + return out +} + + +func to_Balances_Raw(fields []*registry.DecodedField) any { + out := &pbgear.Balances_Raw{} + out.Value_0 = to_repeated_uint32(fields[0].Value.([]*registry.DecodedField)) + return out +} + + +func to_Balances_TransferKeepAliveCall(fields []*registry.DecodedField) any { + out := &pbgear.Balances_TransferKeepAliveCall{} + out.Dest = to_Balances_Dest(fields[0].Value.([]*registry.DecodedField)) + out.Value = to_Balances_CompactString(fields[1].Value.([]*registry.DecodedField)) + return out +} + + + +func to_CompactTupleNull(fields []*registry.DecodedField) any { + out := &pbgear.CompactTupleNull{} + out.Value = to_TupleNull(fields[0].Value.([]*registry.DecodedField)) + return out +} + + +func to_SpCoreCryptoAccountId32(fields []*registry.DecodedField) any { + out := &pbgear.SpCoreCryptoAccountId32{} + out.Value_0 = to_repeated_uint32(fields[0].Value.([]*registry.DecodedField)) + return out +} + + +func to_TupleNull(fields []*registry.DecodedField) any { + out := &pbgear.TupleNull{} + return out +} -func to_repeated_uint32(fields []*registry.DecodedField) []uint32 { - out := make([]uint32, len(fields)) - data := fields[0].Value.([]interface{}) - for _, f := range data { - u := uint8(f.(types.U8)) - out = append(out, uint32(u)) - } - return out -} func matchFields(fields []*registry.DecodedField, ids []int64) bool { if len(fields) != len(ids) { return false } for i, f := range fields { - if ids[i] != f.LookupIndex { + if ids[i] != f.LookupIndex{ return false } } return true -} +} \ No newline at end of file diff --git a/templates/gen_types.go.gotmpl b/templates/gen_types.go.gotmpl index da65c67..16d983f 100644 --- a/templates/gen_types.go.gotmpl +++ b/templates/gen_types.go.gotmpl @@ -3,31 +3,11 @@ package gen_types import ( "github.com/centrifuge/go-substrate-rpc-client/v4/registry" pbgear "github.com/streamingfast/firehose-gear/pb/sf/gear/metadata/type/v1" - "reflect" ) {{- $generator := . }} {{- $metadata := $generator.Metadata }} {{- $messages := $generator.Messages }} -{{- $idToField := $generator.IdToField }} - -type regEntry struct{ - fn func(fields []*registry.DecodedField) any - returnType reflect.Type -} - -var reg map[int64]*regEntry - -func init() { - reg = make(map[int64]*regEntry) - {{- range $k, $f:= $idToField}} - reg[{{$k}}] = ®Entry{ - fn: {{$f.ToFuncName $metadata}}, - {{/* returnType: "{{$f.ToGoTypeName $metadata}}", */}} - } - {{- end}} -} - {{ range $msg := $messages }} @@ -126,11 +106,9 @@ func {{ $field.ToFuncName $meta}}(fields []*registry.DecodedField) {{ $field.Ret {{- $goType := $field.ToGoTypeName $meta }} func {{ $field.ToFuncName $meta}}(fields []*registry.DecodedField) any { - =========== {{- range $t := $field.Types }} {{- $t.Name}} {{- end }} - ========== var out *{{ $field.OutputType $meta }} out.Value = {{ $field.ToFuncName $meta}}(fields) //Passthrough fields... return out