Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix encoding of bytes in path params to be hex #6695

Merged
merged 4 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions api/node/client/client.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 10 additions & 5 deletions api/node/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client
import (
"bytes"
"context"
"encoding/hex"
"errors"
"fmt"
"net/http"
Expand Down Expand Up @@ -54,7 +55,7 @@ func NewNodeServiceClient(server string, logger *zap.Logger, cfg *Config) (*Node
}

func (s *NodeService) Atx(ctx context.Context, id types.ATXID) (*types.ActivationTx, error) {
resp, err := s.client.GetActivationAtxAtxIdWithResponse(ctx, id.Bytes())
resp, err := s.client.GetActivationAtxAtxIdWithResponse(ctx, hex.EncodeToString(id.Bytes()))
if err != nil {
return nil, err
}
Expand All @@ -69,7 +70,7 @@ func (s *NodeService) Atx(ctx context.Context, id types.ATXID) (*types.Activatio
}

func (s *NodeService) LastATX(ctx context.Context, nodeID types.NodeID) (*types.ActivationTx, error) {
resp, err := s.client.GetActivationLastAtxNodeIdWithResponse(ctx, nodeID.Bytes())
resp, err := s.client.GetActivationLastAtxNodeIdWithResponse(ctx, hex.EncodeToString(nodeID.Bytes()))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -199,7 +200,7 @@ func (s *NodeService) TotalWeight(ctx context.Context, epoch types.EpochID) (uin
}

func (s *NodeService) MinerWeight(ctx context.Context, epoch types.EpochID, node types.NodeID) (uint64, error) {
resp, err := s.client.GetHareWeightNodeIdEpochWithResponse(ctx, node.Bytes(), epoch.Uint32())
resp, err := s.client.GetHareWeightNodeIdEpochWithResponse(ctx, hex.EncodeToString(node.Bytes()), epoch.Uint32())
if err != nil {
return 0, fmt.Errorf("get miner weight: %w", err)
}
Expand Down Expand Up @@ -231,7 +232,7 @@ func (s *NodeService) Beacon(ctx context.Context, epoch types.EpochID) (types.Be
func (s *NodeService) Proposal(ctx context.Context, layer types.LayerID, node types.NodeID) (
*types.Proposal, uint64, error,
) {
resp, err := s.client.GetProposalLayerNodeWithResponse(ctx, layer.Uint32(), node.Bytes())
resp, err := s.client.GetProposalLayerNodeWithResponse(ctx, layer.Uint32(), hex.EncodeToString(node.Bytes()))
if err != nil {
return nil, 0, fmt.Errorf("get proposal layer: %w", err)
}
Expand Down Expand Up @@ -325,7 +326,11 @@ func (s *NodeService) Proposal(ctx context.Context, layer types.LayerID, node ty
func (s *NodeService) CalculateEligibilitySlotsFor(
ctx context.Context, node types.NodeID, epoch types.EpochID,
) (uint32, types.VRFPostIndex, error) {
resp, err := s.client.GetEligibilitySlotsNodeEpochWithResponse(ctx, node.Bytes(), models.EpochID(epoch))
resp, err := s.client.GetEligibilitySlotsNodeEpochWithResponse(
ctx,
hex.EncodeToString(node.Bytes()),
models.EpochID(epoch),
)
if err != nil {
return 0, 0, err
}
Expand Down
5 changes: 5 additions & 0 deletions api/node/models/components.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ components:
format: byte
minLength: 44
maxLength: 44
Bytes32Hex:
type: string
format: hex
minLength: 64
maxLength: 64
Beacon:
type: string
format: byte
Expand Down
3 changes: 3 additions & 0 deletions api/node/models/models.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions api/node/models/models.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,46 @@
package models

import (
"encoding/hex"
"fmt"

"github.com/spacemeshos/go-spacemesh/common/types"
)

func ParseBytes32Hex(b Bytes32Hex) ([32]byte, error) {
if len(b) != 64 {
return [32]byte{}, fmt.Errorf("invalid length: %d (expected 64)", len(b))
}
hash, err := hex.DecodeString(b)
if err != nil {
return [32]byte{}, fmt.Errorf("decoding hex: %w", err)
}
return [32]byte(hash), nil
}

func ParseNodeIDHex(id Bytes32Hex) (types.NodeID, error) {
b32, err := ParseBytes32Hex(id)
if err != nil {
return types.NodeID{}, err
}
return types.NodeID(b32), nil
}

func ParseNodeID(id Bytes32) (types.NodeID, error) {
if len(id) != len(types.NodeID{}) {
return types.NodeID{}, fmt.Errorf("invalid node ID length: %d", len(id))
}
return types.BytesToNodeID(id), nil
}

func ParseATXIDHex(id Bytes32Hex) (types.ATXID, error) {
b32, err := ParseBytes32Hex(id)
if err != nil {
return types.ATXID{}, err
}
return types.ATXID(b32), nil
}

func ParseATXID(id Bytes32) (types.ATXID, error) {
if len(id) != len(types.ATXID{}) {
return types.ATXID{}, fmt.Errorf("invalid atx ID length: %d", len(id))
Expand Down
Loading
Loading