Skip to content

Commit

Permalink
api: v2alpha1: ATX improvements and sort order filter (#6059)
Browse files Browse the repository at this point in the history
* Add NumUnits to Activation message
* Change name of NodeId filter to SmesherId.
* Modify Id to allow multiple entries.
* Remove signature and previous_atx from Activation message
* Update stream filters to match list filters
* Add sort_order field to tx, layer and reward List requests
  • Loading branch information
kacpersaw committed Jun 21, 2024
1 parent a608922 commit 1be211b
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 29 deletions.
31 changes: 15 additions & 16 deletions api/grpcserver/v2alpha1/activation.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package v2alpha1

import (
"bytes"
"context"
"errors"
"io"
"slices"

"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
Expand Down Expand Up @@ -146,11 +148,12 @@ func (s *ActivationStreamService) Stream(
func toAtx(atx *types.ActivationTx) *spacemeshv2alpha1.Activation {
return &spacemeshv2alpha1.Activation{
Id: atx.ID().Bytes(),
SmesherId: atx.SmesherID.Bytes(),
PublishEpoch: atx.PublishEpoch.Uint32(),
PreviousAtx: atx.PrevATXID[:],
Coinbase: atx.Coinbase.String(),
Weight: atx.GetWeight(),
Height: atx.TickHeight(),
NumUnits: atx.TotalNumUnits(),
}
}

Expand Down Expand Up @@ -232,7 +235,7 @@ func (s *ActivationService) ActivationsCount(

func toAtxRequest(filter *spacemeshv2alpha1.ActivationStreamRequest) *spacemeshv2alpha1.ActivationRequest {
return &spacemeshv2alpha1.ActivationRequest{
NodeId: filter.NodeId,
SmesherId: filter.SmesherId,
Id: filter.Id,
Coinbase: filter.Coinbase,
StartEpoch: filter.StartEpoch,
Expand All @@ -245,17 +248,17 @@ func toAtxOperations(filter *spacemeshv2alpha1.ActivationRequest) (builder.Opera
if filter == nil {
return ops, nil
}
if filter.NodeId != nil {
if len(filter.SmesherId) > 0 {
ops.Filter = append(ops.Filter, builder.Op{
Field: builder.Smesher,
Token: builder.Eq,
Value: filter.NodeId,
Token: builder.In,
Value: filter.SmesherId,
})
}
if filter.Id != nil {
if len(filter.Id) > 0 {
ops.Filter = append(ops.Filter, builder.Op{
Field: builder.Id,
Token: builder.Eq,
Token: builder.In,
Value: filter.Id,
})
}
Expand Down Expand Up @@ -312,20 +315,16 @@ type atxsMatcher struct {
}

func (m *atxsMatcher) match(t *events.ActivationTx) bool {
if len(m.NodeId) > 0 {
var nodeId types.NodeID
copy(nodeId[:], m.NodeId)

if t.SmesherID != nodeId {
if len(m.SmesherId) > 0 {
idx := slices.IndexFunc(m.SmesherId, func(id []byte) bool { return bytes.Equal(id, t.SmesherID.Bytes()) })
if idx == -1 {
return false
}
}

if len(m.Id) > 0 {
var atxId types.ATXID
copy(atxId[:], m.Id)

if t.ID() != atxId {
idx := slices.IndexFunc(m.Id, func(id []byte) bool { return bytes.Equal(id, t.ID().Bytes()) })
if idx == -1 {
return false
}
}
Expand Down
14 changes: 7 additions & 7 deletions api/grpcserver/v2alpha1/activation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ func TestActivationService_List(t *testing.T) {
require.Equal(t, activations[3].ID().Bytes(), list.GetActivations()[0].GetId())
})

t.Run("nodeId", func(t *testing.T) {
t.Run("smesherId", func(t *testing.T) {
list, err := client.List(ctx, &spacemeshv2alpha1.ActivationRequest{
Limit: 1,
NodeId: activations[1].SmesherID.Bytes(),
Limit: 1,
SmesherId: [][]byte{activations[1].SmesherID.Bytes()},
})
require.NoError(t, err)
require.Equal(t, activations[1].ID().Bytes(), list.GetActivations()[0].GetId())
Expand All @@ -98,7 +98,7 @@ func TestActivationService_List(t *testing.T) {
t.Run("id", func(t *testing.T) {
list, err := client.List(ctx, &spacemeshv2alpha1.ActivationRequest{
Limit: 1,
Id: activations[3].ID().Bytes(),
Id: [][]byte{activations[3].ID().Bytes()},
})
require.NoError(t, err)
require.Equal(t, activations[3].ID().Bytes(), list.GetActivations()[0].GetId())
Expand Down Expand Up @@ -167,15 +167,15 @@ func TestActivationStreamService_Stream(t *testing.T) {
{
desc: "ID",
request: &spacemeshv2alpha1.ActivationStreamRequest{
Id: streamed[3].ID().Bytes(),
Id: [][]byte{streamed[3].ID().Bytes()},
StartEpoch: start,
Watch: true,
},
},
{
desc: "NodeID",
desc: "SmesherId",
request: &spacemeshv2alpha1.ActivationStreamRequest{
NodeId: streamed[3].SmesherID.Bytes(),
SmesherId: [][]byte{streamed[3].SmesherID.Bytes()},
StartEpoch: start,
Watch: true,
},
Expand Down
2 changes: 1 addition & 1 deletion api/grpcserver/v2alpha1/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func toLayerOperations(filter *spacemeshv2alpha1.LayerRequest) (builder.Operatio

ops.Modifiers = append(ops.Modifiers, builder.Modifier{
Key: builder.OrderBy,
Value: "l.id asc",
Value: "l.id " + filter.SortOrder.String(),
})

if filter.Limit != 0 {
Expand Down
2 changes: 1 addition & 1 deletion api/grpcserver/v2alpha1/reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func toRewardOperations(filter *spacemeshv2alpha1.RewardRequest) (builder.Operat

ops.Modifiers = append(ops.Modifiers, builder.Modifier{
Key: builder.OrderBy,
Value: "layer asc",
Value: "layer " + filter.SortOrder.String(),
})

if filter.Limit != 0 {
Expand Down
2 changes: 1 addition & 1 deletion api/grpcserver/v2alpha1/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func toTransactionOperations(filter *spacemeshv2alpha1.TransactionRequest) (buil

ops.Modifiers = append(ops.Modifiers, builder.Modifier{
Key: builder.OrderBy,
Value: "layer asc, id",
Value: fmt.Sprintf("layer %s, id", filter.SortOrder.String()),
})

if filter.Limit != 0 {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ require (
github.com/rs/cors v1.11.0
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/seehuhn/mt19937 v1.0.0
github.com/spacemeshos/api/release/go v1.46.0
github.com/spacemeshos/api/release/go v1.48.0
github.com/spacemeshos/economics v0.1.3
github.com/spacemeshos/fixed v0.1.1
github.com/spacemeshos/go-scale v1.2.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,8 @@ github.com/sourcegraph/annotate v0.0.0-20160123013949-f4cad6c6324d/go.mod h1:Udh
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e/go.mod h1:HuIsMU8RRBOtsCgI77wP899iHVBQpCmg4ErYMZB+2IA=
github.com/spacemeshos/api/release/go v1.46.0 h1:wm+VpTwP1mvCligEwgPo1ykDKY/omPCJtGYwatKLaE8=
github.com/spacemeshos/api/release/go v1.46.0/go.mod h1:8pxGN6/di8iBpQReiOgY+Cppi7bhJ+qJ3QiRQtJfoag=
github.com/spacemeshos/api/release/go v1.48.0 h1:RSg2D6ocHlsyV5XL97uuecQAXA4qHDmlDmYc8yejVOs=
github.com/spacemeshos/api/release/go v1.48.0/go.mod h1:8pxGN6/di8iBpQReiOgY+Cppi7bhJ+qJ3QiRQtJfoag=
github.com/spacemeshos/economics v0.1.3 h1:ACkq3mTebIky4Zwbs9SeSSRZrUCjU/Zk0wq9Z0BTh2A=
github.com/spacemeshos/economics v0.1.3/go.mod h1:FH7u0FzTIm6Kpk+X5HOZDvpkgNYBKclmH86rVwYaDAo=
github.com/spacemeshos/fixed v0.1.1 h1:N1y4SUpq1EV+IdJrWJwUCt1oBFzeru/VKVcBsvPc2Fk=
Expand Down

0 comments on commit 1be211b

Please sign in to comment.