Skip to content

Commit

Permalink
chore: fix aimd fuzz testing (#87)
Browse files Browse the repository at this point in the history
* debugging

* tests

* fix

* wf

* wf
  • Loading branch information
Alex Johnson authored May 30, 2024
1 parent 57d7094 commit 1026235
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 24 deletions.
22 changes: 21 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,24 @@ jobs:
- name: tests
if: env.GIT_DIFF
run: |
make test-integration
make test-integration
test-fuzz:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: 1.22.3
cache: true
cache-dependency-path: go.sum
- uses: technote-space/[email protected]
id: git_diff
with:
PATTERNS: |
**/*.go
go.mod
go.sum
- name: tests
if: env.GIT_DIFF
run: |
make test-fuzz
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ test-e2e: $(TEST_E2E_DEPS)
test-unit:
@go test -v -race $(shell go list ./... | grep -v tests/)

test-fuzz:
@cd ./x/feemarket/fuzz && go test -v -race -rapid.checks=100_000 -p 4

test-integration:
@go test -v -race ./tests/integration

Expand All @@ -140,7 +143,7 @@ test-cover:
@go tool cover -html=$(COVER_FILE) -o $(COVER_HTML_FILE)
@rm $(COVER_FILE)

test-all: test-unit test-integration test-e2e
test-all: test-unit test-integration test-e2e test-fuzz

.PHONY: test-unit test-e2e test-integration test-cover test-all

Expand Down
65 changes: 43 additions & 22 deletions x/feemarket/fuzz/aimd_eip1559_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import (
"github.com/skip-mev/feemarket/x/feemarket/types"
)

// TestAIMDLearningRate ensure's that the additive increase
// TestAIMDLearningRate ensures that the additive increase
// multiplicative decrease learning rate algorithm correctly
// adjusts the learning rate. In particular, if the block
// utilization is greater than theta or less than 1 - theta, then
// the learning rate is increased by the additive increase
// parameter. Otherwise, the learning rate is decreased by
// the multiplicative decrease parameter.
func TestAIMDLearningRate(t *testing.T) {
t.Parallel()

rapid.Check(t, func(t *rapid.T) {
state := types.DefaultAIMDState()
window := rapid.Int64Range(1, 50).Draw(t, "window")
Expand Down Expand Up @@ -59,7 +61,7 @@ func TestAIMDLearningRate(t *testing.T) {
})
}

// TestAIMDBaseFee ensure's that the additive increase multiplicative
// TestAIMDBaseFee ensures that the additive increase multiplicative
// decrease base fee adjustment algorithm correctly adjusts the base
// fee. In particular, the base fee should function the same as the
// default EIP-1559 base fee adjustment algorithm.
Expand All @@ -84,32 +86,51 @@ func TestAIMDBaseFee(t *testing.T) {
t.Fatalf("block update errors: %v", err)
}

var total uint64
for _, utilization := range state.Window {
total += utilization
}

// Update the learning rate.
state.UpdateLearningRate(params)
lr := state.UpdateLearningRate(params)
// Update the base gas price.

var newPrice math.LegacyDec
func() {
defer func() {
if rec := recover(); rec != nil {
newPrice = params.MinBaseGasPrice
}
}()

// Calculate the new base gasPrice with the learning rate adjustment.
currentBlockSize := math.LegacyNewDecFromInt(math.NewIntFromUint64(state.Window[state.Index]))
targetBlockSize := math.LegacyNewDecFromInt(math.NewIntFromUint64(params.TargetBlockUtilization))
utilization := (currentBlockSize.Sub(targetBlockSize)).Quo(targetBlockSize)

// Truncate the learning rate adjustment to an integer.
//
// This is equivalent to
// 1 + (learningRate * (currentBlockSize - targetBlockSize) / targetBlockSize)
learningRateAdjustment := math.LegacyOneDec().Add(lr.Mul(utilization))

// Calculate the delta adjustment.
net := math.LegacyNewDecFromInt(state.GetNetUtilization(params)).Mul(params.Delta)

// Update the base gasPrice.
newPrice = prevBaseGasPrice.Mul(learningRateAdjustment).Add(net)
// Ensure the base gasPrice is greater than the minimum base gasPrice.
if newPrice.LT(params.MinBaseGasPrice) {
newPrice = params.MinBaseGasPrice
}
}()

state.UpdateBaseGasPrice(params)

// Ensure that the minimum base fee is always less than the base fee.
// Ensure that the minimum base fee is always less than the base gas price.
require.True(t, params.MinBaseGasPrice.LTE(state.BaseGasPrice))

switch {
case blockUtilization > params.TargetBlockUtilization:
require.True(t, state.BaseGasPrice.GTE(prevBaseGasPrice))
case blockUtilization < params.TargetBlockUtilization:
require.True(t, state.BaseGasPrice.LTE(prevBaseGasPrice))
default:

// Account for the delta adjustment.
net := state.GetNetUtilization(params)
switch {
case net.GT(math.ZeroInt()):
require.True(t, state.BaseGasPrice.GTE(prevBaseGasPrice))
case net.LT(math.ZeroInt()):
require.True(t, state.BaseGasPrice.LTE(prevBaseGasPrice))
default:
require.True(t, state.BaseGasPrice.Equal(prevBaseGasPrice))
}
}
require.Equal(t, newPrice, state.BaseGasPrice)

// Update the current height.
state.IncrementHeight()
Expand Down

0 comments on commit 1026235

Please sign in to comment.