From 1c6f06aacf91651523209e380aea97922e8735f2 Mon Sep 17 00:00:00 2001 From: Adolfo Builes Date: Wed, 20 Nov 2019 19:33:35 -0500 Subject: [PATCH] Add max for fee_charged and max_fee. --- protocols/horizon/main.go | 1 + .../internal/actions_operation_fee_stats.go | 2 + .../actions_operation_fee_stats_test.go | 143 ++++++++++-------- services/horizon/internal/app.go | 4 + services/horizon/internal/db2/history/main.go | 2 + .../horizon/internal/db2/history/operation.go | 4 +- .../internal/operationfeestats/main.go | 2 + 7 files changed, 92 insertions(+), 66 deletions(-) diff --git a/protocols/horizon/main.go b/protocols/horizon/main.go index 058c66dfa6..b1e846cdaf 100644 --- a/protocols/horizon/main.go +++ b/protocols/horizon/main.go @@ -625,6 +625,7 @@ type Metrics struct { } type feeStatsBase struct { + Max int `json:"max,string"` Min int `json:"min,string"` Mode int `json:"mode,string"` P10 int `json:"p10,string"` diff --git a/services/horizon/internal/actions_operation_fee_stats.go b/services/horizon/internal/actions_operation_fee_stats.go index 8e9fb93e9d..6f88bcc5c9 100644 --- a/services/horizon/internal/actions_operation_fee_stats.go +++ b/services/horizon/internal/actions_operation_fee_stats.go @@ -67,6 +67,7 @@ func (action *FeeStatsAction) loadRecords() { action.FeeStats.LedgerCapacityUsage = ledgerCapacityUsage // FeeCharged + action.FeeStats.FeeCharged.Max = int(cur.FeeChargedMax) action.FeeStats.FeeCharged.Min = int(cur.FeeChargedMin) action.FeeStats.FeeCharged.Mode = int(cur.FeeChargedMode) action.FeeStats.FeeCharged.P10 = int(cur.FeeChargedP10) @@ -82,6 +83,7 @@ func (action *FeeStatsAction) loadRecords() { action.FeeStats.FeeCharged.P99 = int(cur.FeeChargedP99) // MaxFee + action.FeeStats.MaxFee.Max = int(cur.FeeMax) action.FeeStats.MaxFee.Min = int(cur.FeeMin) action.FeeStats.MaxFee.Mode = int(cur.FeeMode) action.FeeStats.MaxFee.P10 = int(cur.FeeP10) diff --git a/services/horizon/internal/actions_operation_fee_stats_test.go b/services/horizon/internal/actions_operation_fee_stats_test.go index 03fedd41be..f98fa46310 100644 --- a/services/horizon/internal/actions_operation_fee_stats_test.go +++ b/services/horizon/internal/actions_operation_fee_stats_test.go @@ -10,6 +10,7 @@ func TestOperationFeeTestsActions_Show(t *testing.T) { testCases := []struct { scenario string lastbasefee int + max int min int mode int p10 int @@ -23,6 +24,7 @@ func TestOperationFeeTestsActions_Show(t *testing.T) { p90 int p95 int p99 int + feeChargedMax int feeChargedMin int feeChargedMode int feeChargedP10 int @@ -42,6 +44,7 @@ func TestOperationFeeTestsActions_Show(t *testing.T) { { scenario: "operation_fee_stats_1", lastbasefee: 100, + max: 100, min: 100, mode: 100, p10: 100, @@ -55,6 +58,7 @@ func TestOperationFeeTestsActions_Show(t *testing.T) { p90: 100, p95: 100, p99: 100, + feeChargedMax: 100, feeChargedMin: 100, feeChargedMode: 100, feeChargedP10: 100, @@ -75,6 +79,7 @@ func TestOperationFeeTestsActions_Show(t *testing.T) { scenario: "operation_fee_stats_2", ledgerCapacityUsage: 0.00, lastbasefee: 100, + max: 100, min: 100, mode: 100, p10: 100, @@ -88,6 +93,7 @@ func TestOperationFeeTestsActions_Show(t *testing.T) { p90: 100, p95: 100, p99: 100, + feeChargedMax: 100, feeChargedMin: 100, feeChargedMode: 100, feeChargedP10: 100, @@ -107,6 +113,7 @@ func TestOperationFeeTestsActions_Show(t *testing.T) { scenario: "operation_fee_stats_3", ledgerCapacityUsage: 0.03, lastbasefee: 100, + max: 400, min: 200, mode: 400, p10: 200, @@ -120,6 +127,7 @@ func TestOperationFeeTestsActions_Show(t *testing.T) { p90: 400, p95: 400, p99: 400, + feeChargedMax: 100, feeChargedMin: 100, feeChargedMode: 100, feeChargedP10: 100, @@ -185,19 +193,20 @@ func TestOperationFeeTestsActions_Show(t *testing.T) { ht.Assert.Equal(kase.p95, result.MaxFee.P95, "p95") ht.Assert.Equal(kase.p99, result.MaxFee.P99, "p99") - ht.Assert.Equal(kase.feeChargedMin, result.FeeCharged.Min, "min") - ht.Assert.Equal(kase.feeChargedMode, result.FeeCharged.Mode, "mode") - ht.Assert.Equal(kase.feeChargedP10, result.FeeCharged.P10, "p10") - ht.Assert.Equal(kase.feeChargedP20, result.FeeCharged.P20, "p20") - ht.Assert.Equal(kase.feeChargedP30, result.FeeCharged.P30, "p30") - ht.Assert.Equal(kase.feeChargedP40, result.FeeCharged.P40, "p40") - ht.Assert.Equal(kase.feeChargedP50, result.FeeCharged.P50, "p50") - ht.Assert.Equal(kase.feeChargedP60, result.FeeCharged.P60, "p60") - ht.Assert.Equal(kase.feeChargedP70, result.FeeCharged.P70, "p70") - ht.Assert.Equal(kase.feeChargedP80, result.FeeCharged.P80, "p80") - ht.Assert.Equal(kase.feeChargedP90, result.FeeCharged.P90, "p90") - ht.Assert.Equal(kase.feeChargedP95, result.FeeCharged.P95, "p95") - ht.Assert.Equal(kase.feeChargedP99, result.FeeCharged.P99, "p99") + ht.Assert.Equal(kase.feeChargedMax, result.FeeCharged.Max, "fee_charged_max") + ht.Assert.Equal(kase.feeChargedMin, result.FeeCharged.Min, "fee_charged_min") + ht.Assert.Equal(kase.feeChargedMode, result.FeeCharged.Mode, "fee_charged_mode") + ht.Assert.Equal(kase.feeChargedP10, result.FeeCharged.P10, "fee_charged_p10") + ht.Assert.Equal(kase.feeChargedP20, result.FeeCharged.P20, "fee_charged_p20") + ht.Assert.Equal(kase.feeChargedP30, result.FeeCharged.P30, "fee_charged_p30") + ht.Assert.Equal(kase.feeChargedP40, result.FeeCharged.P40, "fee_charged_p40") + ht.Assert.Equal(kase.feeChargedP50, result.FeeCharged.P50, "fee_charged_p50") + ht.Assert.Equal(kase.feeChargedP60, result.FeeCharged.P60, "fee_charged_p60") + ht.Assert.Equal(kase.feeChargedP70, result.FeeCharged.P70, "fee_charged_p70") + ht.Assert.Equal(kase.feeChargedP80, result.FeeCharged.P80, "fee_charged_p80") + ht.Assert.Equal(kase.feeChargedP90, result.FeeCharged.P90, "fee_charged_p90") + ht.Assert.Equal(kase.feeChargedP95, result.FeeCharged.P95, "fee_charged_p95") + ht.Assert.Equal(kase.feeChargedP99, result.FeeCharged.P99, "fee_charged_p99") } }) } @@ -243,33 +252,35 @@ func TestOperationFeeTestsActions_ShowMultiOp(t *testing.T) { ht.Assert.Equal(200, result.P99AcceptedFee, "p99") // 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") + ht.Assert.Equal(200, result.MaxFee.Max, "max_fee_max") + ht.Assert.Equal(100, result.MaxFee.Min, "max_fee_min") + ht.Assert.Equal(200, result.MaxFee.Mode, "max_fee_mode") + ht.Assert.Equal(100, result.MaxFee.P10, "max_fee_p10") + ht.Assert.Equal(150, result.MaxFee.P20, "max_fee_p20") + ht.Assert.Equal(200, result.MaxFee.P30, "max_fee_p30") + ht.Assert.Equal(200, result.MaxFee.P40, "max_fee_p40") + ht.Assert.Equal(200, result.MaxFee.P50, "max_fee_p50") + ht.Assert.Equal(200, result.MaxFee.P60, "max_fee_p60") + ht.Assert.Equal(200, result.MaxFee.P70, "max_fee_p70") + ht.Assert.Equal(200, result.MaxFee.P80, "max_fee_p80") + ht.Assert.Equal(200, result.MaxFee.P90, "max_fee_p90") + ht.Assert.Equal(200, result.MaxFee.P95, "max_fee_p95") + ht.Assert.Equal(200, result.MaxFee.P99, "max_fee_p99") - ht.Assert.Equal(50, result.FeeCharged.Min, "min") - ht.Assert.Equal(50, result.FeeCharged.Mode, "mode") - ht.Assert.Equal(50, result.FeeCharged.P10, "p10") - ht.Assert.Equal(50, result.FeeCharged.P20, "p20") - ht.Assert.Equal(50, result.FeeCharged.P30, "p30") - ht.Assert.Equal(50, result.FeeCharged.P40, "p40") - ht.Assert.Equal(50, result.FeeCharged.P50, "p50") - ht.Assert.Equal(50, result.FeeCharged.P60, "p60") - ht.Assert.Equal(50, result.FeeCharged.P70, "p70") - ht.Assert.Equal(50, result.FeeCharged.P80, "p80") - ht.Assert.Equal(50, result.FeeCharged.P90, "p90") - ht.Assert.Equal(50, result.FeeCharged.P95, "p95") - ht.Assert.Equal(50, result.FeeCharged.P99, "p99") + ht.Assert.Equal(50, result.FeeCharged.Max, "fee_charged_max") + ht.Assert.Equal(50, result.FeeCharged.Min, "fee_charged_min") + ht.Assert.Equal(50, result.FeeCharged.Mode, "fee_charged_mode") + ht.Assert.Equal(50, result.FeeCharged.P10, "fee_charged_p10") + ht.Assert.Equal(50, result.FeeCharged.P20, "fee_charged_p20") + ht.Assert.Equal(50, result.FeeCharged.P30, "fee_charged_p30") + ht.Assert.Equal(50, result.FeeCharged.P40, "fee_charged_p40") + ht.Assert.Equal(50, result.FeeCharged.P50, "fee_charged_p50") + ht.Assert.Equal(50, result.FeeCharged.P60, "fee_charged_p60") + ht.Assert.Equal(50, result.FeeCharged.P70, "fee_charged_p70") + ht.Assert.Equal(50, result.FeeCharged.P80, "fee_charged_p80") + ht.Assert.Equal(50, result.FeeCharged.P90, "fee_charged_p90") + ht.Assert.Equal(50, result.FeeCharged.P95, "fee_charged_p95") + ht.Assert.Equal(50, result.FeeCharged.P99, "fee_charged_p99") } } @@ -310,32 +321,34 @@ func TestOperationFeeTestsActions_NotInterpolating(t *testing.T) { ht.Assert.Equal(16000, result.P99AcceptedFee, "p99") // 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") + ht.Assert.Equal(16000, result.MaxFee.Max, "max_fee_max") + ht.Assert.Equal(200, result.MaxFee.Min, "max_fee_min") + ht.Assert.Equal(400, result.MaxFee.Mode, "max_fee_mode") + ht.Assert.Equal(200, result.MaxFee.P10, "max_fee_p10") + ht.Assert.Equal(300, result.MaxFee.P20, "max_fee_p20") + ht.Assert.Equal(400, result.MaxFee.P30, "max_fee_p30") + ht.Assert.Equal(400, result.MaxFee.P40, "max_fee_p40") + ht.Assert.Equal(400, result.MaxFee.P50, "max_fee_p50") + ht.Assert.Equal(400, result.MaxFee.P60, "max_fee_p60") + ht.Assert.Equal(400, result.MaxFee.P70, "max_fee_p70") + ht.Assert.Equal(400, result.MaxFee.P80, "max_fee_p80") + ht.Assert.Equal(16000, result.MaxFee.P90, "max_fee_p90") + ht.Assert.Equal(16000, result.MaxFee.P95, "max_fee_p95") + ht.Assert.Equal(16000, result.MaxFee.P99, "max_fee_p99") - ht.Assert.Equal(6, result.FeeCharged.Min, "min") - ht.Assert.Equal(100, result.FeeCharged.Mode, "mode") - ht.Assert.Equal(6, result.FeeCharged.P10, "p10") - ht.Assert.Equal(100, result.FeeCharged.P20, "p20") - ht.Assert.Equal(100, result.FeeCharged.P30, "p30") - ht.Assert.Equal(100, result.FeeCharged.P40, "p40") - ht.Assert.Equal(100, result.FeeCharged.P50, "p50") - ht.Assert.Equal(100, result.FeeCharged.P60, "p60") - ht.Assert.Equal(100, result.FeeCharged.P70, "p70") - ht.Assert.Equal(100, result.FeeCharged.P80, "p80") - ht.Assert.Equal(100, result.FeeCharged.P90, "p90") - ht.Assert.Equal(100, result.FeeCharged.P95, "p95") - ht.Assert.Equal(100, result.FeeCharged.P99, "p99") + ht.Assert.Equal(100, result.FeeCharged.Max, "fee_charged_max") + ht.Assert.Equal(6, result.FeeCharged.Min, "fee_charged_min") + ht.Assert.Equal(100, result.FeeCharged.Mode, "fee_charged_mode") + ht.Assert.Equal(6, result.FeeCharged.P10, "fee_charged_p10") + ht.Assert.Equal(100, result.FeeCharged.P20, "fee_charged_p20") + ht.Assert.Equal(100, result.FeeCharged.P30, "fee_charged_p30") + ht.Assert.Equal(100, result.FeeCharged.P40, "fee_charged_p40") + ht.Assert.Equal(100, result.FeeCharged.P50, "fee_charged_p50") + ht.Assert.Equal(100, result.FeeCharged.P60, "fee_charged_p60") + ht.Assert.Equal(100, result.FeeCharged.P70, "fee_charged_p70") + ht.Assert.Equal(100, result.FeeCharged.P80, "fee_charged_p80") + ht.Assert.Equal(100, result.FeeCharged.P90, "fee_charged_p90") + ht.Assert.Equal(100, result.FeeCharged.P95, "fee_charged_p95") + ht.Assert.Equal(100, result.FeeCharged.P99, "fee_charged_p99") } } diff --git a/services/horizon/internal/app.go b/services/horizon/internal/app.go index 85a21b0c32..e8fc9c54f4 100644 --- a/services/horizon/internal/app.go +++ b/services/horizon/internal/app.go @@ -263,6 +263,7 @@ func (a *App) UpdateFeeStatsState() { // latest ledger's base fee for all if !feeStats.MaxFeeMode.Valid && !feeStats.MaxFeeMin.Valid { // MaxFee + next.FeeMax = next.LastBaseFee next.FeeMin = next.LastBaseFee next.FeeMode = next.LastBaseFee next.FeeP10 = next.LastBaseFee @@ -278,6 +279,7 @@ func (a *App) UpdateFeeStatsState() { next.FeeP99 = next.LastBaseFee // FeeCharged + next.FeeChargedMax = next.LastBaseFee next.FeeChargedMin = next.LastBaseFee next.FeeChargedMode = next.LastBaseFee next.FeeChargedP10 = next.LastBaseFee @@ -294,6 +296,7 @@ func (a *App) UpdateFeeStatsState() { } else { // MaxFee + next.FeeMax = feeStats.MaxFeeMax.Int64 next.FeeMin = feeStats.MaxFeeMin.Int64 next.FeeMode = feeStats.MaxFeeMode.Int64 next.FeeP10 = feeStats.MaxFeeP10.Int64 @@ -309,6 +312,7 @@ func (a *App) UpdateFeeStatsState() { next.FeeP99 = feeStats.MaxFeeP99.Int64 // FeeCharged + next.FeeChargedMax = feeStats.FeeChargedMax.Int64 next.FeeChargedMin = feeStats.FeeChargedMin.Int64 next.FeeChargedMode = feeStats.FeeChargedMode.Int64 next.FeeChargedP10 = feeStats.FeeChargedP10.Int64 diff --git a/services/horizon/internal/db2/history/main.go b/services/horizon/internal/db2/history/main.go index 55116d5e07..025200757f 100644 --- a/services/horizon/internal/db2/history/main.go +++ b/services/horizon/internal/db2/history/main.go @@ -292,6 +292,7 @@ type EffectType int // FeeStats is a row of data from the min, mode, percentile aggregate functions over the // `history_transactions` table. type FeeStats struct { + FeeChargedMax null.Int `db:"fee_charged_max"` FeeChargedMin null.Int `db:"fee_charged_min"` FeeChargedMode null.Int `db:"fee_charged_mode"` FeeChargedP10 null.Int `db:"fee_charged_p10"` @@ -305,6 +306,7 @@ type FeeStats struct { FeeChargedP90 null.Int `db:"fee_charged_p90"` FeeChargedP95 null.Int `db:"fee_charged_p95"` FeeChargedP99 null.Int `db:"fee_charged_p99"` + MaxFeeMax null.Int `db:"max_fee_max"` MaxFeeMin null.Int `db:"max_fee_min"` MaxFeeMode null.Int `db:"max_fee_mode"` MaxFeeP10 null.Int `db:"max_fee_p10"` diff --git a/services/horizon/internal/db2/history/operation.go b/services/horizon/internal/db2/history/operation.go index eae56e5869..e73d4fe753 100644 --- a/services/horizon/internal/db2/history/operation.go +++ b/services/horizon/internal/db2/history/operation.go @@ -44,6 +44,7 @@ func (r *Operation) UnmarshalDetails(dest interface{}) error { func (q *Q) FeeStats(currentSeq int32, dest *FeeStats) error { return q.GetRaw(dest, ` SELECT + ceil(max(fee_charged/operation_count))::bigint AS "fee_charged_max", ceil(min(fee_charged/operation_count))::bigint AS "fee_charged_min", ceil(mode() within group (order by fee_charged/operation_count))::bigint AS "fee_charged_mode", ceil(percentile_disc(0.10) WITHIN GROUP (ORDER BY fee_charged/operation_count))::bigint AS "fee_charged_p10", @@ -56,7 +57,8 @@ func (q *Q) FeeStats(currentSeq int32, dest *FeeStats) error { ceil(percentile_disc(0.80) WITHIN GROUP (ORDER BY fee_charged/operation_count))::bigint AS "fee_charged_p80", ceil(percentile_disc(0.90) WITHIN GROUP (ORDER BY fee_charged/operation_count))::bigint AS "fee_charged_p90", ceil(percentile_disc(0.95) WITHIN GROUP (ORDER BY fee_charged/operation_count))::bigint AS "fee_charged_p95", - ceil(percentile_disc(0.99) WITHIN GROUP (ORDER BY fee_charged/operation_count))::bigint AS "fee_charged_p99", + ceil(percentile_disc(0.99) WITHIN GROUP (ORDER BY fee_charged/operation_count))::bigint AS "fee_charged_p99", + ceil(max(max_fee/operation_count))::bigint AS "max_fee_max", ceil(min(max_fee/operation_count))::bigint AS "max_fee_min", ceil(mode() within group (order by max_fee/operation_count))::bigint AS "max_fee_mode", ceil(percentile_disc(0.10) WITHIN GROUP (ORDER BY max_fee/operation_count))::bigint AS "max_fee_p10", diff --git a/services/horizon/internal/operationfeestats/main.go b/services/horizon/internal/operationfeestats/main.go index c187eb0123..c30f0c9305 100644 --- a/services/horizon/internal/operationfeestats/main.go +++ b/services/horizon/internal/operationfeestats/main.go @@ -12,6 +12,7 @@ import ( // State represents a snapshot of horizon's view of the state of operation fee's // on the network. type State struct { + FeeChargedMax int64 FeeChargedMin int64 FeeChargedMode int64 FeeChargedP10 int64 @@ -27,6 +28,7 @@ type State struct { FeeChargedP99 int64 // MaxFee + FeeMax int64 FeeMin int64 FeeMode int64 FeeP10 int64