Skip to content

Commit

Permalink
fix: set fix fee 0.1 PAC for transactions (#1315)
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f authored Jun 2, 2024
1 parent e89c14d commit 0b2ed3b
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 116 deletions.
3 changes: 2 additions & 1 deletion cmd/gtk/dialog_transaction_unbond.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ func broadcastTransactionUnbond(wlt *wallet.Wallet) {
You are going to sign and broadcast this transaction:
<tt>
Validator: %s
Fee: %s
Memo: %s
</tt>
<b>THIS ACTION IS NOT REVERSIBLE. Do you want to continue?</b>`, validator, trx.Memo())
<b>THIS ACTION IS NOT REVERSIBLE. Do you want to continue?</b>`, validator, trx.Fee(), trx.Memo())

signAndBroadcastTransaction(dlg, msg, wlt, trx)

Expand Down
2 changes: 1 addition & 1 deletion cmd/gtk/dialog_transaction_withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
</tt>
<b>THIS ACTION IS NOT REVERSIBLE. Do you want to continue?</b>`,
sender, receiver, amt, trx.Fee(), trx.Memo())
Expand Down
4 changes: 0 additions & 4 deletions config/example_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 1 addition & 23 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions txpool/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
2 changes: 1 addition & 1 deletion txpool/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()+
Expand Down
23 changes: 2 additions & 21 deletions txpool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 4 additions & 4 deletions txpool/txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
}
}
}
Expand Down Expand Up @@ -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 {
Expand Down
71 changes: 12 additions & 59 deletions txpool/txpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -144,15 +97,15 @@ 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))
}

// 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)
Expand Down Expand Up @@ -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)
Expand All @@ -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(),
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 0b2ed3b

Please sign in to comment.