I’ve been making an attempt to construct my very own blockchain from scratch in Golang
I’ve coded the execution layer and lately acquired finished with implementing the Consensus layer for my chain.
There’s one persistent difficulty although; I’ve this following perform that’s used to mine a specific block
func (bc *Blockchain) MineBlock(b *Block) error
bc.logger.Log(
"msg", "mining block..",
)
var targetForBlock *huge.Int
var err error
if (bc.Peak() % HEIGHT_DIVISOR) == 0
targetForBlock, err = bc.calcTargetValue(b)
bc.Goal = targetForBlock
if err != nil
return err
else
targetForBlock = bc.Goal
fmt.Printf("goal is %xn", targetForBlock)
fmt.Printf("goal for block %xn", targetForBlock)
bHash := b.HashWithoutCache(BlockHasher)
hashBigInt, _ := new(huge.Int).SetString(bHash.String(), 16)
for isLowerThanTarget(hashBigInt, targetForBlock) != -1
nonce := b.Header.Nonce
b.Header.Nonce++
bHash = b.HashWithoutCache(BlockHasher)
hashBigInt.SetString(bHash.String(), 16)
fmt.Printf("making an attempt new combo with nonce %v block hash %s and goal %x n", nonce, bHash.String(), targetForBlock)
// updating timestamp
b.Header.Timestamp = uint64(time.Now().UnixNano())
b.Header.Goal = targetForBlock
b.Header.NBits = targetToCompact(targetForBlock)
fmt.Printf("block mined with hash %s and goal %x n", bHash.String(), targetForBlock)
return nil
- First the goal for the block is calculated; the goal is adjusted each 5 blocks
- As soon as that’s finished I begin the mining; by hashing the Block and evaluating it in opposition to the goal; within the occasion that it does not I increment the NONCE till the situation is resolved.
What tends to occur is that; after 30k or so iterations; my program merely crashes. I need to know what I’m doing mistaken within the software program structure. The preliminary goal for the chain is "0x00ffff0000000000000000000000000000000000000000000000000000"
Ought to I take advantage of GPU for mining ? How can I correctly debug the problem?
Leave a Reply