From 4d3d3c20330347a4fafc04ad538b4ae0832ed50b Mon Sep 17 00:00:00 2001 From: Oleg Kovalov Date: Mon, 23 Dec 2024 10:55:33 +0100 Subject: [PATCH 1/2] refactor(docgen): simplify init (#3997) --- api/docgen/examples.go | 196 ++++++++++++++++++++--------------------- api/docgen/openrpc.go | 2 +- 2 files changed, 96 insertions(+), 102 deletions(-) diff --git a/api/docgen/examples.go b/api/docgen/examples.go index 640cfe8ab3..572d5c3f04 100644 --- a/api/docgen/examples.go +++ b/api/docgen/examples.go @@ -35,171 +35,158 @@ import ( "github.com/celestiaorg/celestia-node/state" ) -//go:embed "exampledata/extendedHeader.json" -var exampleExtendedHeader string - -//go:embed "exampledata/samplingStats.json" -var exampleSamplingStats string - -//go:embed "exampledata/txResponse.json" -var exampleTxResponse string - -//go:embed "exampledata/resourceManagerStats.json" -var exampleResourceMngrStats string - -//go:embed "exampledata/blob.json" -var exampleBlob string - -//go:embed "exampledata/blobProof.json" -var exampleBlobProof string - -var ExampleValues = map[reflect.Type]interface{}{ - reflect.TypeOf(""): "string value", - reflect.TypeOf(uint64(42)): uint64(42), - reflect.TypeOf(uint32(42)): uint32(42), - reflect.TypeOf(int32(42)): int32(42), - reflect.TypeOf(int64(42)): int64(42), - reflect.TypeOf(42): 42, - reflect.TypeOf(byte(7)): byte(7), - reflect.TypeOf(float64(42)): float64(42), - reflect.TypeOf(true): true, - reflect.TypeOf([]byte{}): []byte("byte array"), - reflect.TypeOf(time.Duration(0)): time.Second, - reflect.TypeOf(node.Full): node.Full, - reflect.TypeOf(auth.Permission("admin")): auth.Permission("admin"), - reflect.TypeOf(byzantine.BadEncoding): byzantine.BadEncoding, - reflect.TypeOf((*fraud.Proof[*header.ExtendedHeader])(nil)).Elem(): byzantine.CreateBadEncodingProof( +var ( + //go:embed "exampledata/extendedHeader.json" + exampleExtendedHeader string + + //go:embed "exampledata/samplingStats.json" + exampleSamplingStats string + + //go:embed "exampledata/txResponse.json" + exampleTxResponse string + + //go:embed "exampledata/resourceManagerStats.json" + exampleResourceMngrStats string + + //go:embed "exampledata/blob.json" + exampleBlob string + + //go:embed "exampledata/blobProof.json" + exampleBlobProof string +) + +var exampleValues = map[reflect.Type]any{} + +func add(v any) { + typ := reflect.TypeOf(v) + exampleValues[typ] = v +} + +func init() { + add("string value") + add(uint64(42)) + add(uint32(42)) + add(int32(42)) + add(int64(42)) + add(42) + add(byte(7)) + add(float64(42)) + add(float64(42)) + add(true) + add([]byte("byte array")) + add(time.Second) + add(node.Full) + add(auth.Permission("admin")) + add(byzantine.BadEncoding) + + // TODO: this case requires more debugging, simple to leave it as it was. + exampleValues[reflect.TypeOf((*fraud.Proof[*header.ExtendedHeader])(nil)).Elem()] = byzantine.CreateBadEncodingProof( []byte("bad encoding proof"), 42, &byzantine.ErrByzantine{ Index: 0, - Axis: rsmt2d.Axis(0), Shares: []*byzantine.ShareWithProof{}, + Axis: rsmt2d.Axis(0), }, - ), - reflect.TypeOf((*error)(nil)).Elem(): errors.New("error"), - reflect.TypeOf(state.Balance{}): state.Balance{Amount: sdk.NewInt(42), Denom: "utia"}, -} + ) -func init() { - addToExampleValues(share.EmptyEDS()) - addr, err := sdk.AccAddressFromBech32("celestia1377k5an3f94v6wyaceu0cf4nq6gk2jtpc46g7h") - if err != nil { - panic(err) - } - addToExampleValues(addr) - ExampleValues[reflect.TypeOf((*sdk.Address)(nil)).Elem()] = addr + add(errors.New("error")) + add(state.Balance{Amount: sdk.NewInt(42), Denom: "utia"}) + add(share.EmptyEDS()) + add(rsmt2d.Row) + add(network.Connected) + add(network.ReachabilityPrivate) - valAddr, err := sdk.ValAddressFromBech32("celestiavaloper1q3v5cugc8cdpud87u4zwy0a74uxkk6u4q4gx4p") - if err != nil { - panic(err) - } - addToExampleValues(valAddr) + addr := must(sdk.AccAddressFromBech32("celestia1377k5an3f94v6wyaceu0cf4nq6gk2jtpc46g7h")) + add(addr) + add(state.Address{Address: addr}) + exampleValues[reflect.TypeOf((*sdk.Address)(nil)).Elem()] = addr - addToExampleValues(state.Address{Address: addr}) + valAddr := must(sdk.ValAddressFromBech32("celestiavaloper1q3v5cugc8cdpud87u4zwy0a74uxkk6u4q4gx4p")) + add(valAddr) var txResponse *state.TxResponse - err = json.Unmarshal([]byte(exampleTxResponse), &txResponse) + err := json.Unmarshal([]byte(exampleTxResponse), &txResponse) if err != nil { panic(err) } + add(txResponse) var samplingStats das.SamplingStats err = json.Unmarshal([]byte(exampleSamplingStats), &samplingStats) if err != nil { panic(err) } + add(samplingStats) var extendedHeader *header.ExtendedHeader err = json.Unmarshal([]byte(exampleExtendedHeader), &extendedHeader) if err != nil { panic(err) } + add(extendedHeader) var resourceMngrStats rcmgr.ResourceManagerStat err = json.Unmarshal([]byte(exampleResourceMngrStats), &resourceMngrStats) if err != nil { panic(err) } + add(resourceMngrStats) var exBlob *blob.Blob err = json.Unmarshal([]byte(exampleBlob), &exBlob) if err != nil { panic(err) } + add(exBlob) + add(exBlob.Blob) var blobProof *blob.Proof err = json.Unmarshal([]byte(exampleBlobProof), &blobProof) if err != nil { panic(err) } - - addToExampleValues(exBlob) - addToExampleValues(exBlob.Blob) - addToExampleValues(blobProof) - addToExampleValues(txResponse) - addToExampleValues(samplingStats) - addToExampleValues(extendedHeader) - addToExampleValues(resourceMngrStats) + add(blobProof) mathInt, _ := math.NewIntFromString("42") - addToExampleValues(mathInt) - - addToExampleValues(network.Connected) - addToExampleValues(network.ReachabilityPrivate) + add(mathInt) pID := protocol.ID("/celestia/mocha/ipfs/bitswap") - addToExampleValues(pID) + add(pID) peerID := peer.ID("12D3KooWPRb5h3g9MH7sx9qfbSQZG5cXv1a2Qs3o4aW5YmmzPq82") - addToExampleValues(peerID) + add(peerID) ma, _ := multiaddr.NewMultiaddr("/ip6/::1/udp/2121/quic-v1") addrInfo := peer.AddrInfo{ ID: peerID, Addrs: []multiaddr.Multiaddr{ma}, } - addToExampleValues(addrInfo) + add(addrInfo) - commitment, err := base64.StdEncoding.DecodeString("aHlbp+J9yub6hw/uhK6dP8hBLR2mFy78XNRRdLf2794=") - if err != nil { - panic(err) - } - addToExampleValues(blob.Commitment(commitment)) + commitment := must(base64.StdEncoding.DecodeString("aHlbp+J9yub6hw/uhK6dP8hBLR2mFy78XNRRdLf2794=")) + add(blob.Commitment(commitment)) // randomly generated namespace that's used in the blob example above // (AAAAAAAAAAAAAAAAAAAAAAAAAAAAAMJ/xGlNMdE=) - namespace, err := libshare.NewV0Namespace([]byte{0xc2, 0x7f, 0xc4, 0x69, 0x4d, 0x31, 0xd1}) - if err != nil { - panic(err) - } - addToExampleValues(namespace) + namespace := must(libshare.NewV0Namespace([]byte{0xc2, 0x7f, 0xc4, 0x69, 0x4d, 0x31, 0xd1})) + add(namespace) hashStr := "453D0BC3CB88A2ED6F2E06021383B22C72D25D7741AE51B4CAE1AD34D72A3F07" - hash, err := hex.DecodeString(hashStr) - if err != nil { - panic(err) - } - addToExampleValues(libhead.Hash(hash)) + hash := must(hex.DecodeString(hashStr)) + add(libhead.Hash(hash)) - txConfig := state.NewTxConfig( + add(state.NewTxConfig( state.WithGasPrice(0.002), state.WithGas(142225), state.WithKeyName("my_celes_key"), state.WithSignerAddress("celestia1pjcmwj8w6hyr2c4wehakc5g8cfs36aysgucx66"), state.WithFeeGranterAddress("celestia1hakc56ax66ypjcmwj8w6hyr2c4g8cfs3wesguc"), - ) - addToExampleValues(txConfig) - - addToExampleValues(rsmt2d.Row) -} - -func addToExampleValues(v interface{}) { - ExampleValues[reflect.TypeOf(v)] = v + )) } -func ExampleValue(t, parent reflect.Type) (interface{}, error) { - v, ok := ExampleValues[t] +func exampleValue(t, parent reflect.Type) (any, error) { + v, ok := exampleValues[t] if ok { return v, nil } @@ -207,26 +194,26 @@ func ExampleValue(t, parent reflect.Type) (interface{}, error) { switch t.Kind() { case reflect.Slice: out := reflect.New(t).Elem() - val, err := ExampleValue(t.Elem(), t) + val, err := exampleValue(t.Elem(), t) if err != nil { return nil, err } out = reflect.Append(out, reflect.ValueOf(val)) return out.Interface(), nil case reflect.Chan: - return ExampleValue(t.Elem(), nil) + return exampleValue(t.Elem(), nil) case reflect.Struct: es, err := exampleStruct(t, parent) if err != nil { return nil, err } v := reflect.ValueOf(es).Elem().Interface() - ExampleValues[t] = v + exampleValues[t] = v return v, nil case reflect.Array: out := reflect.New(t).Elem() for i := 0; i < t.Len(); i++ { - val, err := ExampleValue(t.Elem(), t) + val, err := exampleValue(t.Elem(), t) if err != nil { return nil, err } @@ -248,7 +235,7 @@ func ExampleValue(t, parent reflect.Type) (interface{}, error) { return nil, fmt.Errorf("failed to retrieve example value for type: %s on parent '%s')", t, parent) } -func exampleStruct(t, parent reflect.Type) (interface{}, error) { +func exampleStruct(t, parent reflect.Type) (any, error) { ns := reflect.New(t) for i := 0; i < t.NumField(); i++ { f := t.Field(i) @@ -256,7 +243,7 @@ func exampleStruct(t, parent reflect.Type) (interface{}, error) { continue } if cases.Title(language.Und, cases.NoLower).String(f.Name) == f.Name { - val, err := ExampleValue(f.Type, t) + val, err := exampleValue(f.Type, t) if err != nil { return nil, err } @@ -266,3 +253,10 @@ func exampleStruct(t, parent reflect.Type) (interface{}, error) { return ns.Interface(), nil } + +func must[T any](v T, err error) T { + if err != nil { + panic(err) + } + return v +} diff --git a/api/docgen/openrpc.go b/api/docgen/openrpc.go index a5e52e7ee1..86d901372f 100644 --- a/api/docgen/openrpc.go +++ b/api/docgen/openrpc.go @@ -185,7 +185,7 @@ func NewOpenRPCDocument(comments, permissions Comments) *go_openrpc_reflect.Docu } appReflector.FnSchemaExamples = func(ty reflect.Type) (examples *meta_schema.Examples, err error) { - v, err := ExampleValue(ty, ty) // This isn't ideal, but seems to work well enough. + v, err := exampleValue(ty, ty) // This isn't ideal, but seems to work well enough. if err != nil { fmt.Println(err) } From 87ece3a84e2ceb301edff6c4d0c8647c006ee28c Mon Sep 17 00:00:00 2001 From: rene <41963722+renaynay@users.noreply.github.com> Date: Tue, 7 Jan 2025 14:07:28 +0100 Subject: [PATCH 2/2] fix(nodebuilder/da): remove random print (#4029) --- nodebuilder/da/service.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/nodebuilder/da/service.go b/nodebuilder/da/service.go index aded1f0ea9..c7e403fbff 100644 --- a/nodebuilder/da/service.go +++ b/nodebuilder/da/service.go @@ -4,7 +4,6 @@ import ( "context" "encoding/binary" "encoding/json" - "fmt" "strings" logging "github.com/ipfs/go-log/v2" @@ -256,7 +255,6 @@ func (s *Service) Validate( // invalid proof") but analysis of the code in celestia-node implies this should never happen - // maybe it's caused by openrpc? there is no way of gently handling errors here, but returned // value is fine for us - fmt.Println("proof", proofs[i] == nil, "commitment", commitment == nil) isIncluded, _ := s.blobServ.Included(ctx, height, ns, proofs[i], commitment) included[i] = isIncluded }