diff --git a/src/main.go b/src/main.go index f4ea1ce..f8ea35d 100644 --- a/src/main.go +++ b/src/main.go @@ -6,13 +6,18 @@ import ( "github.com/humblenginr/btc-miner/mining" txn "github.com/humblenginr/btc-miner/transaction" "github.com/humblenginr/btc-miner/utils" + "github.com/humblenginr/btc-miner/txnpicker" ) -var OutputFilePath = "../output.txt" - - +var ( + OutputFilePath = "../output.txt" + MempoolDirPath = "../mempool" + MaxTxWeight = 4000000 + BlockHeaderWeight = 320 + MaxTotalWeight = 4000000 - BlockHeaderWeight +) func LogDetailsAboutTx(tx txn.Transaction){ txid := tx.TxHash() @@ -23,7 +28,8 @@ func LogDetailsAboutTx(tx txn.Transaction){ } func main() { - pq := GetTxnsQ() - candidateBlock := mining.GetCandidateBlock(pq, true) + picker := txnpicker.NewTransactionPicker(MempoolDirPath, MaxTxWeight, MaxTotalWeight) + txns := picker.PickUsingPQ() + candidateBlock := mining.GetCandidateBlock(txns, true) mining.MineBlock(candidateBlock, OutputFilePath) } diff --git a/src/transaction/tx.go b/src/transaction/tx.go index 968d0f9..cb0eba4 100644 --- a/src/transaction/tx.go +++ b/src/transaction/tx.go @@ -119,22 +119,8 @@ func (t *Transaction) HasWitness() bool { return false } - -func (tx *Transaction) SerializeSizeWithWitness() int { - witnessSize := 0 - for _, t := range tx.Vin { - witnessByteArray := make([][]byte, 0) - for _, w := range t.Witness{ - witness,_ := hex.DecodeString(w) - witnessByteArray = append(witnessByteArray, witness) - } - witnessSize += SerializeWitnessSize(witnessByteArray) - } - return tx.SerializeSize() + witnessSize -} - // SerializeSize returns the serialized size of the transaction without accounting for any witness data. -func (tx *Transaction) SerializeSize() int { +func (tx *Transaction) SerializeSize(witness bool) int { // Version 4 bytes + LockTime 4 bytes + Serialized varint size for the // number of transaction inputs and outputs. n := 8 + VarIntSerializeSize(uint64(len(tx.Vin))) + @@ -148,7 +134,7 @@ func (tx *Transaction) SerializeSize() int { n += txOut.SerializeSize() } - if tx.HasWitness() { + if tx.HasWitness() && witness { // The marker, and flag fields take up two additional bytes. n += 2 @@ -176,25 +162,9 @@ func (t Transaction) getFeeByWeight() float64 { } 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 + nonWitnessSize := t.SerializeSize(false) + witnessSize := t.SerializeSize(true) - nonWitnessSize + return nonWitnessSize*4 + witnessSize } func (t Transaction) GetFees() int { @@ -217,7 +187,7 @@ func (t Transaction) String() string { // RawHex gives the serialized transaction with witness data if any in bytes func (t Transaction) RawHex() []byte { - w := bytes.NewBuffer(make([]byte, 0, t.SerializeSize())) + w := bytes.NewBuffer(make([]byte, 0, t.SerializeSize(true))) err := t.Serialize(true, w) if err != nil { panic(err) @@ -227,7 +197,7 @@ func (t Transaction) RawHex() []byte { } func (t Transaction) TxHash() []byte { - w := bytes.NewBuffer(make([]byte, 0, t.SerializeSize())) + w := bytes.NewBuffer(make([]byte, 0, t.SerializeSize(false))) // For calculating txid, we don't need the witness data err := t.Serialize(false, w) if err != nil { @@ -239,7 +209,7 @@ func (t Transaction) TxHash() []byte { } func (t Transaction) WitnessHash() []byte { - w := bytes.NewBuffer(make([]byte, 0, t.SerializeSize())) + w := bytes.NewBuffer(make([]byte, 0, t.SerializeSize(true))) // For calculating txid, we don't need the witness data err := t.Serialize(true, w) if err != nil { diff --git a/src/txnpicker/picker.go b/src/txnpicker/picker.go index 11d8655..e1c2213 100644 --- a/src/txnpicker/picker.go +++ b/src/txnpicker/picker.go @@ -1,7 +1,10 @@ package txnpicker import ( + "fmt" + txn "github.com/humblenginr/btc-miner/transaction" + "github.com/humblenginr/btc-miner/utils" ) type TransactionsPicker struct { @@ -16,7 +19,23 @@ func NewTransactionPicker(mempoolDirPath string, maxTxWeight int, 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 { +func (tp *TransactionsPicker) PickUsingPQ() []*txn.Transaction { q := getTxnsQ(tp.MempoolDirPath) + txns := make([]*txn.Transaction, 0) + totalWeight := 0 + + item := q.Pop(); + for item != nil { + tx := txn.Transaction(item.(Item)) + weight := tx.GetWeight() + fmt.Printf("Weight of the transaction: %d\n", weight) + fmt.Printf("Txid: %x\n", utils.ReverseBytes(tx.TxHash())) + if(weight+ totalWeight < tp.MaxTotalWeight) { + txns = append(txns, &tx) + totalWeight += weight + } + item = q.Pop() + } + return txns } diff --git a/src/validation/sighash/sighash.go b/src/validation/sighash/sighash.go index 622e86f..1a96396 100644 --- a/src/validation/sighash/sighash.go +++ b/src/validation/sighash/sighash.go @@ -91,7 +91,7 @@ func CalcSignatureHash(sigScript []byte, hashType SigHashType, tx *transaction.T // double sha256 of the modified serialized // transaction with hash type appended. - wbuf := bytes.NewBuffer(make([]byte, 0, txCopy.SerializeSize()+4)) + wbuf := bytes.NewBuffer(make([]byte, 0, txCopy.SerializeSize(false)+4)) txCopy.Serialize(false, wbuf) binary.Write(wbuf, binary.LittleEndian, hashType) return utils.DoubleHash(wbuf.Bytes())