Skip to content

Commit

Permalink
ordering of txns in mempool
Browse files Browse the repository at this point in the history
  • Loading branch information
OsauravO committed Apr 26, 2024
1 parent f4bf6e6 commit 7fd82a8
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
Expand Down

0 comments on commit 7fd82a8

Please sign in to comment.