-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithm.go
69 lines (58 loc) · 1.96 KB
/
algorithm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package poc
import (
"crypto/sha256"
"strings"
"github.com/pocethereum/pochain/common"
"github.com/pocethereum/pochain/common/hexutil"
plotpoc "github.com/pocethereum/pochain/consensus/poc/plot"
"github.com/pocethereum/pochain/core/types"
plotparams "github.com/pocethereum/pochain/params/plot"
"math/big"
)
func CalcGenerationSignature(lastGenSigBytes []byte, lastGeneratorBytes []byte) []byte {
h := sha256.New()
h.Write(lastGenSigBytes)
h.Write(lastGeneratorBytes)
return h.Sum(nil)
}
func CalcScoop(genSigBytes []byte, height uint64) uint64 {
h := sha256.New()
h.Write(genSigBytes)
h.Write(common.Uint64ToBytes(height))
digest := h.Sum(nil)
hashnum := new(big.Int).SetBytes(digest)
return hashnum.Mod(hashnum, new(big.Int).SetUint64(plotparams.ScoopsPerPlot)).Uint64()
}
func CalcHit(scoopDataBytes []byte, genSigBytes []byte) *big.Int {
h := sha256.New()
h.Write(scoopDataBytes)
h.Write(genSigBytes)
if common.ISBINGDEBUG {
return big.NewInt(0).SetBytes(hexutil.MustDecode("0x84"))
} else {
digest := h.Sum(nil)
return big.NewInt(0).SetBytes([]byte{digest[7], digest[6],
digest[5], digest[4], digest[3], digest[2], digest[1], digest[0]})
}
}
func CalcDeadline(scoopDataBytes []byte, genSigBytes []byte, baseTarget *big.Int) *big.Int {
hit := CalcHit(scoopDataBytes, genSigBytes)
return hit.Div(hit, baseTarget)
}
func CalcBlockPoc(header *types.Header) *types.BlockPoc {
nonce := header.Nonce.Uint64()
seed := strings.ToLower(header.Coinbase.Hex()[2:])
mp := plotpoc.NewMiningPlot(seed, nonce)
genSigBytes := header.GetGenerationSignature().Bytes()
scoopNumber := CalcScoop(genSigBytes, header.Number.Uint64())
scoopDataBytes := mp.GetScoop(scoopNumber)
deadline := CalcHit(scoopDataBytes, genSigBytes)
baseTarget := plotparams.DifficultyToBaseTarget(header.Difficulty)
deadline.Div(deadline, baseTarget)
return &types.BlockPoc{
Nonce: header.Nonce,
ScoopNumber: scoopNumber,
Deadline: deadline,
BaseTarget: baseTarget,
}
}