From 08b4cc34b056030219b6276dc8ca30dc02505149 Mon Sep 17 00:00:00 2001 From: Nithish Karthik Date: Fri, 12 Apr 2024 04:47:56 +0530 Subject: [PATCH] change weight func --- src/mining/mining.go | 10 ++++++---- src/transaction/tx.go | 25 +++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/mining/mining.go b/src/mining/mining.go index 6f003d9..ef4060a 100644 --- a/src/mining/mining.go +++ b/src/mining/mining.go @@ -34,21 +34,23 @@ func (i Item) HigherPriorityThan(other priorityQueue.Interface) bool { var MaxBlockWeight = 4000000 - 100 // 100 is a buffer amount + + func GetCandidateBlock(txnPq *priorityQueue.Queue, hasWitness bool) Block { tarDif := new(big.Int) txns := make([]*txn.Transaction, 0) totalWeight := 0 + j: for { item := txnPq.Pop() - if (item == nil) { break } + if (item == nil) { break j } tx := txn.Transaction((item).(Item)) - fmt.Printf("TOTw8: %v \n\n", totalWeight) - if(tx.GetWeight() + totalWeight <= MaxBlockWeight) { + if((tx.GetWeight() + totalWeight) <= MaxBlockWeight) { txns = append(txns, &tx) totalWeight += tx.GetWeight() - } else {break} + } else {break j} } fmt.Sscanf(targetDifficultyHexString, "%064x", tarDif) diff --git a/src/transaction/tx.go b/src/transaction/tx.go index 7df857e..91ab1bb 100644 --- a/src/transaction/tx.go +++ b/src/transaction/tx.go @@ -176,6 +176,7 @@ func (t Transaction) getFeeByWeight() float64 { } func (t Transaction) weightCalc(witness bool) int { + witness = witness && t.HasWitness() weight := 8 if(witness) {weight+=2} weight += VarIntSerializeSize(uint64(len(t.Vin))) @@ -200,11 +201,31 @@ func (t Transaction) weightCalc(witness bool) int { } func (t Transaction) GetWeight() int { + weight := 0 + weight += 16 + weight += 1 + weight += 1 + for _, i := range t.Vin { + weight += i.SerializeSize() * 4 + + witness := make([][]byte, 0) + for _, w := range i.Witness { + wt,_ := hex.DecodeString(w) + witness = append(witness, wt) + } + weight += SerializeWitnessSize(witness) + } + for _, o := range t.Vout { + weight += o.SerializeSize() * 4 + } + weight += 16 + return weight - baseSize := t.weightCalc(false) +/* baseSize := t.weightCalc(false) totalSize := t.weightCalc(true) - return (baseSize * 3) + totalSize + return ((baseSize * 4) + totalSize) + */ } func (t Transaction) GetFees() int {