Skip to content

Commit

Permalink
Add max_fee to fee_stats.
Browse files Browse the repository at this point in the history
  • Loading branch information
abuiles committed Nov 21, 2019
1 parent 72d5e66 commit 3d90ebd
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 34 deletions.
49 changes: 33 additions & 16 deletions protocols/horizon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,25 +624,42 @@ type Metrics struct {
TxsubTotal LogTotalMetric `json:"txsub.total"`
}

type feeStatsBase struct {
Min int `json:"min,string"`
Mode int `json:"mode,string"`
P10 int `json:"p10,string"`
P20 int `json:"p20,string"`
P30 int `json:"p30,string"`
P40 int `json:"p40,string"`
P50 int `json:"p50,string"`
P60 int `json:"p60,string"`
P70 int `json:"p70,string"`
P80 int `json:"p80,string"`
P90 int `json:"p90,string"`
P95 int `json:"p95,string"`
P99 int `json:"p99,string"`
}

// FeeStats represents a response of fees from horizon
// To do: implement fee suggestions if agreement is reached in https://github.com/stellar/go/issues/926
type FeeStats struct {
LastLedger int `json:"last_ledger,string"`
LastLedgerBaseFee int `json:"last_ledger_base_fee,string"`
LedgerCapacityUsage float64 `json:"ledger_capacity_usage,string"`
MinAcceptedFee int `json:"min_accepted_fee,string"`
ModeAcceptedFee int `json:"mode_accepted_fee,string"`
P10AcceptedFee int `json:"p10_accepted_fee,string"`
P20AcceptedFee int `json:"p20_accepted_fee,string"`
P30AcceptedFee int `json:"p30_accepted_fee,string"`
P40AcceptedFee int `json:"p40_accepted_fee,string"`
P50AcceptedFee int `json:"p50_accepted_fee,string"`
P60AcceptedFee int `json:"p60_accepted_fee,string"`
P70AcceptedFee int `json:"p70_accepted_fee,string"`
P80AcceptedFee int `json:"p80_accepted_fee,string"`
P90AcceptedFee int `json:"p90_accepted_fee,string"`
P95AcceptedFee int `json:"p95_accepted_fee,string"`
P99AcceptedFee int `json:"p99_accepted_fee,string"`
LastLedger int `json:"last_ledger,string"`
LastLedgerBaseFee int `json:"last_ledger_base_fee,string"`
LedgerCapacityUsage float64 `json:"ledger_capacity_usage,string"`
MinAcceptedFee int `json:"min_accepted_fee,string"`
ModeAcceptedFee int `json:"mode_accepted_fee,string"`
P10AcceptedFee int `json:"p10_accepted_fee,string"`
P20AcceptedFee int `json:"p20_accepted_fee,string"`
P30AcceptedFee int `json:"p30_accepted_fee,string"`
P40AcceptedFee int `json:"p40_accepted_fee,string"`
P50AcceptedFee int `json:"p50_accepted_fee,string"`
P60AcceptedFee int `json:"p60_accepted_fee,string"`
P70AcceptedFee int `json:"p70_accepted_fee,string"`
P80AcceptedFee int `json:"p80_accepted_fee,string"`
P90AcceptedFee int `json:"p90_accepted_fee,string"`
P95AcceptedFee int `json:"p95_accepted_fee,string"`
P99AcceptedFee int `json:"p99_accepted_fee,string"`
MaxFee feeStatsBase `json:"max_fee"`
}

// TransactionsPage contains records of transaction information returned by Horizon
Expand Down
45 changes: 32 additions & 13 deletions services/horizon/internal/actions_operation_fee_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,8 @@ func (action *FeeStatsAction) JSON() error {

func (action *FeeStatsAction) loadRecords() {
cur := operationfeestats.CurrentState()
action.FeeStats.MinAcceptedFee = int(cur.FeeMin)
action.FeeStats.ModeAcceptedFee = int(cur.FeeMode)
action.FeeStats.LastLedgerBaseFee = int(cur.LastBaseFee)
action.FeeStats.LastLedger = int(cur.LastLedger)
action.FeeStats.P10AcceptedFee = int(cur.FeeP10)
action.FeeStats.P20AcceptedFee = int(cur.FeeP20)
action.FeeStats.P30AcceptedFee = int(cur.FeeP30)
action.FeeStats.P40AcceptedFee = int(cur.FeeP40)
action.FeeStats.P50AcceptedFee = int(cur.FeeP50)
action.FeeStats.P60AcceptedFee = int(cur.FeeP60)
action.FeeStats.P70AcceptedFee = int(cur.FeeP70)
action.FeeStats.P80AcceptedFee = int(cur.FeeP80)
action.FeeStats.P90AcceptedFee = int(cur.FeeP90)
action.FeeStats.P95AcceptedFee = int(cur.FeeP95)
action.FeeStats.P99AcceptedFee = int(cur.FeeP99)

ledgerCapacityUsage, err := strconv.ParseFloat(cur.LedgerCapacityUsage, 64)
if err != nil {
Expand All @@ -78,4 +65,36 @@ func (action *FeeStatsAction) loadRecords() {
}

action.FeeStats.LedgerCapacityUsage = ledgerCapacityUsage

// MaxFee
action.FeeStats.MaxFee.Min = int(cur.FeeMin)
action.FeeStats.MaxFee.Mode = int(cur.FeeMode)
action.FeeStats.MaxFee.P10 = int(cur.FeeP10)
action.FeeStats.MaxFee.P20 = int(cur.FeeP20)
action.FeeStats.MaxFee.P30 = int(cur.FeeP30)
action.FeeStats.MaxFee.P40 = int(cur.FeeP40)
action.FeeStats.MaxFee.P50 = int(cur.FeeP50)
action.FeeStats.MaxFee.P60 = int(cur.FeeP60)
action.FeeStats.MaxFee.P70 = int(cur.FeeP70)
action.FeeStats.MaxFee.P80 = int(cur.FeeP80)
action.FeeStats.MaxFee.P90 = int(cur.FeeP90)
action.FeeStats.MaxFee.P95 = int(cur.FeeP95)
action.FeeStats.MaxFee.P99 = int(cur.FeeP99)

// AcceptedFee is an alias for MaxFee
// Action needed in release: horizon-v1.0.0
// Remove AcceptedFee fields
action.FeeStats.MinAcceptedFee = action.FeeStats.MaxFee.Min
action.FeeStats.ModeAcceptedFee = action.FeeStats.MaxFee.Mode
action.FeeStats.P10AcceptedFee = action.FeeStats.MaxFee.P10
action.FeeStats.P20AcceptedFee = action.FeeStats.MaxFee.P20
action.FeeStats.P30AcceptedFee = action.FeeStats.MaxFee.P30
action.FeeStats.P40AcceptedFee = action.FeeStats.MaxFee.P40
action.FeeStats.P50AcceptedFee = action.FeeStats.MaxFee.P50
action.FeeStats.P60AcceptedFee = action.FeeStats.MaxFee.P60
action.FeeStats.P70AcceptedFee = action.FeeStats.MaxFee.P70
action.FeeStats.P80AcceptedFee = action.FeeStats.MaxFee.P80
action.FeeStats.P90AcceptedFee = action.FeeStats.MaxFee.P90
action.FeeStats.P95AcceptedFee = action.FeeStats.MaxFee.P95
action.FeeStats.P99AcceptedFee = action.FeeStats.MaxFee.P99
}
57 changes: 52 additions & 5 deletions services/horizon/internal/actions_operation_fee_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ func TestOperationFeeTestsActions_Show(t *testing.T) {
err := json.Unmarshal(w.Body.Bytes(), &result)
ht.Require.NoError(err)
ht.Assert.Equal(kase.lastbasefee, result.LastLedgerBaseFee, "base_fee")
ht.Assert.Equal(kase.ledgerCapacityUsage, result.LedgerCapacityUsage, "ledger_capacity_usage")

ht.Assert.Equal(kase.min, result.MinAcceptedFee, "min")
ht.Assert.Equal(kase.mode, result.ModeAcceptedFee, "mode")
ht.Assert.Equal(kase.p10, result.P10AcceptedFee, "p10")
Expand All @@ -115,7 +117,21 @@ func TestOperationFeeTestsActions_Show(t *testing.T) {
ht.Assert.Equal(kase.p90, result.P90AcceptedFee, "p90")
ht.Assert.Equal(kase.p95, result.P95AcceptedFee, "p95")
ht.Assert.Equal(kase.p99, result.P99AcceptedFee, "p99")
ht.Assert.Equal(kase.ledgerCapacityUsage, result.LedgerCapacityUsage, "ledger_capacity_usage")

// AcceptedFee is an alias for MaxFee data
ht.Assert.Equal(kase.min, result.MaxFee.Min, "min")
ht.Assert.Equal(kase.mode, result.MaxFee.Mode, "mode")
ht.Assert.Equal(kase.p10, result.MaxFee.P10, "p10")
ht.Assert.Equal(kase.p20, result.MaxFee.P20, "p20")
ht.Assert.Equal(kase.p30, result.MaxFee.P30, "p30")
ht.Assert.Equal(kase.p40, result.MaxFee.P40, "p40")
ht.Assert.Equal(kase.p50, result.MaxFee.P50, "p50")
ht.Assert.Equal(kase.p60, result.MaxFee.P60, "p60")
ht.Assert.Equal(kase.p70, result.MaxFee.P70, "p70")
ht.Assert.Equal(kase.p80, result.MaxFee.P80, "p80")
ht.Assert.Equal(kase.p90, result.MaxFee.P90, "p90")
ht.Assert.Equal(kase.p95, result.MaxFee.P95, "p95")
ht.Assert.Equal(kase.p99, result.MaxFee.P99, "p99")
}
})
}
Expand Down Expand Up @@ -143,9 +159,11 @@ func TestOperationFeeTestsActions_ShowMultiOp(t *testing.T) {
var result hProtocol.FeeStats
err := json.Unmarshal(w.Body.Bytes(), &result)
ht.Require.NoError(err)
ht.Assert.Equal(100, result.LastLedgerBaseFee, "base_fee")
ht.Assert.Equal(0.06, result.LedgerCapacityUsage, "ledger_capacity_usage")

ht.Assert.Equal(100, result.MinAcceptedFee, "min")
ht.Assert.Equal(200, result.ModeAcceptedFee, "mode")
ht.Assert.Equal(100, result.LastLedgerBaseFee, "base_fee")
ht.Assert.Equal(100, result.P10AcceptedFee, "p10")
ht.Assert.Equal(150, result.P20AcceptedFee, "p20")
ht.Assert.Equal(200, result.P30AcceptedFee, "p30")
Expand All @@ -157,7 +175,21 @@ func TestOperationFeeTestsActions_ShowMultiOp(t *testing.T) {
ht.Assert.Equal(200, result.P90AcceptedFee, "p90")
ht.Assert.Equal(200, result.P95AcceptedFee, "p95")
ht.Assert.Equal(200, result.P99AcceptedFee, "p99")
ht.Assert.Equal(0.06, result.LedgerCapacityUsage, "ledger_capacity_usage")

// AcceptedFee is an alias for MaxFee data
ht.Assert.Equal(100, result.MaxFee.Min, "min")
ht.Assert.Equal(200, result.MaxFee.Mode, "mode")
ht.Assert.Equal(100, result.MaxFee.P10, "p10")
ht.Assert.Equal(150, result.MaxFee.P20, "p20")
ht.Assert.Equal(200, result.MaxFee.P30, "p30")
ht.Assert.Equal(200, result.MaxFee.P40, "p40")
ht.Assert.Equal(200, result.MaxFee.P50, "p50")
ht.Assert.Equal(200, result.MaxFee.P60, "p60")
ht.Assert.Equal(200, result.MaxFee.P70, "p70")
ht.Assert.Equal(200, result.MaxFee.P80, "p80")
ht.Assert.Equal(200, result.MaxFee.P90, "p90")
ht.Assert.Equal(200, result.MaxFee.P95, "p95")
ht.Assert.Equal(200, result.MaxFee.P99, "p99")
}
}

Expand All @@ -181,9 +213,10 @@ func TestOperationFeeTestsActions_NotInterpolating(t *testing.T) {
var result hProtocol.FeeStats
err := json.Unmarshal(w.Body.Bytes(), &result)
ht.Require.NoError(err)
ht.Assert.Equal(100, result.LastLedgerBaseFee, "base_fee")
ht.Assert.Equal(0.09, result.LedgerCapacityUsage, "ledger_capacity_usage")
ht.Assert.Equal(200, result.MinAcceptedFee, "min")
ht.Assert.Equal(400, result.ModeAcceptedFee, "mode")
ht.Assert.Equal(100, result.LastLedgerBaseFee, "base_fee")
ht.Assert.Equal(200, result.P10AcceptedFee, "p10")
ht.Assert.Equal(300, result.P20AcceptedFee, "p20")
ht.Assert.Equal(400, result.P30AcceptedFee, "p30")
Expand All @@ -195,6 +228,20 @@ func TestOperationFeeTestsActions_NotInterpolating(t *testing.T) {
ht.Assert.Equal(16000, result.P90AcceptedFee, "p90")
ht.Assert.Equal(16000, result.P95AcceptedFee, "p95")
ht.Assert.Equal(16000, result.P99AcceptedFee, "p99")
ht.Assert.Equal(0.09, result.LedgerCapacityUsage, "ledger_capacity_usage")

// AcceptedFee is an alias for MaxFee data
ht.Assert.Equal(200, result.MaxFee.Min, "min")
ht.Assert.Equal(400, result.MaxFee.Mode, "mode")
ht.Assert.Equal(200, result.MaxFee.P10, "p10")
ht.Assert.Equal(300, result.MaxFee.P20, "p20")
ht.Assert.Equal(400, result.MaxFee.P30, "p30")
ht.Assert.Equal(400, result.MaxFee.P40, "p40")
ht.Assert.Equal(400, result.MaxFee.P50, "p50")
ht.Assert.Equal(400, result.MaxFee.P60, "p60")
ht.Assert.Equal(400, result.MaxFee.P70, "p70")
ht.Assert.Equal(400, result.MaxFee.P80, "p80")
ht.Assert.Equal(16000, result.MaxFee.P90, "p90")
ht.Assert.Equal(16000, result.MaxFee.P95, "p95")
ht.Assert.Equal(16000, result.MaxFee.P99, "p99")
}
}

0 comments on commit 3d90ebd

Please sign in to comment.