Skip to content

Commit

Permalink
Collect/peak filter rolling tx fee stats
Browse files Browse the repository at this point in the history
  • Loading branch information
Djadih committed Dec 17, 2024
1 parent a0fc38f commit a46f628
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 25 deletions.
4 changes: 4 additions & 0 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,10 @@ func (c *Core) SendTxToSharingClients(tx *types.Transaction) {
c.sl.txPool.SendTxToSharingClients(tx)
}

func (c *Core) GetRollingFeeInfo() (min, max, avg *big.Int) {
return c.Processor().GetRollingFeeInfo()
}

func (c *Core) SuggestFinalityDepth(qiValue *big.Int, correlatedRisk *big.Int) *big.Int {
qiRewardPerBlock := misc.CalculateQiReward(c.CurrentHeader().WorkObjectHeader())

Expand Down
111 changes: 87 additions & 24 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
lru "github.com/hashicorp/golang-lru/v2"

"github.com/dominant-strategies/go-quai/common"
bigMath "github.com/dominant-strategies/go-quai/common/math"
"github.com/dominant-strategies/go-quai/common/prque"
"github.com/dominant-strategies/go-quai/consensus"
"github.com/dominant-strategies/go-quai/consensus/misc"
Expand Down Expand Up @@ -107,19 +108,20 @@ var defaultCacheConfig = &CacheConfig{
//
// StateProcessor implements Processor.
type StateProcessor struct {
config *params.ChainConfig // Chain configuration options
hc *HeaderChain // Canonical block chain
engine consensus.Engine // Consensus engine used for block rewards
logsFeed event.Feed
rmLogsFeed event.Feed
cacheConfig *CacheConfig // CacheConfig for StateProcessor
stateCache state.Database // State database to reuse between imports (contains state cache)
etxCache state.Database // ETX database to reuse between imports (contains ETX cache)
receiptsCache *lru.Cache[common.Hash, types.Receipts] // Cache for the most recent receipts per block
txLookupCache *lru.Cache[common.Hash, rawdb.LegacyTxLookupEntry]
validator Validator // Block and state validator interface
prefetcher Prefetcher
vmConfig vm.Config
config *params.ChainConfig // Chain configuration options
hc *HeaderChain // Canonical block chain
engine consensus.Engine // Consensus engine used for block rewards
logsFeed event.Feed
rmLogsFeed event.Feed
cacheConfig *CacheConfig // CacheConfig for StateProcessor
stateCache state.Database // State database to reuse between imports (contains state cache)
etxCache state.Database // ETX database to reuse between imports (contains ETX cache)
receiptsCache *lru.Cache[common.Hash, types.Receipts] // Cache for the most recent receipts per block
txLookupCache *lru.Cache[common.Hash, rawdb.LegacyTxLookupEntry]
validator Validator // Block and state validator interface
prefetcher Prefetcher
vmConfig vm.Config
minFee, maxFee, avgFee *big.Int

scope event.SubscriptionScope
wg sync.WaitGroup // chain processing wait group for shutting down
Expand Down Expand Up @@ -221,17 +223,19 @@ type UtxosCreatedDeleted struct {
// transactions failed to execute due to insufficient gas it will return an error.
func (p *StateProcessor) Process(block *types.WorkObject, batch ethdb.Batch) (types.Receipts, []*types.Transaction, []*types.Log, *state.StateDB, uint64, uint64, uint64, *multiset.MultiSet, []common.Unlock, error) {
var (
receipts types.Receipts
usedGas = new(uint64)
usedState = new(uint64)
header = types.CopyWorkObject(block)
blockHash = block.Hash()
nodeLocation = p.hc.NodeLocation()
nodeCtx = p.hc.NodeCtx()
blockNumber = block.Number(nodeCtx)
parentHash = block.ParentHash(nodeCtx)
allLogs []*types.Log
gp = new(types.GasPool).AddGas(block.GasLimit())
receipts types.Receipts
usedGas = new(uint64)
usedState = new(uint64)
header = types.CopyWorkObject(block)
blockHash = block.Hash()
nodeLocation = p.hc.NodeLocation()
nodeCtx = p.hc.NodeCtx()
blockNumber = block.Number(nodeCtx)
parentHash = block.ParentHash(nodeCtx)
allLogs []*types.Log
gp = new(types.GasPool).AddGas(block.GasLimit())
numTxsProcessed = big.NewInt(0)
blockMinFee, blockMaxFee, blockAvgFee *big.Int
)
start := time.Now()
parent := p.hc.GetBlock(block.ParentHash(nodeCtx), block.NumberU64(nodeCtx)-1)
Expand Down Expand Up @@ -392,6 +396,8 @@ func (p *StateProcessor) Process(block *types.WorkObject, batch ethdb.Batch) (ty
}
}

calcQiTxStats(blockMinFee, blockMaxFee, blockAvgFee, qiTxFee, numTxsProcessed)

totalEtxCoinbaseTime += time.Since(startEtxCoinbase)
totalQiTime += time.Since(qiTimeBefore)
totalQiProcessTimes["Sanity Checks"] += timing["Sanity Checks"]
Expand Down Expand Up @@ -768,6 +774,8 @@ func (p *StateProcessor) Process(block *types.WorkObject, batch ethdb.Batch) (ty
}
time5 := common.PrettyDuration(time.Since(start))

calcRollingFeeInfo(p.minFee, p.maxFee, p.avgFee, blockMinFee, blockMaxFee, blockAvgFee, numTxsProcessed)

p.logger.WithFields(log.Fields{
"signing time": common.PrettyDuration(timeSign),
"prepare state time": common.PrettyDuration(timePrepare),
Expand Down Expand Up @@ -1867,6 +1875,61 @@ func (p *StateProcessor) StateAtTransaction(block *types.WorkObject, txIndex int
return nil, vm.BlockContext{}, nil, fmt.Errorf("transaction index %d out of range for block %#x", txIndex, block.Hash())
}

func calcQiTxStats(blockMinFee, blockMaxFee, blockAvgFee, qiTxFee, numTxsProcessed *big.Int) (newBlockMinFee, newBlockMaxFee, newBlockAvgFee *big.Int) {
numTxsProcessed = numTxsProcessed.Add(numTxsProcessed, common.Big1)
val := numTxsProcessed.Cmp(common.Big0)

if val == 1 {
blockMinFee = new(big.Int).Set(qiTxFee)
blockMaxFee = new(big.Int).Set(qiTxFee)
blockAvgFee = new(big.Int).Set(qiTxFee)
return blockMinFee, blockMaxFee, blockAvgFee
}

blockMinFee = bigMath.BigMin(qiTxFee, blockMinFee)
blockMaxFee = bigMath.BigMax(qiTxFee, blockMaxFee)
intermediateAvg := new(big.Int).Add(blockAvgFee, qiTxFee)
blockAvgFee = intermediateAvg.Div(intermediateAvg, numTxsProcessed)

return blockMinFee, blockMaxFee, blockAvgFee
}

func calcRollingFeeInfo(rollingMinFee, rollingMaxFee, rollingAvgFee, blockMinFee, blockMaxFee, blockAvgFee, numTxsProcessed *big.Int) (min, max, avg *big.Int) {
decay := func(fee *big.Int) {
fee = fee.Mul(fee, big.NewInt(99))
fee.Set(fee.Div(fee, big.NewInt(100)))
}

// Implement peak/envelope filter
if rollingMinFee == nil || blockMinFee.Cmp(rollingMinFee) < 0 {
// If the new minimum is less than the old minimum, overwrite it.
rollingMinFee = blockMinFee
} else {
// If not, decay the old minimum by 1%.
decay(rollingMinFee)
}

if rollingMaxFee == nil || blockMaxFee.Cmp(rollingMaxFee) > 0 {
rollingMaxFee = blockMaxFee
} else {
decay(rollingMaxFee)
}

// Implement running average
if rollingAvgFee == nil {
rollingAvgFee = common.Big1
}
intermediateVal := new(big.Int).Mul(new(big.Int).Sub(numTxsProcessed, common.Big1), rollingAvgFee)
intermediateVal = intermediateVal.Add(intermediateVal, blockAvgFee)
rollingAvgFee = intermediateVal.Div(intermediateVal, numTxsProcessed)

return rollingMinFee, rollingMaxFee, rollingAvgFee
}

func (p *StateProcessor) GetRollingFeeInfo() (min, max, avg *big.Int) {
return p.minFee, p.maxFee, p.avgFee
}

func (p *StateProcessor) Stop() {
// Ensure all live cached entries be saved into disk, so that we can skip
// cache warmup when node restarts.
Expand Down
72 changes: 72 additions & 0 deletions core/state_processor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package core

import (
"math/big"
"math/rand"
"testing"

"github.com/dominant-strategies/go-quai/common"
"github.com/stretchr/testify/require"
)

func generateRandomNumbers(min, max, count int) (simpleNums []int, bigNums []*big.Int) {
simpleNums = make([]int, count)
bigNums = make([]*big.Int, count)

for i := 0; i < count; i++ {
randNum := rand.Intn(max-min+1) + min
simpleNums[i] = randNum
bigNums[i] = big.NewInt(int64(randNum))
}

return simpleNums, bigNums
}

func TestSingleBlockTxStats(t *testing.T) {
const testLength = 1000
simpleFees, bigFees := generateRandomNumbers(1000, 1000000000, testLength)

numTxsProcessed := 0
var expectedMin int
var expectedMax int
var sum int
for _, fee := range simpleFees {
if numTxsProcessed == 0 {
expectedMin = fee
expectedMax = fee
sum = 0
} else {
if fee < expectedMin {
expectedMin = fee
} else {
expectedMin = expectedMin * 99 / 100
}

if fee > expectedMax {
expectedMax = fee
} else {
expectedMax = expectedMax * 99 / 100
}

}

sum += fee
numTxsProcessed += 1
}
expectedAvg := sum / numTxsProcessed

bigTxsProcessed := new(big.Int).Set(common.Big0)
var blockMin, blockMax, blockAvg *big.Int
var actualMin *big.Int
var actualMax *big.Int
var actualAvg *big.Int
for _, fee := range bigFees {
blockMin, blockMax, blockAvg := calcQiTxStats(blockMin, blockMax, blockAvg, fee, bigTxsProcessed)
actualMin, actualMax, actualAvg = calcRollingFeeInfo(actualMin, actualMax, actualAvg, blockMin, blockMax, blockAvg, bigTxsProcessed)
}

require.Equal(t, uint64(expectedMin), actualMin.Uint64(), "Expected min not equal")
require.Equal(t, uint64(expectedMax), actualMax.Uint64(), "Expected max not equal")
// Account for maxmium error of 1 unit of gas due to float math for average
require.InEpsilon(t, uint64(expectedAvg), actualAvg.Uint64(), float64(1), "Expected average not equal")
}
2 changes: 1 addition & 1 deletion core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const (
c_MaxTxForSorting = 1500
)

// Transaction is a Quai transaction.
// Transaction can be a Quai, Qi, or External transaction.
type Transaction struct {
inner TxData // Consensus contents of a transaction
time time.Time // Time first seen locally (spam avoidance)
Expand Down
9 changes: 9 additions & 0 deletions internal/quaiapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ func (s *PublicTxPoolAPI) Inspect() map[string]map[string]map[string]string {
return content
}

func (s *PublicTxPoolAPI) GetRollingFeeInfo() (min, max, avg *hexutil.Big) {
bigMin, bigMax, bigAvg := s.b.GetRollingFeeInfo()
min = (*hexutil.Big)(bigMin)
max = (*hexutil.Big)(bigMax)
avg = (*hexutil.Big)(bigAvg)

return min, max, avg
}

// PublicBlockChainAPI provides an API to access the Quai blockchain.
// It offers only methods that operate on public data that is freely available to anyone.
type PublicBlockChainAPI struct {
Expand Down
1 change: 1 addition & 0 deletions internal/quaiapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ type Backend interface {
GetMinGasPrice() *big.Int
GetPoolGasPrice() *big.Int
SendTxToSharingClients(tx *types.Transaction)
GetRollingFeeInfo() (min, max, avg *big.Int)

// Filter API
BloomStatus() (uint64, uint64)
Expand Down
4 changes: 4 additions & 0 deletions quai/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,10 @@ func (b *QuaiAPIBackend) SendTxToSharingClients(tx *types.Transaction) {
b.quai.core.SendTxToSharingClients(tx)
}

func (b *QuaiAPIBackend) GetRollingFeeInfo() (min, max, avg *big.Int) {
return b.quai.core.GetRollingFeeInfo()
}

func (b *QuaiAPIBackend) GetMinGasPrice() *big.Int {
return b.quai.core.GetMinGasPrice()
}
Expand Down

0 comments on commit a46f628

Please sign in to comment.