diff --git a/src/main.go b/src/main.go index f8ea35d..6ab7f75 100644 --- a/src/main.go +++ b/src/main.go @@ -16,6 +16,7 @@ var ( MaxTxWeight = 4000000 BlockHeaderWeight = 320 MaxTotalWeight = 4000000 - BlockHeaderWeight + MaxFee = 20616923 ) @@ -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) diff --git a/src/txnpicker/picker.go b/src/txnpicker/picker.go index e1c2213..1bd80b3 100644 --- a/src/txnpicker/picker.go +++ b/src/txnpicker/picker.go @@ -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() }