erigon-pulse/cmd/lightclient/sentinel/proto/p2p/p2pgen/main.go
a 9f6fd488d2
[cl lightclient] - packet decoding & ssz code generation (#5558)
* added subscription to gossip

* added all handlers with logs

* disconnecting from peers with goodbye message received

* added time out for stream

* wip

* Remove unused

* remove extra structs

* status handler

* minor clean up

* add structs for altair light client

* begin writing out altair light client sync protocol to figure out what other structs are needed

* remove sszgen

* cleanup pt 1

* cleanup pt 2

* remove go 1.19 function

* less ambigious variable name

* run go fmt

* rename snappy_ssz to ssz_snappy to better align with wire name

* move more structs over

* poof set deadline

Co-authored-by: Enrique Jose Avila Asapche <eavilaasapche@gmail.com>
Co-authored-by: a <a@a.a>
2022-09-29 19:07:57 +02:00

97 lines
1.7 KiB
Go

package main
import (
"bytes"
"flag"
"go/format"
"os"
"path"
"strings"
"text/template"
"gopkg.in/yaml.v3"
)
var input string
var output string
type Spec struct {
Aliases map[string]string
Structs map[string][]Field
}
type Field struct {
Name string
Type string
Tags TagMap
}
type TagMap map[string]string
func (t TagMap) String() string {
sb := new(strings.Builder)
sb.WriteString("`")
for k, v := range t {
sb.WriteString(k)
sb.WriteString(":")
sb.WriteRune('"')
sb.WriteString(v)
sb.WriteString(`" `)
}
sb.WriteString("`")
return sb.String()
}
func main() {
flag.StringVar(&input, "i", "spec_p2p.yaml", "yaml file to read")
flag.StringVar(&output, "o", ".", "directory to output")
flag.Parse()
b, err := os.ReadFile(input)
if err != nil {
panic(err)
}
tmp := template.Must(template.New("p2pspec").Parse(tmpl))
s := &Spec{}
err = yaml.Unmarshal(b, s)
if err != nil {
panic(err)
}
buf := new(bytes.Buffer)
err = tmp.Execute(buf, s)
if err != nil {
panic(err)
}
src, err := format.Source(buf.Bytes())
if err != nil {
panic(err)
}
os.WriteFile(path.Join(output, "generated.go"), src, 0600)
}
const tmpl = `package p2p
//go:generate go run github.com/ferranbt/fastssz/sszgen -path generated.go -exclude-objs {{range $key, $val := .Aliases}}{{$key}},{{end}}Ignore
import (
"github.com/ledgerwatch/erigon/cmd/lightclient/sentinel/proto"
)
{{range $key, $val := .Aliases}}
type {{$key}} {{$val}}
{{end}}
{{range $key, $val := .Structs}}
type {{$key}} struct {
{{range $name, $field := $val}}
{{$field.Name}} {{$field.Type}} {{$field.Tags.String}}
{{end}}
}
func (typ *{{$key}}) Clone() proto.Packet {
return &{{$key}}{}
}
{{end}}
`