Skip to content

Commit

Permalink
Merge branch 'feat/api-counts' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
altergui committed Aug 7, 2024
2 parents c9cf3e0 + 2491a75 commit e9744d3
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 53 deletions.
30 changes: 19 additions & 11 deletions api/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,27 @@ func (a *API) accountHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext) er
return ErrGettingSIK.WithErr(err)
}

_, transfersCount, err := a.indexer.TokenTransfersList(1, 0, hex.EncodeToString(addr.Bytes()), "", "")
if err != nil {
return ErrCantFetchTokenTransfers.WithErr(err)
}

_, feesCount, err := a.indexer.TokenFeesList(1, 0, "", "", hex.EncodeToString(addr.Bytes()))
if err != nil {
return ErrCantFetchTokenFees.WithErr(err)
}

var data []byte
if data, err = json.Marshal(Account{
Address: addr.Bytes(),
Nonce: acc.GetNonce(),
Balance: acc.GetBalance(),
ElectionIndex: acc.GetProcessIndex(),
InfoURL: acc.GetInfoURI(),
Metadata: accMetadata,
SIK: types.HexBytes(sik),
Address: addr.Bytes(),
Nonce: acc.GetNonce(),
Balance: acc.GetBalance(),
ElectionIndex: acc.GetProcessIndex(),
TransfersCount: transfersCount,
FeesCount: feesCount,
InfoURL: acc.GetInfoURI(),
Metadata: accMetadata,
SIK: types.HexBytes(sik),
}); err != nil {
return err
}
Expand Down Expand Up @@ -571,10 +583,6 @@ func (a *API) accountListHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext
//
// Errors returned are always of type APIerror.
func (a *API) sendAccountList(ctx *httprouter.HTTPContext, params *AccountParams) error {
if params.AccountID != "" && !a.indexer.AccountExists(params.AccountID) {
return ErrAccountNotFound
}

accounts, total, err := a.indexer.AccountList(
params.Limit,
params.Page*params.Limit,
Expand Down
18 changes: 10 additions & 8 deletions api/api_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,16 @@ type ChainInfo struct {
}

type Account struct {
Address types.HexBytes `json:"address" `
Nonce uint32 `json:"nonce"`
Balance uint64 `json:"balance"`
ElectionIndex uint32 `json:"electionIndex"`
InfoURL string `json:"infoURL,omitempty"`
Token *uuid.UUID `json:"token,omitempty" swaggerignore:"true"`
Metadata *AccountMetadata `json:"metadata,omitempty"`
SIK types.HexBytes `json:"sik"`
Address types.HexBytes `json:"address" `
Nonce uint32 `json:"nonce"`
Balance uint64 `json:"balance"`
ElectionIndex uint32 `json:"electionIndex"`
TransfersCount uint64 `json:"transfersCount,omitempty"`
FeesCount uint64 `json:"feesCount,omitempty"`
InfoURL string `json:"infoURL,omitempty"`
Token *uuid.UUID `json:"token,omitempty" swaggerignore:"true"`
Metadata *AccountMetadata `json:"metadata,omitempty"`
SIK types.HexBytes `json:"sik"`
}

type AccountsList struct {
Expand Down
4 changes: 0 additions & 4 deletions api/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,6 @@ func (a *API) organizationListByFilterAndPageHandler(msg *apirest.APIdata, ctx *
//
// Errors returned are always of type APIerror.
func (a *API) sendOrganizationList(ctx *httprouter.HTTPContext, params *OrganizationParams) error {
if params.OrganizationID != "" && !a.indexer.EntityExists(params.OrganizationID) {
return ErrOrgNotFound
}

orgs, total, err := a.indexer.EntityList(
params.Limit,
params.Page*params.Limit,
Expand Down
4 changes: 0 additions & 4 deletions api/elections.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,6 @@ func (a *API) electionListHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContex
//
// Errors returned are always of type APIerror.
func (a *API) sendElectionList(ctx *httprouter.HTTPContext, params *ElectionParams) error {
if params.ElectionID != "" && !a.indexer.ProcessExists(params.ElectionID) {
return ErrElectionNotFound
}

if params.OrganizationID != "" && !a.indexer.EntityExists(params.OrganizationID) {
return ErrOrgNotFound
}
Expand Down
1 change: 1 addition & 0 deletions api/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,5 @@ var (
ErrGettingSIK = apirest.APIerror{Code: 5031, HTTPstatus: apirest.HTTPstatusInternalErr, Err: fmt.Errorf("error getting SIK")}
ErrCensusBuild = apirest.APIerror{Code: 5032, HTTPstatus: apirest.HTTPstatusInternalErr, Err: fmt.Errorf("error building census")}
ErrIndexerQueryFailed = apirest.APIerror{Code: 5033, HTTPstatus: apirest.HTTPstatusInternalErr, Err: fmt.Errorf("indexer query failed")}
ErrCantFetchTokenFees = apirest.APIerror{Code: 5034, HTTPstatus: apirest.HTTPstatusInternalErr, Err: fmt.Errorf("cannot fetch token fees")}
)
20 changes: 0 additions & 20 deletions test/apierror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,22 +176,6 @@ func TestAPIerrorWithQuery(t *testing.T) {
args: args{"GET", nil, []string{"accounts"}, "page=1234"},
want: api.ErrPageNotFound,
},
{
args: args{"GET", nil, []string{"accounts"}, "accountId=0123456789"},
want: api.ErrAccountNotFound,
},
{
args: args{"GET", nil, []string{"accounts"}, "accountId=0123456789&page=1234"},
want: api.ErrAccountNotFound,
},
{
args: args{"GET", nil, []string{"elections"}, "electionId=0123456789"},
want: api.ErrElectionNotFound,
},
{
args: args{"GET", nil, []string{"elections"}, "electionId=0123456789&page=1234"},
want: api.ErrElectionNotFound,
},
{
args: args{"GET", nil, []string{"elections"}, "organizationId=0123456789"},
want: api.ErrOrgNotFound,
Expand Down Expand Up @@ -226,10 +210,6 @@ func TestAPIerrorWithQuery(t *testing.T) {
// args: args{"GET", nil, []string{"chain", "transactions"}, "type=FOOBAR"},
// want: api.ErrParamTypeInvalid,
// },
{
args: args{"GET", nil, []string{"chain", "organizations"}, "organizationId=0123456789"},
want: api.ErrOrgNotFound,
},
{
args: args{"GET", nil, []string{"chain", "fees"}, "accountId=0123456789"},
want: api.ErrAccountNotFound,
Expand Down
7 changes: 5 additions & 2 deletions vochain/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,9 +845,12 @@ func (idx *Indexer) AccountList(limit, offset int, accountID string) ([]*indexer
return list, uint64(results[0].TotalCount), nil
}

// AccountExists returns whether the passed accountID matches at least one row in the db.
// accountID is a partial or full hex string.
// AccountExists returns whether the passed accountID exists in the db.
// If passed arg is not the full hex string, returns false (i.e. no substring matching)
func (idx *Indexer) AccountExists(accountID string) bool {
if len(accountID) != 40 {
return false
}
_, count, err := idx.AccountList(1, 0, accountID)
if err != nil {
log.Errorw(err, "indexer query failed")
Expand Down
14 changes: 10 additions & 4 deletions vochain/indexer/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,12 @@ func (idx *Indexer) ProcessList(limit, offset int, entityID string, processID st
return list, uint64(results[0].TotalCount), nil
}

// ProcessExists returns whether the passed processID matches at least one row in the db.
// processID is a partial or full hex string.
// ProcessExists returns whether the passed processID exists in the db.
// If passed arg is not the full hex string, returns false (i.e. no substring matching)
func (idx *Indexer) ProcessExists(processID string) bool {
if len(processID) != 64 {
return false
}
_, count, err := idx.ProcessList(1, 0, "", processID, 0, 0, 0, nil, nil, nil)
if err != nil {
log.Errorw(err, "indexer query failed")
Expand Down Expand Up @@ -138,9 +141,12 @@ func (idx *Indexer) EntityList(limit, offset int, entityID string) ([]indexertyp
return list, uint64(results[0].TotalCount), nil
}

// EntityExists returns whether the passed entityID matches at least one row in the db.
// entityID is a partial or full hex string.
// EntityExists returns whether the passed entityID exists in the db.
// If passed arg is not the full hex string, returns false (i.e. no substring matching)
func (idx *Indexer) EntityExists(entityID string) bool {
if len(entityID) != 40 {
return false
}
_, count, err := idx.EntityList(1, 0, entityID)
if err != nil {
log.Errorw(err, "indexer query failed")
Expand Down

0 comments on commit e9744d3

Please sign in to comment.