Skip to content

Commit

Permalink
refactor: add comments, rename functions and refactor init test chain…
Browse files Browse the repository at this point in the history
… function
  • Loading branch information
paologalligit committed Feb 7, 2025
1 parent b103881 commit 72f9340
Show file tree
Hide file tree
Showing 27 changed files with 59 additions and 69 deletions.
2 changes: 1 addition & 1 deletion api/accounts/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func getStorageWithNonExistingRevision(t *testing.T) {
}

func initAccountServer(t *testing.T, enabledDeprecated bool) {
thorChain, err := testchain.NewIntegrationTestChain()
thorChain, err := testchain.NewIntegrationTestChain(testchain.IntTestChainForkConfig)
require.NoError(t, err)

genesisBlock = thorChain.GenesisBlock()
Expand Down
2 changes: 1 addition & 1 deletion api/admin/health/health_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestHealth(t *testing.T) {
}

func initAPIServer(t *testing.T) {
thorChain, err := testchain.NewIntegrationTestChain()
thorChain, err := testchain.NewIntegrationTestChain(testchain.IntTestChainForkConfig)
require.NoError(t, err)

router := mux.NewRouter()
Expand Down
2 changes: 1 addition & 1 deletion api/blocks/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func initBlockServer(t *testing.T) {
GALACTICA: 1,
VIP214: 2,
}
thorChain, err := testchain.NewIntegrationTestChainWithFork(forks)
thorChain, err := testchain.NewIntegrationTestChain(forks)
require.NoError(t, err)

addr := thor.BytesToAddress([]byte("to"))
Expand Down
2 changes: 1 addition & 1 deletion api/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ func initDebugServer(t *testing.T) {
GALACTICA: 1,
VIP214: 2,
}
thorChain, err := testchain.NewIntegrationTestChainWithFork(forkConf)
thorChain, err := testchain.NewIntegrationTestChain(forkConf)
require.NoError(t, err)

addr := thor.BytesToAddress([]byte("to"))
Expand Down
2 changes: 1 addition & 1 deletion api/events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func testEventWithBlocks(t *testing.T, expectedBlocks int) {

// Init functions
func initEventServer(t *testing.T, limit uint64) *testchain.Chain {
thorChain, err := testchain.NewIntegrationTestChain()
thorChain, err := testchain.NewIntegrationTestChain(testchain.IntTestChainForkConfig)
require.NoError(t, err)

router := mux.NewRouter()
Expand Down
4 changes: 2 additions & 2 deletions api/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func init() {
}

func TestMetricsMiddleware(t *testing.T) {
thorChain, err := testchain.NewIntegrationTestChain()
thorChain, err := testchain.NewIntegrationTestChain(testchain.IntTestChainForkConfig)
require.NoError(t, err)

// inject some invalid data to db
Expand Down Expand Up @@ -99,7 +99,7 @@ func TestMetricsMiddleware(t *testing.T) {
}

func TestWebsocketMetrics(t *testing.T) {
thorChain, err := testchain.NewIntegrationTestChain()
thorChain, err := testchain.NewIntegrationTestChain(testchain.IntTestChainForkConfig)
require.NoError(t, err)

router := mux.NewRouter()
Expand Down
2 changes: 1 addition & 1 deletion api/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestNode(t *testing.T) {
}

func initCommServer(t *testing.T) {
thorChain, err := testchain.NewIntegrationTestChain()
thorChain, err := testchain.NewIntegrationTestChain(testchain.IntTestChainForkConfig)
require.NoError(t, err)

communicator := comm.New(
Expand Down
5 changes: 3 additions & 2 deletions api/subscriptions/beat2_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ func TestBeat2Reader_Read_NoNewBlocksToRead(t *testing.T) {
thorChain := initChain(t)
allBlocks, err := thorChain.GetAllBlocks()
require.NoError(t, err)
newBlock := allBlocks[2]
// taking best block to include also galactica block
bestBlk := allBlocks[len(allBlocks)-1]

// Act
beatReader := newBeat2Reader(thorChain.Repo(), newBlock.Header().ID(), newMessageCache[Beat2Message](10))
beatReader := newBeat2Reader(thorChain.Repo(), bestBlk.Header().ID(), newMessageCache[Beat2Message](10))
res, ok, err := beatReader.Read()

// Assert
Expand Down
5 changes: 3 additions & 2 deletions api/subscriptions/beat_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ func TestBeatReader_Read_NoNewBlocksToRead(t *testing.T) {
thorChain := initChain(t)
allBlocks, err := thorChain.GetAllBlocks()
require.NoError(t, err)
newBlock := allBlocks[len(allBlocks)-1]
// taking best block to include also galactica block
bestBlock := allBlocks[len(allBlocks)-1]

// Act
beatReader := newBeatReader(thorChain.Repo(), newBlock.Header().ID(), newMessageCache[BeatMessage](10))
beatReader := newBeatReader(thorChain.Repo(), bestBlock.Header().ID(), newMessageCache[BeatMessage](10))
res, ok, err := beatReader.Read()

// Assert
Expand Down
2 changes: 1 addition & 1 deletion api/subscriptions/block_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func initChain(t *testing.T) *testchain.Chain {
GALACTICA: 1,
VIP214: 2,
}
thorChain, err := testchain.NewIntegrationTestChainWithFork(forks)
thorChain, err := testchain.NewIntegrationTestChain(forks)
require.NoError(t, err)

addr := thor.BytesToAddress([]byte("to"))
Expand Down
7 changes: 4 additions & 3 deletions api/subscriptions/event_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func TestEventReader_Read(t *testing.T) {
allBlocks, err := thorChain.GetAllBlocks()
require.NoError(t, err)
genesisBlk := allBlocks[0]
newBlock := allBlocks[len(allBlocks)-1]
// taking best block to include also galactica block
bestBlk := allBlocks[len(allBlocks)-1]

er := &eventReader{
repo: thorChain.Repo(),
Expand Down Expand Up @@ -57,8 +58,8 @@ func TestEventReader_Read(t *testing.T) {
}
assert.Equal(t, 2, len(eventMessages))
eventMsg := eventMessages[0]
assert.Equal(t, newBlock.Header().ID(), eventMsg.Meta.BlockID)
assert.Equal(t, newBlock.Header().Number(), eventMsg.Meta.BlockNumber)
assert.Equal(t, bestBlk.Header().ID(), eventMsg.Meta.BlockID)
assert.Equal(t, bestBlk.Header().Number(), eventMsg.Meta.BlockNumber)
}

type mockBlockReaderWithError struct{}
Expand Down
4 changes: 2 additions & 2 deletions api/subscriptions/subscriptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func TestParseAddress(t *testing.T) {
}

func initSubscriptionsServer(t *testing.T, enabledDeprecated bool) {
thorChain, err := testchain.NewIntegrationTestChain()
thorChain, err := testchain.NewIntegrationTestChain(testchain.IntTestChainForkConfig)
require.NoError(t, err)

txPool := txpool.New(thorChain.Repo(), thorChain.Stater(), txpool.Options{
Expand Down Expand Up @@ -280,7 +280,7 @@ func initSubscriptionsServer(t *testing.T, enabledDeprecated bool) {
}

func TestSubscriptionsBacktrace(t *testing.T) {
thorChain, err := testchain.NewIntegrationTestChain()
thorChain, err := testchain.NewIntegrationTestChain(testchain.IntTestChainForkConfig)
require.NoError(t, err)

txPool := txpool.New(thorChain.Repo(), thorChain.Stater(), txpool.Options{
Expand Down
5 changes: 3 additions & 2 deletions api/subscriptions/transfer_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ func TestTransferReader_Read_NoNewBlocksToRead(t *testing.T) {
thorChain := initChain(t)
allBlocks, err := thorChain.GetAllBlocks()
require.NoError(t, err)
newBlock := allBlocks[len(allBlocks)-1]
// taking best block to include also galactica block
bestBlk := allBlocks[len(allBlocks)-1]
filter := &TransferFilter{}

// Act
br := newTransferReader(thorChain.Repo(), newBlock.Header().ID(), filter)
br := newTransferReader(thorChain.Repo(), bestBlk.Header().ID(), filter)
res, ok, err := br.Read()

// Assert
Expand Down
7 changes: 1 addition & 6 deletions api/transactions/transactions_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,6 @@ func packTxsIntoBlock(thorChain *testchain.Chain, proposerAccount *genesis.DevAc
}

func createChain(db *muxdb.MuxDB, accounts []genesis.DevAccount) (*testchain.Chain, error) {
forkConfig := thor.NoFork
forkConfig.VIP191 = 1
forkConfig.BLOCKLIST = 0
forkConfig.VIP214 = 2

// Create the state manager (Stater) with the initialized database.
stater := state.NewStater(db)

Expand All @@ -399,7 +394,7 @@ func createChain(db *muxdb.MuxDB, accounts []genesis.DevAccount) (*testchain.Cha
LaunchTime: 1526400000,
GasLimit: thor.InitialGasLimit,
ExtraData: "",
ForkConfig: &forkConfig,
ForkConfig: &testchain.IntTestChainForkConfig,
Authority: authAccs,
Accounts: stateAccs,
Params: genesis.Params{
Expand Down
7 changes: 2 additions & 5 deletions api/transactions/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,10 @@ func httpPostAndCheckResponseStatus(t *testing.T, url string, obj interface{}, r
}

func initTransactionServer(t *testing.T) {
forkConfig := thor.NoFork
forkConfig.BLOCKLIST = 0
forkConfig.VIP191 = 1
forkConfig.VIP214 = 2
forkConfig := testchain.IntTestChainForkConfig
forkConfig.GALACTICA = 2

thorChain, err := testchain.NewIntegrationTestChainWithFork(forkConfig)
thorChain, err := testchain.NewIntegrationTestChain(forkConfig)
require.NoError(t, err)

chainTag = thorChain.Repo().ChainTag()
Expand Down
2 changes: 1 addition & 1 deletion api/transfers/transfers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func insertBlocks(t *testing.T, db *logdb.LogDB, n int) {
}

func initTransferServer(t *testing.T, logDb *logdb.LogDB, limit uint64) {
thorChain, err := testchain.NewIntegrationTestChain()
thorChain, err := testchain.NewIntegrationTestChain(testchain.IntTestChainForkConfig)
require.NoError(t, err)

router := mux.NewRouter()
Expand Down
4 changes: 2 additions & 2 deletions api/utils/revisions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestAllowNext(t *testing.T) {
}

func TestGetSummary(t *testing.T) {
thorChain, err := testchain.NewIntegrationTestChain()
thorChain, err := testchain.NewIntegrationTestChain(testchain.IntTestChainForkConfig)
require.NoError(t, err)

// Test cases
Expand Down Expand Up @@ -153,7 +153,7 @@ func TestGetSummary(t *testing.T) {
}

func TestGetSummaryAndState(t *testing.T) {
thorChain, err := testchain.NewIntegrationTestChain()
thorChain, err := testchain.NewIntegrationTestChain(testchain.IntTestChainForkConfig)
require.NoError(t, err)

b := thorChain.GenesisBlock()
Expand Down
7 changes: 1 addition & 6 deletions cmd/thor/node/node_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,6 @@ func packTxsIntoBlock(thorChain *testchain.Chain, proposerAccount *genesis.DevAc
}

func createChain(db *muxdb.MuxDB, accounts []genesis.DevAccount) (*testchain.Chain, error) {
forkConfig := thor.NoFork
forkConfig.VIP191 = 1
forkConfig.BLOCKLIST = 0
forkConfig.VIP214 = 2

// Create the state manager (Stater) with the initialized database.
stater := state.NewStater(db)

Expand All @@ -386,7 +381,7 @@ func createChain(db *muxdb.MuxDB, accounts []genesis.DevAccount) (*testchain.Cha
LaunchTime: 1526400000,
GasLimit: thor.InitialGasLimit,
ExtraData: "",
ForkConfig: &forkConfig,
ForkConfig: &testchain.IntTestChainForkConfig,
Authority: authAccs,
Accounts: stateAccs,
Params: genesis.Params{
Expand Down
6 changes: 2 additions & 4 deletions consensus/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/vechain/thor/v2/muxdb"
"github.com/vechain/thor/v2/packer"
"github.com/vechain/thor/v2/state"
"github.com/vechain/thor/v2/test/testchain"
"github.com/vechain/thor/v2/thor"
"github.com/vechain/thor/v2/tx"
"github.com/vechain/thor/v2/vrf"
Expand Down Expand Up @@ -84,10 +85,7 @@ func newTestConsensus() (*testConsensus, error) {
return nil, err
}

forkConfig := thor.NoFork
forkConfig.VIP191 = 1
forkConfig.BLOCKLIST = 0
forkConfig.VIP214 = 2
forkConfig := testchain.IntTestChainForkConfig
forkConfig.GALACTICA = 5

proposer := genesis.DevAccounts()[0]
Expand Down
9 changes: 5 additions & 4 deletions consensus/fork/galactica.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ func CalcBaseFee(config *thor.ForkConfig, parent *block.Header) *big.Int {
y := x.Div(x, parentGasTargetBig)
baseFeeDelta := x.Div(y, baseFeeChangeDenominator)

// Setting the minimun baseFee to InitialBaseFee
// Setting the minimum baseFee to InitialBaseFee
return math.BigMax(
x.Sub(parentBaseFee, baseFeeDelta),
big.NewInt(thor.InitialBaseFee),
)
}
}

func GalacticaTxGasPriceAdapater(tr *tx.Transaction, gasPrice *big.Int) *GalacticaFeeMarketItems {
func GalacticaTxGasPriceAdapter(tr *tx.Transaction, gasPrice *big.Int) *GalacticaFeeMarketItems {
var maxPriorityFee, maxFee *big.Int
switch tr.Type() {
case tx.LegacyTxType:
Expand Down Expand Up @@ -122,8 +122,9 @@ func GalacticaGasPrice(tr *tx.Transaction, baseGasPrice *big.Int, galacticaItems
return gasPrice
}

feeItems := GalacticaTxGasPriceAdapater(tr, gasPrice)
feeItems := GalacticaTxGasPriceAdapter(tr, gasPrice)
// This gasPrice is the same that will be used when refunding the user
// it takes into account the priority fee that will be payed to the validator

Check failure on line 127 in consensus/fork/galactica.go

View workflow job for this annotation

GitHub Actions / Lint / golangci-lint

`payed` is a misspelling of `paid` (misspell)
return math.BigMin(new(big.Int).Add(feeItems.MaxPriorityFee, galacticaItems.BaseFee), feeItems.MaxFee)
}

Expand All @@ -134,7 +135,7 @@ func GalacticaPriorityPrice(tr *tx.Transaction, baseGasPrice, provedWork *big.In
return priorityPrice
}

feeItems := GalacticaTxGasPriceAdapater(tr, priorityPrice)
feeItems := GalacticaTxGasPriceAdapter(tr, priorityPrice)
// This gasPrice will be used to compensate the validator
return math.BigMin(feeItems.MaxPriorityFee, new(big.Int).Sub(feeItems.MaxFee, galacticaItems.BaseFee))
}
4 changes: 2 additions & 2 deletions packer/flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func TestAdoptErr(t *testing.T) {

func TestAdoptErrorAfterGalactica(t *testing.T) {
forks := thor.ForkConfig{GALACTICA: 2}
chain, err := testchain.NewIntegrationTestChainWithFork(forks)
chain, err := testchain.NewIntegrationTestChain(forks)
assert.NoError(t, err)

// Try to adopt a dyn fee tx before galactica fork activates - FAILS
Expand Down Expand Up @@ -348,7 +348,7 @@ func TestAdoptErrorAfterGalactica(t *testing.T) {
}

func TestAdoptAfterGalacticaLowerBaseFeeThreshold(t *testing.T) {
chain, err := testchain.NewIntegrationTestChainWithFork(thor.ForkConfig{GALACTICA: 1})
chain, err := testchain.NewIntegrationTestChain(thor.ForkConfig{GALACTICA: 1})
assert.NoError(t, err)

tr := tx.NewTxBuilder(tx.LegacyTxType).ChainTag(chain.Repo().ChainTag()).Gas(21000).Expiration(100).MustBuild()
Expand Down
5 changes: 3 additions & 2 deletions packer/packer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package packer_test

import (
"fmt"
"math"
"math/big"
"testing"
Expand Down Expand Up @@ -112,7 +111,9 @@ func TestP(t *testing.T) {
}

best := repo.BestBlockSummary()
fmt.Println(best.Header.Number(), best.Header.GasUsed())
assert.NotNil(t, best)
assert.True(t, best.Header.Number() > 0)
assert.True(t, best.Header.GasUsed() > 0)
}

func TestForkVIP191(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ type Runtime struct {
}

var (
// ErrMaxFeePerGasTooLow is returned if the transaction fee cap is less than the
// ErrMaxFeePerGasTooLow is returned if the transaction fee cap is less than
// the base fee of the block.
ErrMaxFeePerGasTooLow = errors.New("max fee per gas is less than block base fee")
)
Expand Down Expand Up @@ -426,7 +426,7 @@ func (rt *Runtime) PrepareTransaction(tx *tx.Transaction) (*TransactionExecutor,
}

if galactica {
feeItems := fork.GalacticaTxGasPriceAdapater(tx, gasPrice)
feeItems := fork.GalacticaTxGasPriceAdapter(tx, gasPrice)
if feeItems.MaxFee.Cmp(rt.ctx.BaseFee) < 0 {
return nil, ErrMaxFeePerGasTooLow
}
Expand Down
17 changes: 8 additions & 9 deletions test/testchain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,17 @@ func New(
}
}

// NewIntegrationTestChain is a convenience function that creates a Chain for testing.
// It uses an in-memory database, development network genesis, and a solo BFT engine.
func NewIntegrationTestChain() (*Chain, error) {
forkConfig := thor.NoFork
forkConfig.VIP191 = 1
forkConfig.BLOCKLIST = 0
forkConfig.VIP214 = 2
var IntTestChainForkConfig = thor.NoFork

return NewIntegrationTestChainWithFork(forkConfig)
func init() {
IntTestChainForkConfig.BLOCKLIST = 0
IntTestChainForkConfig.VIP191 = 1
IntTestChainForkConfig.VIP214 = 2
}

func NewIntegrationTestChainWithFork(forkConfig thor.ForkConfig) (*Chain, error) {
// NewIntegrationTestChain is a convenience function that creates a Chain for testing.
// It uses an in-memory database, development network genesis, and a solo BFT engine.
func NewIntegrationTestChain(forkConfig thor.ForkConfig) (*Chain, error) {
// Initialize the database
db := muxdb.NewMem()

Expand Down
2 changes: 1 addition & 1 deletion thorclient/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var (
)

func initAPIServer(t *testing.T) (*testchain.Chain, *httptest.Server) {
thorChain, err := testchain.NewIntegrationTestChain()
thorChain, err := testchain.NewIntegrationTestChain(testchain.IntTestChainForkConfig)
require.NoError(t, err)

// mint some transactions to be used in the endpoints
Expand Down
Loading

0 comments on commit 72f9340

Please sign in to comment.