diff --git a/common/big.go b/common/big.go index 7d69425997..0e92ba4040 100644 --- a/common/big.go +++ b/common/big.go @@ -37,6 +37,9 @@ var ( Big8 = big.NewInt(8) Big10 = big.NewInt(10) Big32 = big.NewInt(32) + Big99 = big.NewInt(99) + Big100 = big.NewInt(100) + Big101 = big.NewInt(101) Big256 = big.NewInt(256) Big257 = big.NewInt(257) Big2e64 = new(big.Int).Exp(big.NewInt(2), big.NewInt(64), big.NewInt(0)) @@ -90,6 +93,9 @@ func SanityCheck(quitCh chan struct{}) { big8 := big.NewInt(8) big10 := big.NewInt(10) big32 := big.NewInt(32) + big99 := big.NewInt(99) + big100 := big.NewInt(100) + big101 := big.NewInt(101) big256 := big.NewInt(256) big257 := big.NewInt(257) big2e64 := new(big.Int).Exp(big.NewInt(2), big.NewInt(64), big.NewInt(0)) @@ -107,6 +113,9 @@ func SanityCheck(quitCh chan struct{}) { Big8 == nil || big8.Cmp(Big8) != 0 || Big10 == nil || big10.Cmp(Big10) != 0 || Big32 == nil || big32.Cmp(Big32) != 0 || + Big99 == nil || big99.Cmp(Big99) != 0 || + Big100 == nil || big100.Cmp(Big100) != 0 || + Big101 == nil || big101.Cmp(Big101) != 0 || Big256 == nil || big256.Cmp(Big256) != 0 || Big257 == nil || big257.Cmp(Big257) != 0 || Big2e64 == nil || big2e64.Cmp(Big2e64) != 0 || diff --git a/core/core.go b/core/core.go index fe2a1d5a8a..0787a1efbd 100644 --- a/core/core.go +++ b/core/core.go @@ -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()) diff --git a/core/state_processor.go b/core/state_processor.go index dae94b1ac2..da0cb6b13f 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -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" @@ -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, numElements *big.Int scope event.SubscriptionScope wg sync.WaitGroup // chain processing wait group for shutting down @@ -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 *big.Int ) start := time.Now() parent := p.hc.GetBlock(block.ParentHash(nodeCtx), block.NumberU64(nodeCtx)-1) @@ -392,6 +396,8 @@ func (p *StateProcessor) Process(block *types.WorkObject, batch ethdb.Batch) (ty } } + blockMinFee, blockMaxFee = calcTxStats(blockMinFee, blockMaxFee, qiTxFeeInQuai, numTxsProcessed) + totalEtxCoinbaseTime += time.Since(startEtxCoinbase) totalQiTime += time.Since(qiTimeBefore) totalQiProcessTimes["Sanity Checks"] += timing["Sanity Checks"] @@ -623,6 +629,7 @@ func (p *StateProcessor) Process(block *types.WorkObject, batch ethdb.Batch) (ty minGasPrice = new(big.Int).Set(gasPrice) } } + blockMinFee, blockMaxFee = calcTxStats(blockMinFee, blockMaxFee, fees, numTxsProcessed) } else { return nil, nil, nil, nil, 0, 0, 0, nil, nil, ErrTxTypeNotSupported @@ -768,6 +775,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, p.numElements, blockMinFee, blockMaxFee, quaiFees, numTxsProcessed) + p.logger.WithFields(log.Fields{ "signing time": common.PrettyDuration(timeSign), "prepare state time": common.PrettyDuration(timePrepare), @@ -1865,6 +1874,68 @@ 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 calcTxStats(blockMinFee, blockMaxFee, txFee, numTxsProcessed *big.Int) (newBlockMinFee, newBlockMaxFee *big.Int) { + + if numTxsProcessed.Cmp(common.Big0) == 0 { + numTxsProcessed.Add(numTxsProcessed, common.Big1) + blockMinFee = new(big.Int).Set(txFee) + blockMaxFee = new(big.Int).Set(txFee) + return blockMinFee, blockMaxFee + } + + numTxsProcessed = numTxsProcessed.Add(numTxsProcessed, common.Big1) + blockMinFee = bigMath.BigMin(txFee, blockMinFee) + blockMaxFee = bigMath.BigMax(txFee, blockMaxFee) + + return blockMinFee, blockMaxFee +} + +func calcRollingFeeInfo(rollingMinFee, rollingMaxFee, rollingAvgFee, rollingNumElements, blockMinFee, blockMaxFee, blockTotalFees, numTxsProcessed *big.Int) (min, max, avg, num *big.Int) { + + // Implement peak/envelope filter + if numTxsProcessed.Cmp(common.Big0) == 0 { + // Block values will be nil, so don't compare or update. + return rollingMinFee, rollingMaxFee, rollingAvgFee, rollingNumElements + } + if rollingMinFee == nil || blockMinFee.Cmp(rollingMinFee) < 0 { + // If the new minimum is less than the old minimum, overwrite it. + rollingMinFee = new(big.Int).Set(blockMinFee) + } else { + // If not, increase the old minimum by 1%. + rollingMinFee.Mul(rollingMinFee, common.Big101) + rollingMinFee.Div(rollingMinFee, common.Big100) + } + + if rollingMaxFee == nil || blockMaxFee.Cmp(rollingMaxFee) > 0 { + rollingMaxFee = new(big.Int).Set(blockMaxFee) + } else { + // Decay the max fee by 1%. + rollingMaxFee.Mul(rollingMaxFee, common.Big99) + rollingMaxFee.Div(rollingMaxFee, common.Big100) + } + + // Implement running average + if rollingAvgFee == nil { + rollingAvgFee = big.NewInt(1) + rollingNumElements = big.NewInt(0) + } + + if numTxsProcessed.Cmp(common.Big0) > 0 { + blockAvgFee := blockTotalFees.Div(blockTotalFees, numTxsProcessed) + intermediateVal := new(big.Int).Mul(rollingNumElements, rollingAvgFee) + intermediateVal = intermediateVal.Add(intermediateVal, blockAvgFee) + + rollingNumElements.Add(rollingNumElements, common.Big1) + rollingAvgFee = intermediateVal.Div(intermediateVal, rollingNumElements) + } + + return rollingMinFee, rollingMaxFee, rollingAvgFee, rollingNumElements +} + +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. diff --git a/core/types/transaction.go b/core/types/transaction.go index ed93ae672a..c15fda8831 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -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) diff --git a/internal/quaiapi/api.go b/internal/quaiapi/api.go index 45f507cf67..f3f1a8b9e7 100644 --- a/internal/quaiapi/api.go +++ b/internal/quaiapi/api.go @@ -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 { diff --git a/internal/quaiapi/backend.go b/internal/quaiapi/backend.go index 3c4be44f3e..59334320db 100644 --- a/internal/quaiapi/backend.go +++ b/internal/quaiapi/backend.go @@ -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) diff --git a/quai/api_backend.go b/quai/api_backend.go index 6041cc7a1d..80e0c7bac0 100644 --- a/quai/api_backend.go +++ b/quai/api_backend.go @@ -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() }