POW analysis of blockchain implementation

All implementations of this code have been synchronized open source on github, project address:
link

PoW Algorithm Introduction

Proof of Work (POW for short), simple understanding is a proof to confirm that you have done a certain amount of work. The entire process of monitoring work is usually extremely inefficient, and it is a very efficient way to prove that the corresponding workload has been completed by certifying the results of the work. For example, graduation certificates, driver's licenses, etc. in real life are also proofs obtained by means of inspection results (passing relevant exams).

The proof-of-work system (or protocol, function) is an economic countermeasure against denial-of-service attacks and other service abuses. It requires the initiator to perform a certain amount of calculation, which means that the computer needs to consume a certain amount of time.

PoW Algorithm Ideas

The consensus algorithm is realized by continuously hashing the node data and comparing the target hash with the current hash value.
insert image description here

Code and Implementation Ideas

Pseudo-code ideas:
1. Create a ProofOfWork class with two attributes {target hash, target block}
2. Calculate the hash of the target block through the ProofOfWork class and compare it with the target hash.
3. If it is determined that the hash value of the target block is less than the target hash value, the workload proof is completed, broadcast, and uploaded to the chain.
The structure of ProofOfWork:

// 工作量证明的结构
type ProofOfWork struct {
    
    
	//需要共识验证的区块
	Block *Block
	//目标难度的哈希,大数存储
	target *big.Int
}

Compare the hash values:

func (pow *ProofOfWork) run() ([]byte, int64) {
    
    
	//碰撞次数
	var nonce = int64(0)
	//用于比较的大数hash
	var hashInt big.Int
	//目标hash值
	var hash [32]byte
	//无限循环,生成符合条件的哈希
	for {
    
    
		//生成准备数据
		dataByte := pow.prepareData(int64(nonce))
		hash = sha256.Sum256(dataByte)
		//将byte数据转换为大数
		hashInt.SetBytes(hash[:])
		if pow.target.Cmp(&hashInt) == 1 {
    
    
			//找到了符合条件的hash
			break
		}
		nonce++
	}
	fmt.Printf("打印碰撞次数%v\n", nonce)
	return hash[:], nonce
}

Stitch block data to facilitate calculation of hash value:

// 生成准备数据,对ProofOfWork数据拼接形成哈希值并返回
func (pow *ProofOfWork) prepareData(nonce int64) []byte {
    
    
	var data []byte
	timeStampBytes := IntToHex(pow.Block.TimeStamp)
	heightBytes := IntToHex(pow.Block.Height)
	//将多个[]byte数组转换为一个[]byte数组
	data = bytes.Join([][]byte{
    
    
		timeStampBytes,
		heightBytes,
		pow.Block.PrevBlockHash,
		pow.Block.Data,
		IntToHex(nonce),
		IntToHex(targetBit),
	}, []byte{
    
    })
	return data
}

Experimental results

It can be seen that a total of three blocks are generated, and the number of collisions in each block is as follows. Finally, the entire blockchain is output, and it can be seen that the hash values ​​of the blocks present a linked list structure.
insert image description here

Guess you like

Origin blog.csdn.net/qq_45931661/article/details/128258768
Recommended