From 0b2ed3ba08c76e828eb45a0723e046a688fb417e Mon Sep 17 00:00:00 2001 From: b00f Date: Sun, 2 Jun 2024 23:25:46 +0800 Subject: [PATCH] fix: set fix fee 0.1 PAC for transactions (#1315) --- cmd/gtk/dialog_transaction_unbond.go | 3 +- cmd/gtk/dialog_transaction_withdraw.go | 2 +- config/example_config.toml | 4 -- state/state.go | 24 +-------- txpool/config.go | 4 +- txpool/config_test.go | 2 +- txpool/pool.go | 23 +-------- txpool/txpool.go | 8 +-- txpool/txpool_test.go | 71 +++++--------------------- 9 files changed, 25 insertions(+), 116 deletions(-) diff --git a/cmd/gtk/dialog_transaction_unbond.go b/cmd/gtk/dialog_transaction_unbond.go index 9722865a2..3d265435a 100644 --- a/cmd/gtk/dialog_transaction_unbond.go +++ b/cmd/gtk/dialog_transaction_unbond.go @@ -54,9 +54,10 @@ func broadcastTransactionUnbond(wlt *wallet.Wallet) { You are going to sign and broadcast this transaction: Validator: %s +Fee: %s Memo: %s -THIS ACTION IS NOT REVERSIBLE. Do you want to continue?`, validator, trx.Memo()) +THIS ACTION IS NOT REVERSIBLE. Do you want to continue?`, validator, trx.Fee(), trx.Memo()) signAndBroadcastTransaction(dlg, msg, wlt, trx) diff --git a/cmd/gtk/dialog_transaction_withdraw.go b/cmd/gtk/dialog_transaction_withdraw.go index 62560cae8..871f8d023 100644 --- a/cmd/gtk/dialog_transaction_withdraw.go +++ b/cmd/gtk/dialog_transaction_withdraw.go @@ -86,8 +86,8 @@ You are going to sign and broadcast this transaction: From: %s To: %s Amount: %s -Memo: %s Fee: %s +Memo: %s THIS ACTION IS NOT REVERSIBLE. Do you want to continue?`, sender, receiver, amt, trx.Fee(), trx.Memo()) diff --git a/config/example_config.toml b/config/example_config.toml index 2d91f2bd4..5bfa788b3 100644 --- a/config/example_config.toml +++ b/config/example_config.toml @@ -118,10 +118,6 @@ # Default is `1000`. max_size = 1000 - # `min_fee` indicates the minimum fee in PAC for the transaction to enter into the pool. - # Default is `0.000001`. - min_fee = 0.000001 - # `logger` contains configuration options for the logger. [logger] # `colorful` indicates whether log can be colorful or not. diff --git a/state/state.go b/state/state.go index 6c475dd24..022572921 100644 --- a/state/state.go +++ b/state/state.go @@ -773,29 +773,7 @@ func (st *state) publishEvents(height uint32, blk *block.Block) { } func (st *state) CalculateFee(amt amount.Amount, payloadType payload.Type) amount.Amount { - switch payloadType { - case payload.TypeUnbond, - payload.TypeSortition: - - return 0 - - case payload.TypeTransfer, - payload.TypeBond, - payload.TypeWithdraw: - fee := amt.MulF64(st.params.FeeFractionDeprecated) - fee = util.Max(fee, st.params.MinimumFeeDeprecated) - fee = util.Min(fee, st.params.MaximumFeeDeprecated) - - return fee - - default: - return 0 - } - - // TODO: - // The above code should be replaced by the below code once the majority of the nodes are upgraded. - // This helps to smoothly migrate the nodes. - // return st.txPool.EstimatedFee(amt, payloadType) + return st.txPool.EstimatedFee(amt, payloadType) } func (st *state) PublicKey(addr crypto.Address) (crypto.PublicKey, error) { diff --git a/txpool/config.go b/txpool/config.go index bfe95026b..4aebe91dc 100644 --- a/txpool/config.go +++ b/txpool/config.go @@ -6,13 +6,13 @@ import ( type Config struct { MaxSize int `toml:"max_size"` - MinFeePAC float64 `toml:"min_fee"` + MinFeePAC float64 `toml:"-"` } func DefaultConfig() *Config { return &Config{ MaxSize: 1000, - MinFeePAC: 0.000001, + MinFeePAC: 0.1, } } diff --git a/txpool/config_test.go b/txpool/config_test.go index b15dd71ab..55a0f4b54 100644 --- a/txpool/config_test.go +++ b/txpool/config_test.go @@ -16,7 +16,7 @@ func TestDefaultConfigCheck(t *testing.T) { assert.Equal(t, 100, c.unbondPoolSize()) assert.Equal(t, 100, c.withdrawPoolSize()) assert.Equal(t, 100, c.sortitionPoolSize()) - assert.Equal(t, amount.Amount(1000), c.minFee()) + assert.Equal(t, amount.Amount(100000000), c.minFee()) assert.Equal(t, c.transferPoolSize()+ diff --git a/txpool/pool.go b/txpool/pool.go index 326eb7873..7700da6e9 100644 --- a/txpool/pool.go +++ b/txpool/pool.go @@ -18,25 +18,6 @@ func newPool(maxSize int, minFee amount.Amount) pool { } } -func (p *pool) calculateDynamicFee() amount.Amount { - capacity := p.list.Capacity() - size := p.list.Size() - usageRatio := float64(size) / float64(capacity) - - switch { - case usageRatio > 0.90: - return p.minFee * 1000000 - case usageRatio > 0.80: - return p.minFee * 100000 - case usageRatio > 0.70: - return p.minFee * 10000 - case usageRatio > 0.60: - return p.minFee * 1000 - case usageRatio > 0.50: - return p.minFee * 100 - case usageRatio > 0.40: - return p.minFee * 10 - default: - return p.minFee - } +func (p *pool) estimatedFee() amount.Amount { + return p.minFee } diff --git a/txpool/txpool.go b/txpool/txpool.go index 24d54a351..012d20550 100644 --- a/txpool/txpool.go +++ b/txpool/txpool.go @@ -104,11 +104,11 @@ func (p *txPool) appendTx(trx *tx.Tx) error { } if !trx.IsFreeTx() { - if trx.Fee() < pool.calculateDynamicFee() { - p.logger.Warn("low fee transaction", "tx", trx, "minFee", pool.calculateDynamicFee()) + if trx.Fee() < pool.estimatedFee() { + p.logger.Warn("low fee transaction", "tx", trx, "minFee", pool.estimatedFee()) return AppendError{ - Err: fmt.Errorf("low fee transaction, expected to be more than %s", pool.calculateDynamicFee()), + Err: fmt.Errorf("low fee transaction, expected to be more than %s", pool.estimatedFee()), } } } @@ -235,7 +235,7 @@ func (p *txPool) EstimatedFee(_ amount.Amount, payloadType payload.Type) amount. return 0 } - return pool.calculateDynamicFee() + return pool.estimatedFee() } func (p *txPool) String() string { diff --git a/txpool/txpool_test.go b/txpool/txpool_test.go index 2d903b1ce..d2302583c 100644 --- a/txpool/txpool_test.go +++ b/txpool/txpool_test.go @@ -9,7 +9,6 @@ import ( "github.com/pactus-project/pactus/sandbox" "github.com/pactus-project/pactus/sync/bundle/message" "github.com/pactus-project/pactus/types/account" - "github.com/pactus-project/pactus/types/amount" "github.com/pactus-project/pactus/types/tx" "github.com/pactus-project/pactus/types/validator" "github.com/pactus-project/pactus/util/logger" @@ -33,16 +32,14 @@ func testConfig() *Config { } } -func setup(t *testing.T, config *Config) *testData { +func setup(t *testing.T) *testData { t.Helper() ts := testsuite.NewTestSuite(t) - if config == nil { - config = testConfig() - } ch := make(chan message.Message, 10) sb := sandbox.MockingSandbox(ts) + config := testConfig() p := NewTxPool(config, ch) p.SetNewSandboxAndRecheck(sb) pool := p.(*txPool) @@ -80,52 +77,8 @@ func (td *testData) shouldPublishTransaction(t *testing.T, id tx.ID) { } } -func TestCalculateDynamicFee(t *testing.T) { - minFee := amount.Amount(1000) - config := Config{ - MaxSize: 16, - MinFeePAC: minFee.ToPAC(), - } - td := setup(t, &config) - - randHeight := td.RandHeight() - _ = td.sandbox.TestStore.AddTestBlock(randHeight) - - senderAddr := td.RandAccAddress() - senderAcc := account.NewAccount(0) - senderAcc.AddToBalance(10000e9) - td.sandbox.UpdateAccount(senderAddr, senderAcc) - - // Make sure the pool is empty - assert.Equal(t, td.pool.Size(), 0) - - trx01 := tx.NewTransferTx(randHeight+1, senderAddr, td.RandAccAddress(), td.RandAmount(), minFee, "ok") - trx02 := tx.NewTransferTx(randHeight+1, senderAddr, td.RandAccAddress(), td.RandAmount(), minFee, "ok") - trx03 := tx.NewTransferTx(randHeight+1, senderAddr, td.RandAccAddress(), td.RandAmount(), minFee, "ok") - trx04 := tx.NewTransferTx(randHeight+1, senderAddr, td.RandAccAddress(), td.RandAmount(), minFee, "ok") - trx05 := tx.NewTransferTx(randHeight+1, senderAddr, td.RandAccAddress(), td.RandAmount(), minFee*100, "ok") - trx06 := tx.NewTransferTx(randHeight+1, senderAddr, td.RandAccAddress(), td.RandAmount(), minFee*100, "ok") - trx07 := tx.NewTransferTx(randHeight+1, senderAddr, td.RandAccAddress(), td.RandAmount(), minFee*1000, "ok") - trx08 := tx.NewTransferTx(randHeight+1, senderAddr, td.RandAccAddress(), td.RandAmount(), minFee*10000, "ok") - trx09 := tx.NewTransferTx(randHeight+1, senderAddr, td.RandAccAddress(), td.RandAmount(), minFee*100000, "ok") - trx10 := tx.NewTransferTx(randHeight+1, senderAddr, td.RandAccAddress(), td.RandAmount(), minFee*1000000, "ok") - trx11 := tx.NewTransferTx(randHeight+1, senderAddr, td.RandAccAddress(), td.RandAmount(), minFee, "ok") - - assert.NoError(t, td.pool.AppendTx(trx01)) - assert.NoError(t, td.pool.AppendTx(trx02)) - assert.NoError(t, td.pool.AppendTx(trx03)) - assert.NoError(t, td.pool.AppendTx(trx04)) - assert.NoError(t, td.pool.AppendTx(trx05)) - assert.NoError(t, td.pool.AppendTx(trx06)) - assert.NoError(t, td.pool.AppendTx(trx07)) - assert.NoError(t, td.pool.AppendTx(trx08)) - assert.NoError(t, td.pool.AppendTx(trx09)) - assert.NoError(t, td.pool.AppendTx(trx10)) - assert.Error(t, td.pool.AppendTx(trx11)) -} - func TestAppendAndRemove(t *testing.T) { - td := setup(t, nil) + td := setup(t) height := td.RandHeight() td.sandbox.TestStore.AddTestBlock(height) @@ -144,7 +97,7 @@ func TestAppendAndRemove(t *testing.T) { } func TestAppendInvalidTransaction(t *testing.T) { - td := setup(t, nil) + td := setup(t) invalidTx, _ := td.GenerateTestTransferTx() assert.Error(t, td.pool.AppendTx(invalidTx)) @@ -152,7 +105,7 @@ func TestAppendInvalidTransaction(t *testing.T) { // TestFullPool tests if the pool prunes the old transactions when it is full. func TestFullPool(t *testing.T) { - td := setup(t, nil) + td := setup(t) randHeight := td.RandHeight() _ = td.sandbox.TestStore.AddTestBlock(randHeight) @@ -180,13 +133,13 @@ func TestFullPool(t *testing.T) { } func TestEmptyPool(t *testing.T) { - td := setup(t, nil) + td := setup(t) assert.Empty(t, td.pool.PrepareBlockTransactions(), "pool should be empty") } func TestPrepareBlockTransactions(t *testing.T) { - td := setup(t, nil) + td := setup(t) randHeight := td.RandHeight() + td.sandbox.TestParams.UnbondInterval _ = td.sandbox.TestStore.AddTestBlock(randHeight) @@ -213,16 +166,16 @@ func TestPrepareBlockTransactions(t *testing.T) { td.sandbox.UpdateValidator(val3) transferTx := tx.NewTransferTx(randHeight+1, acc1Addr, - td.RandAccAddress(), 1e9, 100_000, "transfer-tx") + td.RandAccAddress(), 1e9, 100_000_000, "transfer-tx") pub, _ := td.RandBLSKeyPair() bondTx := tx.NewBondTx(randHeight+2, acc1Addr, - pub.ValidatorAddress(), pub, 1e9, 100_000, "bond-tx") + pub.ValidatorAddress(), pub, 1e9, 100_000_000, "bond-tx") unbondTx := tx.NewUnbondTx(randHeight+3, val1.Address(), "unbond-tx") withdrawTx := tx.NewWithdrawTx(randHeight+4, val2.Address(), - td.RandAccAddress(), 1e9, 100_000, "withdraw-tx") + td.RandAccAddress(), 1e9, 100_000_000, "withdraw-tx") td.sandbox.TestAcceptSortition = true sortitionTx := tx.NewSortitionTx(randHeight, val3.Address(), @@ -244,7 +197,7 @@ func TestPrepareBlockTransactions(t *testing.T) { } func TestAppendAndBroadcast(t *testing.T) { - td := setup(t, nil) + td := setup(t) height := td.RandHeight() td.sandbox.TestStore.AddTestBlock(height) @@ -258,7 +211,7 @@ func TestAppendAndBroadcast(t *testing.T) { } func TestAddSubsidyTransactions(t *testing.T) { - td := setup(t, nil) + td := setup(t) randHeight := td.RandHeight() td.sandbox.TestStore.AddTestBlock(randHeight)