Skip to content

Commit

Permalink
Add LowFeeTxnSizePerBlock to control low fee txn size per block
Browse files Browse the repository at this point in the history
Signed-off-by: Yilun <[email protected]>
  • Loading branch information
yilunzhang committed Aug 10, 2019
1 parent 1f8a140 commit a1790fa
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
45 changes: 31 additions & 14 deletions chain/mining.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ func NewBuiltinMining(account *vault.Account, txnCollector *TxnCollector) *Built
}
}

func isBlockFull(txnCount uint32, txnSize uint32) bool {
if config.Parameters.NumTxnPerBlock > 0 && txnCount > config.Parameters.NumTxnPerBlock {
return true
}
if config.MaxBlockSize > 0 && txnSize > config.MaxBlockSize {
return true
}
return false
}

func isLowFeeTxnFull(txnCount uint32, txnSize uint32) bool {
if config.Parameters.NumLowFeeTxnPerBlock > 0 && txnCount > config.Parameters.NumLowFeeTxnPerBlock {
return true
}
if config.Parameters.LowFeeTxnSizePerBlock > 0 && txnSize > config.Parameters.LowFeeTxnSizePerBlock {
return true
}
return false
}

func (bm *BuiltinMining) BuildBlock(ctx context.Context, height uint32, chordID []byte, winnerHash common.Uint256, winnerType pb.WinnerType) (*block.Block, error) {
var txnList []*transaction.Transaction
var txnHashList []common.Uint256
Expand All @@ -44,9 +64,9 @@ func (bm *BuiltinMining) BuildBlock(ctx context.Context, height uint32, chordID
coinbase := bm.CreateCoinbaseTransaction(GetRewardByHeight(height) + donationAmount)
txnList = append(txnList, coinbase)
txnHashList = append(txnHashList, coinbase.Hash())
totalTxsSize := coinbase.GetSize()
txCount := 1
lowFeeTxCount := 0
totalTxSize := coinbase.GetSize()
totalTxCount := uint32(1)
var lowFeeTxCount, lowFeeTxSize uint32

if winnerType == pb.TXN_SIGNER {
if _, err = DefaultLedger.Store.GetTransaction(winnerHash); err != nil {
Expand All @@ -57,8 +77,8 @@ func (bm *BuiltinMining) BuildBlock(ctx context.Context, height uint32, chordID
}
txnList = append(txnList, miningSigChainTxn)
txnHashList = append(txnHashList, miningSigChainTxn.Hash())
totalTxsSize = totalTxsSize + miningSigChainTxn.GetSize()
txCount++
totalTxSize += miningSigChainTxn.GetSize()
totalTxCount++
}
}

Expand All @@ -76,22 +96,17 @@ func (bm *BuiltinMining) BuildBlock(ctx context.Context, height uint32, chordID
default:
}

if txCount >= int(config.Parameters.NumTxnPerBlock) {
break
}

txn := txnCollection.Peek()
if txn == nil {
break
}

if txn.UnsignedTx.Fee < int64(config.Parameters.MinTxnFee) && uint32(lowFeeTxCount) >= config.Parameters.NumLowFeeTxnPerBlock {
log.Warning("transaction fee is too low")
if isBlockFull(totalTxCount+1, totalTxSize+txn.GetSize()) {
break
}

totalTxsSize = totalTxsSize + txn.GetSize()
if totalTxsSize > config.MaxBlockSize {
if txn.UnsignedTx.Fee < int64(config.Parameters.MinTxnFee) && isLowFeeTxnFull(lowFeeTxCount+1, lowFeeTxSize+txn.GetSize()) {
log.Info("Low fee transaction full in block")
break
}

Expand Down Expand Up @@ -121,9 +136,11 @@ func (bm *BuiltinMining) BuildBlock(ctx context.Context, height uint32, chordID
}

bvs.Commit()
txCount++
totalTxCount++
totalTxSize += txn.GetSize()
if txn.UnsignedTx.Fee < int64(config.Parameters.MinTxnFee) {
lowFeeTxCount++
lowFeeTxSize += txn.GetSize()
}
}

Expand Down
2 changes: 1 addition & 1 deletion chain/pool/txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func (tp *TxnPool) processTx(txn *transaction.Transaction) error {
tp.blockValidationState.Commit()
default:
if oldTxn, err := list.Get(txn.UnsignedTx.Nonce); err == nil {
log.Warning("replace old tx")
log.Debug("replace old tx")
tp.blockValidationState.Lock()
defer tp.blockValidationState.Unlock()
if err := tp.CleanBlockValidationState([]*transaction.Transaction{oldTxn}); err != nil {
Expand Down
5 changes: 4 additions & 1 deletion util/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ var (
WalletFile: "wallet.json",
MaxGetIDSeeds: 3,
DBFilesCacheCapacity: 100,
NumLowFeeTxnPerBlock: 4,
NumLowFeeTxnPerBlock: 0,
LowFeeTxnSizePerBlock: 4096,
MinTxnFee: 10000000,
}
)
Expand All @@ -161,6 +162,7 @@ type Configuration struct {
CAPath string `json:"CAPath"`
GenesisBlockProposer string `json:"GenesisBlockProposer"`
NumLowFeeTxnPerBlock uint32 `json:"NumLowFeeTxnPerBlock"`
LowFeeTxnSizePerBlock uint32 `json:"LowFeeTxnSizePerBlock"` // in bytes
MinTxnFee int64 `json:"MinTxnFee"`
RegisterIDRegFee int64 `json:"RegisterIDRegFee"`
RegisterIDTxnFee int64 `json:"RegisterIDTxnFee"`
Expand Down Expand Up @@ -250,6 +252,7 @@ func Init() error {
if Parameters.SyncBatchWindowSize == 0 {
Parameters.SyncBatchWindowSize = defaultSyncBatchWindowSize
}
log.Printf("Set SyncBatchWindowSize to %vMB", Parameters.SyncBatchWindowSize)
}

err := Parameters.verify()
Expand Down

0 comments on commit a1790fa

Please sign in to comment.