/
validator_mapper.go
98 lines (76 loc) · 2.51 KB
/
validator_mapper.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
package validator
import (
"bytes"
"github.com/QOSGroup/qbase/context"
"github.com/QOSGroup/qbase/mapper"
"github.com/QOSGroup/qbase/types"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto"
tmtypes "github.com/tendermint/tendermint/types"
)
type ValidatorMapper struct {
*mapper.BaseMapper
}
func (mapper *ValidatorMapper) ClearValidatorUpdateSet() {
mapper.Del(ValidatorUpdateSetKey)
}
func (mapper *ValidatorMapper) GetValidatorUpdateSet() (allValidatorUpdate []abci.ValidatorUpdate) {
mapper.Get(ValidatorUpdateSetKey, &allValidatorUpdate)
return
}
func (mapper *ValidatorMapper) AddValidatorUpdate(pubkey crypto.PubKey, power uint64) error {
updateInfo := abci.ValidatorUpdate{
PubKey: tmtypes.TM2PB.PubKey(pubkey),
Power: int64(power),
}
allValidatorUpdate := mapper.GetValidatorUpdateSet()
isReplace := false
for i, vu := range allValidatorUpdate {
if bytes.Equal(updateInfo.PubKey.Data, vu.PubKey.Data) {
isReplace = true
allValidatorUpdate[i] = updateInfo
break
}
}
if !isReplace {
allValidatorUpdate = append(allValidatorUpdate, updateInfo)
}
mapper.SetValidatorUpdateSet(allValidatorUpdate)
return nil
}
func (mapper *ValidatorMapper) SetValidatorUpdateSet(allValidatorUpdate []abci.ValidatorUpdate) {
mapper.Set(ValidatorUpdateSetKey, allValidatorUpdate)
}
func (mapper *ValidatorMapper) SetLastBlockProposer(address types.ConsAddress) {
mapper.Set(LastBlockProposerKey, address)
}
func (mapper *ValidatorMapper) GetLastBlockProposer() (address types.ConsAddress, exsits bool) {
exsits = mapper.Get(LastBlockProposerKey, &address)
return
}
func (mapper *ValidatorMapper) IsEnableValidatorUpdated() bool {
if v, exsits := mapper.GetBool(EnableValidatorUpdatedKey); exsits {
return v
}
return false
}
func (mapper *ValidatorMapper) EnableValidatorUpdated() {
mapper.Set(EnableValidatorUpdatedKey, true)
}
func (mapper *ValidatorMapper) DisableValidatorUpdated() {
mapper.Set(EnableValidatorUpdatedKey, false)
}
var _ mapper.IMapper = (*ValidatorMapper)(nil)
func NewValidatorMapper() *ValidatorMapper {
var validatorMapper = ValidatorMapper{}
validatorMapper.BaseMapper = mapper.NewBaseMapper(nil, ValidatorMapperName)
return &validatorMapper
}
func GetValidatorMapper(ctx context.Context) *ValidatorMapper {
return ctx.Mapper(ValidatorMapperName).(*ValidatorMapper)
}
func (mapper *ValidatorMapper) Copy() mapper.IMapper {
validatorMapper := &ValidatorMapper{}
validatorMapper.BaseMapper = mapper.BaseMapper.Copy()
return validatorMapper
}