Skip to content
This repository has been archived by the owner on Sep 23, 2023. It is now read-only.

Setup for stateful test of txpool #981

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,6 @@ require (
golang.org/x/tools v0.7.0 // indirect
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
pgregory.net/rapid v0.5.5 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -602,5 +602,7 @@ gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
pgregory.net/rapid v0.5.5 h1:jkgx1TjbQPD/feRoK+S/mXw9e1uj6WilpHrXJowi6oA=
pgregory.net/rapid v0.5.5/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04=
rsc.io/tmplfunc v0.0.3 h1:53XFQh69AfOa8Tw0Jm7t+GV7KZhOi6jzsCzTtKbMvzU=
rsc.io/tmplfunc v0.0.3/go.mod h1:AG3sTPzElb1Io3Yg4voV9AGZJuleGAwaVRxL9M49PhA=
139 changes: 139 additions & 0 deletions txpool/pool_stateful_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package txpool

import (
"context"
"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/gointerfaces/remote"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/kvcache"
"github.com/ledgerwatch/erigon-lib/kv/memdb"
"github.com/ledgerwatch/erigon-lib/txpool/txpoolcfg"
"github.com/ledgerwatch/erigon-lib/types"
"os"
"pgregory.net/rapid"
"testing"
)

// helper functions
func createDB(t *rapid.T) kv.RwDB {
t.Helper()

tempDir, err := os.MkdirTemp("", "pool-stateful-*")

if err != nil {
t.Fatalf("Could not create tempdir for test database: %v", err)
}

return memdb.New(tempDir)
}

func createPoolDB(t *rapid.T) kv.RwDB {
t.Helper()

tempDir, err := os.MkdirTemp("", "pool-stateful-*")

if err != nil {
t.Fatalf("Could not create tempdir for test database: %v", err)
}

return memdb.NewPoolDB(tempDir)
}

// Generators (aka Strategies)
var (
addressGenerator = rapid.Custom(func(t *rapid.T) common.Address {
bytes := rapid.SliceOfN(rapid.Byte(), 20, 20).Draw(t, "addressBytes")
return common.BytesToAddress(bytes)
})
)

// State machine initialization
type poolMachine struct {
p *TxPool
newTxs chan types.Announcements
ctx context.Context
coreDB kv.RwDB
poolDB kv.RwDB
blockHeight uint64
}

func (m *poolMachine) Init(t *rapid.T) {
newTxs := make(chan types.Announcements)
testDb := createDB(t)
cache := kvcache.New(kvcache.DefaultCoherentConfig)
pool, err := New(newTxs, testDb, txpoolcfg.DefaultConfig, cache, uint256.Int{10000}, nil)

if err != nil {
t.Fatalf("Error initializing TxPool: %v", err)
}

ctx := context.Background()

m.p = pool
m.newTxs = newTxs
m.ctx = ctx
m.coreDB = testDb
m.poolDB = createPoolDB(t)
m.blockHeight = 0
}

// Invariants
func (m *poolMachine) Check(t *rapid.T) {
// todo
}

// Test rules
func (m *poolMachine) AddLocalTxs(t *rapid.T) {
tx, err := m.coreDB.BeginRo(m.ctx)
if err != nil {
t.Fatalf("Could not open coreDB transaction")
}

transactions := types.TxSlots{}
m.p.AddLocalTxs(context.Background(), transactions, tx)
}

func (m *poolMachine) EmptyBlock(t *rapid.T) {
tx, err := m.poolDB.BeginRo(m.ctx)
if err != nil {
t.Fatalf("Could not open coreDB transaction")
}
defer tx.Rollback()

var (
batch remote.StateChangeBatch
unwindTxs types.TxSlots
newTxs types.TxSlots
stateChange remote.StateChange
)

gasFee := m.drawGasFee(t)
batch.PendingBlockBaseFee = gasFee

stateChange.BlockHeight = m.blockHeight + 1
batch.ChangeBatch = []*remote.StateChange{&stateChange}

err = m.p.OnNewBlock(m.ctx, &batch, unwindTxs, newTxs, tx)
if err != nil {
t.Fatalf("Error calling OnNewBlock: %v", err)
}

m.blockHeight += 1

actualGasFee := m.p.pendingBaseFee.Load()
if actualGasFee != gasFee {
t.Errorf("Pending base fee not set as expected: expected %d, got %d", gasFee, actualGasFee)
}
}

func (m *poolMachine) drawGasFee(t *rapid.T) uint64 {
return rapid.Uint64Range(9_000_000_000, 11_000_000_000).Draw(t, "GasFee")
}

// todo remaining rules

// Main test function
func TestTxPoolStateful(t *testing.T) {
rapid.Check(t, rapid.Run[*poolMachine]())
}