diff --git a/txpool/config.go b/txpool/config.go index dc94f2fc8..9c64f7769 100644 --- a/txpool/config.go +++ b/txpool/config.go @@ -5,9 +5,11 @@ import ( ) type Config struct { - MaxSize int `toml:"max_size"` - Fee *FeeConfig `toml:"fee"` - ConsumptionBlocks uint32 `toml:"-"` // Private configs + MaxSize int `toml:"max_size"` + Fee *FeeConfig `toml:"fee"` + + // Private configs + ConsumptionWindow uint32 `toml:"-"` } type FeeConfig struct { @@ -20,7 +22,7 @@ func DefaultConfig() *Config { return &Config{ MaxSize: 1000, Fee: DefaultFeeConfig(), - ConsumptionBlocks: 8640, + ConsumptionWindow: 8640, } } diff --git a/txpool/txpool.go b/txpool/txpool.go index 687359e7b..5c1dd02bb 100644 --- a/txpool/txpool.go +++ b/txpool/txpool.go @@ -30,6 +30,8 @@ type txPool struct { logger *logger.SubLogger } +// NewTxPool constructs a new transaction pool with various sub-pools for different transaction types. +// The transaction pool also maintains a consumption map for tracking byte usage per address. func NewTxPool(conf *Config, storeReader store.Reader, broadcastCh chan message.Message) TxPool { pools := make(map[payload.Type]pool) pools[payload.TypeTransfer] = newPool(conf.transferPoolSize(), conf.minFee()) @@ -51,6 +53,8 @@ func NewTxPool(conf *Config, storeReader store.Reader, broadcastCh chan message. return pool } +// SetNewSandboxAndRecheck updates the sandbox and rechecks all transactions, +// removing expired or invalid ones. func (p *txPool) SetNewSandboxAndRecheck(sb sandbox.Sandbox) { p.lk.Lock() defer p.lk.Unlock() @@ -161,14 +165,14 @@ func (p *txPool) handleIncreaseConsumption(trx *tx.Tx) { } func (p *txPool) handleDecreaseConsumption(height uint32) error { - // If height is less than or equal to ConsumptionBlocks, nothing to do - if height <= p.config.ConsumptionBlocks { + // If height is less than or equal to ConsumptionWindow, nothing to do. + if height <= p.config.ConsumptionWindow { return nil } - // Calculate the height of the old block based on ConsumptionBlocks - oldConsumptionHeight := height - p.config.ConsumptionBlocks - committedBlock, err := p.strReader.Block(oldConsumptionHeight) + // Calculate the block height that has passed out of the consumption window. + windowedBlockHeight := height - p.config.ConsumptionWindow + committedBlock, err := p.strReader.Block(windowedBlockHeight) if err != nil { return err } diff --git a/txpool/txpool_test.go b/txpool/txpool_test.go index 9f734eef6..452e31c06 100644 --- a/txpool/txpool_test.go +++ b/txpool/txpool_test.go @@ -31,7 +31,7 @@ type testData struct { func testConfig() *Config { return &Config{ MaxSize: 10, - ConsumptionBlocks: 3, + ConsumptionWindow: 3, Fee: &FeeConfig{ FixedFee: 0.000001, DailyLimit: 280,