Skip to content

Commit

Permalink
fix: base fee initialization, add logs and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paologalligit committed Feb 11, 2025
1 parent 91184c7 commit e082285
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 20 deletions.
1 change: 0 additions & 1 deletion consensus/fork/report.json

This file was deleted.

15 changes: 15 additions & 0 deletions packer/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/vechain/thor/v2/builtin"
"github.com/vechain/thor/v2/chain"
"github.com/vechain/thor/v2/consensus/fork"
"github.com/vechain/thor/v2/log"
"github.com/vechain/thor/v2/poa"
"github.com/vechain/thor/v2/runtime"
"github.com/vechain/thor/v2/state"
Expand All @@ -20,6 +21,8 @@ import (
"github.com/vechain/thor/v2/xenv"
)

var logger = log.WithContext("pkg", "packer")

Check failure on line 24 in packer/packer.go

View workflow job for this annotation

GitHub Actions / Lint / golangci-lint

var `logger` is unused (unused)

// Packer to pack txs and build new blocks.
type Packer struct {
repo *chain.Repository
Expand Down Expand Up @@ -126,6 +129,12 @@ func (p *Packer) Schedule(parent *chain.BlockSummary, nowTimestamp uint64) (flow

var baseFee *big.Int
if parent.Header.Number()+1 >= p.forkConfig.GALACTICA {
// TODO: log to be removed when fork is stable
if parent.Header.Number()-1 == p.forkConfig.GALACTICA {
log.Info("Last block before Galactica fork activates")
} else if parent.Header.Number() == p.forkConfig.GALACTICA {
log.Info("Galactica fork activated")
}
baseFee = fork.CalcBaseFee(&p.forkConfig, parent.Header)
}

Expand Down Expand Up @@ -164,6 +173,12 @@ func (p *Packer) Mock(parent *chain.BlockSummary, targetTime uint64, gasLimit ui

var baseFee *big.Int
if parent.Header.Number()+1 >= p.forkConfig.GALACTICA {
// TODO: log to be removed when fork is stable
if parent.Header.Number()-1 == p.forkConfig.GALACTICA {
log.Info("Last block before Galactica fork activates")
} else if parent.Header.Number() == p.forkConfig.GALACTICA {
log.Info("Galactica fork activated")
}
baseFee = fork.CalcBaseFee(&p.forkConfig, parent.Header)
}

Expand Down
17 changes: 5 additions & 12 deletions txpool/tx_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ func (o *txObject) Executable(chain *chain.Chain, state *state.State, headBlock
checkpoint := state.NewCheckpoint()
defer state.RevertTo(checkpoint)

isGalactica := headBlock.Number() >= forkConfig.GALACTICA
isGalactica := headBlock.Number()+1 >= forkConfig.GALACTICA

baseFee := headBlock.BaseFee()
// The block header before GALACTICA has no base fee, so it's initialized with InitialBaseFee.
if headBlock.Number() == forkConfig.GALACTICA {
baseFee = big.NewInt(thor.InitialBaseFee)
var baseFee *big.Int
// If the best block is the last block before galactica, we need to estimate the new block's base fee.
if isGalactica {
baseFee = fork.CalcBaseFee(forkConfig, headBlock)
}
galacticaItems := &fork.GalacticaItems{IsActive: isGalactica, BaseFee: baseFee}
_, _, payer, prepaid, _, err := o.resolved.BuyGas(state, headBlock.Timestamp()+thor.BlockInterval, galacticaItems)
Expand All @@ -124,13 +124,6 @@ func (o *txObject) Executable(chain *chain.Chain, state *state.State, headBlock
func sortTxObjsByOverallGasPriceDesc(txObjs []*txObject) {
sort.Slice(txObjs, func(i, j int) bool {
gp1, gp2 := txObjs[i].priorityGasPrice, txObjs[j].priorityGasPrice
// This is to make sure the zero gas price txs are always at the end of the list.
if gp1.Sign() == 0 {
return false
}
if gp2.Sign() == 0 {
return true
}
return gp1.Cmp(gp2) >= 0
})
}
2 changes: 2 additions & 0 deletions txpool/tx_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ func TestExecutable(t *testing.T) {
expectedErr string
}{
{newTx(tx.LegacyTxType, 0, nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), acc), true, ""},
{newTx(tx.LegacyTxType, 0, nil, b1.Header().GasLimit(), tx.BlockRef{}, 100, nil, tx.Features(0), acc), true, ""},
{newTx(tx.LegacyTxType, 0, nil, b1.Header().GasLimit()+1, tx.BlockRef{}, 100, nil, tx.Features(0), acc), false, "gas too large"},
{newTx(tx.LegacyTxType, 0, nil, math.MaxUint64, tx.BlockRef{}, 100, nil, tx.Features(0), acc), false, "gas too large"},
{newTx(tx.LegacyTxType, 0, nil, 21000, tx.BlockRef{1}, 100, nil, tx.Features(0), acc), true, "block ref out of schedule"},
{newTx(tx.LegacyTxType, 0, nil, 21000, tx.BlockRef{0}, 0, nil, tx.Features(0), acc), true, "expired"},
Expand Down
39 changes: 32 additions & 7 deletions txpool/tx_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"encoding/hex"
"fmt"
"math/big"
r "math/rand/v2"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -122,6 +123,9 @@ func FillPoolWithLegacyTxs(pool *TxPool, t *testing.T) {
}

func FillPoolWithDynFeeTxs(pool *TxPool, t *testing.T) {
// Advance one block to activate galactica and accept dynamic fee transactions
addOneBlock(t, pool)

// Create a slice of transactions to be added to the pool.
txs := make(Tx.Transactions, 0, 15)
for i := 0; i < 12; i++ {
Expand All @@ -138,6 +142,9 @@ func FillPoolWithDynFeeTxs(pool *TxPool, t *testing.T) {
}

func FillPoolWithMixedTxs(pool *TxPool, t *testing.T) {
// Advance one block to activate galactica and accept dynamic fee transactions
addOneBlock(t, pool)

// Create a slice of transactions to be added to the pool.
txs := make(Tx.Transactions, 0, 15)
for i := 0; i < 6; i++ {
Expand All @@ -155,6 +162,24 @@ func FillPoolWithMixedTxs(pool *TxPool, t *testing.T) {
assert.Equal(t, "tx rejected: pool is full", err.Error())
}

func addOneBlock(t *testing.T, pool *TxPool) {
var sig [65]byte
rand.Read(sig[:])

b1 := new(block.Builder).
ParentID(pool.repo.GenesisBlock().Header().ID()).
Timestamp(uint64(time.Now().Unix())).
TotalScore(100).
GasLimit(10000000).
StateRoot(pool.Dump().RootHash()).
BaseFee(big.NewInt(thor.InitialBaseFee)).
Build().WithSignature(sig[:])
if err := pool.repo.AddBlock(b1, nil, 0); err != nil {
t.Fatal(err)
}
pool.repo.SetBestBlockID(b1.Header().ID())
}

func TestAddWithFullErrorUnsyncedChain(t *testing.T) {
// First fill the pool with legacy transactions
pool := newPool(LIMIT, LIMIT_PER_ACCOUNT, &thor.NoFork)
Expand All @@ -163,11 +188,11 @@ func TestAddWithFullErrorUnsyncedChain(t *testing.T) {
FillPoolWithLegacyTxs(pool, t)

// Now fill the pool with dynamic fee transactions
pool = newPool(LIMIT, LIMIT_PER_ACCOUNT, &thor.ForkConfig{GALACTICA: 0})
pool = newPool(LIMIT, LIMIT_PER_ACCOUNT, &thor.ForkConfig{GALACTICA: 1})
FillPoolWithDynFeeTxs(pool, t)

// Now fill the pool with mixed transactions
pool = newPool(LIMIT, LIMIT_PER_ACCOUNT, &thor.ForkConfig{GALACTICA: 0})
pool = newPool(LIMIT, LIMIT_PER_ACCOUNT, &thor.ForkConfig{GALACTICA: 1})
FillPoolWithMixedTxs(pool, t)
}

Expand All @@ -177,10 +202,10 @@ func TestAddWithFullErrorSyncedChain(t *testing.T) {

FillPoolWithLegacyTxs(pool, t)

pool = newPoolWithParams(LIMIT, LIMIT_PER_ACCOUNT, "./", "", uint64(time.Now().Unix()), &thor.ForkConfig{GALACTICA: 0})
pool = newPoolWithParams(LIMIT, LIMIT_PER_ACCOUNT, "./", "", uint64(time.Now().Unix()), &thor.ForkConfig{GALACTICA: 1})
FillPoolWithDynFeeTxs(pool, t)

pool = newPoolWithParams(LIMIT, LIMIT_PER_ACCOUNT, "./", "", uint64(time.Now().Unix()), &thor.ForkConfig{GALACTICA: 0})
pool = newPoolWithParams(LIMIT, LIMIT_PER_ACCOUNT, "./", "", uint64(time.Now().Unix()), &thor.ForkConfig{GALACTICA: 1})
FillPoolWithMixedTxs(pool, t)
}

Expand Down Expand Up @@ -487,7 +512,7 @@ func TestOrderTxsAfterGalacticaForkSameValues(t *testing.T) {
repo.AddBlock(b1, tx.Receipts{}, 0)
repo.SetBestBlockID(b1.Header().ID())

totalPoolTxs := 10
totalPoolTxs := 10_000
pool := New(repo, state.NewStater(db), Options{
Limit: totalPoolTxs,
LimitPerAccount: totalPoolTxs,
Expand Down Expand Up @@ -524,8 +549,8 @@ func generateRandomTx(t *testing.T, seed int, chainTag byte) *tx.Transaction {
txType = tx.DynamicFeeTxType
}

maxFeePerGas := int64(thor.InitialBaseFee) // #nosec G404
maxPriorityFeePerGas := maxFeePerGas / 100 // #nosec G404
maxFeePerGas := int64(thor.InitialBaseFee + r.IntN(thor.InitialBaseFee)) // #nosec G404
maxPriorityFeePerGas := maxFeePerGas / int64(r.IntN(10)+1) // #nosec G404

tx, err := tx.NewTxBuilder(txType).
ChainTag(chainTag).
Expand Down

0 comments on commit e082285

Please sign in to comment.