Not familiar to POW students can refer to this article: https://juejin.cn/post/6844903573684240391

Preparations:

  1. Unidirectional encryption: Unidirectional encryption takes human-readable text (plaintext) as input, such as the string “666”, and uses a mathematical function to produce an illegible output (ciphertext).
  2. Mining: Bitcoin is produced by awarding prizes to “winning miners,” who compete with each other for the prize. This process is called “mining”.

Why mine?

Believes the classmates last article is according to the steps to realize the function of basic block chain, but have found that it is easy to generate a block, if so, scarcity is very low, no value, so we need to add the difficulty of the generated block, but also with the passage of time, the difficulty coefficient is higher and higher. This ensures that a certain amount of effort, in this case solving a mathematical problem, is required to obtain the block.

What do you dig for?

The sha-256 algorithm is also known as the hash algorithm. This algorithm is used to calculate the text given by a function to produce a 256-bit text. For example, “old iron 666” is calculated to produce the hash value: 7 bde6d3ea9a23cf15b0e01fd223649a9f9db10ea7b18150b04ce5dc18c191fcb students can have a try, find a SHA – 256 encrypted online website, the same text, the result is the same. Now, some of you might think, “Can a hash be like an ID number, representing a unique person?” Yeah, that’s one of its properties, its uniqueness, and the other property is that it’s almost impossible to work backwards, Who can see “7 bde6d3ea9a23cf15b0e01fd223649a9f9db10ea7b18150b04ce5dc18c191fcb”, which is a string representing the “brother” 666 small sum up:

  1. Hash values are unique
  2. Given the plaintext and hash values, it is easy to verify that the hash value is correct by reevaluating the plaintext hash using the same SHA-256 algorithm and then comparing the two hashes to see if they are consistent.
  3. Given a single hash, it’s almost impossible to infer that its plaintext is private and that’s what we’re going to dig for, with the condition that the first two bits of the hash are zeros, and people will say, “Didn’t it just say unique? ‘brother 666 0 is how to make it before 2 “, this classmate, we will add a random number in the code and other information block, plus your clear, go to calculate a hash value, if the first two is not zero, then change the random number and other information remains the same, calculate again, until it is calculated, these calculations you do is your work certificate, The diagram below:

Then we refresh the browser and see the following result:

Code implementation:

Here’s the code, but let’s think about what new features we need to add from the previous article:

  1. A variable that controls the value of the hash and starts with zeros, also known as the difficulty system
  2. A function that determines whether a hash value meets the requirement
  3. Add a loop to the original generating block function to evaluate

Check whether the hash value meets the requirement

func isHashValid(hash string, difficulty int) bool {
	prefix := strings.Repeat("0", difficulty)
	return strings.HasPrefix(hash, prefix)} Go language strings Repeat method is very convenientCopy the code

Generate block function transformation:

func generateBlock(oldBlock Block, Content string) Block {
	var newBlock Block

	t := time.Now()
	newBlock.Index = oldBlock.Index + 1
	newBlock.Timestamp = t.String()
	newBlock.Content = Content
	newBlock.PrevHash = oldBlock.Hash
	newBlock.Difficulty = difficulty

	for i := 0; ; i++ {
		hex := fmt.Sprintf("%x", i)
		newBlock.Nonce = hex
		if! isHashValid(calculateHash(newBlock), newBlock.Difficulty) { fmt.Println(calculateHash(newBlock),"Keep counting!)
			continue
		} else {
			fmt.Println(calculateHash(newBlock), " 中!")
			newBlock.Hash = calculateHash(newBlock)
			break}}return newBlock
}
Copy the code

Conclusion:

The principle of proof of work is introduced. SHA256 features uniqueness and privacy, which adds proof of work mechanism to our blockchain. The next chapter will introduce how to generate multi-node blockchain. Code at: https://github.com/sunqichao/blockchainPOW

Reference: https://medium.com/@mycoralhealth/code-your-own-blockchain-in-less-than-200-lines-of-go-e296282bcffc