-
Notifications
You must be signed in to change notification settings - Fork 0
/
operator_blockproduce.go
208 lines (175 loc) · 5.35 KB
/
operator_blockproduce.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
// Copyright (c) 2018 The MATRIX Authors
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php
package matrixstate
import (
"github.com/MatrixAINetwork/go-matrix/common"
"github.com/MatrixAINetwork/go-matrix/core/types"
"github.com/MatrixAINetwork/go-matrix/log"
"github.com/MatrixAINetwork/go-matrix/mc"
"github.com/MatrixAINetwork/go-matrix/rlp"
)
/////////////////////////////////////////////////////////////////////////////////////////
//
type operatorBlockProduceStatsStatus struct {
key common.Hash
}
func newBlockProduceStatsStatusOpt() *operatorBlockProduceStatsStatus {
return &operatorBlockProduceStatsStatus{
key: types.RlpHash(matrixStatePrefix + mc.MSKeyBlockProduceStatsStatus),
}
}
func (opt *operatorBlockProduceStatsStatus) KeyHash() common.Hash {
return opt.key
}
func (opt *operatorBlockProduceStatsStatus) GetValue(st StateDB) (interface{}, error) {
if err := checkStateDB(st); err != nil {
return nil, err
}
data := st.GetMatrixData(opt.key)
if len(data) == 0 {
return &mc.BlockProduceSlashStatsStatus{Number: 0}, nil
}
value := new(mc.BlockProduceSlashStatsStatus)
err := rlp.DecodeBytes(data, &value)
if err != nil {
log.Error(logInfo, "blockProduceStatsStatus rlp decode failed", err)
return nil, err
}
return value, nil
}
func (opt *operatorBlockProduceStatsStatus) SetValue(st StateDB, value interface{}) error {
if err := checkStateDB(st); err != nil {
return err
}
data, err := rlp.EncodeToBytes(value)
if err != nil {
log.Error(logInfo, "blockProduceStatsStatus rlp encode failed", err)
return err
}
st.SetMatrixData(opt.key, data)
return nil
}
/////////////////////////////////////////////////////////////////////////////////////////
//
type operatorBlockProduceSlashCfg struct {
key common.Hash
}
func newBlockProduceSlashCfgOpt() *operatorBlockProduceSlashCfg {
return &operatorBlockProduceSlashCfg{
key: types.RlpHash(matrixStatePrefix + mc.MSKeyBlockProduceSlashCfg),
}
}
func (opt *operatorBlockProduceSlashCfg) KeyHash() common.Hash {
return opt.key
}
func (opt *operatorBlockProduceSlashCfg) GetValue(st StateDB) (interface{}, error) {
if err := checkStateDB(st); err != nil {
return nil, err
}
data := st.GetMatrixData(opt.key)
if len(data) == 0 {
return &mc.BlockProduceSlashCfg{Switcher: false, LowTHR: 1, ProhibitCycleNum: 2}, nil
}
value := new(mc.BlockProduceSlashCfg)
err := rlp.DecodeBytes(data, &value)
if err != nil {
log.Error(logInfo, "blockProduceSlashCfg rlp decode failed", err)
return nil, err
}
return value, nil
}
func (opt *operatorBlockProduceSlashCfg) SetValue(st StateDB, value interface{}) error {
if err := checkStateDB(st); err != nil {
return err
}
data, err := rlp.EncodeToBytes(value)
if err != nil {
log.Error(logInfo, "blockProduceSlashCfg rlp encode failed", err)
return err
}
st.SetMatrixData(opt.key, data)
return nil
}
/////////////////////////////////////////////////////////////////////////////////////////
//
type operatorBlockProduceStats struct {
key common.Hash
}
func newBlockProduceStatsOpt() *operatorBlockProduceStats {
return &operatorBlockProduceStats{
key: types.RlpHash(matrixStatePrefix + mc.MSKeyBlockProduceStats),
}
}
func (opt *operatorBlockProduceStats) KeyHash() common.Hash {
return opt.key
}
func (opt *operatorBlockProduceStats) GetValue(st StateDB) (interface{}, error) {
if err := checkStateDB(st); err != nil {
return nil, err
}
data := st.GetMatrixData(opt.key)
if len(data) == 0 {
return &mc.BlockProduceStats{StatsList: make([]mc.UserBlockProduceNum, 0)}, nil
}
value := new(mc.BlockProduceStats)
err := rlp.DecodeBytes(data, &value)
if err != nil {
log.Error(logInfo, "blockProduceStats rlp decode failed", err)
return nil, err
}
return value, nil
}
func (opt *operatorBlockProduceStats) SetValue(st StateDB, value interface{}) error {
if err := checkStateDB(st); err != nil {
return err
}
data, err := rlp.EncodeToBytes(value)
if err != nil {
log.Error(logInfo, "blockProduceStats rlp encode failed", err)
return err
}
st.SetMatrixData(opt.key, data)
return nil
}
/////////////////////////////////////////////////////////////////////////////////////////
//
type operatorBlockProduceBlackList struct {
key common.Hash
}
func newBlockProduceBlackListOpt() *operatorBlockProduceBlackList {
return &operatorBlockProduceBlackList{
key: types.RlpHash(matrixStatePrefix + mc.MSKeyBlockProduceBlackList),
}
}
func (opt *operatorBlockProduceBlackList) KeyHash() common.Hash {
return opt.key
}
func (opt *operatorBlockProduceBlackList) GetValue(st StateDB) (interface{}, error) {
if err := checkStateDB(st); err != nil {
return nil, err
}
data := st.GetMatrixData(opt.key)
if len(data) == 0 {
return &mc.BlockProduceSlashBlackList{BlackList: make([]mc.UserBlockProduceSlash, 0)}, nil
}
value := new(mc.BlockProduceSlashBlackList)
err := rlp.DecodeBytes(data, &value)
if err != nil {
log.Error(logInfo, "blockProduceBlackList rlp decode failed", err)
return nil, err
}
return value, nil
}
func (opt *operatorBlockProduceBlackList) SetValue(st StateDB, value interface{}) error {
if err := checkStateDB(st); err != nil {
return err
}
data, err := rlp.EncodeToBytes(value)
if err != nil {
log.Error(logInfo, "blockProduceBlackList rlp encode failed", err)
return err
}
st.SetMatrixData(opt.key, data)
return nil
}