Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #977 from hermeznetwork/release/v1.5.0-3
Browse files Browse the repository at this point in the history
Release/v1.5.0 3
  • Loading branch information
arnaubennassar authored Jul 13, 2021
2 parents ceac3aa + 3c41469 commit 51f1971
Show file tree
Hide file tree
Showing 47 changed files with 1,906 additions and 1,383 deletions.
28 changes: 5 additions & 23 deletions api/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@ import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/hermeznetwork/hermez-node/api/parsers"
"github.com/hermeznetwork/hermez-node/db/historydb"
)

func (a *API) getAccount(c *gin.Context) {
for id := range c.Request.URL.Query() {
if id != "accountIndex" {
retBadReq(fmt.Errorf("invalid Param: %s", id), c)
return
}
}
// Get Addr
idx, err := parseParamIdx(c)
idx, err := parsers.ParseAccountFilter(c)
if err != nil {
retBadReq(err, c)
return
Expand All @@ -38,28 +33,15 @@ func (a *API) getAccounts(c *gin.Context) {
return
}
}
// Account filters
tokenIDs, addr, bjj, err := parseAccountFilters(c)
if err != nil {
retBadReq(err, c)
return
}
// Pagination
fromItem, order, limit, err := parsePagination(c)

accountsFilter, err := parsers.ParseAccountsFilters(c, a.validate)
if err != nil {
retBadReq(err, c)
return
}

// Fetch Accounts from historyDB
apiAccounts, pendingItems, err := a.h.GetAccountsAPI(historydb.GetAccountsAPIRequest{
TokenIDs: tokenIDs,
EthAddr: addr,
Bjj: bjj,
FromItem: fromItem,
Limit: limit,
Order: order,
})
apiAccounts, pendingItems, err := a.h.GetAccountsAPI(accountsFilter)
if err != nil {
retSQLErr(err, c)
return
Expand Down
2 changes: 1 addition & 1 deletion api/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func genTestAccounts(accounts []common.Account, tokens []historydb.TokenWithUSD)
token := getTokenByID(account.TokenID, tokens)
tAccount := testAccount{
ItemID: uint64(x + 1),
Idx: apitypes.HezIdx(idxToHez(account.Idx, token.Symbol)),
Idx: apitypes.HezIdx(common.IdxToHez(account.Idx, token.Symbol)),
PublicKey: apitypes.NewHezBJJ(account.BJJ),
EthAddr: apitypes.NewHezEthAddr(account.EthAddr),
Nonce: account.Nonce,
Expand Down
3 changes: 2 additions & 1 deletion api/accountcreationauths.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/gin-gonic/gin"
"github.com/hermeznetwork/hermez-node/api/apitypes"
"github.com/hermeznetwork/hermez-node/api/parsers"
"github.com/hermeznetwork/hermez-node/common"
"github.com/iden3/go-iden3-crypto/babyjub"
)
Expand Down Expand Up @@ -41,7 +42,7 @@ func (a *API) postAccountCreationAuth(c *gin.Context) {

func (a *API) getAccountCreationAuth(c *gin.Context) {
// Get hezEthereumAddress
addr, err := parseParamHezEthAddr(c)
addr, err := parsers.ParseGetAccountCreationAuthFilter(c)
if err != nil {
retBadReq(err, c)
return
Expand Down
6 changes: 3 additions & 3 deletions api/accountcreationauths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func genTestAuths(auths []*common.AccountCreationAuth) []testAuth {
testAuths := []testAuth{}
for _, auth := range auths {
testAuths = append(testAuths, testAuth{
EthAddr: ethAddrToHez(auth.EthAddr),
BJJ: bjjToString(auth.BJJ),
EthAddr: common.EthAddrToHez(auth.EthAddr),
BJJ: common.BjjToString(auth.BJJ),
Signature: "0x" + hex.EncodeToString(auth.Signature),
Timestamp: auth.Timestamp,
})
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestAccountCreationAuth(t *testing.T) {
// 400
// Wrong addr
badAuth := tc.auths[0]
badAuth.EthAddr = ethAddrToHez(ethCommon.BigToAddress(big.NewInt(1)))
badAuth.EthAddr = common.EthAddrToHez(ethCommon.BigToAddress(big.NewInt(1)))
jsonAuthBytes, err := json.Marshal(badAuth)
assert.NoError(t, err)
jsonAuthReader := bytes.NewReader(jsonAuthBytes)
Expand Down
26 changes: 26 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ package api

import (
"errors"
"reflect"
"strings"

ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/gin-gonic/gin"
"github.com/hermeznetwork/hermez-node/api/parsers"
"github.com/hermeznetwork/hermez-node/db/historydb"
"github.com/hermeznetwork/hermez-node/db/l2db"
"github.com/hermeznetwork/hermez-node/metric"
"github.com/hermeznetwork/tracerr"
"gopkg.in/go-playground/validator.v9"
)

// API serves HTTP requests to allow external interaction with the Hermez node
Expand All @@ -42,6 +46,7 @@ type API struct {
cg *configAPI
l2 *l2db.L2DB
hermezAddress ethCommon.Address
validate *validator.Validate
}

// NewAPI sets the endpoints and the appropriate handlers, but doesn't start the server
Expand All @@ -65,6 +70,7 @@ func NewAPI(
if err != nil {
return nil, err
}

a := &API{
h: hdb,
cg: &configAPI{
Expand All @@ -75,6 +81,7 @@ func NewAPI(
},
l2: l2db,
hermezAddress: consts.HermezAddress,
validate: newValidate(),
}

middleware, err := metric.PrometheusMiddleware()
Expand Down Expand Up @@ -134,3 +141,22 @@ func NewAPI(

return a, nil
}

func newValidate() *validator.Validate {
validate := validator.New()
validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
name := strings.SplitN(fld.Tag.Get("form"), ",", 2)[0]
if name == "-" {
return ""
}
return name
})

validate.RegisterStructValidation(parsers.ExitsFiltersStructValidation, parsers.ExitsFilters{})
validate.RegisterStructValidation(parsers.BidsFiltersStructValidation, parsers.BidsFilters{})
validate.RegisterStructValidation(parsers.AccountsFiltersStructValidation, parsers.AccountsFilters{})
validate.RegisterStructValidation(parsers.HistoryTxsFiltersStructValidation, parsers.HistoryTxsFilters{})
validate.RegisterStructValidation(parsers.PoolTxsTxsFiltersStructValidation, parsers.PoolTxsFilters{})

return validate
}
2 changes: 1 addition & 1 deletion api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ func TestMain(m *testing.M) {
for i := 0; i < len(accounts); i++ {
balance := new(big.Int)
balance.SetString(string(*accounts[i].Balance), 10)
idx, err := stringToIdx(string(accounts[i].Idx), "foo")
idx, err := common.StringToIdx(string(accounts[i].Idx), "foo")
if err != nil {
panic(err)
}
Expand Down
51 changes: 5 additions & 46 deletions api/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package api

import (
"database/sql"
"errors"
"net/http"

"github.com/gin-gonic/gin"
"github.com/hermeznetwork/hermez-node/api/parsers"
"github.com/hermeznetwork/hermez-node/common"
"github.com/hermeznetwork/hermez-node/db"
"github.com/hermeznetwork/hermez-node/db/historydb"
Expand All @@ -14,46 +14,13 @@ import (

func (a *API) getBatches(c *gin.Context) {
// Get query parameters
// minBatchNum
minBatchNum, err := parseQueryUint("minBatchNum", nil, 0, maxUint32, c)
if err != nil {
retBadReq(err, c)
return
}
// maxBatchNum
maxBatchNum, err := parseQueryUint("maxBatchNum", nil, 0, maxUint32, c)
if err != nil {
retBadReq(err, c)
return
}
// slotNum
slotNum, err := parseQueryUint("slotNum", nil, 0, maxUint32, c)
if err != nil {
retBadReq(err, c)
return
}
// forgerAddr
forgerAddr, err := parseQueryEthAddr("forgerAddr", c)
if err != nil {
retBadReq(err, c)
return
}
// pagination
fromItem, order, limit, err := parsePagination(c)
filter, err := parsers.ParseBatchesFilter(c)
if err != nil {
retBadReq(err, c)
return
}
// Fetch batches from historyDB
batches, pendingItems, err := a.h.GetBatchesAPI(historydb.GetBatchesAPIRequest{
MinBatchNum: minBatchNum,
MaxBatchNum: maxBatchNum,
SlotNum: slotNum,
ForgerAddr: forgerAddr,
FromItem: fromItem,
Limit: limit,
Order: order,
})
batches, pendingItems, err := a.h.GetBatchesAPI(filter)
if err != nil {
retSQLErr(err, c)
return
Expand All @@ -72,15 +39,11 @@ func (a *API) getBatches(c *gin.Context) {

func (a *API) getBatch(c *gin.Context) {
// Get batchNum
batchNum, err := parseParamUint("batchNum", nil, 0, maxUint32, c)
batchNum, err := parsers.ParseBatchFilter(c)
if err != nil {
retBadReq(err, c)
return
}
if batchNum == nil { // batchNum is required
retBadReq(errors.New("Invalid batchNum"), c)
return
}
// Fetch batch from historyDB
batch, err := a.h.GetBatchAPI(common.BatchNum(*batchNum))
if err != nil {
Expand All @@ -98,15 +61,11 @@ type fullBatch struct {

func (a *API) getFullBatch(c *gin.Context) {
// Get batchNum
batchNum, err := parseParamUint("batchNum", nil, 0, maxUint32, c)
batchNum, err := parsers.ParseBatchFilter(c)
if err != nil {
retBadReq(err, c)
return
}
if batchNum == nil {
retBadReq(errors.New("Invalid batchNum"), c)
return
}
// Fetch batch from historyDB
batch, err := a.h.GetBatchAPI(common.BatchNum(*batchNum))
if err != nil {
Expand Down
24 changes: 7 additions & 17 deletions api/bids.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,26 @@
package api

import (
"errors"
"net/http"

"github.com/gin-gonic/gin"
"github.com/hermeznetwork/hermez-node/api/parsers"
"github.com/hermeznetwork/hermez-node/db/historydb"
)

func (a *API) getBids(c *gin.Context) {
slotNum, bidderAddr, err := parseBidFilters(c)
if err != nil {
retBadReq(err, c)
return
}
if slotNum == nil && bidderAddr == nil {
retBadReq(errors.New("It is necessary to add at least one filter: slotNum or/and bidderAddr"), c)
return
}
// Pagination
fromItem, order, limit, err := parsePagination(c)
filters, err := parsers.ParseBidsFilters(c, a.validate)
if err != nil {
retBadReq(err, c)
return
}

bids, pendingItems, err := a.h.GetBidsAPI(historydb.GetBidsAPIRequest{
SlotNum: slotNum,
BidderAddr: bidderAddr,
FromItem: fromItem,
Limit: limit,
Order: order,
SlotNum: filters.SlotNum,
BidderAddr: filters.BidderAddr,
FromItem: filters.FromItem,
Limit: filters.Limit,
Order: filters.Order,
})

if err != nil {
Expand Down
22 changes: 3 additions & 19 deletions api/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,19 @@ import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/hermeznetwork/hermez-node/api/parsers"
"github.com/hermeznetwork/hermez-node/db/historydb"
)

func (a *API) getCoordinators(c *gin.Context) {
bidderAddr, err := parseQueryEthAddr("bidderAddr", c)
if err != nil {
retBadReq(err, c)
return
}
forgerAddr, err := parseQueryEthAddr("forgerAddr", c)
if err != nil {
retBadReq(err, c)
return
}
// Pagination
fromItem, order, limit, err := parsePagination(c)
filters, err := parsers.ParseCoordinatorsFilters(c)
if err != nil {
retBadReq(err, c)
return
}

// Fetch coordinators from historyDB
coordinators, pendingItems, err := a.h.GetCoordinatorsAPI(historydb.GetCoordinatorsAPIRequest{
BidderAddr: bidderAddr,
ForgerAddr: forgerAddr,
FromItem: fromItem,
Limit: limit,
Order: order,
})
coordinators, pendingItems, err := a.h.GetCoordinatorsAPI(filters)
if err != nil {
retSQLErr(err, c)
return
Expand Down
Loading

0 comments on commit 51f1971

Please sign in to comment.