diff --git a/src/mining/block.go b/src/mining/block.go index ecfdea8..47cf474 100644 --- a/src/mining/block.go +++ b/src/mining/block.go @@ -9,6 +9,7 @@ import ( "os" txn "github.com/humblenginr/btc-miner/transaction" + "github.com/humblenginr/btc-miner/utils" ) // Data types taken from: https://developer.bitcoin.org/reference/block_chain.html @@ -35,7 +36,7 @@ func (bh *BlockHeader) Serialize(w io.Writer) error { return err } - if _, err := w.Write(bh.MerkleRoot[:]); err != nil { + if _, err := w.Write(utils.ReverseBytes(bh.MerkleRoot[:])); err != nil { return err } diff --git a/src/mining/mining.go b/src/mining/mining.go index 44d59e5..21303d2 100644 --- a/src/mining/mining.go +++ b/src/mining/mining.go @@ -17,16 +17,6 @@ var BlockVersion int32 = 0x00000004 var targetDifficultyHexString = "0000ffff00000000000000000000000000000000000000000000000000000000" var prevBlockHash = "1cc89d151ecc14e3c323fd538f8259c3e6a5ecfeb467395f651de50818a50000" -func findValidPrevBlockHash(nBits uint32) [32]byte { - for { - hash := utils.RandomSha256() - if HashToBig(&hash).Cmp(NbitsToTarget(nBits)) <= 0 { - return hash - } - - } -} - func GetCandidateBlock(txns []*txn.Transaction, hasWitness bool) Block { tarDif := new(big.Int) fmt.Sscanf(targetDifficultyHexString, "%064x", tarDif) @@ -49,7 +39,7 @@ func GetCandidateBlock(txns []*txn.Transaction, hasWitness bool) Block { // header nBits := TargetToNbits(tarDif) prevBH,_ := hex.DecodeString(prevBlockHash) - header := NewBlockHeader(BlockVersion, *utils.NewHash(prevBH), CalcMerkleRoot(blockTxns, false), time.Now().Unix(),nBits, 0) + header := NewBlockHeader(BlockVersion, [32]byte(prevBH), CalcMerkleRoot(blockTxns, false), time.Now().Unix(),nBits, 0) candidateBlock.BlockHeader = header // transactions