mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Allow serving API only via new cli command
- Add new command to the cli/node: `serveapi` that alows serving the API just
by connecting to the PostgreSQL database. The mode flag should me passed in
order to select whether we are connecting to a synchronizer database or a
coordinator database. If `coord` is chosen as mode, the coordinator
endpoints can be activated in order to allow inserting l2txs and
authorizations into the L2DB.
Summary of the implementation details
- New SQL table with 3 columns (plus `item_id` pk). The table only contains a
single row with `item_id` = 1. Columns:
- state: historydb.StateAPI in JSON. This is the struct that is served via
the `/state` API endpoint. The node will periodically update this struct
and store it int he DB. The api server will query it from the DB to
serve it.
- config: historydb.NodeConfig in JSON. This struct contains node
configuration parameters that the API needs to be aware of. It's updated
once every time the node starts.
- constants: historydb.Constants in JSON. This struct contains all the
hermez network constants gathered via the ethereum client by the node.
It's written once every time the node starts.
- The HistoryDB contains methods to get and update each one of these columns
individually.
- The HistoryDB contains all methods that query the DB and prepare objects that
will appear in the StateAPI endpoint.
- The configuration used in for the `serveapi` cli/node command is defined in
`config.APIServer`, and is a subset of `node.Config` in order to allow
reusing the same configuration file of the node if desired.
- A new object is introduced in the api: `StateAPIUpdater`, which contains all
the necessary information to update the StateAPI in the DB periodically by
the node.
- Moved the types `SCConsts`, `SCVariables` and `SCVariablesPtr` from
`syncrhonizer` to `common` for convenience.
This commit is contained in:
155
stateapiupdater/stateapiupdater.go
Normal file
155
stateapiupdater/stateapiupdater.go
Normal file
@@ -0,0 +1,155 @@
|
||||
package stateapiupdater
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"sync"
|
||||
|
||||
"github.com/hermeznetwork/hermez-node/common"
|
||||
"github.com/hermeznetwork/hermez-node/db/historydb"
|
||||
"github.com/hermeznetwork/tracerr"
|
||||
)
|
||||
|
||||
// Updater is an utility object to facilitate updating the StateAPI
|
||||
type Updater struct {
|
||||
hdb *historydb.HistoryDB
|
||||
state historydb.StateAPI
|
||||
config historydb.NodeConfig
|
||||
vars common.SCVariablesPtr
|
||||
consts historydb.Constants
|
||||
rw sync.RWMutex
|
||||
}
|
||||
|
||||
// NewUpdater creates a new Updater
|
||||
func NewUpdater(hdb *historydb.HistoryDB, config *historydb.NodeConfig, vars *common.SCVariables,
|
||||
consts *historydb.Constants) *Updater {
|
||||
u := Updater{
|
||||
hdb: hdb,
|
||||
config: *config,
|
||||
consts: *consts,
|
||||
state: historydb.StateAPI{
|
||||
NodePublicConfig: historydb.NodePublicConfig{
|
||||
ForgeDelay: config.ForgeDelay,
|
||||
},
|
||||
},
|
||||
}
|
||||
u.SetSCVars(vars.AsPtr())
|
||||
return &u
|
||||
}
|
||||
|
||||
// Store the State in the HistoryDB
|
||||
func (u *Updater) Store() error {
|
||||
u.rw.RLock()
|
||||
defer u.rw.RUnlock()
|
||||
return tracerr.Wrap(u.hdb.SetStateInternalAPI(&u.state))
|
||||
}
|
||||
|
||||
// SetSCVars sets the smart contract vars (ony updates those that are not nil)
|
||||
func (u *Updater) SetSCVars(vars *common.SCVariablesPtr) {
|
||||
u.rw.Lock()
|
||||
defer u.rw.Unlock()
|
||||
if vars.Rollup != nil {
|
||||
u.vars.Rollup = vars.Rollup
|
||||
rollupVars := historydb.NewRollupVariablesAPI(u.vars.Rollup)
|
||||
u.state.Rollup = *rollupVars
|
||||
}
|
||||
if vars.Auction != nil {
|
||||
u.vars.Auction = vars.Auction
|
||||
auctionVars := historydb.NewAuctionVariablesAPI(u.vars.Auction)
|
||||
u.state.Auction = *auctionVars
|
||||
}
|
||||
if vars.WDelayer != nil {
|
||||
u.vars.WDelayer = vars.WDelayer
|
||||
u.state.WithdrawalDelayer = *u.vars.WDelayer
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateRecommendedFee update Status.RecommendedFee information
|
||||
func (u *Updater) UpdateRecommendedFee() error {
|
||||
recommendedFee, err := u.hdb.GetRecommendedFee(u.config.MinFeeUSD)
|
||||
if err != nil {
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
u.rw.Lock()
|
||||
u.state.RecommendedFee = *recommendedFee
|
||||
u.rw.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateMetrics update Status.Metrics information
|
||||
func (u *Updater) UpdateMetrics() error {
|
||||
u.rw.RLock()
|
||||
lastBatch := u.state.Network.LastBatch
|
||||
u.rw.RUnlock()
|
||||
if lastBatch == nil {
|
||||
return nil
|
||||
}
|
||||
lastBatchNum := lastBatch.BatchNum
|
||||
metrics, err := u.hdb.GetMetricsInternalAPI(lastBatchNum)
|
||||
if err != nil {
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
u.rw.Lock()
|
||||
u.state.Metrics = *metrics
|
||||
u.rw.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateNetworkInfoBlock update Status.Network block related information
|
||||
func (u *Updater) UpdateNetworkInfoBlock(lastEthBlock, lastSyncBlock common.Block) {
|
||||
u.rw.Lock()
|
||||
u.state.Network.LastSyncBlock = lastSyncBlock.Num
|
||||
u.state.Network.LastEthBlock = lastEthBlock.Num
|
||||
u.rw.Unlock()
|
||||
}
|
||||
|
||||
// UpdateNetworkInfo update Status.Network information
|
||||
func (u *Updater) UpdateNetworkInfo(
|
||||
lastEthBlock, lastSyncBlock common.Block,
|
||||
lastBatchNum common.BatchNum, currentSlot int64,
|
||||
) error {
|
||||
// Get last batch in API format
|
||||
lastBatch, err := u.hdb.GetBatchInternalAPI(lastBatchNum)
|
||||
if tracerr.Unwrap(err) == sql.ErrNoRows {
|
||||
lastBatch = nil
|
||||
} else if err != nil {
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
u.rw.RLock()
|
||||
auctionVars := u.vars.Auction
|
||||
u.rw.RUnlock()
|
||||
// Get next forgers
|
||||
lastClosedSlot := currentSlot + int64(auctionVars.ClosedAuctionSlots)
|
||||
nextForgers, err := u.hdb.GetNextForgersInternalAPI(auctionVars, &u.consts.Auction,
|
||||
lastSyncBlock, currentSlot, lastClosedSlot)
|
||||
if tracerr.Unwrap(err) == sql.ErrNoRows {
|
||||
nextForgers = nil
|
||||
} else if err != nil {
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
bucketUpdates, err := u.hdb.GetBucketUpdatesInternalAPI()
|
||||
if err == sql.ErrNoRows {
|
||||
bucketUpdates = nil
|
||||
} else if err != nil {
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
u.rw.Lock()
|
||||
// Update NodeInfo struct
|
||||
for i, bucketParams := range u.state.Rollup.Buckets {
|
||||
for _, bucketUpdate := range bucketUpdates {
|
||||
if bucketUpdate.NumBucket == i {
|
||||
bucketParams.Withdrawals = bucketUpdate.Withdrawals
|
||||
u.state.Rollup.Buckets[i] = bucketParams
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
u.state.Network.LastSyncBlock = lastSyncBlock.Num
|
||||
u.state.Network.LastEthBlock = lastEthBlock.Num
|
||||
u.state.Network.LastBatch = lastBatch
|
||||
u.state.Network.CurrentSlot = currentSlot
|
||||
u.state.Network.NextForgers = nextForgers
|
||||
u.rw.Unlock()
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user