/
miner.go
256 lines (216 loc) · 6.27 KB
/
miner.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright (C) 2019 ZVChain
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package types
import (
"bytes"
"fmt"
"github.com/vmihailenco/msgpack"
"github.com/zvchain/zvchain/common"
"math/big"
)
type MinerType byte
// defines the miner role type
const (
MinerTypeVerify MinerType = iota // Proposal role
MinerTypeProposal // Verify role
)
type MinerStatus byte
type NodeIdentity byte
const (
MinerStatusPrepare MinerStatus = 0 // Miner prepare status, maybe abort or not enough stake or without pks,cannot participated any mining process
MinerStatusActive = 1 // Miner is activated, can participated in mining
MinerStatusFrozen = 2 // Miner frozen by system, cannot participated any mining process
)
const (
MinerNormal NodeIdentity = 0 // this is normal miner node,cannot be stake by others and stake to others
MinerGuard = 1 // this miner is gurad miner node,cannot be stake by others
MinerPool = 2 // this is miner pool node,can stake by others
InValidMinerPool = 3 // this is invalid miner pool.only can be reduce before meeting the minimum number of votes
)
const (
pkSize = 128
vrfPkSize = 32
)
// Miner is the miner info including public keys and pledges
type Miner struct {
ID []byte
PublicKey []byte
VrfPublicKey []byte
ApplyHeight uint64
Stake uint64
StatusUpdateHeight uint64
Type MinerType
Status MinerStatus
Identity NodeIdentity
IdentityUpdateHeight uint64
}
func (m *Miner) IsActive() bool {
return m.Status == MinerStatusActive
}
func (m *Miner) IsFrozen() bool {
return m.Status == MinerStatusFrozen
}
func (m *Miner) IsPrepare() bool {
return m.Status == MinerStatusPrepare
}
func (m *Miner) IsGuard() bool {
return m.Identity == MinerGuard
}
func (m *Miner) IsNormal() bool {
return m.Identity == MinerNormal
}
func (m *Miner) IsMinerPool() bool {
return m.Identity == MinerPool
}
func (m *Miner) IsInvalidMinerPool() bool {
return m.Identity == InValidMinerPool
}
func (m *Miner) PksCompleted() bool {
return len(m.PublicKey) == pkSize && len(m.VrfPublicKey) == vrfPkSize
}
func (m *Miner) UpdateStatus(status MinerStatus, height uint64) {
m.StatusUpdateHeight = height
m.Status = status
}
func (m *Miner) UpdateIdentity(identity NodeIdentity, height uint64) {
m.IdentityUpdateHeight = height
m.Identity = identity
}
func (m *Miner) IsProposalRole() bool {
return IsProposalRole(m.Type)
}
func (m *Miner) IsVerifyRole() bool {
return IsVerifyRole(m.Type)
}
// StakeStatus indicates the stake status
type StakeStatus = byte
const (
Staked StakeStatus = iota // Normal status
StakeFrozen // Frozen status
StakePunishment // Punishment status, can't never refund
)
// StakeDetail expresses the stake detail
type StakeDetail struct {
Source common.Address
Target common.Address
Value uint64
Status StakeStatus
UpdateHeight uint64
MType MinerType
DisMissHeight uint64
}
type MinerPks struct {
MType MinerType
Pk []byte
VrfPk []byte
}
const PayloadVersion = 1
func (tx *Transaction) OpType() int8 {
return tx.Type
}
func (tx *Transaction) Operator() *common.Address {
return tx.Source
}
func (tx *Transaction) OpTarget() *common.Address {
return tx.Target
}
func (tx *Transaction) Amount() *big.Int {
if tx.Value == nil {
return nil
}
return tx.Value.Value()
}
func (tx *Transaction) Payload() []byte {
return tx.Data
}
// EncodePayload encodes pk and vrf pk into byte array storing in transaction data field
func EncodePayload(pks *MinerPks) ([]byte, error) {
buf := bytes.NewBuffer([]byte{})
buf.WriteByte(PayloadVersion)
buf.WriteByte(byte(pks.MType))
pkLen := len(pks.Pk)
vrfPkLen := len(pks.VrfPk)
if pkLen == pkSize && vrfPkLen == vrfPkSize {
buf.Write(pks.Pk)
buf.Write(pks.VrfPk)
}
return buf.Bytes(), nil
}
// DecodePayload decodes the data field of the miner related transaction
func DecodePayload(bs []byte) (*MinerPks, error) {
totalLen := 2 + pkSize + vrfPkSize
if len(bs) != 2 && len(bs) != totalLen {
return nil, fmt.Errorf("length error")
}
version := bs[0]
if version != PayloadVersion {
return nil, fmt.Errorf("version error %v", version)
}
pks := &MinerPks{
MType: MinerType(bs[1]),
}
if len(bs) == totalLen {
pks.Pk = bs[2 : 2+pkSize]
pks.VrfPk = bs[2+pkSize:]
}
return pks, nil
}
func IsProposalRole(typ MinerType) bool {
return typ == MinerTypeProposal
}
func IsVerifyRole(typ MinerType) bool {
return typ == MinerTypeVerify
}
// CastRewardShare represents for the block generation reward
type CastRewardShare struct {
ForBlockProposal uint64
ForBlockVerify uint64
ForRewardTxPacking uint64
FeeForProposer uint64
FeeForVerifier uint64
}
func (crs *CastRewardShare) TotalForVerifier() uint64 {
return crs.FeeForVerifier + crs.ForBlockVerify
}
type BlackOperator struct {
Addrs []common.Address
OpType byte
Signs [][]byte // sign of guard nodes
}
func EncodeBlackOperator(b *BlackOperator) ([]byte, error) {
bs, err := msgpack.Marshal(b)
if err != nil {
return nil, err
}
return bs, nil
}
func DecodeBlackOperator(bs []byte) (*BlackOperator, error) {
var b BlackOperator
err := msgpack.Unmarshal(bs, &b)
if err != nil {
return nil, err
}
return &b, nil
}
func GenBlackOperateSignData(operator common.Address, nonce uint64, opType byte, addrs []common.Address) []byte {
buff := new(bytes.Buffer)
buff.Write(operator.Bytes())
buff.Write(common.Uint64ToByte(nonce))
buff.WriteByte(opType)
for _, addr := range addrs {
buff.Write(addr.Bytes())
}
return common.Sha256(buff.Bytes())
}