Skip to content

Commit

Permalink
Merge pull request #6627 from spacemeshos/node-split-api-return-json-…
Browse files Browse the repository at this point in the history
…part1

return JSON from hare weight, total weight and beacon APIs
  • Loading branch information
poszu authored Jan 16, 2025
2 parents 837e7fa + 571b246 commit e771d2e
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 152 deletions.
73 changes: 59 additions & 14 deletions api/node/client/client.gen.go

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

50 changes: 21 additions & 29 deletions api/node/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,50 +158,42 @@ func (s *NodeService) GetHareMessage(ctx context.Context, layer types.LayerID, r
}

func (s *NodeService) TotalWeight(ctx context.Context, layer types.LayerID) (uint64, error) {
resp, err := s.client.GetHareTotalWeightLayer(ctx, uint32(layer))
resp, err := s.client.GetHareTotalWeightLayerWithResponse(ctx, uint32(layer))
if err != nil {
return 0, fmt.Errorf("get total weight: %w", err)
}

Check warning on line 164 in api/node/client/client.go

View check run for this annotation

Codecov / codecov/patch

api/node/client/client.go#L163-L164

Added lines #L163 - L164 were not covered by tests
if resp.StatusCode != http.StatusOK {
return 0, fmt.Errorf("unexpected status: %s", resp.Status)
}
bytes, err := io.ReadAll(resp.Body)
if err != nil {
return 0, fmt.Errorf("read all: %w", err)
switch resp.StatusCode() {
case http.StatusOK:
return resp.JSON200.Weight, nil
default:
return 0, fmt.Errorf("unexpected status: %q", resp.Status())

Check warning on line 169 in api/node/client/client.go

View check run for this annotation

Codecov / codecov/patch

api/node/client/client.go#L168-L169

Added lines #L168 - L169 were not covered by tests
}
return strconv.ParseUint(string(bytes), 10, 64)
}

func (s *NodeService) MinerWeight(ctx context.Context, layer types.LayerID, node types.NodeID) (uint64, error) {
resp, err := s.client.GetHareWeightNodeIdLayer(ctx, node.String(), uint32(layer))
resp, err := s.client.GetHareWeightNodeIdLayerWithResponse(ctx, node.String(), uint32(layer))
if err != nil {
return 0, fmt.Errorf("get miner weight: %w", err)
}

Check warning on line 177 in api/node/client/client.go

View check run for this annotation

Codecov / codecov/patch

api/node/client/client.go#L176-L177

Added lines #L176 - L177 were not covered by tests
if resp.StatusCode != http.StatusOK {
return 0, fmt.Errorf("unexpected status: %s", resp.Status)
}
bytes, err := io.ReadAll(resp.Body)
if err != nil {
return 0, fmt.Errorf("read all: %w", err)
switch resp.StatusCode() {
case http.StatusOK:
return resp.JSON200.Weight, nil
default:
return 0, fmt.Errorf("unexpected status: %q", resp.Status())

Check warning on line 182 in api/node/client/client.go

View check run for this annotation

Codecov / codecov/patch

api/node/client/client.go#L181-L182

Added lines #L181 - L182 were not covered by tests
}
return strconv.ParseUint(string(bytes), 10, 64)
}

func (s *NodeService) Beacon(ctx context.Context, epoch types.EpochID) (types.Beacon, error) {
v := types.Beacon{}
resp, err := s.client.GetHareBeaconEpoch(ctx, externalRef0.EpochID(epoch))
resp, err := s.client.GetHareBeaconEpochWithResponse(ctx, externalRef0.EpochID(epoch))
if err != nil {
return v, fmt.Errorf("get hare beacon: %w", err)
}
if resp.StatusCode != http.StatusOK {
return v, fmt.Errorf("unexpected status: %s", resp.Status)
return types.Beacon{}, fmt.Errorf("get hare beacon: %w", err)
}

Check warning on line 190 in api/node/client/client.go

View check run for this annotation

Codecov / codecov/patch

api/node/client/client.go#L189-L190

Added lines #L189 - L190 were not covered by tests
bytes, err := io.ReadAll(resp.Body)
if err != nil {
return v, fmt.Errorf("read all: %w", err)
switch resp.StatusCode() {
case http.StatusOK:
return types.Beacon(resp.JSON200.Beacon), nil
default:
return types.Beacon{}, fmt.Errorf("unexpected status: %q", resp.Status())

Check warning on line 195 in api/node/client/client.go

View check run for this annotation

Codecov / codecov/patch

api/node/client/client.go#L194-L195

Added lines #L194 - L195 were not covered by tests
}
copy(v[:], bytes)
return v, nil
}

func (s *NodeService) Proposal(ctx context.Context, layer types.LayerID, node types.NodeID) (
Expand Down Expand Up @@ -258,8 +250,8 @@ func (s *NodeService) CalculateEligibilitySlotsFor(
}
switch resp.StatusCode() {
case http.StatusOK:
return resp.JSON200.Slots, types.VRFPostIndex(resp.JSON200.Nonce), nil
default:
return 0, 0, fmt.Errorf("unexpected status: %s", resp.Status())
return 0, 0, fmt.Errorf("unexpected status: %q", resp.Status())

Check warning on line 255 in api/node/client/client.go

View check run for this annotation

Codecov / codecov/patch

api/node/client/client.go#L246-L255

Added lines #L246 - L255 were not covered by tests
}
return resp.JSON200.Slots, types.VRFPostIndex(resp.JSON200.Nonce), nil
}
2 changes: 1 addition & 1 deletion api/node/models/components.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ components:
format: uint32
LayerID:
type: integer
format: uint64
format: uint32
HareIter:
type: integer
format: uint8
Expand Down
2 changes: 1 addition & 1 deletion api/node/models/models.gen.go

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

57 changes: 37 additions & 20 deletions api/node/node_service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,20 @@ paths:
name: layer
required: true
schema:
type: integer
format: uint32
$ref: "models/components.yaml#/components/schemas/LayerID"
responses:
"200":
description: successfully found the total weight to return to client
description: successfully found the total weight
content:
application/octet-stream:
application/json:
schema:
type: string
format: binary
type: object
properties:
weight:
type: integer
format: uint64
required:
- weight
"204":
description: did not find a message to retrieve
/hare/weight/{node_id}/{layer}:
Expand All @@ -190,16 +194,20 @@ paths:
name: layer
required: true
schema:
type: integer
format: uint32
$ref: "models/components.yaml#/components/schemas/LayerID"
responses:
"200":
description: successfully found the total weight to return to client
description: successfully found the miner's weight
content:
application/octet-stream:
application/json:
schema:
type: string
format: binary
type: object
properties:
weight:
type: integer
format: uint64
required:
- weight
"204":
description: did not find a message to retrieve
/hare/beacon/{epoch}:
Expand All @@ -217,10 +225,19 @@ paths:
"200":
description: successfully found the epoch id
content:
application/octet-stream:
application/json:
schema:
type: string
format: binary
type: object
properties:
beacon:
type: array
items:
type: integer
format: uint8
minItems: 4
maxItems: 4
required:
- beacon
"204":
description: did not find a message to retrieve
/proposal/{layer}/{node}:
Expand Down Expand Up @@ -253,7 +270,7 @@ paths:
description: could not generate proposal
/eligibility/slots/{node}/{epoch}:
get:
summary: Get epoch eligibility slots for a given node id
summary: Get eligibility slots for a given node id in given epoch
tags:
- "proposals"
parameters:
Expand All @@ -275,15 +292,15 @@ paths:
schema:
type: object
properties:
Slots:
slots:
type: integer
format: uint32
Nonce:
nonce:
type: integer
format: uint64
required:
- Slots
- Nonce
- slots
- nonce
"500":
description: could not calculate eligibility slots

Loading

0 comments on commit e771d2e

Please sign in to comment.