Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored Jan 6, 2025
2 parents d1e1853 + 4d3d3c2 commit a232746
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 102 deletions.
196 changes: 95 additions & 101 deletions api/docgen/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,198 +35,185 @@ 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
}

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
}
Expand All @@ -248,15 +235,15 @@ 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)
if f.Type == parent {
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
}
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion api/docgen/openrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit a232746

Please sign in to comment.