-
Notifications
You must be signed in to change notification settings - Fork 468
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Collect/peak filter rolling tx fee stats
- Loading branch information
Showing
7 changed files
with
178 additions
and
25 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
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,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") | ||
} |
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