From c44d1a30b868b860d4bec616027b8f8409dea373 Mon Sep 17 00:00:00 2001 From: Nithish Karthik Date: Thu, 11 Apr 2024 01:44:54 +0530 Subject: [PATCH] fix: Update nonce in the block header while hashing --- src/main.go | 2 ++ src/mining/block.go | 4 ---- src/mining/mining.go | 26 +++++++++++++++----------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/main.go b/src/main.go index 968d0ca..33e5151 100644 --- a/src/main.go +++ b/src/main.go @@ -105,6 +105,8 @@ func SelectTransactionsFromPaths(validTxnPaths []string) []*txn.Transaction { return txnSlice } +// Block Hash does not meet difficulty target + func main() { UpdateValidTxns() txns := SelectTransactionsFromPaths(TemporaryValidTxns) diff --git a/src/mining/block.go b/src/mining/block.go index 7fcb433..ecfdea8 100644 --- a/src/mining/block.go +++ b/src/mining/block.go @@ -95,13 +95,9 @@ func (b *Block) WriteToFile(filePath string) error { cb := b.Coinbase w.WriteString(hex.EncodeToString(cb.RawHex())+"\n") - // Txid of coinbase transaction - w.WriteString(hex.EncodeToString(cb.TxHash())+"\n") - // 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 4a442a6..0e78854 100644 --- a/src/mining/mining.go +++ b/src/mining/mining.go @@ -16,25 +16,30 @@ var BlockVersion int32 = 0x00000004 var targetDifficultyHexString = "0000ffff00000000000000000000000000000000000000000000000000000000" func GetCandidateBlock(txns []*txn.Transaction, hasWitness bool) Block { - tarDif := new(big.Int) - tarDif.SetString(targetDifficultyHexString, 16) - fmt.Printf("TargetDifficulty: %064x\n", tarDif) + tarDif := new(big.Int) + fmt.Sscanf(targetDifficultyHexString, "%064x", tarDif) candidateBlock := Block{} + + var blockTxns []*txn.Transaction // coinbase transaction cb := NewCoinbaseTransaction(calculateFees(txns)) - txns = append(txns, &cb) + blockTxns = append(blockTxns, &cb) + for _, t := range txns { + blockTxns = append(blockTxns, t) + } + if(hasWitness){ - AddWitnessCommitment(&cb, txns) + AddWitnessCommitment(&cb, blockTxns) } candidateBlock.Coinbase = cb // header - header := NewBlockHeader(BlockVersion, utils.RandomSha256(), CalcMerkleRoot(txns, false), time.Now().Unix(),TargetToNbits(tarDif), 0) + header := NewBlockHeader(BlockVersion, utils.RandomSha256(), CalcMerkleRoot(blockTxns, false), time.Now().Unix(),TargetToNbits(tarDif), 0) candidateBlock.BlockHeader = header // transactions - for _, t := range txns { + for _, t := range blockTxns { candidateBlock.AddTransaction(*t) } @@ -42,8 +47,7 @@ func GetCandidateBlock(txns []*txn.Transaction, hasWitness bool) Block { } func MineBlock(candidateBlock Block, outputFilePath string) error { - nonce := findNonce(candidateBlock) - candidateBlock.BlockHeader.Nonce = nonce + nonce := findNonce(&candidateBlock) fmt.Printf("Found nonce: %d", nonce) err := candidateBlock.WriteToFile(outputFilePath) if err != nil { @@ -52,16 +56,16 @@ func MineBlock(candidateBlock Block, outputFilePath string) error { return nil } -func findNonce(candidateBlock Block) uint32 { +func findNonce(candidateBlock *Block) uint32 { // serialized block will be of 80 byte w := bytes.NewBuffer(make([]byte, 0, 80)) for { header := candidateBlock.BlockHeader nBits := candidateBlock.BlockHeader.Bits nonce := GetRandomNonce() + candidateBlock.BlockHeader.Nonce = nonce // hash the block header - // TODO: Properly calculate the capacity err := header.Serialize(w) if err != nil { fmt.Printf("WARN: Could not serialize block header: %v", err)