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

[Merged by Bors] - api: v2alpha1: add total field to all List responses #6049

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
19 changes: 13 additions & 6 deletions api/grpcserver/v2alpha1/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,18 @@
ctx context.Context,
request *spacemeshv2alpha1.AccountRequest,
) (*spacemeshv2alpha1.AccountList, error) {
ops, err := toAccountOperations(request)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}

switch {
case request.Limit > 100:
return nil, status.Error(codes.InvalidArgument, "limit is capped at 100")
case request.Limit == 0:
return nil, status.Error(codes.InvalidArgument, "limit must be set to <= 100")
}

ops, err := toAccountOperations(request)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())

Check warning on line 64 in api/grpcserver/v2alpha1/account.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/account.go#L64

Added line #L64 was not covered by tests
}

rst := make([]*spacemeshv2alpha1.Account, 0, request.Limit)
if err := accounts.IterateAccountsOps(s.db, ops, func(account *types.Account) bool {
counterProjected, balanceProjected := s.conState.GetProjection(account.Address)
Expand All @@ -83,7 +83,14 @@
}); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &spacemeshv2alpha1.AccountList{Accounts: rst}, nil

ops.Modifiers = nil
count, err := accounts.CountAccountsByOps(s.db, ops)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())

Check warning on line 90 in api/grpcserver/v2alpha1/account.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/account.go#L90

Added line #L90 was not covered by tests
}

return &spacemeshv2alpha1.AccountList{Accounts: rst, Total: count}, nil
}

func toAccountOperations(filter *spacemeshv2alpha1.AccountRequest) (builder.Operations, error) {
Expand Down
1 change: 1 addition & 0 deletions api/grpcserver/v2alpha1/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func TestAccountService_List(t *testing.T) {
list, err := client.List(ctx, &spacemeshv2alpha1.AccountRequest{Limit: 100})
require.NoError(t, err)
require.Len(t, list.Accounts, len(accs))
require.Equal(t, len(accs), int(list.Total))
})

t.Run("address", func(t *testing.T) {
Expand Down
21 changes: 15 additions & 6 deletions api/grpcserver/v2alpha1/activation.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,25 +179,34 @@
ctx context.Context,
request *spacemeshv2alpha1.ActivationRequest,
) (*spacemeshv2alpha1.ActivationList, error) {
ops, err := toAtxOperations(request)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
// every full atx is ~1KB. 100 atxs is ~100KB.
switch {
case request.Limit > 100:
return nil, status.Error(codes.InvalidArgument, "limit is capped at 100")
case request.Limit == 0:
return nil, status.Error(codes.InvalidArgument, "limit must be set to <= 100")
}

ops, err := toAtxOperations(request)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())

Check warning on line 191 in api/grpcserver/v2alpha1/activation.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/activation.go#L191

Added line #L191 was not covered by tests
}

// every full atx is ~1KB. 100 atxs is ~100KB.
rst := make([]*spacemeshv2alpha1.Activation, 0, request.Limit)
if err := atxs.IterateAtxsOps(s.db, ops, func(atx *types.ActivationTx) bool {
rst = append(rst, toAtx(atx))
return true
}); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &spacemeshv2alpha1.ActivationList{Activations: rst}, nil

ops.Modifiers = nil
count, err := atxs.CountAtxsByOps(s.db, ops)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())

Check warning on line 206 in api/grpcserver/v2alpha1/activation.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/activation.go#L206

Added line #L206 was not covered by tests
}

return &spacemeshv2alpha1.ActivationList{Activations: rst, Total: count}, nil
}

func (s *ActivationService) ActivationsCount(
Expand Down
1 change: 1 addition & 0 deletions api/grpcserver/v2alpha1/activation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func TestActivationService_List(t *testing.T) {
list, err := client.List(ctx, &spacemeshv2alpha1.ActivationRequest{Limit: 100})
require.NoError(t, err)
require.Equal(t, len(activations), len(list.Activations))
require.Equal(t, len(activations), int(list.Total))
})

t.Run("coinbase", func(t *testing.T) {
Expand Down
18 changes: 12 additions & 6 deletions api/grpcserver/v2alpha1/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,18 @@
ctx context.Context,
request *spacemeshv2alpha1.LayerRequest,
) (*spacemeshv2alpha1.LayerList, error) {
ops, err := toLayerOperations(request)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}

switch {
case request.Limit > 100:
return nil, status.Error(codes.InvalidArgument, "limit is capped at 100")
case request.Limit == 0:
return nil, status.Error(codes.InvalidArgument, "limit must be set to <= 100")
}

ops, err := toLayerOperations(request)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())

Check warning on line 206 in api/grpcserver/v2alpha1/layer.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/layer.go#L206

Added line #L206 was not covered by tests
}

rst := make([]*spacemeshv2alpha1.Layer, 0, request.Limit)
var derr error
if err := layers.IterateLayersWithBlockOps(s.db, ops, func(layer *layers.Layer) bool {
Expand All @@ -218,7 +218,13 @@
return nil, derr
}

return &spacemeshv2alpha1.LayerList{Layers: rst}, nil
ops.Modifiers = nil
count, err := layers.CountLayersByOps(s.db, ops)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())

Check warning on line 224 in api/grpcserver/v2alpha1/layer.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/layer.go#L224

Added line #L224 was not covered by tests
}

return &spacemeshv2alpha1.LayerList{Layers: rst, Total: count}, nil
}

func toLayerOperations(filter *spacemeshv2alpha1.LayerRequest) (builder.Operations, error) {
Expand Down
1 change: 1 addition & 0 deletions api/grpcserver/v2alpha1/layer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func TestLayerService_List(t *testing.T) {
})
require.NoError(t, err)
require.Len(t, lrs, len(ls.Layers))
require.Equal(t, len(lrs), int(ls.Total))
})
}

Expand Down
9 changes: 8 additions & 1 deletion api/grpcserver/v2alpha1/reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,14 @@
}); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &spacemeshv2alpha1.RewardList{Rewards: rst}, nil

ops.Modifiers = nil
count, err := rewards.CountRewardsByOps(s.db, ops)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())

Check warning on line 194 in api/grpcserver/v2alpha1/reward.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/reward.go#L194

Added line #L194 was not covered by tests
}

return &spacemeshv2alpha1.RewardList{Rewards: rst, Total: count}, nil
}

func toRewardRequest(filter *spacemeshv2alpha1.RewardStreamRequest) *spacemeshv2alpha1.RewardRequest {
Expand Down
1 change: 1 addition & 0 deletions api/grpcserver/v2alpha1/reward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func TestRewardService_List(t *testing.T) {
list, err := client.List(ctx, &spacemeshv2alpha1.RewardRequest{Limit: 100})
require.NoError(t, err)
require.Len(t, rwds, len(list.Rewards))
require.Equal(t, len(rwds), int(list.Total))
})

t.Run("coinbase", func(t *testing.T) {
Expand Down
8 changes: 7 additions & 1 deletion api/grpcserver/v2alpha1/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,13 @@
return nil, status.Error(codes.Internal, err.Error())
}

return &spacemeshv2alpha1.TransactionList{Transactions: rst}, nil
ops.Modifiers = nil
count, err := transactions.CountTransactionsByOps(s.db, ops)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())

Check warning on line 142 in api/grpcserver/v2alpha1/transaction.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/transaction.go#L142

Added line #L142 was not covered by tests
}

return &spacemeshv2alpha1.TransactionList{Transactions: rst, Total: count}, nil
}

func (s *TransactionService) ParseTransaction(
Expand Down
1 change: 1 addition & 0 deletions api/grpcserver/v2alpha1/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func TestTransactionService_List(t *testing.T) {
list, err := client.List(ctx, &spacemeshv2alpha1.TransactionRequest{Limit: 100})
require.NoError(t, err)
require.Len(t, list.Transactions, len(txsList))
require.Equal(t, len(txsList), int(list.Total))
})

t.Run("address", func(t *testing.T) {
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.45.0
github.com/spacemeshos/api/release/go v1.46.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.45.0 h1:w8Ra0mjphMxiHMeFQoVn4AudsV+JO3NmpeKL25H74HA=
github.com/spacemeshos/api/release/go v1.45.0/go.mod h1:8pxGN6/di8iBpQReiOgY+Cppi7bhJ+qJ3QiRQtJfoag=
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/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
12 changes: 12 additions & 0 deletions sql/accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,18 @@ func Revert(db sql.Executor, after types.LayerID) error {
return nil
}

func CountAccountsByOps(db sql.Executor, operations builder.Operations) (count uint32, err error) {
_, err = db.Exec(
"SELECT COUNT(DISTINCT address) FROM accounts"+builder.FilterFrom(operations),
builder.BindingsFrom(operations),
func(stmt *sql.Statement) bool {
count = uint32(stmt.ColumnInt32(0))
return true
},
)
return
}

func IterateAccountsOps(
db sql.Executor,
operations builder.Operations,
Expand Down
13 changes: 13 additions & 0 deletions sql/layers/layers.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,19 @@ type Layer struct {
Block *types.Block
}

func CountLayersByOps(
db sql.Executor,
operations builder.Operations,
) (count uint32, err error) {
_, err = db.Exec(`select count(*) from layers l`+builder.FilterFrom(operations),
builder.BindingsFrom(operations),
func(stmt *sql.Statement) bool {
count = uint32(stmt.ColumnInt32(0))
return true
})
return
}

func IterateLayersWithBlockOps(
db sql.Executor,
operations builder.Operations,
Expand Down
13 changes: 13 additions & 0 deletions sql/rewards/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ func ListBySmesherId(db sql.Executor, smesherID types.NodeID) (rst []*types.Rewa
return ListByKey(db, nil, &smesherID)
}

func CountRewardsByOps(
db sql.Executor,
operations builder.Operations,
) (count uint32, err error) {
_, err = db.Exec(`select count(*) from rewards`+builder.FilterFrom(operations),
builder.BindingsFrom(operations),
func(stmt *sql.Statement) bool {
count = uint32(stmt.ColumnInt32(0))
return true
})
return
}

func IterateRewardsOps(
db sql.Executor,
operations builder.Operations,
Expand Down
15 changes: 15 additions & 0 deletions sql/transactions/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,21 @@ func TransactionInBlock(
return bid, rst, nil
}

func CountTransactionsByOps(
db sql.Executor,
operations builder.Operations,
) (count uint32, err error) {
_, err = db.Exec(`select COUNT(DISTINCT tx)
from transactions
left join transactions_results_addresses on id=tid`+builder.FilterFrom(operations),
builder.BindingsFrom(operations),
func(stmt *sql.Statement) bool {
count = uint32(stmt.ColumnInt32(0))
return true
})
return
}

func IterateTransactionsOps(
db sql.Executor,
operations builder.Operations,
Expand Down