有關PoP
概要
計算方法
PoP = hash(PIN_ID + Merkle_Root) * Block_Hashpackage pop
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"math/big"
"strconv"
)
func CalculateHash(pinid string, merkleRoot string) string {
h := sha256.New()
h.Write([]byte(pinid + merkleRoot))
return hex.EncodeToString(h.Sum(nil))
}
func CalculateProductToHexStr(blockhash string, pinHash string) string {
blockhashByte, _ := hex.DecodeString(blockhash)
blockhashInt, _ := new(big.Int).SetString(blockhash, 16)
pinHashByte, _ := hex.DecodeString(pinHash)
pinHashInt, _ := new(big.Int).SetString(pinHash, 16)
popByte := new(big.Int).Mul(blockhashInt, pinHashInt).Bytes()
// 計算總位數:32+32=64
totalLen := len(blockhashByte) + len(pinHashByte)
// 需要補 0 的位數
remainingLen := totalLen - len(popByte)
for i := 0; i < remainingLen; i++ {
popByte = append([]byte{0}, popByte...)
}
return hex.EncodeToString(popByte)
}
func ConvertToOctalHex(productHex string) (string, int64) {
productByte, _ := hex.DecodeString(productHex)
// 轉二進制
bList := make([]string, 0)
for _, b := range productByte {
binaryB := fmt.Sprintf("%b", b)
bList = append(bList, fmt.Sprintf("%08s", binaryB))
}
productBinaryStr := ""
for _, b := range bList {
productBinaryStr += b
}
productBinaryStr = productBinaryStr[:510]
bCount := int64(0)
for _, b := range productBinaryStr {
if b == '0' {
bCount++
} else {
break
}
}
// 二進制 str 轉 8 進制 str
octal := ""
for i := 0; i < len(productBinaryStr); i += 3 {
binaryStr := productBinaryStr[i : i+3]
num, err := strconv.ParseInt(binaryStr, 2, 64)
if err != nil {
fmt.Println("ParseInt error:", err)
return "", 0
}
octal += strconv.FormatInt(num, 8)
}
return octal, bCount
}
func GenPop(pinid, merkleRoot, blockHash string) (string, int64) {
// 計算 pinHash
pinHash := CalculateHash(pinid, merkleRoot)
// blockhash * pinHash
productHexStr := CalculateProductToHexStr(blockHash, pinHash)
// 轉 8 進制
octal, bCount := ConvertToOctalHex(productHexStr)
return octal, bCount
}PoP Level(難度等級)
PoP Score(PoP分數)
PoP Score 的定義與應用場景
PoP Score 的計算方法
計算結果
Last updated