mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-06 19:06:42 +01:00
API change names and add poolLoad, add maxFeeUSD
This commit is contained in:
@@ -1083,12 +1083,8 @@ func (hdb *HistoryDB) GetNextForgersInternalAPI(auctionVars *common.AuctionVaria
|
||||
}
|
||||
|
||||
// GetMetricsInternalAPI returns the MetricsAPI
|
||||
func (hdb *HistoryDB) GetMetricsInternalAPI(lastBatchNum common.BatchNum) (*MetricsAPI, error) {
|
||||
var metrics MetricsAPI
|
||||
// Get the first and last batch of the last 24h and their timestamps
|
||||
// if u.state.Network.LastBatch == nil {
|
||||
// return &metrics, nil
|
||||
// }
|
||||
func (hdb *HistoryDB) GetMetricsInternalAPI(lastBatchNum common.BatchNum) (metrics *MetricsAPI, poolLoad int64, err error) {
|
||||
metrics = &MetricsAPI{}
|
||||
type period struct {
|
||||
FromBatchNum common.BatchNum `meddler:"from_batch_num"`
|
||||
FromTimestamp time.Time `meddler:"from_timestamp"`
|
||||
@@ -1106,7 +1102,7 @@ func (hdb *HistoryDB) GetMetricsInternalAPI(lastBatchNum common.BatchNum) (*Metr
|
||||
FROM batch INNER JOIN block ON batch.eth_block_num = block.eth_block_num
|
||||
WHERE block.timestamp >= NOW() - INTERVAL '24 HOURS';`,
|
||||
); err != nil {
|
||||
return nil, tracerr.Wrap(err)
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
// Get the amount of txs of that period
|
||||
row := hdb.dbRead.QueryRow(
|
||||
@@ -1115,7 +1111,7 @@ func (hdb *HistoryDB) GetMetricsInternalAPI(lastBatchNum common.BatchNum) (*Metr
|
||||
)
|
||||
var nTxs int
|
||||
if err := row.Scan(&nTxs); err != nil {
|
||||
return nil, tracerr.Wrap(err)
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
// Set txs/s
|
||||
seconds := p.ToTimestamp.Sub(p.FromTimestamp).Seconds()
|
||||
@@ -1141,7 +1137,7 @@ func (hdb *HistoryDB) GetMetricsInternalAPI(lastBatchNum common.BatchNum) (*Metr
|
||||
)
|
||||
var totalFee float64
|
||||
if err := row.Scan(&totalFee); err != nil {
|
||||
return nil, tracerr.Wrap(err)
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
// Set batch frequency
|
||||
metrics.BatchFrequency = seconds / float64(nBatches)
|
||||
@@ -1152,7 +1148,7 @@ func (hdb *HistoryDB) GetMetricsInternalAPI(lastBatchNum common.BatchNum) (*Metr
|
||||
)
|
||||
var nL2Txs int
|
||||
if err := row.Scan(&nL2Txs); err != nil {
|
||||
return nil, tracerr.Wrap(err)
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
if nL2Txs > 0 {
|
||||
metrics.AvgTransactionFee = totalFee / float64(nL2Txs)
|
||||
@@ -1161,18 +1157,18 @@ func (hdb *HistoryDB) GetMetricsInternalAPI(lastBatchNum common.BatchNum) (*Metr
|
||||
}
|
||||
// Get and set amount of registered accounts
|
||||
type registeredAccounts struct {
|
||||
TotalIdx int64 `meddler:"total_idx"`
|
||||
TotalBJJ int64 `meddler:"total_bjj"`
|
||||
TokenAccounts int64 `meddler:"token_accounts"`
|
||||
Wallets int64 `meddler:"wallets"`
|
||||
}
|
||||
ra := ®isteredAccounts{}
|
||||
if err := meddler.QueryRow(
|
||||
hdb.dbRead, ra,
|
||||
`SELECT COUNT(*) AS total_bjj, COUNT(DISTINCT(bjj)) AS total_idx FROM account;`,
|
||||
`SELECT COUNT(*) AS token_accounts, COUNT(DISTINCT(bjj)) AS wallets FROM account;`,
|
||||
); err != nil {
|
||||
return nil, tracerr.Wrap(err)
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
metrics.TotalAccounts = ra.TotalIdx
|
||||
metrics.TotalBJJs = ra.TotalBJJ
|
||||
metrics.TokenAccounts = ra.TokenAccounts
|
||||
metrics.Wallets = ra.Wallets
|
||||
// Get and set estimated time to forge L1 tx
|
||||
row = hdb.dbRead.QueryRow(
|
||||
`SELECT COALESCE (AVG(EXTRACT(EPOCH FROM (forged.timestamp - added.timestamp))), 0) FROM tx
|
||||
@@ -1184,10 +1180,18 @@ func (hdb *HistoryDB) GetMetricsInternalAPI(lastBatchNum common.BatchNum) (*Metr
|
||||
)
|
||||
var timeToForgeL1 float64
|
||||
if err := row.Scan(&timeToForgeL1); err != nil {
|
||||
return nil, tracerr.Wrap(err)
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
metrics.EstimatedTimeToForgeL1 = timeToForgeL1
|
||||
return &metrics, nil
|
||||
// Get amount of txs in the pool
|
||||
row = hdb.dbRead.QueryRow(
|
||||
`SELECT COUNT(*) FROM tx_pool WHERE state = $1 AND NOT external_delete;`,
|
||||
common.PoolL2TxStatePending,
|
||||
)
|
||||
if err := row.Scan(&poolLoad); err != nil {
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
return metrics, poolLoad, nil
|
||||
}
|
||||
|
||||
// GetStateAPI returns the StateAPI
|
||||
|
||||
@@ -1181,7 +1181,7 @@ const (
|
||||
)
|
||||
|
||||
// GetRecommendedFee returns the RecommendedFee information
|
||||
func (hdb *HistoryDB) GetRecommendedFee(minFeeUSD float64) (*common.RecommendedFee, error) {
|
||||
func (hdb *HistoryDB) GetRecommendedFee(minFeeUSD, maxFeeUSD float64) (*common.RecommendedFee, error) {
|
||||
var recommendedFee common.RecommendedFee
|
||||
// Get total txs and the batch of the first selected tx of the last hour
|
||||
type totalTxsSinceBatchNum struct {
|
||||
@@ -1217,11 +1217,11 @@ func (hdb *HistoryDB) GetRecommendedFee(minFeeUSD float64) (*common.RecommendedF
|
||||
} else {
|
||||
avgTransactionFee = 0
|
||||
}
|
||||
recommendedFee.ExistingAccount =
|
||||
math.Max(avgTransactionFee, minFeeUSD)
|
||||
recommendedFee.CreatesAccount =
|
||||
math.Max(CreateAccountExtraFeePercentage*avgTransactionFee, minFeeUSD)
|
||||
recommendedFee.CreatesAccountInternal =
|
||||
math.Max(CreateAccountInternalExtraFeePercentage*avgTransactionFee, minFeeUSD)
|
||||
recommendedFee.ExistingAccount = math.Min(maxFeeUSD,
|
||||
math.Max(avgTransactionFee, minFeeUSD))
|
||||
recommendedFee.CreatesAccount = math.Min(maxFeeUSD,
|
||||
math.Max(CreateAccountExtraFeePercentage*avgTransactionFee, minFeeUSD))
|
||||
recommendedFee.CreatesAccountInternal = math.Min(maxFeeUSD,
|
||||
math.Max(CreateAccountInternalExtraFeePercentage*avgTransactionFee, minFeeUSD))
|
||||
return &recommendedFee, nil
|
||||
}
|
||||
|
||||
@@ -1177,7 +1177,7 @@ func TestGetMetricsAPI(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
res, err := historyDB.GetMetricsInternalAPI(common.BatchNum(numBatches))
|
||||
res, _, err := historyDB.GetMetricsInternalAPI(common.BatchNum(numBatches))
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, float64(numTx)/float64(numBatches), res.TransactionsPerBatch)
|
||||
@@ -1186,8 +1186,8 @@ func TestGetMetricsAPI(t *testing.T) {
|
||||
// There is a -2 as time for first and last batch is not taken into account
|
||||
assert.InEpsilon(t, float64(frequency)*float64(numBatches-2)/float64(numBatches), res.BatchFrequency, 0.01)
|
||||
assert.InEpsilon(t, float64(numTx)/float64(frequency*blockNum-frequency), res.TransactionsPerSecond, 0.01)
|
||||
assert.Equal(t, int64(3), res.TotalAccounts)
|
||||
assert.Equal(t, int64(3), res.TotalBJJs)
|
||||
assert.Equal(t, int64(3), res.TokenAccounts)
|
||||
assert.Equal(t, int64(3), res.Wallets)
|
||||
// Til does not set fees
|
||||
assert.Equal(t, float64(0), res.AvgTransactionFee)
|
||||
}
|
||||
@@ -1255,22 +1255,22 @@ func TestGetMetricsAPIMoreThan24Hours(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
res, err := historyDBWithACC.GetMetricsInternalAPI(common.BatchNum(numBatches))
|
||||
res, _, err := historyDBWithACC.GetMetricsInternalAPI(common.BatchNum(numBatches))
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.InEpsilon(t, 1.0, res.TransactionsPerBatch, 0.1)
|
||||
|
||||
assert.InEpsilon(t, res.BatchFrequency, float64(blockTime/time.Second), 0.1)
|
||||
assert.InEpsilon(t, 1.0/float64(blockTime/time.Second), res.TransactionsPerSecond, 0.1)
|
||||
assert.Equal(t, int64(3), res.TotalAccounts)
|
||||
assert.Equal(t, int64(3), res.TotalBJJs)
|
||||
assert.Equal(t, int64(3), res.TokenAccounts)
|
||||
assert.Equal(t, int64(3), res.Wallets)
|
||||
// Til does not set fees
|
||||
assert.Equal(t, float64(0), res.AvgTransactionFee)
|
||||
}
|
||||
|
||||
func TestGetMetricsAPIEmpty(t *testing.T) {
|
||||
test.WipeDB(historyDB.DB())
|
||||
_, err := historyDBWithACC.GetMetricsInternalAPI(0)
|
||||
_, _, err := historyDBWithACC.GetMetricsInternalAPI(0)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
@@ -1485,7 +1485,7 @@ func TestNodeInfo(t *testing.T) {
|
||||
addr := ethCommon.HexToAddress("0x1234")
|
||||
hash := ethCommon.HexToHash("0x5678")
|
||||
stateAPI := &StateAPI{
|
||||
NodePublicConfig: NodePublicConfig{
|
||||
NodePublicInfo: NodePublicInfo{
|
||||
ForgeDelay: 3.1,
|
||||
},
|
||||
Network: NetworkAPI{
|
||||
@@ -1544,7 +1544,7 @@ func TestNodeInfo(t *testing.T) {
|
||||
},
|
||||
Metrics: MetricsAPI{
|
||||
TransactionsPerBatch: 1.1,
|
||||
TotalAccounts: 42,
|
||||
TokenAccounts: 42,
|
||||
},
|
||||
Rollup: *NewRollupVariablesAPI(clientSetup.RollupVariables),
|
||||
Auction: *NewAuctionVariablesAPI(clientSetup.AuctionVariables),
|
||||
|
||||
@@ -34,16 +34,17 @@ type NetworkAPI struct {
|
||||
NextForgers []NextForgerAPI `json:"nextForgers"`
|
||||
}
|
||||
|
||||
// NodePublicConfig is the configuration of the node that is exposed via API
|
||||
type NodePublicConfig struct {
|
||||
// NodePublicInfo is the configuration and metrics of the node that is exposed via API
|
||||
type NodePublicInfo struct {
|
||||
// ForgeDelay in seconds
|
||||
ForgeDelay float64 `json:"forgeDelay"`
|
||||
// PoolLoad amount of transactions in the pool
|
||||
PoolLoad int64 `json:"poolLoad"`
|
||||
}
|
||||
|
||||
// StateAPI is an object representing the node and network state exposed via the API
|
||||
type StateAPI struct {
|
||||
// NodePublicConfig is the configuration of the node that is exposed via API
|
||||
NodePublicConfig NodePublicConfig `json:"nodeConfig"`
|
||||
NodePublicInfo NodePublicInfo `json:"node"`
|
||||
Network NetworkAPI `json:"network"`
|
||||
Metrics MetricsAPI `json:"metrics"`
|
||||
Rollup RollupVariablesAPI `json:"rollup"`
|
||||
@@ -63,6 +64,7 @@ type Constants struct {
|
||||
type NodeConfig struct {
|
||||
MaxPoolTxs uint32
|
||||
MinFeeUSD float64
|
||||
MaxFeeUSD float64
|
||||
ForgeDelay float64
|
||||
}
|
||||
|
||||
|
||||
@@ -314,8 +314,8 @@ type MetricsAPI struct {
|
||||
TransactionsPerBatch float64 `json:"transactionsPerBatch"`
|
||||
BatchFrequency float64 `json:"batchFrequency"`
|
||||
TransactionsPerSecond float64 `json:"transactionsPerSecond"`
|
||||
TotalAccounts int64 `json:"totalAccounts" meddler:"total_accounts"`
|
||||
TotalBJJs int64 `json:"totalBJJs" meddler:"total_bjjs"`
|
||||
TokenAccounts int64 `json:"tokenAccounts"`
|
||||
Wallets int64 `json:"wallets"`
|
||||
AvgTransactionFee float64 `json:"avgTransactionFee"`
|
||||
EstimatedTimeToForgeL1 float64 `json:"estimatedTimeToForgeL1" meddler:"estimated_time_to_forge_l1"`
|
||||
}
|
||||
|
||||
@@ -62,6 +62,10 @@ func (l2db *L2DB) AddTxAPI(tx *PoolL2TxWrite) error {
|
||||
return tracerr.Wrap(fmt.Errorf("tx.feeUSD (%v) < minFeeUSD (%v)",
|
||||
feeUSD, l2db.minFeeUSD))
|
||||
}
|
||||
if feeUSD > l2db.maxFeeUSD {
|
||||
return tracerr.Wrap(fmt.Errorf("tx.feeUSD (%v) > maxFeeUSD (%v)",
|
||||
feeUSD, l2db.maxFeeUSD))
|
||||
}
|
||||
|
||||
// Prepare insert SQL query argument parameters
|
||||
namesPart, err := meddler.Default.ColumnsQuoted(tx, false)
|
||||
|
||||
@@ -27,6 +27,7 @@ type L2DB struct {
|
||||
ttl time.Duration
|
||||
maxTxs uint32 // limit of txs that are accepted in the pool
|
||||
minFeeUSD float64
|
||||
maxFeeUSD float64
|
||||
apiConnCon *db.APIConnectionController
|
||||
}
|
||||
|
||||
@@ -38,6 +39,7 @@ func NewL2DB(
|
||||
safetyPeriod common.BatchNum,
|
||||
maxTxs uint32,
|
||||
minFeeUSD float64,
|
||||
maxFeeUSD float64,
|
||||
TTL time.Duration,
|
||||
apiConnCon *db.APIConnectionController,
|
||||
) *L2DB {
|
||||
@@ -48,6 +50,7 @@ func NewL2DB(
|
||||
ttl: TTL,
|
||||
maxTxs: maxTxs,
|
||||
minFeeUSD: minFeeUSD,
|
||||
maxFeeUSD: maxFeeUSD,
|
||||
apiConnCon: apiConnCon,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,9 +37,9 @@ func TestMain(m *testing.M) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
l2DB = NewL2DB(db, db, 10, 1000, 0.0, 24*time.Hour, nil)
|
||||
l2DB = NewL2DB(db, db, 10, 1000, 0.0, 1000.0, 24*time.Hour, nil)
|
||||
apiConnCon := dbUtils.NewAPIConnectionController(1, time.Second)
|
||||
l2DBWithACC = NewL2DB(db, db, 10, 1000, 0.0, 24*time.Hour, apiConnCon)
|
||||
l2DBWithACC = NewL2DB(db, db, 10, 1000, 0.0, 1000.0, 24*time.Hour, apiConnCon)
|
||||
test.WipeDB(l2DB.DB())
|
||||
historyDB = historydb.NewHistoryDB(db, db, nil)
|
||||
// Run tests
|
||||
|
||||
Reference in New Issue
Block a user