Add bootCoord to coord table, get coord by forgerAddr

This commit is contained in:
Arnau B
2020-12-18 17:21:44 +01:00
parent 8a2df8de0d
commit bc5fa92898
7 changed files with 80 additions and 78 deletions

View File

@@ -100,7 +100,6 @@ func NewAPI(
server.GET("/tokens", a.getTokens)
server.GET("/tokens/:id", a.getToken)
server.GET("/coordinators", a.getCoordinators)
server.GET("/coordinators/:bidderAddr", a.getCoordinator)
}
return a, nil

View File

@@ -7,29 +7,17 @@ import (
"github.com/hermeznetwork/hermez-node/db/historydb"
)
func (a *API) getCoordinator(c *gin.Context) {
// Get bidderAddr
const name = "bidderAddr"
bidderAddr, err := parseParamEthAddr(name, c)
func (a *API) getCoordinators(c *gin.Context) {
bidderAddr, err := parseQueryEthAddr("bidderAddr", c)
if err != nil {
retBadReq(err, c)
return
} else if bidderAddr == nil {
retBadReq(ErrNillBidderAddr, c)
return
}
coordinator, err := a.h.GetCoordinatorAPI(*bidderAddr)
forgerAddr, err := parseQueryEthAddr("forgerAddr", c)
if err != nil {
retSQLErr(err, c)
retBadReq(err, c)
return
}
c.JSON(http.StatusOK, coordinator)
}
func (a *API) getCoordinators(c *gin.Context) {
// Pagination
fromItem, order, limit, err := parsePagination(c)
if err != nil {
@@ -38,7 +26,7 @@ func (a *API) getCoordinators(c *gin.Context) {
}
// Fetch coordinators from historyDB
coordinators, pendingItems, err := a.h.GetCoordinatorsAPI(fromItem, limit, order)
coordinators, pendingItems, err := a.h.GetCoordinatorsAPI(bidderAddr, forgerAddr, fromItem, limit, order)
if err != nil {
retSQLErr(err, c)
return

View File

@@ -68,20 +68,31 @@ func TestGetCoordinators(t *testing.T) {
}
assertCoordinators(t, reversedCoordinators, fetchedCoordinators)
// Test GetCoordinator
for _, coord := range tc.coordinators {
path = fmt.Sprintf("%s/%s", endpoint, coord.Forger.String())
fetchedCoordinator := historydb.CoordinatorAPI{}
assert.NoError(t, doGoodReq("GET", path, nil, &fetchedCoordinator))
assertCoordinator(t, coord, fetchedCoordinator)
for _, filteredCoord := range tc.coordinators {
// By bidder
fetchedCoordinators = []historydb.CoordinatorAPI{}
err = doGoodReqPaginated(
fmt.Sprintf(path+"&bidderAddr=%s", filteredCoord.Bidder.String()),
historydb.OrderAsc, &testCoordinatorsResponse{}, appendIter,
)
assert.NoError(t, err)
assertCoordinators(t, []historydb.CoordinatorAPI{filteredCoord}, fetchedCoordinators)
// By forger
fetchedCoordinators = []historydb.CoordinatorAPI{}
err = doGoodReqPaginated(
fmt.Sprintf(path+"&forgerAddr=%s", filteredCoord.Forger.String()),
historydb.OrderAsc, &testCoordinatorsResponse{}, appendIter,
)
assert.NoError(t, err)
assertCoordinators(t, []historydb.CoordinatorAPI{filteredCoord}, fetchedCoordinators)
}
// 400
path = fmt.Sprintf("%s/0x001", endpoint)
path = fmt.Sprintf("%s?bidderAddr=0x001", endpoint)
err = doBadReq("GET", path, nil, 400)
assert.NoError(t, err)
// 404
path = fmt.Sprintf("%s/0xaa942cfcd25ad4d90a62358b0dd84f33b398262a", endpoint)
path = fmt.Sprintf("%s?bidderAddr=0xaa942cfcd25ad4d90a62358b0dd84f33b398262a", endpoint)
err = doBadReq("GET", path, nil, 404)
assert.NoError(t, err)
}

View File

@@ -406,14 +406,6 @@ func parseQueryEthAddr(name string, c querier) (*ethCommon.Address, error) {
return parseEthAddr(addrStr)
}
func parseParamEthAddr(name string, c paramer) (*ethCommon.Address, error) {
addrStr := c.Param(name)
if addrStr == "" {
return nil, nil
}
return parseEthAddr(addrStr)
}
func parseEthAddr(ethAddrStr string) (*ethCommon.Address, error) {
var addr ethCommon.Address
err := addr.UnmarshalText([]byte(ethAddrStr))

View File

@@ -1144,6 +1144,18 @@ paths:
description: Get information about coordinators.
operationId: getCoordinators
parameters:
- name: forgerAddr
in: query
required: false
description: Get coordinators by it's forger address.
schema:
$ref: '#/components/schemas/EthereumAddress'
- name: bidderAddr
in: query
required: false
description: Get coordinators by it's bidder address.
schema:
$ref: '#/components/schemas/EthereumAddress'
- name: fromItem
in: query
required: false
@@ -1187,45 +1199,6 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error500'
'/coordinators/{bidderAddr}':
get:
tags:
- Hermez status
summary: Get the information of a coordinator.
description: Get the information of a coordinator.
operationId: getCoordinator
parameters:
- name: bidderAddr
in: path
description: Coordinator identifier
required: true
schema:
$ref: '#/components/schemas/EthereumAddress'
responses:
'200':
description: Successful operation.
content:
application/json:
schema:
$ref: '#/components/schemas/Coordinator'
'400':
description: Bad request.
content:
application/json:
schema:
$ref: '#/components/schemas/Error400'
'404':
description: Not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error404'
'500':
description: Internal server error.
content:
application/json:
schema:
$ref: '#/components/schemas/Error500'
components:
schemas:
ItemId: