From 7fd82a8fff829bc72a51bc5c3225a7c9aa1fa650 Mon Sep 17 00:00:00 2001 From: OsauravO Date: Fri, 26 Apr 2024 12:17:22 +0530 Subject: [PATCH] ordering of txns in mempool --- main.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index d3c1c99..f514666 100644 --- a/main.go +++ b/main.go @@ -285,7 +285,7 @@ func NewMerkleTree(leaves []string) *MerkleNode { } func CreateWitnessMerkle() string { - _, _, wTxIDs := Prioritize() + _, _, wTxIDs := Ordering() wTxIDs = append([]string{"0000000000000000000000000000000000000000000000000000000000000000"}, wTxIDs...) merkleRoot := NewMerkleTree(wTxIDs) fmt.Println("WMKR: ", hex.EncodeToString(merkleRoot.Data)) @@ -296,8 +296,53 @@ func CreateWitnessMerkle() string { return hex.EncodeToString(WitnessCommitment) } +func Ordering() (uint64, []string, []string) { + var permittedTxIDs []string + var permittedWTxIDs []string + dir := "./mempool" + files, _ := os.ReadDir(dir) + var txInfo []TxInfo + for _, file := range files { + txData, err := JsonData(dir + "/" + file.Name()) + Handle(err) + var tx Transaction + err = json.Unmarshal([]byte(txData), &tx) + var fee uint64 = 0 + for _, vin := range tx.Vin { + fee += vin.Prevout.Value + } + for _, vout := range tx.Vout { + fee -= vout.Value + } + serialized, _ := SerializeTransaction(&tx) + segserialized, _ := SegWitSerialize(&tx) + txID := ReverseBytes(To_sha(To_sha(serialized))) + wtxID := ReverseBytes(To_sha(To_sha(segserialized))) + txInfo = append(txInfo, TxInfo{TxID: hex.EncodeToString(txID), WTxID: hex.EncodeToString(wtxID), Fee: fee, Weight: uint64(CalculateWitnessSize(&tx) + CalculateBaseSize(&tx)*4)}) + + } + sort.Slice(txInfo, func(i, j int) bool { + return Comp(txInfo[i], txInfo[j]) + }) + var PermissibleTxs []TxInfo + var PermissibleWeight uint64 = 3999300 + var reward uint64 = 0 + for _, tx := range txInfo { + if PermissibleWeight >= tx.Weight { + PermissibleTxs = append(PermissibleTxs, tx) + PermissibleWeight -= tx.Weight + permittedTxIDs = append(permittedTxIDs, tx.TxID) + permittedWTxIDs = append(permittedWTxIDs, tx.WTxID) + reward += tx.Fee + } + } + fmt.Println("weight: ", PermissibleWeight) + fmt.Println("reward: ", reward) + return reward, permittedTxIDs, permittedWTxIDs +} + func main() { - networkReward, transactionIDs, _ := Prioritize() + networkReward, transactionIDs, _ := Ordering() // Create a coinbase transaction for the network reward coinbaseTx := CreateCoinbase(networkReward)