Wednesday, July 3, 2024

mining {hardware} – Why does my program crash after 30k iterations throughout the Mine operate in my blockchain?

I’ve been attempting to construct my very own blockchain from scratch in Golang

I’ve coded the execution layer and not too long ago received performed with implementing the Consensus layer for my chain.

There’s one persistent concern although; I’ve this following operate that’s used to mine a specific block

func (bc *Blockchain) MineBlock(b *Block) error {
    bc.logger.Log(
        "msg", "mining block..",
    )
    var targetForBlock *massive.Int
    var err error
    if (bc.Top() % 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(massive.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("attempting 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 performed I begin the mining; by hashing the Block and evaluating it in opposition to the goal; within the occasion that it would 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 flawed within the software program structure. The preliminary goal for the chain is "0x00ffff0000000000000000000000000000000000000000000000000000"

Ought to I exploit GPU for mining ? How can I correctly debug the difficulty?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles