Skip to content

Commit

Permalink
fix: Update nonce in the block header while hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
humblenginr committed Apr 10, 2024
1 parent 344aceb commit c44d1a3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 0 additions & 4 deletions src/mining/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
26 changes: 15 additions & 11 deletions src/mining/mining.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,38 @@ 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)
}

return candidateBlock
}

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 {
Expand All @@ -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)
Expand Down

0 comments on commit c44d1a3

Please sign in to comment.