[API] Fix governor endpoints (#202)
Fix governor endpoints A few governor endpoints were handling addresses incorrectly, which resulted in errors. * `GET /api/v1/governor/config/{governor_address}` * `GET /api/v1/governor/status/{governor_address}` This commit fixes the issue and adds doc comments to prevent the same caveat from happening again in the future.
This commit is contained in:
parent
32c1361782
commit
50547e1140
|
@ -47,18 +47,21 @@ func NewRepository(db *mongo.Database, logger *zap.Logger) *Repository {
|
||||||
// GovernorQuery respresent a query for the governors mongodb documents.
|
// GovernorQuery respresent a query for the governors mongodb documents.
|
||||||
type GovernorQuery struct {
|
type GovernorQuery struct {
|
||||||
pagination.Pagination
|
pagination.Pagination
|
||||||
id string
|
id *types.Address
|
||||||
}
|
}
|
||||||
|
|
||||||
// QueryGovernor create a new GovernorQuery with default pagination values.
|
// NewGovernorQuery creates a new `*GovernorQuery` with default pagination values.
|
||||||
func QueryGovernor() *GovernorQuery {
|
func NewGovernorQuery() *GovernorQuery {
|
||||||
page := pagination.Default()
|
p := pagination.Default()
|
||||||
return &GovernorQuery{Pagination: *page}
|
return &GovernorQuery{Pagination: *p}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetID set the id field of the GovernorQuery struct.
|
// SetID sets the `id` field of the GovernorQuery struct.
|
||||||
func (q *GovernorQuery) SetID(id string) *GovernorQuery {
|
func (q *GovernorQuery) SetID(id *types.Address) *GovernorQuery {
|
||||||
q.id = id
|
|
||||||
|
// Make a deep copy to avoid aliasing bugs
|
||||||
|
q.id = id.Copy()
|
||||||
|
|
||||||
return q
|
return q
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,10 +72,17 @@ func (q *GovernorQuery) SetPagination(p *pagination.Pagination) *GovernorQuery {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *GovernorQuery) toBSON() *bson.D {
|
func (q *GovernorQuery) toBSON() *bson.D {
|
||||||
|
|
||||||
r := bson.D{}
|
r := bson.D{}
|
||||||
if q.id != "" {
|
|
||||||
r = append(r, bson.E{Key: "_id", Value: q.id})
|
if q.id != nil {
|
||||||
|
// In the `governorConfig` and `governorStatus` collections, addresses
|
||||||
|
// are stored as 40 hex digits (instead of the full 64 hex digits).
|
||||||
|
//
|
||||||
|
// Hence, we're using the `ShortHex()` function instead of just `Hex()`.
|
||||||
|
r = append(r, bson.E{Key: "_id", Value: q.id.ShortHex()})
|
||||||
}
|
}
|
||||||
|
|
||||||
return &r
|
return &r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,7 @@ func (s *Service) FindGovernorConfig(ctx context.Context, p *pagination.Paginati
|
||||||
if p == nil {
|
if p == nil {
|
||||||
p = pagination.Default()
|
p = pagination.Default()
|
||||||
}
|
}
|
||||||
query := QueryGovernor().SetPagination(p)
|
query := NewGovernorQuery().SetPagination(p)
|
||||||
govConfigs, err := s.repo.FindGovConfigurations(ctx, query)
|
govConfigs, err := s.repo.FindGovConfigurations(ctx, query)
|
||||||
res := response.Response[[]*GovConfig]{Data: govConfigs}
|
res := response.Response[[]*GovConfig]{Data: govConfigs}
|
||||||
return &res, err
|
return &res, err
|
||||||
|
@ -36,14 +36,14 @@ func (s *Service) FindGovernorConfig(ctx context.Context, p *pagination.Paginati
|
||||||
// FindGovernorConfigByGuardianAddress get a governor configuration by guardianAddress.
|
// FindGovernorConfigByGuardianAddress get a governor configuration by guardianAddress.
|
||||||
func (s *Service) FindGovernorConfigByGuardianAddress(
|
func (s *Service) FindGovernorConfigByGuardianAddress(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
guardianAddress string,
|
guardianAddress *types.Address,
|
||||||
) ([]*GovConfig, error) {
|
) ([]*GovConfig, error) {
|
||||||
|
|
||||||
p := pagination.
|
p := pagination.
|
||||||
Default().
|
Default().
|
||||||
SetLimit(1)
|
SetLimit(1)
|
||||||
|
|
||||||
query := QueryGovernor().
|
query := NewGovernorQuery().
|
||||||
SetID(guardianAddress).
|
SetID(guardianAddress).
|
||||||
SetPagination(p)
|
SetPagination(p)
|
||||||
|
|
||||||
|
@ -56,7 +56,7 @@ func (s *Service) FindGovernorStatus(ctx context.Context, p *pagination.Paginati
|
||||||
if p == nil {
|
if p == nil {
|
||||||
p = pagination.Default()
|
p = pagination.Default()
|
||||||
}
|
}
|
||||||
query := QueryGovernor().SetPagination(p)
|
query := NewGovernorQuery().SetPagination(p)
|
||||||
govStatus, err := s.repo.FindGovernorStatus(ctx, query)
|
govStatus, err := s.repo.FindGovernorStatus(ctx, query)
|
||||||
res := response.Response[[]*GovStatus]{Data: govStatus}
|
res := response.Response[[]*GovStatus]{Data: govStatus}
|
||||||
return &res, err
|
return &res, err
|
||||||
|
@ -65,11 +65,13 @@ func (s *Service) FindGovernorStatus(ctx context.Context, p *pagination.Paginati
|
||||||
// FindGovernorStatusByGuardianAddress get a governor status by guardianAddress.
|
// FindGovernorStatusByGuardianAddress get a governor status by guardianAddress.
|
||||||
func (s *Service) FindGovernorStatusByGuardianAddress(
|
func (s *Service) FindGovernorStatusByGuardianAddress(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
guardianAddress string,
|
guardianAddress *types.Address,
|
||||||
p *pagination.Pagination,
|
p *pagination.Pagination,
|
||||||
) (*response.Response[*GovStatus], error) {
|
) (*response.Response[*GovStatus], error) {
|
||||||
|
|
||||||
query := QueryGovernor().SetID(guardianAddress).SetPagination(p)
|
query := NewGovernorQuery().
|
||||||
|
SetID(guardianAddress).
|
||||||
|
SetPagination(p)
|
||||||
|
|
||||||
govStatus, err := s.repo.FindOneGovernorStatus(ctx, query)
|
govStatus, err := s.repo.FindOneGovernorStatus(ctx, query)
|
||||||
|
|
||||||
|
@ -150,7 +152,7 @@ func (s *Service) GetGovernorLimit(ctx context.Context, p *pagination.Pagination
|
||||||
if p == nil {
|
if p == nil {
|
||||||
p = pagination.Default()
|
p = pagination.Default()
|
||||||
}
|
}
|
||||||
query := QueryGovernor().SetPagination(p)
|
query := NewGovernorQuery().SetPagination(p)
|
||||||
governorLimit, err := s.repo.GetGovernorLimit(ctx, query)
|
governorLimit, err := s.repo.GetGovernorLimit(ctx, query)
|
||||||
res := response.Response[[]*GovernorLimit]{Data: governorLimit}
|
res := response.Response[[]*GovernorLimit]{Data: governorLimit}
|
||||||
return &res, err
|
return &res, err
|
||||||
|
|
|
@ -65,7 +65,7 @@ func (c *Controller) FindGovernorConfigurationByGuardianAddress(ctx *fiber.Ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
// query the database
|
// query the database
|
||||||
govConfigs, err := c.srv.FindGovernorConfigByGuardianAddress(ctx.Context(), guardianAddress.Hex())
|
govConfigs, err := c.srv.FindGovernorConfigByGuardianAddress(ctx.Context(), guardianAddress)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
} else if len(govConfigs) == 0 {
|
} else if len(govConfigs) == 0 {
|
||||||
|
@ -127,7 +127,7 @@ func (c *Controller) FindGovernorStatusByGuardianAddress(ctx *fiber.Ctx) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
govStatus, err := c.srv.FindGovernorStatusByGuardianAddress(ctx.Context(), guardianAddress.Hex(), p)
|
govStatus, err := c.srv.FindGovernorStatusByGuardianAddress(ctx.Context(), guardianAddress, p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,6 +44,12 @@ func (addr *Address) Hex() string {
|
||||||
//
|
//
|
||||||
// If the full address returned by Hex() is prefixed 12 bytes set to zero,
|
// If the full address returned by Hex() is prefixed 12 bytes set to zero,
|
||||||
// this function will trim those bytes.
|
// this function will trim those bytes.
|
||||||
|
//
|
||||||
|
// The reason we need this function is that a few database collections
|
||||||
|
// (governorConfig, governorStatus, heartbeats) store guardian addresses
|
||||||
|
// as 40 hex digits instead of the full 64-digit hex representation.
|
||||||
|
// When performing lookups over those collections, this function
|
||||||
|
// can perform the conversion.
|
||||||
func (addr *Address) ShortHex() string {
|
func (addr *Address) ShortHex() string {
|
||||||
|
|
||||||
full := addr.Hex()
|
full := addr.Hex()
|
||||||
|
@ -54,3 +60,12 @@ func (addr *Address) ShortHex() string {
|
||||||
|
|
||||||
return full
|
return full
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Copy returns a deep copy of the address.
|
||||||
|
func (addr *Address) Copy() *Address {
|
||||||
|
|
||||||
|
var tmp vaa.Address
|
||||||
|
copy(tmp[:], addr.address[:])
|
||||||
|
|
||||||
|
return &Address{address: tmp}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue