-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d521bdb
commit 8e69364
Showing
5 changed files
with
81 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package txnpicker | ||
|
||
import ( | ||
txn "github.com/humblenginr/btc-miner/transaction" | ||
) | ||
|
||
type TransactionsPicker struct { | ||
MempoolDirPath string | ||
MaxTxWeight int | ||
MaxTotalWeight int | ||
} | ||
|
||
func NewTransactionPicker(mempoolDirPath string, maxTxWeight int, maxTotalWeight int) TransactionsPicker { | ||
return TransactionsPicker{MempoolDirPath: mempoolDirPath, MaxTxWeight: maxTxWeight, MaxTotalWeight: maxTotalWeight} | ||
} | ||
|
||
|
||
// 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) PickTransactionsUsingPQ() []txn.Transaction { | ||
q := getTxnsQ(tp.MempoolDirPath) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package txnpicker | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
txn "github.com/humblenginr/btc-miner/transaction" | ||
"github.com/humblenginr/btc-miner/validation" | ||
"github.com/x1m3/priorityQueue" | ||
) | ||
|
||
var ( | ||
MempoolDirPath = "../mempool" | ||
) | ||
|
||
type Item txn.Transaction | ||
|
||
func (i Item) HigherPriorityThan(other priorityQueue.Interface) bool { | ||
return i.Priority > other.(Item).Priority | ||
} | ||
|
||
func validateHelper(tx txn.Transaction) bool { | ||
for inputIdx := range tx.Vin { | ||
if(!validation.Validate(tx, inputIdx)){ | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// GetTxnsQ returns a priority queue of valid transactions. It uses the mempoolDirPath as the folder to look for transactions. | ||
func getTxnsQ(mempoolDirPath string) *priorityQueue.Queue { | ||
pq := priorityQueue.New() | ||
files, err := os.ReadDir(MempoolDirPath) | ||
if err != nil { | ||
panic(err) | ||
} | ||
for _, f := range files { | ||
var transaction txn.Transaction | ||
txnPath := fmt.Sprintf("%s/%s", MempoolDirPath, f.Name()) | ||
byteResult, _ := os.ReadFile(txnPath) | ||
err = json.Unmarshal(byteResult, &transaction) | ||
if err != nil { | ||
panic(err) | ||
} | ||
isValid := validateHelper(transaction) | ||
if(isValid && !transaction.Vin[0].IsCoinbase){ | ||
transaction.UpdatePriority() | ||
pq.Push(Item(transaction)) | ||
} | ||
} | ||
return pq | ||
} |