From 344aceb4850f6fe175b38040ee8a42cb764f168a Mon Sep 17 00:00:00 2001 From: Nithish Karthik Date: Thu, 11 Apr 2024 01:02:45 +0530 Subject: [PATCH] fix: Add coinbase transaction for merkle root calculation --- src/main.go | 40 +++++++++++++++++++++++++++++++++++++++- src/mining/block.go | 2 ++ src/mining/mining.go | 15 +++++++-------- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/src/main.go b/src/main.go index 8495424..968d0ca 100644 --- a/src/main.go +++ b/src/main.go @@ -67,9 +67,47 @@ func UpdateValidTxns() { } } +var TemporaryValidTxns = []string{ + "000cb561188c762c81f76976f816829424e2af9e0e491c617b7bf41038df3d35.json", + "001035505afbf143e51bd667099190943a38eee20092bb691e72eaa44992b2f7.json", + "0018c221bca3da35128baabe412a14c95b6864b2e6f7f7a8ffdd8eb0923dec49.json", + "001e296ba3feddda174e62df57506861823b0831af983364d2c1808779b443f7.json", + "0022a52ad27796a1a2d9eddd6f4b055c097b51ad7cb8f000fe0d78b26cb71639.json", + "002f5ff2f870154b109d823bbad6fd349582d5b85ead5ce0f9f7a4a0270ce37a.json", + "0030b203ff93ff7f4c6fdabda1026a8167038dfb94985669721086df9ad4337a.json", + "00359dc6a7cf0d808eb5cf6450cf8243408395c6fa5be649f4f6c3b5a394d1b7.json", + "003d95255dacb65b0896ab1fc7d3f88d347c762d5164de45a5bea75da95c3830.json", + "004c2dec582638c26fed3d55b2fee8bbf1c2d4b70449b0a3f03faa105ad03f15.json", + "00550d2c315129f77a97d1b5f8483d1efc9e9edebbb229dcf4a87e0c988f6840.json", + "005747a8401a6ef30f3d55172fea54a4c4e940d0dcde372087aea286661e04c5.json", + "00703f54c52da70ce7a94f2f59b73c2435476eb531362adb20e78ecc159dd376.json", + "007b0fd78cdb709f83823b79fd9824bc39873632c6472f4e4d7c766f9a7e0e82.json", + "00b6d3b2d204a97a8877e34c1f3ce454cba5d299aab705e263b49328cbc683f1.json", + "00b8ffa99b973547fb665bdee70d3123f345c12664046e767777a822284fea3f.json", + "00c4387b3de5d0376b3df4db81a6016b584aad10c5aff619d15627e43ca4d697.json", + "00cd2a7549860949e9b7b1c182060ace476f5127f3ee561d72633ffaaea2b2ec.json", + "00d12b523d8b7ad90e2269767478764c243625539dc59bcd457d14ca1aa4e38c.json", + "00d7c8ddc2e75f6ba97520623390f01a910dc66a9e6a2052ee31f1b99aabdea5.json", + "00d9c01fd8722f63cc327c93e59de64395d1e6ca5861ae6b9b149b364d082352.json", +} + + +func SelectTransactionsFromPaths(validTxnPaths []string) []*txn.Transaction { + txnSlice := make([]*txn.Transaction, 0) + + for _, fileName := range validTxnPaths { + var transaction txn.Transaction + txnPath := fmt.Sprintf("%s/%s", MempoolDirPath, fileName) + byteResult, _ := os.ReadFile(txnPath) + json.Unmarshal(byteResult, &transaction) + txnSlice = append(txnSlice, &transaction) + } + return txnSlice +} + func main() { UpdateValidTxns() - txns := mining.SelectTransactionsFromFolder(ValidTxnsDirPath) + txns := SelectTransactionsFromPaths(TemporaryValidTxns) candidateBlock := mining.GetCandidateBlock(txns, true) mining.MineBlock(candidateBlock, OutputFilePath) } diff --git a/src/mining/block.go b/src/mining/block.go index adc42f3..7fcb433 100644 --- a/src/mining/block.go +++ b/src/mining/block.go @@ -100,6 +100,8 @@ func (b *Block) WriteToFile(filePath string) error { // Txid of rest of the transactions for _, txn := range b.Transactions { + // because the txid of coinbase is already added + if(txn.Vin[0].IsCoinbase){continue} txid := txn.TxHash() w.WriteString(hex.EncodeToString(txid)+"\n") } diff --git a/src/mining/mining.go b/src/mining/mining.go index 0a2d2a0..4a442a6 100644 --- a/src/mining/mining.go +++ b/src/mining/mining.go @@ -18,19 +18,21 @@ var targetDifficultyHexString = "0000ffff000000000000000000000000000000000000000 func GetCandidateBlock(txns []*txn.Transaction, hasWitness bool) Block { tarDif := new(big.Int) tarDif.SetString(targetDifficultyHexString, 16) + fmt.Printf("TargetDifficulty: %064x\n", tarDif) candidateBlock := Block{} - // header - header := NewBlockHeader(BlockVersion, utils.RandomSha256(), CalcMerkleRoot(txns, false), time.Now().Unix(),TargetToNbits(tarDif), 0) - candidateBlock.BlockHeader = header - // coinbase transaction cb := NewCoinbaseTransaction(calculateFees(txns)) + txns = append(txns, &cb) if(hasWitness){ AddWitnessCommitment(&cb, txns) } candidateBlock.Coinbase = cb + // header + header := NewBlockHeader(BlockVersion, utils.RandomSha256(), CalcMerkleRoot(txns, false), time.Now().Unix(),TargetToNbits(tarDif), 0) + candidateBlock.BlockHeader = header + // transactions for _, t := range txns { candidateBlock.AddTransaction(*t) @@ -53,13 +55,10 @@ func MineBlock(candidateBlock Block, outputFilePath string) error { func findNonce(candidateBlock Block) uint32 { // serialized block will be of 80 byte w := bytes.NewBuffer(make([]byte, 0, 80)) - - for { - nonce := GetRandomNonce() - header := candidateBlock.BlockHeader nBits := candidateBlock.BlockHeader.Bits + nonce := GetRandomNonce() // hash the block header // TODO: Properly calculate the capacity