|
@ -6,6 +6,7 @@ import ( |
|
|
|
|
|
|
|
|
"github.com/hermeznetwork/hermez-node/common" |
|
|
"github.com/hermeznetwork/hermez-node/common" |
|
|
"github.com/hermeznetwork/hermez-node/db/historydb" |
|
|
"github.com/hermeznetwork/hermez-node/db/historydb" |
|
|
|
|
|
"github.com/hermeznetwork/hermez-node/log" |
|
|
"github.com/hermeznetwork/tracerr" |
|
|
"github.com/hermeznetwork/tracerr" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
@ -17,11 +18,45 @@ type Updater struct { |
|
|
vars common.SCVariablesPtr |
|
|
vars common.SCVariablesPtr |
|
|
consts historydb.Constants |
|
|
consts historydb.Constants |
|
|
rw sync.RWMutex |
|
|
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
|
|
|
// NewUpdater creates a new Updater
|
|
|
func NewUpdater(hdb *historydb.HistoryDB, config *historydb.NodeConfig, vars *common.SCVariables, |
|
|
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{ |
|
|
u := Updater{ |
|
|
hdb: hdb, |
|
|
hdb: hdb, |
|
|
config: *config, |
|
|
config: *config, |
|
@ -31,9 +66,10 @@ func NewUpdater(hdb *historydb.HistoryDB, config *historydb.NodeConfig, vars *co |
|
|
ForgeDelay: config.ForgeDelay, |
|
|
ForgeDelay: config.ForgeDelay, |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
|
|
|
rfp: rfp, |
|
|
} |
|
|
} |
|
|
u.SetSCVars(vars.AsPtr()) |
|
|
u.SetSCVars(vars.AsPtr()) |
|
|
return &u |
|
|
|
|
|
|
|
|
return &u, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Store the State in the HistoryDB
|
|
|
// Store the State in the HistoryDB
|
|
@ -65,13 +101,27 @@ func (u *Updater) SetSCVars(vars *common.SCVariablesPtr) { |
|
|
|
|
|
|
|
|
// UpdateRecommendedFee update Status.RecommendedFee information
|
|
|
// UpdateRecommendedFee update Status.RecommendedFee information
|
|
|
func (u *Updater) UpdateRecommendedFee() error { |
|
|
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 |
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|