Skip to content

Commit

Permalink
Merge 'feat/index-blocks' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
altergui committed Aug 23, 2024
2 parents 323d2c7 + e27cf8a commit 3856ac8
Show file tree
Hide file tree
Showing 29 changed files with 990 additions and 280 deletions.
75 changes: 63 additions & 12 deletions api/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,16 @@ func (a *API) accountElectionsListByPageHandler(_ *apirest.APIdata, ctx *httprou
return ErrMissingParameter
}

return a.sendElectionList(ctx, params)
list, err := a.electionList(params)
if err != nil {
// keep the odd legacy behaviour of sending an empty json "{}"" rather than a 404
if errors.Is(err, ErrPageNotFound) {
return marshalAndSend(ctx, struct{}{})
}
return err
}

return marshalAndSend(ctx, list)
}

// accountElectionsListByStatusAndPageHandler
Expand Down Expand Up @@ -398,7 +407,16 @@ func (a *API) accountElectionsListByStatusAndPageHandler(_ *apirest.APIdata, ctx
return ErrMissingParameter
}

return a.sendElectionList(ctx, params)
list, err := a.electionList(params)
if err != nil {
// keep the odd legacy behaviour of sending an empty json "{}"" rather than a 404
if errors.Is(err, ErrPageNotFound) {
return marshalAndSend(ctx, struct{}{})
}
return err
}

return marshalAndSend(ctx, list)
}

// accountElectionsCountHandler
Expand Down Expand Up @@ -458,7 +476,16 @@ func (a *API) tokenTransfersListHandler(_ *apirest.APIdata, ctx *httprouter.HTTP
return err
}

return a.sendTransfersList(ctx, params)
list, err := a.transfersList(params)
if err != nil {
// keep legacy behaviour of sending an empty list rather than a 404
if errors.Is(err, ErrPageNotFound) {
return marshalAndSend(ctx, emptyTransfersList())
}
return err
}

return marshalAndSend(ctx, list)
}

// tokenFeesHandler
Expand Down Expand Up @@ -490,7 +517,16 @@ func (a *API) tokenFeesHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext)
return ErrMissingParameter
}

return a.sendFeesList(ctx, params)
list, err := a.feesList(params)
if err != nil {
// keep legacy behaviour of sending an empty list rather than a 404
if errors.Is(err, ErrPageNotFound) {
return marshalAndSend(ctx, emptyFeesList())
}
return err
}

return marshalAndSend(ctx, list)
}

// tokenTransfersCountHandler
Expand Down Expand Up @@ -546,7 +582,17 @@ func (a *API) accountListByPageHandler(_ *apirest.APIdata, ctx *httprouter.HTTPC
if err != nil {
return err
}
return a.sendAccountList(ctx, params)

list, err := a.accountList(params)
if err != nil {
// keep legacy behaviour of sending an empty list rather than a 404
if errors.Is(err, ErrPageNotFound) {
return marshalAndSend(ctx, emptyAccountsList())
}
return err
}

return marshalAndSend(ctx, list)
}

// accountListHandler
Expand All @@ -570,33 +616,38 @@ func (a *API) accountListHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext
if err != nil {
return err
}
return a.sendAccountList(ctx, params)

list, err := a.accountList(params)
if err != nil {
return err
}

return marshalAndSend(ctx, list)
}

// sendAccountList produces a paginated AccountsList,
// and sends it marshalled over ctx.Send
// accountList produces a paginated AccountsList.
//
// Errors returned are always of type APIerror.
func (a *API) sendAccountList(ctx *httprouter.HTTPContext, params *AccountParams) error {
func (a *API) accountList(params *AccountParams) (*AccountsList, error) {
accounts, total, err := a.indexer.AccountList(
params.Limit,
params.Page*params.Limit,
params.AccountID,
)
if err != nil {
return ErrIndexerQueryFailed.WithErr(err)
return nil, ErrIndexerQueryFailed.WithErr(err)
}

pagination, err := calculatePagination(params.Page, params.Limit, total)
if err != nil {
return err
return nil, err
}

list := &AccountsList{
Accounts: accounts,
Pagination: pagination,
}
return marshalAndSend(ctx, list)
return list, nil
}

// parseAccountParams returns an AccountParams filled with the passed params
Expand Down
3 changes: 3 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ const (
ParamWithResults = "withResults"
ParamFinalResults = "finalResults"
ParamManuallyEnded = "manuallyEnded"
ParamChainId = "chainId"
ParamHash = "hash"
ParamProposerAddress = "proposerAddress"
ParamHeight = "height"
ParamReference = "reference"
ParamType = "type"
Expand Down
18 changes: 16 additions & 2 deletions api/api_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ type TransactionParams struct {
Type string `json:"type,omitempty"`
}

// BlockParams allows the client to filter blocks
type BlockParams struct {
PaginationParams
ChainID string `json:"chainId,omitempty"`
Hash string `json:"hash,omitempty"`
ProposerAddress string `json:"proposerAddress,omitempty"`
}

// FeesParams allows the client to filter fees
type FeesParams struct {
PaginationParams
Expand Down Expand Up @@ -267,8 +275,8 @@ type TransactionReference struct {

// TransactionsList is used to return a paginated list to the client
type TransactionsList struct {
Transactions []*indexertypes.Transaction `json:"transactions"`
Pagination *Pagination `json:"pagination"`
Transactions []*indexertypes.TransactionMetadata `json:"transactions"`
Pagination *Pagination `json:"pagination"`
}

// FeesList is used to return a paginated list to the client
Expand Down Expand Up @@ -439,3 +447,9 @@ type Block struct {
comettypes.Block `json:",inline"`
Hash types.HexBytes `json:"hash" `
}

// BlockList is used to return a paginated list to the client
type BlockList struct {
Blocks []*indexertypes.Block `json:"blocks"`
Pagination *Pagination `json:"pagination"`
}
Loading

0 comments on commit 3856ac8

Please sign in to comment.