mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-06 19:06:42 +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:
@@ -183,28 +183,6 @@ type StartBlockNums struct {
|
||||
WDelayer int64
|
||||
}
|
||||
|
||||
// SCVariables joins all the smart contract variables in a single struct
|
||||
type SCVariables struct {
|
||||
Rollup common.RollupVariables `validate:"required"`
|
||||
Auction common.AuctionVariables `validate:"required"`
|
||||
WDelayer common.WDelayerVariables `validate:"required"`
|
||||
}
|
||||
|
||||
// SCVariablesPtr joins all the smart contract variables as pointers in a single
|
||||
// struct
|
||||
type SCVariablesPtr struct {
|
||||
Rollup *common.RollupVariables `validate:"required"`
|
||||
Auction *common.AuctionVariables `validate:"required"`
|
||||
WDelayer *common.WDelayerVariables `validate:"required"`
|
||||
}
|
||||
|
||||
// SCConsts joins all the smart contract constants in a single struct
|
||||
type SCConsts struct {
|
||||
Rollup common.RollupConstants
|
||||
Auction common.AuctionConstants
|
||||
WDelayer common.WDelayerConstants
|
||||
}
|
||||
|
||||
// Config is the Synchronizer configuration
|
||||
type Config struct {
|
||||
StatsRefreshPeriod time.Duration
|
||||
@@ -214,14 +192,14 @@ type Config struct {
|
||||
// Synchronizer implements the Synchronizer type
|
||||
type Synchronizer struct {
|
||||
ethClient eth.ClientInterface
|
||||
consts SCConsts
|
||||
consts common.SCConsts
|
||||
historyDB *historydb.HistoryDB
|
||||
l2DB *l2db.L2DB
|
||||
stateDB *statedb.StateDB
|
||||
cfg Config
|
||||
initVars SCVariables
|
||||
initVars common.SCVariables
|
||||
startBlockNum int64
|
||||
vars SCVariables
|
||||
vars common.SCVariables
|
||||
stats *StatsHolder
|
||||
resetStateFailed bool
|
||||
}
|
||||
@@ -244,7 +222,7 @@ func NewSynchronizer(ethClient eth.ClientInterface, historyDB *historydb.History
|
||||
return nil, tracerr.Wrap(fmt.Errorf("NewSynchronizer ethClient.WDelayerConstants(): %w",
|
||||
err))
|
||||
}
|
||||
consts := SCConsts{
|
||||
consts := common.SCConsts{
|
||||
Rollup: *rollupConstants,
|
||||
Auction: *auctionConstants,
|
||||
WDelayer: *wDelayerConstants,
|
||||
@@ -310,11 +288,11 @@ func (s *Synchronizer) WDelayerConstants() *common.WDelayerConstants {
|
||||
}
|
||||
|
||||
// SCVars returns a copy of the Smart Contract Variables
|
||||
func (s *Synchronizer) SCVars() SCVariablesPtr {
|
||||
return SCVariablesPtr{
|
||||
Rollup: s.vars.Rollup.Copy(),
|
||||
Auction: s.vars.Auction.Copy(),
|
||||
WDelayer: s.vars.WDelayer.Copy(),
|
||||
func (s *Synchronizer) SCVars() *common.SCVariables {
|
||||
return &common.SCVariables{
|
||||
Rollup: *s.vars.Rollup.Copy(),
|
||||
Auction: *s.vars.Auction.Copy(),
|
||||
WDelayer: *s.vars.WDelayer.Copy(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -727,7 +705,7 @@ func (s *Synchronizer) reorg(uncleBlock *common.Block) (int64, error) {
|
||||
}
|
||||
|
||||
func getInitialVariables(ethClient eth.ClientInterface,
|
||||
consts *SCConsts) (*SCVariables, *StartBlockNums, error) {
|
||||
consts *common.SCConsts) (*common.SCVariables, *StartBlockNums, error) {
|
||||
rollupInit, rollupInitBlock, err := ethClient.RollupEventInit()
|
||||
if err != nil {
|
||||
return nil, nil, tracerr.Wrap(fmt.Errorf("RollupEventInit: %w", err))
|
||||
@@ -743,7 +721,7 @@ func getInitialVariables(ethClient eth.ClientInterface,
|
||||
rollupVars := rollupInit.RollupVariables()
|
||||
auctionVars := auctionInit.AuctionVariables(consts.Auction.InitialMinimalBidding)
|
||||
wDelayerVars := wDelayerInit.WDelayerVariables()
|
||||
return &SCVariables{
|
||||
return &common.SCVariables{
|
||||
Rollup: *rollupVars,
|
||||
Auction: *auctionVars,
|
||||
WDelayer: *wDelayerVars,
|
||||
|
||||
@@ -378,9 +378,9 @@ func TestSyncGeneral(t *testing.T) {
|
||||
assert.Equal(t, int64(1), stats.Eth.LastBlock.Num)
|
||||
assert.Equal(t, int64(1), stats.Sync.LastBlock.Num)
|
||||
vars := s.SCVars()
|
||||
assert.Equal(t, clientSetup.RollupVariables, vars.Rollup)
|
||||
assert.Equal(t, clientSetup.AuctionVariables, vars.Auction)
|
||||
assert.Equal(t, clientSetup.WDelayerVariables, vars.WDelayer)
|
||||
assert.Equal(t, *clientSetup.RollupVariables, vars.Rollup)
|
||||
assert.Equal(t, *clientSetup.AuctionVariables, vars.Auction)
|
||||
assert.Equal(t, *clientSetup.WDelayerVariables, vars.WDelayer)
|
||||
|
||||
dbBlocks, err := s.historyDB.GetAllBlocks()
|
||||
require.NoError(t, err)
|
||||
@@ -541,9 +541,9 @@ func TestSyncGeneral(t *testing.T) {
|
||||
assert.Equal(t, int64(4), stats.Eth.LastBlock.Num)
|
||||
assert.Equal(t, int64(4), stats.Sync.LastBlock.Num)
|
||||
vars = s.SCVars()
|
||||
assert.Equal(t, clientSetup.RollupVariables, vars.Rollup)
|
||||
assert.Equal(t, clientSetup.AuctionVariables, vars.Auction)
|
||||
assert.Equal(t, clientSetup.WDelayerVariables, vars.WDelayer)
|
||||
assert.Equal(t, *clientSetup.RollupVariables, vars.Rollup)
|
||||
assert.Equal(t, *clientSetup.AuctionVariables, vars.Auction)
|
||||
assert.Equal(t, *clientSetup.WDelayerVariables, vars.WDelayer)
|
||||
|
||||
dbExits, err := s.historyDB.GetAllExits()
|
||||
require.NoError(t, err)
|
||||
@@ -673,9 +673,9 @@ func TestSyncGeneral(t *testing.T) {
|
||||
assert.Equal(t, false, stats.Synced())
|
||||
assert.Equal(t, int64(6), stats.Eth.LastBlock.Num)
|
||||
vars = s.SCVars()
|
||||
assert.Equal(t, clientSetup.RollupVariables, vars.Rollup)
|
||||
assert.Equal(t, clientSetup.AuctionVariables, vars.Auction)
|
||||
assert.Equal(t, clientSetup.WDelayerVariables, vars.WDelayer)
|
||||
assert.Equal(t, *clientSetup.RollupVariables, vars.Rollup)
|
||||
assert.Equal(t, *clientSetup.AuctionVariables, vars.Auction)
|
||||
assert.Equal(t, *clientSetup.WDelayerVariables, vars.WDelayer)
|
||||
|
||||
// At this point, the DB only has data up to block 1
|
||||
dbBlock, err := s.historyDB.GetLastBlock()
|
||||
@@ -712,9 +712,9 @@ func TestSyncGeneral(t *testing.T) {
|
||||
}
|
||||
|
||||
vars = s.SCVars()
|
||||
assert.Equal(t, clientSetup.RollupVariables, vars.Rollup)
|
||||
assert.Equal(t, clientSetup.AuctionVariables, vars.Auction)
|
||||
assert.Equal(t, clientSetup.WDelayerVariables, vars.WDelayer)
|
||||
assert.Equal(t, *clientSetup.RollupVariables, vars.Rollup)
|
||||
assert.Equal(t, *clientSetup.AuctionVariables, vars.Auction)
|
||||
assert.Equal(t, *clientSetup.WDelayerVariables, vars.WDelayer)
|
||||
}
|
||||
|
||||
dbBlock, err = s.historyDB.GetLastBlock()
|
||||
|
||||
Reference in New Issue
Block a user