Skip to content

Commit

Permalink
refactor: better search result types
Browse files Browse the repository at this point in the history
See: BEDS-934
  • Loading branch information
LuccaBitfly committed Nov 6, 2024
1 parent 3498031 commit f9a5f4d
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 99 deletions.
15 changes: 9 additions & 6 deletions backend/pkg/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,9 @@ func TestInternalSearchHandler(t *testing.T) {
}`)).Expect().Status(http.StatusOK).JSON().Decode(&resp)

assert.NotEqual(t, 0, len(resp.Data), "response data should not be empty")
assert.NotNil(t, resp.Data[0].NumValue, "validator index should not be nil")
assert.Equal(t, uint64(5), *resp.Data[0].NumValue, "validator index should be 5")
validatorByIndex, ok := resp.Data[0].Value.(api_types.SearchValidator)
assert.True(t, ok, "response data should be of type SearchValidator")
assert.Equal(t, uint64(5), validatorByIndex.Index, "validator index should be 5")

// search for validator by pubkey
resp = api_types.InternalPostSearchResponse{}
Expand All @@ -324,8 +325,9 @@ func TestInternalSearchHandler(t *testing.T) {
}`)).Expect().Status(http.StatusOK).JSON().Decode(&resp)

assert.NotEqual(t, 0, len(resp.Data), "response data should not be empty")
assert.NotNil(t, resp.Data[0].NumValue, "validator index should not be nil")
assert.Equal(t, uint64(5), *resp.Data[0].NumValue, "validator index should be 5")
validatorsByWithdrawalCredential, ok := resp.Data[0].Value.(api_types.SearchValidator)
assert.True(t, ok, "response data should be of type SearchValidator")
assert.Equal(t, uint64(5), validatorsByWithdrawalCredential.Index, "validator index should be 5")

// search for validator by withdawal address
resp = api_types.InternalPostSearchResponse{}
Expand All @@ -349,8 +351,9 @@ func TestInternalSearchHandler(t *testing.T) {
}`)).Expect().Status(http.StatusOK).JSON().Decode(&resp)

assert.NotEqual(t, 0, len(resp.Data), "response data should not be empty")
assert.NotNil(t, resp.Data[0].NumValue, "validator index should not be nil")
assert.Greater(t, *resp.Data[0].NumValue, uint64(0), "returned number of validators should be greater than 0")
validatorsByWithdrawalAddress, ok := resp.Data[0].Value.(api_types.SearchValidatorsByWithdrwalCredential)
assert.True(t, ok, "response data should be of type SearchValidator")
assert.Greater(t, validatorsByWithdrawalAddress.ValidatorCount, uint64(0), "returned number of validators should be greater than 0")
}

func TestPublicAndSharedDashboards(t *testing.T) {
Expand Down
21 changes: 11 additions & 10 deletions backend/pkg/api/data_access/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (d *DataAccessService) GetSearchValidatorByIndex(ctx context.Context, chain
if int(index) < len(validatorMapping.ValidatorPubkeys) {
return &t.SearchValidator{
Index: index,
PublicKey: hexutil.MustDecode(validatorMapping.ValidatorPubkeys[index]),
PublicKey: validatorMapping.ValidatorPubkeys[index],
}, nil
}

Expand All @@ -46,7 +46,7 @@ func (d *DataAccessService) GetSearchValidatorByPublicKey(ctx context.Context, c
if index, found := validatorMapping.ValidatorIndices[b]; found {
return &t.SearchValidator{
Index: index,
PublicKey: publicKey,
PublicKey: b,
}, nil
}

Expand All @@ -56,13 +56,14 @@ func (d *DataAccessService) GetSearchValidatorByPublicKey(ctx context.Context, c
func (d *DataAccessService) GetSearchValidatorsByDepositAddress(ctx context.Context, chainId uint64, address []byte) (*t.SearchValidatorsByDepositAddress, error) {
// TODO: implement handling of chainid
ret := &t.SearchValidatorsByDepositAddress{
Address: address,
Address: hexutil.Encode(address),
}
err := db.ReaderDb.GetContext(ctx, &ret.Count, "select count(validatorindex) from validators where pubkey in (select publickey from eth1_deposits where from_address = $1);", address)
err := db.ReaderDb.GetContext(ctx, &ret.ValidatorCount, `
select count(validatorindex) from validators where pubkey in (select publickey from eth1_deposits where from_address = $1);`, address)
if err != nil {
return nil, err
}
if ret.Count == 0 {
if ret.ValidatorCount == 0 {
return nil, ErrNotFound
}
return ret, nil
Expand All @@ -77,13 +78,13 @@ func (d *DataAccessService) GetSearchValidatorsByDepositEnsName(ctx context.Cont
func (d *DataAccessService) GetSearchValidatorsByWithdrawalCredential(ctx context.Context, chainId uint64, credential []byte) (*t.SearchValidatorsByWithdrwalCredential, error) {
// TODO: implement handling of chainid
ret := &t.SearchValidatorsByWithdrwalCredential{
WithdrawalCredential: credential,
WithdrawalCredential: hexutil.Encode(credential),
}
err := db.ReaderDb.GetContext(ctx, &ret.Count, "select count(validatorindex) from validators where withdrawalcredentials = $1;", credential)
err := db.ReaderDb.GetContext(ctx, &ret.ValidatorCount, "select count(validatorindex) from validators where withdrawalcredentials = $1;", credential)
if err != nil {
return nil, err
}
if ret.Count == 0 {
if ret.ValidatorCount == 0 {
return nil, ErrNotFound
}
return ret, nil
Expand All @@ -100,11 +101,11 @@ func (d *DataAccessService) GetSearchValidatorsByGraffiti(ctx context.Context, c
ret := &t.SearchValidatorsByGraffiti{
Graffiti: graffiti,
}
err := db.ReaderDb.GetContext(ctx, &ret.Count, "select count(distinct proposer) from blocks where graffiti_text = $1;", graffiti)
err := db.ReaderDb.GetContext(ctx, &ret.ValidatorCount, "select count(distinct proposer) from blocks where graffiti_text = $1;", graffiti)
if err != nil {
return nil, err
}
if ret.Count == 0 {
if ret.ValidatorCount == 0 {
return nil, ErrNotFound
}
return ret, nil
Expand Down
86 changes: 36 additions & 50 deletions backend/pkg/api/handlers/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,14 @@ func (h *HandlerService) handleSearchValidatorByIndex(ctx context.Context, input
return nil, err
}
result, err := h.daService.GetSearchValidatorByIndex(ctx, chainId, index)
if err != nil {
if err != nil || result == nil {
return nil, err
}

return &types.SearchResult{
Type: string(validatorByIndex),
ChainId: chainId,
HashValue: "0x" + hex.EncodeToString(result.PublicKey),
NumValue: &result.Index,
Type: string(validatorByIndex),
ChainId: chainId,
Value: *result,
}, nil
}

Expand All @@ -172,15 +171,14 @@ func (h *HandlerService) handleSearchValidatorByPublicKey(ctx context.Context, i
return nil, err
}
result, err := h.daService.GetSearchValidatorByPublicKey(ctx, chainId, publicKey)
if err != nil {
if err != nil || result == nil {
return nil, err
}

return &types.SearchResult{
Type: string(validatorByPublicKey),
ChainId: chainId,
HashValue: "0x" + hex.EncodeToString(result.PublicKey),
NumValue: &result.Index,
Type: string(validatorByPublicKey),
ChainId: chainId,
Value: *result,
}, nil
}

Expand All @@ -192,18 +190,14 @@ func (h *HandlerService) handleSearchValidatorList(ctx context.Context, input st
return nil, nil // return no error as to not disturb the other search types
}
validators, err := h.daService.GetValidatorsFromSlices(ctx, indices, pubkeys)
if err != nil {
if err != nil || validators == nil || len(validators) == 0 {
return nil, err
}
if len(validators) == 0 {
return nil, nil
}

var resultLength uint64 = uint64(len(validators))
return &types.SearchResult{
Type: string(validatorList),
ChainId: chainId,
NumValue: &resultLength,
Type: string(validatorList),
ChainId: chainId,
Value: types.SearchValidatorList{Validators: validators},
}, nil
}

Expand All @@ -213,30 +207,27 @@ func (h *HandlerService) handleSearchValidatorsByDepositAddress(ctx context.Cont
return nil, err
}
result, err := h.daService.GetSearchValidatorsByDepositAddress(ctx, chainId, address)
if err != nil {
if err != nil || result == nil {
return nil, err
}

return &types.SearchResult{
Type: string(validatorsByDepositAddress),
ChainId: chainId,
HashValue: "0x" + hex.EncodeToString(result.Address),
NumValue: &result.Count,
Type: string(validatorsByDepositAddress),
ChainId: chainId,
Value: *result,
}, nil
}

func (h *HandlerService) handleSearchValidatorsByDepositEnsName(ctx context.Context, input string, chainId uint64) (*types.SearchResult, error) {
result, err := h.daService.GetSearchValidatorsByDepositEnsName(ctx, chainId, input)
if err != nil {
if err != nil || result == nil {
return nil, err
}

return &types.SearchResult{
Type: string(validatorsByDepositEnsName),
ChainId: chainId,
StrValue: result.EnsName,
HashValue: "0x" + hex.EncodeToString(result.Address),
NumValue: &result.Count,
Type: string(validatorsByDepositEnsName),
ChainId: chainId,
Value: *result,
}, nil
}

Expand All @@ -246,15 +237,14 @@ func (h *HandlerService) handleSearchValidatorsByWithdrawalCredential(ctx contex
return nil, err
}
result, err := h.daService.GetSearchValidatorsByWithdrawalCredential(ctx, chainId, withdrawalCredential)
if err != nil {
if err != nil || result == nil {
return nil, err
}

return &types.SearchResult{
Type: string(validatorsByWithdrawalCredential),
ChainId: chainId,
HashValue: "0x" + hex.EncodeToString(result.WithdrawalCredential),
NumValue: &result.Count,
Type: string(validatorsByWithdrawalCredential),
ChainId: chainId,
Value: *result,
}, nil
}

Expand All @@ -265,44 +255,40 @@ func (h *HandlerService) handleSearchValidatorsByWithdrawalAddress(ctx context.C
return nil, err
}
result, err := h.daService.GetSearchValidatorsByWithdrawalCredential(ctx, chainId, withdrawalCredential)
if err != nil {
if err != nil || result == nil {
return nil, err
}

return &types.SearchResult{
Type: string(validatorsByWithdrawalAddress),
ChainId: chainId,
HashValue: "0x" + hex.EncodeToString(result.WithdrawalCredential),
NumValue: &result.Count,
Type: string(validatorsByWithdrawalAddress),
ChainId: chainId,
Value: *result,
}, nil
}

func (h *HandlerService) handleSearchValidatorsByWithdrawalEnsName(ctx context.Context, input string, chainId uint64) (*types.SearchResult, error) {
result, err := h.daService.GetSearchValidatorsByWithdrawalEnsName(ctx, chainId, input)
if err != nil {
if err != nil || result == nil {
return nil, err
}

return &types.SearchResult{
Type: string(validatorsByWithdrawalEns),
ChainId: chainId,
StrValue: result.EnsName,
HashValue: "0x" + hex.EncodeToString(result.Address),
NumValue: &result.Count,
Type: string(validatorsByWithdrawalEns),
ChainId: chainId,
Value: *result,
}, nil
}

func (h *HandlerService) handleSearchValidatorsByGraffiti(ctx context.Context, input string, chainId uint64) (*types.SearchResult, error) {
result, err := h.daService.GetSearchValidatorsByGraffiti(ctx, chainId, input)
if err != nil {
if err != nil || result == nil {
return nil, err
}

return &types.SearchResult{
Type: string(validatorsByGraffiti),
ChainId: chainId,
StrValue: result.Graffiti,
NumValue: &result.Count,
Type: string(validatorsByGraffiti),
ChainId: chainId,
Value: *result,
}, nil
}

Expand Down
10 changes: 0 additions & 10 deletions backend/pkg/api/types/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,6 @@ type ChainConfig struct {
// TODO: add more fields, depending on what frontend needs
}

type SearchResult struct {
Type string `json:"type"`
ChainId uint64 `json:"chain_id"`
HashValue string `json:"hash_value,omitempty"`
StrValue string `json:"str_value,omitempty"`
NumValue *uint64 `json:"num_value,omitempty"`
}

type InternalPostSearchResponse ApiDataResponse[[]SearchResult]

type VDBPublicId struct {
PublicId string `json:"public_id"`
DashboardId int `json:"-"`
Expand Down
44 changes: 29 additions & 15 deletions backend/pkg/api/types/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,47 @@ package types
// search types to be used between the data access layer and the api layer, shouldn't be exported to typescript

type SearchValidator struct {
Index uint64
PublicKey []byte
Index uint64 `json:"index"`
PublicKey string `json:"public_key"`
}

type SearchValidatorsByDepositEnsName struct {
EnsName string
Address []byte
Count uint64
type SearchValidatorList struct {
Validators []uint64 `json:"validators"`
}

type SearchValidatorsByDepositAddress struct {
Address []byte
Count uint64
Address string `json:"address"`
ValidatorCount uint64 `json:"validator_count"`
}

type SearchValidatorsByDepositEnsName struct {
EnsName string `json:"ens_name"`
Address string `json:"address"`
ValidatorCount uint64 `json:"validator_count"`
}

type SearchValidatorsByWithdrwalCredential struct {
WithdrawalCredential []byte
Count uint64
WithdrawalCredential string `json:"withdrawal_credential"`
ValidatorCount uint64 `json:"validator_count"`
}

type SearchValidatorsByWithrawalEnsName struct {
EnsName string
Address []byte
Count uint64
EnsName string `json:"ens_name"`
WithdrawalCredential string `json:"withdrawal_credential"`
ValidatorCount uint64 `json:"validator_count"`
}

type SearchValidatorsByGraffiti struct {
Graffiti string
Count uint64
Graffiti string `json:"graffiti"`
ValidatorCount uint64 `json:"validator_count"`
}

type SearchResult struct {
Type string `json:"type"`
ChainId uint64 `json:"chain_id"`
Value interface{} `json:"value"`
}

type InternalPostSearchResponse struct {
Data []SearchResult `json:"data" tstype:"{ type: 'validator_by_index'; value: SearchValidator } | { type: 'validator_by_public_key'; value: SearchValidator } |{ type: 'validator_list'; value: SearchValidatorList } | { type: 'validators_by_deposit_address'; value: SearchValidatorsByDepositAddress } | { type: 'validators_by_deposit_ens_name'; value: SearchValidatorsByDepositEnsName } | { type: 'validators_by_withdrawal_address'; value: SearchValidatorsByWithdrwalCredential } | { type: 'validators_by_withdrawal_credential'; value: SearchValidatorsByWithdrwalCredential } | { type: 'validators_by_withdrawal_ens_name'; value: SearchValidatorsByWithrawalEnsName } | { type: 'validators_by_graffiti'; value: SearchValidatorsByGraffiti }[]"`
}
8 changes: 0 additions & 8 deletions frontend/types/api/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,6 @@ export interface ChainConfig {
chain_id: number /* uint64 */;
name: string;
}
export interface SearchResult {
type: string;
chain_id: number /* uint64 */;
hash_value?: string;
str_value?: string;
num_value?: number /* uint64 */;
}
export type InternalPostSearchResponse = ApiDataResponse<SearchResult[]>;
export interface VDBPublicId {
public_id: string;
name?: string;
Expand Down
Loading

0 comments on commit f9a5f4d

Please sign in to comment.