Compare commits

...

3 Commits

Author SHA1 Message Date
arnaubennassar
714530e164 Fix mock server 2021-03-29 10:48:50 +02:00
arnaubennassar
378ba669b6 Merge branch 'master' of github.com:hermeznetwork/hermez-node 2021-03-24 09:08:02 +01:00
arnaubennassar
3865c0a9eb Fix mock server 2021-03-23 17:04:54 +01:00
7 changed files with 117 additions and 38 deletions

View File

@@ -522,11 +522,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)

View File

@@ -6,6 +6,7 @@ import (
"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 +18,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
StaticValue float64
}
// RecommendedFeePolicyType describes the different available recommended fee strategies
type RecommendedFeePolicyType string
const (
// Always give the same StaticValue as recommended fee
RecommendedFeePolicyTypeStatic RecommendedFeePolicyType = "Static"
// 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.New("Invalid recommende fee policy")
}
u := Updater{
hdb: hdb,
config: *config,
@@ -31,9 +66,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,6 +101,16 @@ func (u *Updater) SetSCVars(vars *common.SCVariablesPtr) {
// UpdateRecommendedFee update Status.RecommendedFee information
func (u *Updater) UpdateRecommendedFee() error {
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)
@@ -72,6 +118,10 @@ func (u *Updater) UpdateRecommendedFee() error {
u.rw.Lock()
u.state.RecommendedFee = *recommendedFee
u.rw.Unlock()
default:
return tracerr.New("Invalid recommende fee policy")
}
return nil
}

View File

@@ -59,17 +59,21 @@ externalDocs:
description: Find out more about Hermez network.
url: 'https://hermez.io'
servers:
- description: Hosted mock up
url: https://apimock.hermez.network/v1
- description: Localhost mock Up
url: http://localhost:4010/v1
- description: Hosted mock up, returns fake data useful for development
url: https://apimock.hermez.network
- description: Localhost mock up, returns fake data useful for development
url: http://localhost:4010
- description: Testnet (Rinkeby) server
url: https://api.testnet.hermez.io
- description: Mainnet (Ethereum) server, use it carefully, specially if attempting to send transactions. You could lose money!
url: https://api.hermez.io
tags:
- name: Coordinator
description: Endpoints used by the nodes running in coordinator mode. They are used to interact with the network.
- name: Explorer
description: Endpoints used by the nodes running in explorer mode. They are used to get information of the netwrok.
paths:
'/account-creation-authorization':
'/v1/account-creation-authorization':
post:
tags:
- Coordinator
@@ -99,7 +103,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error500'
'/account-creation-authorization/{hezEthereumAddress}':
'/v1/account-creation-authorization/{hezEthereumAddress}':
get:
tags:
- Coordinator
@@ -139,7 +143,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error500'
'/accounts':
'/v1/accounts':
get:
tags:
- Explorer
@@ -210,7 +214,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error500'
'/accounts/{accountIndex}':
'/v1/accounts/{accountIndex}':
get:
tags:
- Explorer
@@ -249,7 +253,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error500'
'/exits':
'/v1/exits':
get:
tags:
- Explorer
@@ -336,7 +340,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error500'
'/exits/{batchNum}/{accountIndex}':
'/v1/exits/{batchNum}/{accountIndex}':
get:
tags:
- Explorer
@@ -381,7 +385,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error500'
'/transactions-pool':
'/v1/transactions-pool':
post:
tags:
- Coordinator
@@ -415,7 +419,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error500'
'/transactions-pool/{id}':
'/v1/transactions-pool/{id}':
get:
tags:
- Coordinator
@@ -458,7 +462,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error500'
'/transactions-history':
'/v1/transactions-history':
get:
tags:
- Explorer
@@ -548,7 +552,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error500'
'/transactions-history/{id}':
'/v1/transactions-history/{id}':
get:
tags:
- Explorer
@@ -588,7 +592,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error500'
'/batches':
'/v1/batches':
get:
tags:
- Explorer
@@ -664,7 +668,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error500'
'/batches/{batchNum}':
'/v1/batches/{batchNum}':
get:
tags:
- Explorer
@@ -704,7 +708,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error500'
'/full-batches/{batchNum}':
'/v1/full-batches/{batchNum}':
get:
tags:
- Explorer
@@ -745,7 +749,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error500'
'/slots':
'/v1/slots':
get:
tags:
- Explorer
@@ -821,7 +825,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error500'
'/slots/{slotNum}':
'/v1/slots/{slotNum}':
get:
tags:
- Explorer
@@ -861,7 +865,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error500'
'/bids':
'/v1/bids':
get:
tags:
- Explorer
@@ -925,7 +929,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error500'
'/state':
'/v1/state':
get:
tags:
- Explorer
@@ -951,7 +955,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error500'
'/config':
'/v1/config':
get:
tags:
- Explorer
@@ -971,7 +975,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error500'
'/tokens':
'/v1/tokens':
get:
tags:
- Explorer
@@ -1044,7 +1048,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error500'
'/tokens/{id}':
'/v1/tokens/{id}':
get:
tags:
- Explorer
@@ -1083,7 +1087,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error500'
'/coordinators':
'/v1/coordinators':
get:
tags:
- Explorer

View File

@@ -145,3 +145,11 @@ Coordinator = true
BatchPath = "/tmp/iden3-test/hermez/batchesdebug"
LightScrypt = true
# RollupVerifierIndex = 0
[RecommendedFeePolicy]
# Strategy used to calculate the recommended fee that the API will expose.
# Available options:
# - Static: always return the same value (StaticValue) in USD
# - AvgLastHour: calculate using the average fee of the forged transactions during the last hour
PolicyType = "Static"
StaticValue = 0.99

View File

@@ -365,6 +365,7 @@ func getConfig(c *cli.Context) (*Config, error) {
}
case modeCoord:
cfg.mode = node.ModeCoordinator
fmt.Println("LOADING CFG")
cfg.node, err = config.LoadNode(nodeCfgPath, true)
if err != nil {
return nil, tracerr.Wrap(err)

View File

@@ -8,6 +8,7 @@ import (
"github.com/BurntSushi/toml"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/hermeznetwork/hermez-node/api/stateapiupdater"
"github.com/hermeznetwork/hermez-node/common"
"github.com/hermeznetwork/hermez-node/priceupdater"
"github.com/hermeznetwork/tracerr"
@@ -347,6 +348,7 @@ type Node struct {
// can wait to stablish a SQL connection
SQLConnectionTimeout Duration
} `validate:"required"`
RecommendedFeePolicy stateapiupdater.RecommendedFeePolicy `validate:"required"`
Debug NodeDebug `validate:"required"`
Coordinator Coordinator `validate:"-"`
}

View File

@@ -288,7 +288,16 @@ func NewNode(mode Mode, cfg *config.Node) (*Node, error) {
return nil, tracerr.Wrap(err)
}
stateAPIUpdater := stateapiupdater.NewUpdater(historyDB, &hdbNodeCfg, initSCVars, &hdbConsts)
stateAPIUpdater, err := stateapiupdater.NewUpdater(
historyDB,
&hdbNodeCfg,
initSCVars,
&hdbConsts,
&cfg.RecommendedFeePolicy,
)
if err != nil {
return nil, tracerr.Wrap(err)
}
var coord *coordinator.Coordinator
if mode == ModeCoordinator {