-
Notifications
You must be signed in to change notification settings - Fork 3
/
ipow.go
151 lines (131 loc) · 3.54 KB
/
ipow.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (c) 2020-2021 The bitcoinpay developers
// license that can be found in the LICENSE file.
// Reference resources of rust bitVector
package pow
import (
"encoding/hex"
"github.com/btceasypay/bitcoinpay/common/hash"
"github.com/btceasypay/bitcoinpay/core/json"
"math/big"
)
// proof data length 188
const POW_LENGTH = 174
//except pow type 4bytes and nonce 8 bytes 176 bytes
const PROOFDATA_LENGTH = 169
type PowType byte
type PowBytes []byte
const (
//pow type enum
BLAKE2BD PowType = 0
CUCKAROO PowType = 1
CUCKATOO PowType = 2
CUCKAROOM PowType = 3
X16RV3 PowType = 4
X8R16 PowType = 5
BITCOINPAYKECCAK256 PowType = 6
)
var PowMapString = map[PowType]interface{}{
BLAKE2BD: "blake2bd",
CUCKAROO: "cuckaroo",
CUCKATOO: "cuckatoo",
CUCKAROOM: "cuckaroom",
X16RV3: "x16rv3",
X8R16: "x8r16",
BITCOINPAYKECCAK256: "bitcoinpay_keccak256",
}
type ProofDataType [PROOFDATA_LENGTH]byte
func (this *ProofDataType) String() string {
return hex.EncodeToString(this[:])
}
func (this *ProofDataType) Bytes() []byte {
return this[:]
}
type IPow interface {
// verify result difficulty
Verify(headerData []byte, blockHash hash.Hash, targetDiff uint32) error
//set header nonce
SetNonce(nonce uint32)
//calc next diff
GetNextDiffBig(weightedSumDiv *big.Int, oldDiffBig *big.Int, currentPowPercent *big.Int) *big.Int
GetNonce() uint32
GetPowType() PowType
//set pow type
SetPowType(powType PowType)
GetProofData() string
//set proof data
SetProofData([]byte)
Bytes() PowBytes
BlockData() PowBytes
//if cur_reduce_diff > 0 compare cur_reduce_diff with powLimitBits or minDiff ,the cur_reduce_diff should less than powLimitBits , and should more than min diff
//if cur_reduce_diff <=0 return powLimit or min diff
GetSafeDiff(cur_reduce_diff uint64) *big.Int
// pow percent
PowPercent() *big.Int
//pow result
GetPowResult() json.PowResult
//SetParams
SetParams(params *PowConfig)
//SetHeight
SetMainHeight(height int64)
CheckAvailable() bool
CompareDiff(newtarget *big.Int, target *big.Int) bool
}
type Pow struct {
PowType PowType //header pow type 1 bytes
Nonce uint32 //header nonce 4 bytes
ProofData ProofDataType // 1 edge_bits 168 bytes circle length total 169 bytes
params *PowConfig
mainHeight int64
}
//get pow instance
func GetInstance(powType PowType, nonce uint32, proofData []byte) IPow {
var instance IPow
switch powType {
case BLAKE2BD:
instance = &Blake2bd{}
case X16RV3:
instance = &X16rv3{}
case X8R16:
instance = &X8r16{}
case BITCOINPAYKECCAK256:
instance = &BitcoinpayKeccak256{}
case CUCKAROO:
instance = &Cuckaroo{}
case CUCKAROOM:
instance = &Cuckaroom{}
case CUCKATOO:
instance = &Cuckatoo{}
default:
instance = &Blake2bd{}
}
instance.SetPowType(powType)
instance.SetNonce(nonce)
instance.SetProofData(proofData)
return instance
}
func (this *Pow) SetPowType(powType PowType) {
this.PowType = powType
}
func (this *Pow) SetParams(params *PowConfig) {
this.params = GetPowConfig().Set(params)
}
func (this *Pow) SetMainHeight(mainHeight int64) {
this.mainHeight = mainHeight
}
func (this *Pow) GetPowType() PowType {
return this.PowType
}
func (this *Pow) GetNonce() uint32 {
return this.Nonce
}
func (this *Pow) SetNonce(nonce uint32) {
this.Nonce = nonce
}
func (this *Pow) GetProofData() string {
return this.ProofData.String()
}
//set proof data except pow type
func (this *Pow) SetProofData(data []byte) {
l := len(data)
copy(this.ProofData[0:l], data[:])
}