Skip to content

Commit

Permalink
Add fee_charged to /fee_stats
Browse files Browse the repository at this point in the history
  • Loading branch information
abuiles committed Nov 21, 2019
1 parent 3d90ebd commit e005f1b
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 17 deletions.
1 change: 1 addition & 0 deletions protocols/horizon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ type FeeStats struct {
P95AcceptedFee int `json:"p95_accepted_fee,string"`
P99AcceptedFee int `json:"p99_accepted_fee,string"`
MaxFee feeStatsBase `json:"max_fee"`
FeeCharged feeStatsBase `json:"fee_charged"`
}

// TransactionsPage contains records of transaction information returned by Horizon
Expand Down
15 changes: 15 additions & 0 deletions services/horizon/internal/actions_operation_fee_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ func (action *FeeStatsAction) loadRecords() {

action.FeeStats.LedgerCapacityUsage = ledgerCapacityUsage

// FeeCharged
action.FeeStats.FeeCharged.Min = int(cur.FeeChargedMin)
action.FeeStats.FeeCharged.Mode = int(cur.FeeChargedMode)
action.FeeStats.FeeCharged.P10 = int(cur.FeeChargedP10)
action.FeeStats.FeeCharged.P20 = int(cur.FeeChargedP20)
action.FeeStats.FeeCharged.P30 = int(cur.FeeChargedP30)
action.FeeStats.FeeCharged.P40 = int(cur.FeeChargedP40)
action.FeeStats.FeeCharged.P50 = int(cur.FeeChargedP50)
action.FeeStats.FeeCharged.P60 = int(cur.FeeChargedP60)
action.FeeStats.FeeCharged.P70 = int(cur.FeeChargedP70)
action.FeeStats.FeeCharged.P80 = int(cur.FeeChargedP80)
action.FeeStats.FeeCharged.P90 = int(cur.FeeChargedP90)
action.FeeStats.FeeCharged.P95 = int(cur.FeeChargedP95)
action.FeeStats.FeeCharged.P99 = int(cur.FeeChargedP99)

// MaxFee
action.FeeStats.MaxFee.Min = int(cur.FeeMin)
action.FeeStats.MaxFee.Mode = int(cur.FeeMode)
Expand Down
98 changes: 96 additions & 2 deletions services/horizon/internal/actions_operation_fee_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ func TestOperationFeeTestsActions_Show(t *testing.T) {
p90 int
p95 int
p99 int
feeChargedMin int
feeChargedMode int
feeChargedP10 int
feeChargedP20 int
feeChargedP30 int
feeChargedP40 int
feeChargedP50 int
feeChargedP60 int
feeChargedP70 int
feeChargedP80 int
feeChargedP90 int
feeChargedP95 int
feeChargedP99 int
ledgerCapacityUsage float64
}{
// happy path
Expand All @@ -42,11 +55,25 @@ func TestOperationFeeTestsActions_Show(t *testing.T) {
p90: 100,
p95: 100,
p99: 100,
feeChargedMin: 100,
feeChargedMode: 100,
feeChargedP10: 100,
feeChargedP20: 100,
feeChargedP30: 100,
feeChargedP40: 100,
feeChargedP50: 100,
feeChargedP60: 100,
feeChargedP70: 100,
feeChargedP80: 100,
feeChargedP90: 100,
feeChargedP95: 100,
feeChargedP99: 100,
ledgerCapacityUsage: 0.04,
},
// no transactions in last 5 ledgers
{
scenario: "operation_fee_stats_2",
ledgerCapacityUsage: 0.00,
lastbasefee: 100,
min: 100,
mode: 100,
Expand All @@ -61,11 +88,24 @@ func TestOperationFeeTestsActions_Show(t *testing.T) {
p90: 100,
p95: 100,
p99: 100,
ledgerCapacityUsage: 0.00,
feeChargedMin: 100,
feeChargedMode: 100,
feeChargedP10: 100,
feeChargedP20: 100,
feeChargedP30: 100,
feeChargedP40: 100,
feeChargedP50: 100,
feeChargedP60: 100,
feeChargedP70: 100,
feeChargedP80: 100,
feeChargedP90: 100,
feeChargedP95: 100,
feeChargedP99: 100,
},
// transactions with varying fees
{
scenario: "operation_fee_stats_3",
ledgerCapacityUsage: 0.03,
lastbasefee: 100,
min: 200,
mode: 400,
Expand All @@ -80,7 +120,19 @@ func TestOperationFeeTestsActions_Show(t *testing.T) {
p90: 400,
p95: 400,
p99: 400,
ledgerCapacityUsage: 0.03,
feeChargedMin: 100,
feeChargedMode: 100,
feeChargedP10: 100,
feeChargedP20: 100,
feeChargedP30: 100,
feeChargedP40: 100,
feeChargedP50: 100,
feeChargedP60: 100,
feeChargedP70: 100,
feeChargedP80: 100,
feeChargedP90: 100,
feeChargedP95: 100,
feeChargedP99: 100,
},
}

Expand Down Expand Up @@ -132,6 +184,20 @@ func TestOperationFeeTestsActions_Show(t *testing.T) {
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")

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")
}
})
}
Expand Down Expand Up @@ -190,6 +256,20 @@ func TestOperationFeeTestsActions_ShowMultiOp(t *testing.T) {
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(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")
}
}

Expand Down Expand Up @@ -243,5 +323,19 @@ func TestOperationFeeTestsActions_NotInterpolating(t *testing.T) {
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(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")
}
}
33 changes: 33 additions & 0 deletions services/horizon/internal/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ func (a *App) UpdateFeeStatsState() {
// if no transactions in last 5 ledgers, return
// latest ledger's base fee for all
if !feeStats.MaxFeeMode.Valid && !feeStats.MaxFeeMin.Valid {
// MaxFee
next.FeeMin = next.LastBaseFee
next.FeeMode = next.LastBaseFee
next.FeeP10 = next.LastBaseFee
Expand All @@ -275,7 +276,24 @@ func (a *App) UpdateFeeStatsState() {
next.FeeP90 = next.LastBaseFee
next.FeeP95 = next.LastBaseFee
next.FeeP99 = next.LastBaseFee

// FeeCharged
next.FeeChargedMin = next.LastBaseFee
next.FeeChargedMode = next.LastBaseFee
next.FeeChargedP10 = next.LastBaseFee
next.FeeChargedP20 = next.LastBaseFee
next.FeeChargedP30 = next.LastBaseFee
next.FeeChargedP40 = next.LastBaseFee
next.FeeChargedP50 = next.LastBaseFee
next.FeeChargedP60 = next.LastBaseFee
next.FeeChargedP70 = next.LastBaseFee
next.FeeChargedP80 = next.LastBaseFee
next.FeeChargedP90 = next.LastBaseFee
next.FeeChargedP95 = next.LastBaseFee
next.FeeChargedP99 = next.LastBaseFee

} else {
// MaxFee
next.FeeMin = feeStats.MaxFeeMin.Int64
next.FeeMode = feeStats.MaxFeeMode.Int64
next.FeeP10 = feeStats.MaxFeeP10.Int64
Expand All @@ -289,6 +307,21 @@ func (a *App) UpdateFeeStatsState() {
next.FeeP90 = feeStats.MaxFeeP90.Int64
next.FeeP95 = feeStats.MaxFeeP95.Int64
next.FeeP99 = feeStats.MaxFeeP99.Int64

// FeeCharged
next.FeeChargedMin = feeStats.FeeChargedMin.Int64
next.FeeChargedMode = feeStats.FeeChargedMode.Int64
next.FeeChargedP10 = feeStats.FeeChargedP10.Int64
next.FeeChargedP20 = feeStats.FeeChargedP20.Int64
next.FeeChargedP30 = feeStats.FeeChargedP30.Int64
next.FeeChargedP40 = feeStats.FeeChargedP40.Int64
next.FeeChargedP50 = feeStats.FeeChargedP50.Int64
next.FeeChargedP60 = feeStats.FeeChargedP60.Int64
next.FeeChargedP70 = feeStats.FeeChargedP70.Int64
next.FeeChargedP80 = feeStats.FeeChargedP80.Int64
next.FeeChargedP90 = feeStats.FeeChargedP90.Int64
next.FeeChargedP95 = feeStats.FeeChargedP95.Int64
next.FeeChargedP99 = feeStats.FeeChargedP99.Int64
}

operationfeestats.SetState(next)
Expand Down
45 changes: 30 additions & 15 deletions services/horizon/internal/operationfeestats/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,37 @@ import (
// State represents a snapshot of horizon's view of the state of operation fee's
// on the network.
type State struct {
FeeMin int64
FeeMode int64
FeeP10 int64
FeeP20 int64
FeeP30 int64
FeeP40 int64
FeeP50 int64
FeeP60 int64
FeeP70 int64
FeeP80 int64
FeeP90 int64
FeeP95 int64
FeeP99 int64
LastBaseFee int64
LastLedger int64
FeeChargedMin int64
FeeChargedMode int64
FeeChargedP10 int64
FeeChargedP20 int64
FeeChargedP30 int64
FeeChargedP40 int64
FeeChargedP50 int64
FeeChargedP60 int64
FeeChargedP70 int64
FeeChargedP80 int64
FeeChargedP90 int64
FeeChargedP95 int64
FeeChargedP99 int64

// MaxFee
FeeMin int64
FeeMode int64
FeeP10 int64
FeeP20 int64
FeeP30 int64
FeeP40 int64
FeeP50 int64
FeeP60 int64
FeeP70 int64
FeeP80 int64
FeeP90 int64
FeeP95 int64
FeeP99 int64

LastBaseFee int64
LastLedger int64
LedgerCapacityUsage string
}

Expand Down

0 comments on commit e005f1b

Please sign in to comment.