Skip to content

Commit

Permalink
api: v2alpha1: add total field to all List responses (#6049)
Browse files Browse the repository at this point in the history
Add field with total count of records for accounts, atxs, layers, rewards and transactions.
  • Loading branch information
kacpersaw committed Jun 19, 2024
1 parent fa21ad1 commit 6d1bab1
Show file tree
Hide file tree
Showing 16 changed files with 121 additions and 23 deletions.
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 @@ func (s *AccountService) List(
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())
}

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 @@ func (s *AccountService) List(
}); 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())
}

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

func toAccountOperations(filter *spacemeshv2alpha1.AccountRequest) (builder.Operations, error) {
Expand Down
2 changes: 2 additions & 0 deletions api/grpcserver/v2alpha1/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,14 @@ func TestAccountService_List(t *testing.T) {
require.NoError(t, err)
fmt.Println(list)
require.Len(t, list.Accounts, 25)
require.Equal(t, len(accs), int(list.Total))
})

t.Run("all", func(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 @@ func (s *ActivationService) List(
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())
}

// 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())
}

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

func (s *ActivationService) ActivationsCount(
Expand Down
2 changes: 2 additions & 0 deletions api/grpcserver/v2alpha1/activation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ func TestActivationService_List(t *testing.T) {
})
require.NoError(t, err)
require.Len(t, list.Activations, 25)
require.Equal(t, len(activations), int(list.Total))
})

t.Run("all", func(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 @@ func (s *LayerService) List(
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())
}

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 @@ func (s *LayerService) List(
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())
}

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

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

t.Run("all", func(t *testing.T) {
Expand All @@ -80,6 +81,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 @@ func (s *RewardService) List(
}); 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())
}

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

func toRewardRequest(filter *spacemeshv2alpha1.RewardStreamRequest) *spacemeshv2alpha1.RewardRequest {
Expand Down
2 changes: 2 additions & 0 deletions api/grpcserver/v2alpha1/reward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ func TestRewardService_List(t *testing.T) {
})
require.NoError(t, err)
require.Len(t, list.Rewards, 25)
require.Equal(t, len(rwds), int(list.Total))
})

t.Run("all", func(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 @@ func (s *TransactionService) List(
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())
}

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

func (s *TransactionService) ParseTransaction(
Expand Down
2 changes: 2 additions & 0 deletions api/grpcserver/v2alpha1/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@ func TestTransactionService_List(t *testing.T) {
list, err := client.List(ctx, &spacemeshv2alpha1.TransactionRequest{Limit: 25, Offset: 50})
require.NoError(t, err)
require.Len(t, list.Transactions, 25)
require.Equal(t, len(txsList), int(list.Total))
})

t.Run("all", func(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

0 comments on commit 6d1bab1

Please sign in to comment.