Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/fee logic #969

Open
wants to merge 26 commits into
base: release/galactica
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
14c0d38
feat: add the dynamic fee tx and support for typed txs
paologalligit Nov 29, 2024
266c43b
test: add tests for txpool and logdb
paologalligit Dec 2, 2024
6d1e25c
refactor: split builder into legacy and dynFee builder
paologalligit Dec 3, 2024
3d9f77e
test: add more tests including dynamic fee txs
paologalligit Dec 5, 2024
deaca0c
test: add tests for legacy and dynamic tx builder
paologalligit Dec 5, 2024
7759a4a
test: add more dynFee txs to tests
paologalligit Dec 18, 2024
be2a0fd
refactor: merge tx builder into one builder
paologalligit Jan 9, 2025
64f717e
refactor: add MustBuild to tx builder and fix error
paologalligit Jan 23, 2025
53df359
refactor: move hashWithoutNonce to concrete tx implementation
paologalligit Jan 23, 2025
a4e88b7
feat: add the dynamic fee tx and support for typed txs
paologalligit Nov 29, 2024
695b942
feat: add baseFee to header struct
paologalligit Dec 6, 2024
b6caeec
refactor: use the extension struct to include the baseFee instead of …
paologalligit Dec 16, 2024
1468709
clean
paologalligit Jan 3, 2025
9986558
fix: revert go.mod
paologalligit Jan 7, 2025
120b7f2
fix: clean code
paologalligit Jan 7, 2025
c218153
fix: go.mod file
paologalligit Jan 14, 2025
1277526
fix: rebase conflicts
paologalligit Jan 24, 2025
fa77b74
feat: add dynamic fee logic
paologalligit Jan 27, 2025
0e807c9
test: add fee calculation and edge cases tests
paologalligit Jan 28, 2025
7fb59cc
test: add new tests for maxFee too large values and including tx with…
paologalligit Jan 29, 2025
bde5fc6
refactor: rename overallGasPrice and add comments
paologalligit Jan 29, 2025
28d467a
refactor: move gas and priority price to fork package and add tests
paologalligit Jan 30, 2025
b103881
Fix baseFee calculation
paologalligit Feb 3, 2025
6205c3b
refactor: add comments, rename functions and refactor init test chain…
paologalligit Feb 7, 2025
91184c7
test: add more tests for galactica package
paologalligit Feb 7, 2025
50fb433
fix: base fee initialization, add logs and more tests
paologalligit Feb 11, 2025
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
5 changes: 3 additions & 2 deletions 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 All @@ -296,7 +296,7 @@ func initAccountServer(t *testing.T, enabledDeprecated bool) {
t.Fatal(err)
}
claCall := tx.NewClause(&contractAddr).WithData(input)
transactionCall := buildTxWithClauses(tx.DynamicFeeTxType, thorChain.Repo().ChainTag(), claCall)
transactionCall := buildTxWithClauses(tx.LegacyTxType, thorChain.Repo().ChainTag(), claCall)
require.NoError(t,
thorChain.MintTransactions(
genesis.DevAccounts()[0],
Expand All @@ -317,6 +317,7 @@ func buildTxWithClauses(txType int, chainTag byte, clauses ...*tx.Clause) *tx.Tr
ChainTag(chainTag).
Expiration(10).
Gas(1000000).
MaxFeePerGas(big.NewInt(1000)).
Clauses(clauses).
MustBuild()
return tx.MustSign(trx, genesis.DevAccounts()[0].PrivateKey)
Expand Down
5 changes: 3 additions & 2 deletions api/admin/health/health_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/vechain/thor/v2/comm"
"github.com/vechain/thor/v2/test/testchain"
"github.com/vechain/thor/v2/thor"
"github.com/vechain/thor/v2/txpool"
)

Expand All @@ -33,12 +34,12 @@ 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()
NewAPI(
New(thorChain.Repo(), comm.New(thorChain.Repo(), txpool.New(thorChain.Repo(), nil, txpool.Options{}))),
New(thorChain.Repo(), comm.New(thorChain.Repo(), txpool.New(thorChain.Repo(), nil, txpool.Options{}, &thor.NoFork))),
).Mount(router, "/health")

ts = httptest.NewServer(router)
Expand Down
18 changes: 12 additions & 6 deletions api/blocks/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func testGetRawBlock(t *testing.T) {
}

func testGetBlockByHeight(t *testing.T) {
res, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/blocks/1")
res, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/blocks/2")
require.NoError(t, err)
rb := new(blocks.JSONCollapsedBlock)
if err := json.Unmarshal(res, &rb); err != nil {
Expand Down Expand Up @@ -225,7 +225,13 @@ func testGetBlockWithRevisionNumberTooHigh(t *testing.T) {
}

func initBlockServer(t *testing.T) {
thorChain, err := testchain.NewIntegrationTestChain()
forks := thor.ForkConfig{
BLOCKLIST: 0,
VIP191: 1,
GALACTICA: 1,
VIP214: 2,
}
thorChain, err := testchain.NewIntegrationTestChain(forks)
require.NoError(t, err)

addr := thor.BytesToAddress([]byte("to"))
Expand All @@ -240,10 +246,11 @@ func initBlockServer(t *testing.T) {
BlockRef(tx.NewBlockRef(0)).
MustBuild()
legacyTx = tx.MustSign(legacyTx, genesis.DevAccounts()[0].PrivateKey)
require.NoError(t, thorChain.MintTransactions(genesis.DevAccounts()[0], legacyTx))

dynFeeTx := tx.NewTxBuilder(tx.DynamicFeeTxType).
ChainTag(thorChain.Repo().ChainTag()).
MaxFeePerGas(big.NewInt(100000)).
MaxFeePerGas(big.NewInt(thor.InitialBaseFee)).
MaxPriorityFeePerGas(big.NewInt(100)).
Expiration(10).
Gas(21000).
Expand All @@ -252,14 +259,13 @@ func initBlockServer(t *testing.T) {
BlockRef(tx.NewBlockRef(0)).
MustBuild()
dynFeeTx = tx.MustSign(dynFeeTx, genesis.DevAccounts()[0].PrivateKey)

require.NoError(t, thorChain.MintTransactions(genesis.DevAccounts()[0], legacyTx, dynFeeTx))
require.NoError(t, thorChain.MintTransactions(genesis.DevAccounts()[0], dynFeeTx))

allBlocks, err := thorChain.GetAllBlocks()
require.NoError(t, err)

genesisBlock = allBlocks[0]
blk = allBlocks[1]
blk = allBlocks[len(allBlocks)-1]

router := mux.NewRouter()
blocks.New(thorChain.Repo(), thorChain.Engine()).Mount(router, "/blocks")
Expand Down
16 changes: 10 additions & 6 deletions api/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,13 @@ func testStorageRangeDefaultOption(t *testing.T) {
}

func initDebugServer(t *testing.T) {
thorChain, err := testchain.NewIntegrationTestChain()
forkConf := thor.ForkConfig{
BLOCKLIST: 0,
VIP191: 1,
GALACTICA: 1,
VIP214: 2,
}
thorChain, err := testchain.NewIntegrationTestChain(forkConf)
require.NoError(t, err)

addr := thor.BytesToAddress([]byte("to"))
Expand All @@ -561,6 +567,7 @@ func initDebugServer(t *testing.T) {
BlockRef(tx.NewBlockRef(0)).
MustBuild()
transaction = tx.MustSign(transaction, genesis.DevAccounts()[0].PrivateKey)
require.NoError(t, thorChain.MintTransactions(genesis.DevAccounts()[0], transaction, noClausesTx))

dynFeeTx := tx.NewTxBuilder(tx.DynamicFeeTxType).
ChainTag(thorChain.Repo().ChainTag()).
Expand All @@ -576,17 +583,14 @@ func initDebugServer(t *testing.T) {
dynFeeTx,
genesis.DevAccounts()[0].PrivateKey,
)

require.NoError(t, thorChain.MintTransactions(genesis.DevAccounts()[0], transaction, noClausesTx, dynFeeTx))
require.NoError(t, thorChain.MintTransactions(genesis.DevAccounts()[0]))
require.NoError(t, thorChain.MintTransactions(genesis.DevAccounts()[0]), dynFeeTx)

allBlocks, err := thorChain.GetAllBlocks()
require.NoError(t, err)
blk = allBlocks[1]

forkConfig := thor.GetForkConfig(blk.Header().ID())
router := mux.NewRouter()
debug = New(thorChain.Repo(), thorChain.Stater(), forkConfig, 21000, true, thorChain.Engine(), []string{"all"}, false)
debug = New(thorChain.Repo(), thorChain.Stater(), forkConf, 21000, true, thorChain.Engine(), []string{"all"}, false)
debug.Mount(router, "/debug")
ts = httptest.NewServer(router)
}
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
6 changes: 3 additions & 3 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,11 +99,11 @@ 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()
sub := subscriptions.New(thorChain.Repo(), []string{"*"}, 10, txpool.New(thorChain.Repo(), thorChain.Stater(), txpool.Options{}), true)
sub := subscriptions.New(thorChain.Repo(), []string{"*"}, 10, txpool.New(thorChain.Repo(), thorChain.Stater(), txpool.Options{}, &thor.NoFork), true)
sub.Mount(router, "/subscriptions")
router.PathPrefix("/metrics").Handler(metrics.HTTPHandler())
router.Use(metricsMiddleware)
Expand Down
5 changes: 3 additions & 2 deletions api/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/vechain/thor/v2/api/node"
"github.com/vechain/thor/v2/comm"
"github.com/vechain/thor/v2/test/testchain"
"github.com/vechain/thor/v2/thor"
"github.com/vechain/thor/v2/thorclient"
"github.com/vechain/thor/v2/txpool"
)
Expand All @@ -31,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 All @@ -40,7 +41,7 @@ func initCommServer(t *testing.T) {
Limit: 10000,
LimitPerAccount: 16,
MaxLifetime: 10 * time.Minute,
}),
}, &thor.NoFork),
)

router := mux.NewRouter()
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[1]
// 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[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
38 changes: 26 additions & 12 deletions api/subscriptions/block_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vechain/thor/v2/genesis"
Expand All @@ -26,7 +25,8 @@ func TestBlockReader_Read(t *testing.T) {
allBlocks, err := thorChain.GetAllBlocks()
require.NoError(t, err)
genesisBlk := allBlocks[0]
newBlock := allBlocks[1]
firstBlk := allBlocks[1]
bestBlk := allBlocks[len(allBlocks)-1]

// Test case 1: Successful read next blocks
br := newBlockReader(thorChain.Repo(), genesisBlk.Header().ID())
Expand All @@ -37,12 +37,22 @@ func TestBlockReader_Read(t *testing.T) {
if resBlock, ok := res[0].(*BlockMessage); !ok {
t.Fatal("unexpected type")
} else {
assert.Equal(t, newBlock.Header().Number(), resBlock.Number)
assert.Equal(t, newBlock.Header().ParentID(), resBlock.ParentID)
assert.Equal(t, firstBlk.Header().Number(), resBlock.Number)
assert.Equal(t, firstBlk.Header().ParentID(), resBlock.ParentID)
}

res, ok, err = br.Read()
assert.NoError(t, err)
assert.True(t, ok)
if resBlock, ok := res[0].(*BlockMessage); !ok {
t.Fatal("unexpected type")
} else {
assert.Equal(t, bestBlk.Header().Number(), resBlock.Number)
assert.Equal(t, bestBlk.Header().ParentID(), resBlock.ParentID)
}

// Test case 2: There is no new block
br = newBlockReader(thorChain.Repo(), newBlock.Header().ID())
br = newBlockReader(thorChain.Repo(), bestBlk.Header().ID())
res, ok, err = br.Read()

assert.NoError(t, err)
Expand All @@ -59,7 +69,13 @@ func TestBlockReader_Read(t *testing.T) {
}

func initChain(t *testing.T) *testchain.Chain {
thorChain, err := testchain.NewIntegrationTestChain()
forks := thor.ForkConfig{
BLOCKLIST: 0,
VIP191: 1,
GALACTICA: 1,
VIP214: 2,
}
thorChain, err := testchain.NewIntegrationTestChain(forks)
require.NoError(t, err)

addr := thor.BytesToAddress([]byte("to"))
Expand All @@ -74,21 +90,19 @@ func initChain(t *testing.T) *testchain.Chain {
BlockRef(tx.NewBlockRef(0)).
MustBuild()
tr = tx.MustSign(tr, genesis.DevAccounts()[0].PrivateKey)
require.NoError(t, thorChain.MintTransactions(genesis.DevAccounts()[0], tr))

txDeploy := tx.NewTxBuilder(tx.DynamicFeeTxType).
ChainTag(thorChain.Repo().ChainTag()).
MaxFeePerGas(big.NewInt(1)).
MaxFeePerGas(big.NewInt(thor.InitialBaseFee)).
Expiration(100).
Gas(1_000_000).
Nonce(3).
Clause(tx.NewClause(nil).WithData(common.Hex2Bytes(eventcontract.HexBytecode))).
BlockRef(tx.NewBlockRef(0)).
MustBuild()
sigTxDeploy, err := crypto.Sign(txDeploy.SigningHash().Bytes(), genesis.DevAccounts()[1].PrivateKey)
require.NoError(t, err)
txDeploy = txDeploy.WithSignature(sigTxDeploy)

require.NoError(t, thorChain.MintTransactions(genesis.DevAccounts()[0], tr, txDeploy))
txDeploy = tx.MustSign(txDeploy, genesis.DevAccounts()[0].PrivateKey)
require.NoError(t, thorChain.MintTransactions(genesis.DevAccounts()[0], txDeploy))

return thorChain
}
15 changes: 11 additions & 4 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[1]
// taking best block to include also galactica block
bestBlk := allBlocks[len(allBlocks)-1]

er := &eventReader{
repo: thorChain.Repo(),
Expand All @@ -34,13 +35,19 @@ func TestEventReader_Read(t *testing.T) {
assert.Empty(t, events)
assert.False(t, ok)

// Test case 2: Events are available to read
// Test case 2: There are no events available to read
er = newEventReader(thorChain.Repo(), genesisBlk.Header().ID(), &EventFilter{})

events, ok, err = er.Read()
assert.NoError(t, err)
assert.True(t, ok)
assert.Zero(t, len(events))

// Test case 3: There are no events available to read
events, ok, err = er.Read()
assert.NoError(t, err)
assert.True(t, ok)

var eventMessages []*EventMessage
for _, event := range events {
if msg, ok := event.(*EventMessage); ok {
Expand All @@ -51,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
Loading
Loading