Skip to content

Commit

Permalink
change weight func
Browse files Browse the repository at this point in the history
  • Loading branch information
humblenginr committed Apr 11, 2024
1 parent 1d31202 commit 08b4cc3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
10 changes: 6 additions & 4 deletions src/mining/mining.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 23 additions & 2 deletions src/transaction/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand All @@ -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 {
Expand Down

0 comments on commit 08b4cc3

Please sign in to comment.