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(txpool): calculate consumption when committing a new block #1554

Merged
merged 7 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
4 changes: 2 additions & 2 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,8 @@ func (st *state) CommitBlock(blk *block.Block, cert *certificate.BlockCertificat
st.store.SaveBlock(blk, cert)

// Remove transactions from pool and update consumption
if err = st.txPool.HandleCommittedBlock(blk); err != nil {
return err
if errHandleCommittedBlk := st.txPool.HandleCommittedBlock(blk); errHandleCommittedBlk != nil {
Ja7ad marked this conversation as resolved.
Show resolved Hide resolved
return errHandleCommittedBlk
}

if err := st.store.WriteBatch(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion txpool/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ type TxPool interface {
SetNewSandboxAndRecheck(sb sandbox.Sandbox)
AppendTxAndBroadcast(trx *tx.Tx) error
AppendTx(trx *tx.Tx) error
HandleCommittedBlock(block *block.Block) error
HandleCommittedBlock(blk *block.Block) error
}
2 changes: 1 addition & 1 deletion txpool/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (m *MockTxPool) RemoveTx(id hash.Hash) {
}
}

func (m *MockTxPool) HandleCommittedBlock(_ *block.Block) error {
func (*MockTxPool) HandleCommittedBlock(_ *block.Block) error {
return nil
}

Expand Down
19 changes: 14 additions & 5 deletions txpool/txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"sync"

"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/store"

"github.com/pactus-project/pactus/execution"
"github.com/pactus-project/pactus/sandbox"
"github.com/pactus-project/pactus/store"
"github.com/pactus-project/pactus/sync/bundle/message"
"github.com/pactus-project/pactus/types/amount"
"github.com/pactus-project/pactus/types/block"
Expand Down Expand Up @@ -157,16 +156,17 @@ func (p *txPool) handleIncreaseConsumption(trx *tx.Tx) {
if trx.IsTransferTx() || trx.IsBondTx() || trx.IsWithdrawTx() {
signer := trx.Payload().Signer()

// retrieve existing consumption or start with 0
p.consumptionMap[signer] = p.consumptionMap[signer] + uint32(trx.SerializeSize())
p.consumptionMap[signer] += uint32(trx.SerializeSize())
}
}

func (p *txPool) handleDecreaseConsumption(height uint32) error {
// If height is less than or equal to ConsumptionBlocks, nothing to do
if height <= p.config.ConsumptionBlocks {
return nil
}

// Calculate the height of the old block based on ConsumptionBlocks
oldConsumptionHeight := height - p.config.ConsumptionBlocks
committedBlock, err := p.strReader.Block(oldConsumptionHeight)
if err != nil {
Expand All @@ -182,7 +182,16 @@ func (p *txPool) handleDecreaseConsumption(height uint32) error {
if trx.IsTransferTx() || trx.IsBondTx() || trx.IsWithdrawTx() {
signer := trx.Payload().Signer()
if v, ok := p.consumptionMap[signer]; ok {
p.consumptionMap[signer] = v - uint32(trx.SerializeSize())
// Decrease the consumption by the size of the transaction
v -= uint32(trx.SerializeSize())

// If the new value is zero, remove the signer from the consumptionMap
Ja7ad marked this conversation as resolved.
Show resolved Hide resolved
if v == 0 {
delete(p.consumptionMap, signer)
} else {
// Otherwise, update the map with the new value
p.consumptionMap[signer] = v
}
}
}
}
Expand Down
52 changes: 39 additions & 13 deletions txpool/txpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"testing"
"time"

"github.com/pactus-project/pactus/store"

"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/execution"
"github.com/pactus-project/pactus/sandbox"
"github.com/pactus-project/pactus/store"
"github.com/pactus-project/pactus/sync/bundle/message"
"github.com/pactus-project/pactus/types/account"
"github.com/pactus-project/pactus/types/tx"
Expand Down Expand Up @@ -110,26 +110,52 @@ func TestAppendAndRemove(t *testing.T) {
func TestCalculatingConsumption(t *testing.T) {
Ja7ad marked this conversation as resolved.
Show resolved Hide resolved
td := setup(t)

_, prv := td.TestSuite.RandEd25519KeyPair()
signer := prv
// Generate keys for different transaction signers
_, prv1 := td.RandEd25519KeyPair()
_, prv2 := td.RandEd25519KeyPair()
_, prv3 := td.RandBLSKeyPair()
_, prv4 := td.RandBLSKeyPair()

// Generate different types of transactions
trx11 := td.GenerateTestTransferTx(testsuite.TransactionWithEd25519Signer(prv1))
trx12 := td.GenerateTestBondTx(testsuite.TransactionWithEd25519Signer(prv1))
trx13 := td.GenerateTestWithdrawTx(testsuite.TransactionWithBLSSigner(prv3))
trx14 := td.GenerateTestUnbondTx(testsuite.TransactionWithBLSSigner(prv4))
trx21 := td.GenerateTestTransferTx(testsuite.TransactionWithEd25519Signer(prv2))
trx31 := td.GenerateTestBondTx(testsuite.TransactionWithBLSSigner(prv4))
trx41 := td.GenerateTestWithdrawTx(testsuite.TransactionWithBLSSigner(prv3))
trx42 := td.GenerateTestTransferTx(testsuite.TransactionWithEd25519Signer(prv2))

// Expected consumption map after transactions
expected := map[crypto.Address]uint32{
prv2.PublicKeyNative().AccountAddress(): uint32(trx21.SerializeSize()) + uint32(trx42.SerializeSize()),
prv4.PublicKeyNative().AccountAddress(): uint32(trx31.SerializeSize()),
prv3.PublicKeyNative().ValidatorAddress(): uint32(trx41.SerializeSize()),
}

for i := uint32(1); i < 10; i++ {
txr := td.GenerateTestTransferTx(func(tm *testsuite.TransactionMaker) {
tm.Signer = signer
})
tests := []struct {
height uint32
txs []*tx.Tx
}{
{1, []*tx.Tx{trx11, trx12, trx13, trx14}},
{2, []*tx.Tx{trx21}},
{3, []*tx.Tx{trx31}},
{4, []*tx.Tx{trx41, trx42}},
}

blk, cert := td.TestSuite.GenerateTestBlock(i, func(bm *testsuite.BlockMaker) {
bm.Txs = []*tx.Tx{txr}
for _, tt := range tests {
// Generate a block with the transactions for the given height
blk, cert := td.TestSuite.GenerateTestBlock(tt.height, func(bm *testsuite.BlockMaker) {
bm.Txs = tt.txs
})

td.str.SaveBlock(blk, cert)

// Handle the block in the transaction pool
err := td.pool.HandleCommittedBlock(blk)
require.NoError(t, err)

//t.Log(td.pool.consumptionMap[txr.Payload().Signer()])
}

require.Equal(t, expected, td.pool.consumptionMap)
}

func TestAppendInvalidTransaction(t *testing.T) {
Expand Down
Loading