Skip to content

Commit

Permalink
set max fee limit
Browse files Browse the repository at this point in the history
  • Loading branch information
humblenginr committed Apr 13, 2024
1 parent acdd775 commit 2b5a122
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (
MaxTxWeight = 4000000
BlockHeaderWeight = 320
MaxTotalWeight = 4000000 - BlockHeaderWeight
MaxFee = 20616923

)

Expand All @@ -28,7 +29,7 @@ func LogDetailsAboutTx(tx txn.Transaction){
}

func main() {
picker := txnpicker.NewTransactionPicker(MempoolDirPath, MaxTxWeight, MaxTotalWeight)
picker := txnpicker.NewTransactionPicker(MempoolDirPath, MaxTxWeight, MaxTotalWeight, MaxFee)
txns := picker.PickUsingPQ()
candidateBlock := mining.GetCandidateBlock(txns, true)
mining.MineBlock(candidateBlock, OutputFilePath)
Expand Down
12 changes: 9 additions & 3 deletions src/txnpicker/picker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,34 @@ type TransactionsPicker struct {
MempoolDirPath string
MaxTxWeight int
MaxTotalWeight int
MaxFees int
}

func NewTransactionPicker(mempoolDirPath string, maxTxWeight int, maxTotalWeight int) TransactionsPicker {
return TransactionsPicker{MempoolDirPath: mempoolDirPath, MaxTxWeight: maxTxWeight, MaxTotalWeight: maxTotalWeight}
func NewTransactionPicker(mempoolDirPath string, maxTxWeight int, maxTotalWeight int, maxFees int) TransactionsPicker {
return TransactionsPicker{MempoolDirPath: mempoolDirPath, MaxTxWeight: maxTxWeight, MaxTotalWeight: maxTotalWeight, MaxFees: maxFees}
}



// PickTransactionsUsingPQ picks valid transactions from the mempool using priority queue. Transaction with higher fee/weight ratio is considered to be high priority.
func (tp *TransactionsPicker) PickUsingPQ() []*txn.Transaction {
q := getTxnsQ(tp.MempoolDirPath)
txns := make([]*txn.Transaction, 0)
totalWeight := 0
totalFee := 0

item := q.Pop();
for item != nil {
tx := txn.Transaction(item.(Item))
weight := tx.GetWeight()
fee := tx.GetFees()
fmt.Printf("Weight of the transaction: %d\n", weight)
fmt.Printf("Txid: %x\n", utils.ReverseBytes(tx.TxHash()))
if(weight+ totalWeight < tp.MaxTotalWeight) {
if((weight+ totalWeight < tp.MaxTotalWeight) && (fee + totalFee < tp.MaxFees)) {
txns = append(txns, &tx)
totalWeight += weight
totalFee += fee

}
item = q.Pop()
}
Expand Down

0 comments on commit 2b5a122

Please sign in to comment.