Blockchain with Golang. Is It a good idea to use Golang for… | by Aman Agarwal | Enlear Academy

Algorithm for creating block

It should allow us to have most of the data we need block in one place, including the counter, so we can hash it and check whether it satisfies our goal requirements. If it doesn’t, we may try again by increasing our nonce. But, first, let’s take a look at what it looks like.

func (pow *ProofOfWork) Run() (int, []byte) { var intHash big.Int var hash [32]byte nonce := 0 // This is essentially an infinite loop due to how large // MaxInt64 is. for nonce < math.MaxInt64 { data := pow.InitNonce(nonce) hash = sha256.Sum256(data) fmt.Printf(“r%x”, hash) intHash.SetBytes(hash[:]) if intHash.Cmp(pow.Target) == -1 { break } else { nonce++ } } fmt.Println() return nonce, hash[:]}

This is the process of hashing the block and our nonce together. Then, we compare it to the target once it has been hashed. Finally, we quit if we have a valid hash; otherwise, we increment our nonce and repeat the process. Hashing is a CPU-intensive operation, and the “work” we’ve been talking about is the result of running all of these hashes and checking them.

Algorithm for validating the block

Now that we’ve figured out how to make valid blocks, we need to figure out how to validate them in a less time-consuming manner. We have kept the nonce used to build a successful hash because we return a tuple from our previous method. This indicates that we should execute a single hash using the data from the block and the nonce from the tuple. The hash we just made should equal the target we set from ProofOfWork.

To summarise, our function should return a boolean, comparing the hash generated by the block to the hash generated by this function and returning the true/false value due to that comparison.

func (pow *ProofOfWork) Validate() bool { var intHash big.Int data := pow.InitNonce(pow.Block.Nonce) hash := sha256.Sum256(data) intHash.SetBytes(hash[:]) return intHash.Cmp(pow.Target) == -1}

Updating the Block structures

type BlockChain struct { Blocks []*Block}type Block struct { Hash []byte Data []byte PrevHash []byte Nonce int}

The DeriveHash function is no longer usable. We aren’t hashing the blocks at all. Therefore this is a dilemma. We need to put this hot, fresh, sexy PoW functionality we just designed into action.

Now we need to do the hashing in our CreateBlock function like we were doing before with the DeriveHash.

func CreateBlock(data string, prevHash []byte) *Block { block := &Block{[]byte{}, []byte(data), prevHash, 0} // Don’t forget to add the 0 at the end for the nonce! pow := NewProofOfWork(block) nonce, hash := pow.Run() block.Hash = hash[:] block.Nonce = nonce return block}

As a result of the above, we are:A. Creating a blank blockB. Making a NewProofOfWorkC. Running the algorithm on the powD. Storing the Hash of the new blockE. Storing the Nonce of the new block

And finally updating our main.go

// main.gopackage mainimport ( “fmt” “strconv” “learning/v1/blockchain”)func main() { chain := blockchain.InitBlockChain() chain.AddBlock(“first block”) chain.AddBlock(“second block”) chain.AddBlock(“third block”) for _, block := range chain.Blocks { fmt.Printf(“Previous hash: %xn”, block.PrevHash) fmt.Printf(“data: %sn”, block.Data) fmt.Printf(“hash: %xn”, block.Hash) pow := blockchain.NewProofOfWork(block) fmt.Printf(“Pow: %sn”, strconv.FormatBool(pow.Validate())) fmt.Println() }}

Run go run main.go to see the results of your efforts. Go to your command prompt. Change the difficulty from 12 to 18 to discover how the difficulty affects the amount of “work” that needs to be done! Play around with it and have a good time with it.

Go through this Repository to better understand and see how we have used this to store in DB and access blockChain through CLI with minimal changes.

Related Articles

Back to top button

Adblock Detected

Please consider supporting us by disabling your ad blocker