-
Notifications
You must be signed in to change notification settings - Fork 502
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
5,854 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package horizon | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/stellar/go/services/horizon/internal/operationfeestats" | ||
"github.com/stellar/go/support/render/hal" | ||
) | ||
|
||
// This file contains the actions: | ||
// | ||
// OperationFeeStatsAction: stats representing current state of network fees | ||
|
||
// OperationFeeStatsAction renders a few useful statistics that describe the | ||
// current state of operation fees on the network. | ||
type OperationFeeStatsAction struct { | ||
Action | ||
Min int64 | ||
Mode int64 | ||
LastBaseFee int64 | ||
LastLedger int64 | ||
} | ||
|
||
// JSON is a method for actions.JSON | ||
func (action *OperationFeeStatsAction) JSON() { | ||
action.Do( | ||
action.loadRecords, | ||
func() { | ||
hal.Render(action.W, map[string]string{ | ||
"min_accepted_fee": fmt.Sprint(action.Min), | ||
"mode_accepted_fee": fmt.Sprint(action.Mode), | ||
"last_ledger_base_fee": fmt.Sprint(action.LastBaseFee), | ||
"last_ledger": fmt.Sprint(action.LastLedger), | ||
}) | ||
}, | ||
) | ||
} | ||
|
||
func (action *OperationFeeStatsAction) loadRecords() { | ||
cur := operationfeestats.CurrentState() | ||
|
||
action.Min = cur.Min | ||
action.Mode = cur.Mode | ||
action.LastBaseFee = cur.LastBaseFee | ||
action.LastLedger = cur.LastLedger | ||
} |
56 changes: 56 additions & 0 deletions
56
services/horizon/internal/actions_operation_fee_stats_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package horizon | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
) | ||
|
||
func TestOperationFeeTestsActions_Show(t *testing.T) { | ||
|
||
testCases := []struct { | ||
scenario string | ||
min string | ||
mode string | ||
lastbasefee string | ||
}{ | ||
// happy path | ||
{ | ||
"operation_fee_stats_1", | ||
"100", | ||
"100", | ||
"100", | ||
}, | ||
// no transactions in last 5 ledgers | ||
{ | ||
"operation_fee_stats_2", | ||
"100", | ||
"100", | ||
"100", | ||
}, | ||
// transactions with varying fees | ||
{ | ||
"operation_fee_stats_3", | ||
"200", | ||
"400", | ||
"100", | ||
}, | ||
} | ||
|
||
for _, kase := range testCases { | ||
t.Run("/operation_fee_stats", func(t *testing.T) { | ||
ht := StartHTTPTest(t, kase.scenario) | ||
defer ht.Finish() | ||
|
||
w := ht.Get("/operation_fee_stats") | ||
|
||
if ht.Assert.Equal(200, w.Code) { | ||
var result map[string]string | ||
err := json.Unmarshal(w.Body.Bytes(), &result) | ||
ht.Require.NoError(err) | ||
ht.Assert.Equal(kase.min, result["min_accepted_fee"]) | ||
ht.Assert.Equal(kase.mode, result["mode_accepted_fee"]) | ||
ht.Assert.Equal(kase.lastbasefee, result["last_ledger_base_fee"]) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Package operationfeestats provides useful utilities concerning operation fee | ||
// stats within stellar,specifically as a central location to store a cached snapshot | ||
// of the state of network per operation fees and surge pricing. This package is | ||
// intended to be at the lowest levels of horizon's dependency tree, please keep | ||
// it free of dependencies to other horizon packages. | ||
package operationfeestats | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
// State represents a snapshot of horizon's view of the state of operation fee's | ||
// on the network. | ||
type State struct { | ||
Min int64 | ||
Mode int64 | ||
LastBaseFee int64 | ||
LastLedger int64 | ||
} | ||
|
||
// CurrentState returns the cached snapshot of operation fee state | ||
func CurrentState() State { | ||
lock.RLock() | ||
ret := current | ||
lock.RUnlock() | ||
return ret | ||
} | ||
|
||
// SetState updates the cached snapshot of the operation fee state | ||
func SetState(next State) { | ||
lock.Lock() | ||
// in case of one query taking longer than another, this makes | ||
// sure we don't overwrite the latest fee stats with old stats | ||
if current.LastLedger < next.LastLedger { | ||
current = next | ||
} | ||
lock.Unlock() | ||
} | ||
|
||
// ResetState is used only for testing purposes | ||
func ResetState() { | ||
current = State{} | ||
} | ||
|
||
var current State | ||
var lock sync.RWMutex |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.