mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 11:26:44 +01:00
Merge branch 'develop' into feature/apiWithoutError
# Conflicts: # api/api.go
This commit is contained in:
@@ -21,7 +21,7 @@ func (a *API) postAccountCreationAuth(c *gin.Context) {
|
||||
}
|
||||
// API to common + verify signature
|
||||
commonAuth := accountCreationAuthAPIToCommon(&apiAuth)
|
||||
if !commonAuth.VerifySignature(a.chainID, a.hermezAddress) {
|
||||
if !commonAuth.VerifySignature(a.cg.ChainID, a.hermezAddress) {
|
||||
retBadReq(errors.New("invalid signature"), c)
|
||||
return
|
||||
}
|
||||
|
||||
45
api/api.go
45
api/api.go
@@ -1,3 +1,27 @@
|
||||
/*
|
||||
Package api implements the public interface of the hermez-node using a HTTP REST API.
|
||||
There are two subsets of endpoints:
|
||||
- coordinatorEndpoints: used to receive L2 transactions and account creation authorizations. Targeted for wallets.
|
||||
- explorerEndpoints: used to provide all sorts of information about the network. Targeted for explorers and similar services.
|
||||
|
||||
About the configuration of the API:
|
||||
- The API is supposed to be launched using the cli found at the package cli/node, and configured through the configuration file.
|
||||
- The mentioned configuration file allows exposing any combination of the endpoint subsets.
|
||||
- Although the API can run in a "standalone" manner using the serveapi command, it won't work properly
|
||||
unless another process acting as a coord or sync is filling the HistoryDB.
|
||||
|
||||
Design principles and considerations:
|
||||
- In order to decouple the API process from the rest of the node, all the communication between this package and the rest of
|
||||
the system is done through the SQL database. As a matter of fact, the only public function of the package is the constructor NewAPI.
|
||||
All the information needed for the API to work should be obtained through the configuration file of the cli or the database.
|
||||
- The format of the requests / responses doesn't match directly with the common types, and for this reason, the package api/apitypes is used
|
||||
to facilitate the format conversion. Most of the time, this is done directly at the db level.
|
||||
- The API endpoints are fully documented using OpenAPI aka Swagger. All the endpoints are tested against the spec to ensure consistency
|
||||
between implementation and specification. To get a sense of which endpoints exist and how they work, it's strongly recommended to check this specification.
|
||||
The specification can be found at api/swagger.yml.
|
||||
- In general, all the API endpoints produce queries to the SQL database in order to retrieve / insert the requested information. The most notable exceptions to this are
|
||||
the /config endpoint, which returns a static object generated at construction time, and the /state, which also is retrieved from the database, but it's generated by API/stateapiupdater package.
|
||||
*/
|
||||
package api
|
||||
|
||||
import (
|
||||
@@ -7,6 +31,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/hermeznetwork/hermez-node/db/historydb"
|
||||
"github.com/hermeznetwork/hermez-node/db/l2db"
|
||||
"github.com/hermeznetwork/hermez-node/metric"
|
||||
"github.com/hermeznetwork/tracerr"
|
||||
)
|
||||
|
||||
@@ -15,7 +40,6 @@ type API struct {
|
||||
h *historydb.HistoryDB
|
||||
cg *configAPI
|
||||
l2 *l2db.L2DB
|
||||
chainID uint16
|
||||
hermezAddress ethCommon.Address
|
||||
}
|
||||
|
||||
@@ -27,7 +51,6 @@ func NewAPI(
|
||||
l2db *l2db.L2DB,
|
||||
) (*API, error) {
|
||||
// Check input
|
||||
// TODO: is stateDB only needed for explorer endpoints or for both?
|
||||
if coordinatorEndpoints && l2db == nil {
|
||||
return nil, tracerr.Wrap(errors.New("cannot serve Coordinator endpoints without L2DB"))
|
||||
}
|
||||
@@ -44,19 +67,25 @@ func NewAPI(
|
||||
RollupConstants: *newRollupConstants(consts.Rollup),
|
||||
AuctionConstants: consts.Auction,
|
||||
WDelayerConstants: consts.WDelayer,
|
||||
ChainID: consts.ChainID,
|
||||
},
|
||||
l2: l2db,
|
||||
chainID: consts.ChainID,
|
||||
hermezAddress: consts.HermezAddress,
|
||||
}
|
||||
|
||||
middleware, err := metric.PrometheusMiddleware()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
server.Use(middleware)
|
||||
|
||||
server.NoRoute(a.noRoute)
|
||||
|
||||
v1 := server.Group("/v1")
|
||||
|
||||
// Add coordinator endpoints
|
||||
if coordinatorEndpoints {
|
||||
// Account
|
||||
// Account creation authorization
|
||||
v1.POST("/account-creation-authorization", a.postAccountCreationAuth)
|
||||
v1.GET("/account-creation-authorization/:hezEthereumAddress", a.getAccountCreationAuth)
|
||||
// Transaction
|
||||
@@ -74,17 +103,23 @@ func NewAPI(
|
||||
// Transaction
|
||||
v1.GET("/transactions-history", a.getHistoryTxs)
|
||||
v1.GET("/transactions-history/:id", a.getHistoryTx)
|
||||
// Status
|
||||
// Batches
|
||||
v1.GET("/batches", a.getBatches)
|
||||
v1.GET("/batches/:batchNum", a.getBatch)
|
||||
v1.GET("/full-batches/:batchNum", a.getFullBatch)
|
||||
// Slots
|
||||
v1.GET("/slots", a.getSlots)
|
||||
v1.GET("/slots/:slotNum", a.getSlot)
|
||||
// Bids
|
||||
v1.GET("/bids", a.getBids)
|
||||
// State
|
||||
v1.GET("/state", a.getState)
|
||||
// Config
|
||||
v1.GET("/config", a.getConfig)
|
||||
// Tokens
|
||||
v1.GET("/tokens", a.getTokens)
|
||||
v1.GET("/tokens/:id", a.getToken)
|
||||
// Coordinators
|
||||
v1.GET("/coordinators", a.getCoordinators)
|
||||
}
|
||||
|
||||
|
||||
@@ -216,6 +216,7 @@ func TestMain(m *testing.M) {
|
||||
chainID := uint16(0)
|
||||
_config := getConfigTest(chainID)
|
||||
config = configAPI{
|
||||
ChainID: chainID,
|
||||
RollupConstants: *newRollupConstants(_config.RollupConstants),
|
||||
AuctionConstants: _config.AuctionConstants,
|
||||
WDelayerConstants: _config.WDelayerConstants,
|
||||
@@ -523,11 +524,16 @@ func TestMain(m *testing.M) {
|
||||
WithdrawalDelay: uint64(3000),
|
||||
}
|
||||
|
||||
stateAPIUpdater = stateapiupdater.NewUpdater(hdb, nodeConfig, &common.SCVariables{
|
||||
stateAPIUpdater, err = stateapiupdater.NewUpdater(hdb, nodeConfig, &common.SCVariables{
|
||||
Rollup: rollupVars,
|
||||
Auction: auctionVars,
|
||||
WDelayer: wdelayerVars,
|
||||
}, constants)
|
||||
}, constants, &stateapiupdater.RecommendedFeePolicy{
|
||||
PolicyType: stateapiupdater.RecommendedFeePolicyTypeAvgLastHour,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Generate test data, as expected to be received/sended from/to the API
|
||||
testCoords := genTestCoordinators(commonCoords)
|
||||
|
||||
@@ -1,3 +1,11 @@
|
||||
/*
|
||||
Package apitypes is used to map the common types used across the node with the format expected by the API.
|
||||
|
||||
This is done using different strategies:
|
||||
- Marshallers: they get triggered when the API marshals the response structs into JSONs
|
||||
- Scanners/Valuers: they get triggered when a struct is sent/received to/from the SQL database
|
||||
- Adhoc functions: when the already mentioned strategies are not suitable, functions are added to the structs to facilitate the conversions
|
||||
*/
|
||||
package apitypes
|
||||
|
||||
import (
|
||||
|
||||
@@ -57,6 +57,7 @@ type Config struct {
|
||||
}
|
||||
|
||||
type configAPI struct {
|
||||
ChainID uint16 `json:"chainId"`
|
||||
RollupConstants rollupConstants `json:"hermez"`
|
||||
AuctionConstants common.AuctionConstants `json:"auction"`
|
||||
WDelayerConstants common.WDelayerConstants `json:"withdrawalDelayer"`
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/hermeznetwork/hermez-node/db/historydb"
|
||||
"github.com/hermeznetwork/hermez-node/log"
|
||||
"github.com/hermeznetwork/hermez-node/metric"
|
||||
"github.com/hermeznetwork/tracerr"
|
||||
"github.com/lib/pq"
|
||||
"github.com/russross/meddler"
|
||||
@@ -46,7 +47,9 @@ var (
|
||||
|
||||
func retSQLErr(err error, c *gin.Context) {
|
||||
log.Warnw("HTTP API SQL request error", "err", err)
|
||||
errMsg := tracerr.Unwrap(err).Error()
|
||||
unwrapErr := tracerr.Unwrap(err)
|
||||
metric.CollectError(unwrapErr)
|
||||
errMsg := unwrapErr.Error()
|
||||
retDupKey := func(errCode pq.ErrorCode) {
|
||||
// https://www.postgresql.org/docs/current/errcodes-appendix.html
|
||||
if errCode == "23505" {
|
||||
@@ -80,6 +83,7 @@ func retSQLErr(err error, c *gin.Context) {
|
||||
|
||||
func retBadReq(err error, c *gin.Context) {
|
||||
log.Warnw("HTTP API Bad request error", "err", err)
|
||||
metric.CollectError(err)
|
||||
c.JSON(http.StatusBadRequest, errorMsg{
|
||||
Message: err.Error(),
|
||||
})
|
||||
|
||||
@@ -1,11 +1,20 @@
|
||||
/*
|
||||
Package stateapiupdater is responsible for generating and storing the object response of the GET /state endpoint exposed through the api package.
|
||||
This object is extensively defined at the OpenAPI spec located at api/swagger.yml.
|
||||
|
||||
Deployment considerations: in a setup where multiple processes are used (dedicated api process, separated coord / sync, ...), only one process should care
|
||||
of using this package.
|
||||
*/
|
||||
package stateapiupdater
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/hermeznetwork/hermez-node/common"
|
||||
"github.com/hermeznetwork/hermez-node/db/historydb"
|
||||
"github.com/hermeznetwork/hermez-node/log"
|
||||
"github.com/hermeznetwork/tracerr"
|
||||
)
|
||||
|
||||
@@ -17,11 +26,45 @@ type Updater struct {
|
||||
vars common.SCVariablesPtr
|
||||
consts historydb.Constants
|
||||
rw sync.RWMutex
|
||||
rfp *RecommendedFeePolicy
|
||||
}
|
||||
|
||||
// RecommendedFeePolicy describes how the recommended fee is calculated
|
||||
type RecommendedFeePolicy struct {
|
||||
PolicyType RecommendedFeePolicyType `validate:"required"`
|
||||
StaticValue float64
|
||||
}
|
||||
|
||||
// RecommendedFeePolicyType describes the different available recommended fee strategies
|
||||
type RecommendedFeePolicyType string
|
||||
|
||||
const (
|
||||
// RecommendedFeePolicyTypeStatic always give the same StaticValue as recommended fee
|
||||
RecommendedFeePolicyTypeStatic RecommendedFeePolicyType = "Static"
|
||||
// RecommendedFeePolicyTypeAvgLastHour set the recommended fee using the average fee of the last hour
|
||||
RecommendedFeePolicyTypeAvgLastHour RecommendedFeePolicyType = "AvgLastHour"
|
||||
)
|
||||
|
||||
func (rfp *RecommendedFeePolicy) valid() bool {
|
||||
switch rfp.PolicyType {
|
||||
case RecommendedFeePolicyTypeStatic:
|
||||
if rfp.StaticValue == 0 {
|
||||
log.Warn("RcommendedFee is set to 0 USD, and the policy is static")
|
||||
}
|
||||
return true
|
||||
case RecommendedFeePolicyTypeAvgLastHour:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// NewUpdater creates a new Updater
|
||||
func NewUpdater(hdb *historydb.HistoryDB, config *historydb.NodeConfig, vars *common.SCVariables,
|
||||
consts *historydb.Constants) *Updater {
|
||||
consts *historydb.Constants, rfp *RecommendedFeePolicy) (*Updater, error) {
|
||||
if ok := rfp.valid(); !ok {
|
||||
return nil, tracerr.Wrap(fmt.Errorf("Invalid recommended fee policy: %v", rfp.PolicyType))
|
||||
}
|
||||
u := Updater{
|
||||
hdb: hdb,
|
||||
config: *config,
|
||||
@@ -31,9 +74,10 @@ func NewUpdater(hdb *historydb.HistoryDB, config *historydb.NodeConfig, vars *co
|
||||
ForgeDelay: config.ForgeDelay,
|
||||
},
|
||||
},
|
||||
rfp: rfp,
|
||||
}
|
||||
u.SetSCVars(vars.AsPtr())
|
||||
return &u
|
||||
return &u, nil
|
||||
}
|
||||
|
||||
// Store the State in the HistoryDB
|
||||
@@ -65,13 +109,27 @@ func (u *Updater) SetSCVars(vars *common.SCVariablesPtr) {
|
||||
|
||||
// UpdateRecommendedFee update Status.RecommendedFee information
|
||||
func (u *Updater) UpdateRecommendedFee() error {
|
||||
recommendedFee, err := u.hdb.GetRecommendedFee(u.config.MinFeeUSD, u.config.MaxFeeUSD)
|
||||
if err != nil {
|
||||
return tracerr.Wrap(err)
|
||||
switch u.rfp.PolicyType {
|
||||
case RecommendedFeePolicyTypeStatic:
|
||||
u.rw.Lock()
|
||||
u.state.RecommendedFee = common.RecommendedFee{
|
||||
ExistingAccount: u.rfp.StaticValue,
|
||||
CreatesAccount: u.rfp.StaticValue,
|
||||
CreatesAccountInternal: u.rfp.StaticValue,
|
||||
}
|
||||
u.rw.Unlock()
|
||||
case RecommendedFeePolicyTypeAvgLastHour:
|
||||
recommendedFee, err := u.hdb.GetRecommendedFee(u.config.MinFeeUSD, u.config.MaxFeeUSD)
|
||||
if err != nil {
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
u.rw.Lock()
|
||||
u.state.RecommendedFee = *recommendedFee
|
||||
u.rw.Unlock()
|
||||
default:
|
||||
return tracerr.New("Invalid recommende fee policy")
|
||||
}
|
||||
u.rw.Lock()
|
||||
u.state.RecommendedFee = *recommendedFee
|
||||
u.rw.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -3040,10 +3040,15 @@ components:
|
||||
- maxEmergencyModeTime
|
||||
- hermezRollup
|
||||
additionalProperties: false
|
||||
chainId:
|
||||
type: integer
|
||||
description: Id of the chain
|
||||
example: 27
|
||||
required:
|
||||
- hermez
|
||||
- auction
|
||||
- withdrawalDelayer
|
||||
- chainId
|
||||
additionalProperties: false
|
||||
Error:
|
||||
type: object
|
||||
|
||||
@@ -187,7 +187,7 @@ func (a *API) verifyPoolL2TxWrite(txw *l2db.PoolL2TxWrite) error {
|
||||
poolTx.TokenID, account.TokenID))
|
||||
}
|
||||
// Check signature
|
||||
if !poolTx.VerifySignature(a.chainID, account.BJJ) {
|
||||
if !poolTx.VerifySignature(a.cg.ChainID, account.BJJ) {
|
||||
return tracerr.Wrap(errors.New("wrong signature"))
|
||||
}
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user