forked from QOSGroup/qbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3dc7d98
commit 78dbb1a
Showing
4 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package validator | ||
|
||
const ( | ||
ValidatorMapperName = "_base_validator_" | ||
) | ||
|
||
var ( | ||
//EnableValidatorUpdatedKey 是否启用更新validator特性,默认关闭 | ||
EnableValidatorUpdatedKey = []byte("_enable_validator_updated_") | ||
|
||
//ValidatorUpdateSetKey 保存本块中validator变化结果 | ||
ValidatorUpdateSetKey = []byte("_validator_update_set_") | ||
|
||
//LastBlockProposerKey 上一块验证人 | ||
LastBlockProposerKey = []byte("_last_block_proposer_") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package validator | ||
|
||
import ( | ||
"github.com/QOSGroup/qbase/context" | ||
"github.com/QOSGroup/qbase/mapper" | ||
"github.com/QOSGroup/qbase/types" | ||
abci "github.com/tendermint/tendermint/abci/types" | ||
) | ||
|
||
type ValidatorMapper struct { | ||
*mapper.BaseMapper | ||
} | ||
|
||
func (mapper *ValidatorMapper) ClearValidatorUpdateSet() { | ||
mapper.Del(ValidatorUpdateSetKey) | ||
} | ||
|
||
func (mapper *ValidatorMapper) GetValidatorUpdateSet() (update []abci.ValidatorUpdate) { | ||
mapper.Get(ValidatorUpdateSetKey, &update) | ||
return | ||
} | ||
|
||
func (mapper *ValidatorMapper) SetValidatorUpdateSet(update []abci.ValidatorUpdate) { | ||
mapper.Set(ValidatorUpdateSetKey, update) | ||
} | ||
|
||
func (mapper *ValidatorMapper) SetLastBlockProposer(address types.Address) { | ||
mapper.Set(LastBlockProposerKey, address) | ||
} | ||
|
||
func (mapper *ValidatorMapper) GetLastBlockProposer() (address types.Address, 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package validator | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/QOSGroup/qbase/context" | ||
"github.com/QOSGroup/qbase/mapper" | ||
"github.com/QOSGroup/qbase/store" | ||
"github.com/QOSGroup/qbase/types" | ||
"github.com/stretchr/testify/require" | ||
go_amino "github.com/tendermint/go-amino" | ||
abci "github.com/tendermint/tendermint/abci/types" | ||
dbm "github.com/tendermint/tendermint/libs/db" | ||
"github.com/tendermint/tendermint/libs/log" | ||
) | ||
|
||
func defaultContext(key store.StoreKey, mapperMap map[string]mapper.IMapper) context.Context { | ||
db := dbm.NewMemDB() | ||
cms := store.NewCommitMultiStore(db) | ||
cms.MountStoreWithDB(key, store.StoreTypeIAVL, db) | ||
cms.LoadLatestVersion() | ||
ctx := context.NewContext(cms, abci.Header{}, false, log.NewNopLogger(), mapperMap) | ||
return ctx | ||
} | ||
|
||
func getValidatorMapper() *ValidatorMapper { | ||
cdc := go_amino.NewCodec() | ||
|
||
seedMapper := NewValidatorMapper() | ||
seedMapper.SetCodec(cdc) | ||
|
||
mapperMap := make(map[string]mapper.IMapper) | ||
mapperMap[seedMapper.MapperName()] = seedMapper | ||
|
||
ctx := defaultContext(seedMapper.GetStoreKey(), mapperMap) | ||
return GetValidatorMapper(ctx) | ||
} | ||
|
||
func TestValidatorMapper(t *testing.T) { | ||
|
||
valMapper := getValidatorMapper() | ||
b := valMapper.IsEnableValidatorUpdated() | ||
require.Equal(t, false, b) | ||
|
||
valMapper.EnableValidatorUpdated() | ||
|
||
b = valMapper.IsEnableValidatorUpdated() | ||
require.Equal(t, true, b) | ||
|
||
valMapper.DisableValidatorUpdated() | ||
|
||
b = valMapper.IsEnableValidatorUpdated() | ||
require.Equal(t, false, b) | ||
|
||
s := valMapper.GetValidatorUpdateSet() | ||
require.Equal(t, 0, len(s)) | ||
|
||
s = []abci.ValidatorUpdate{ | ||
{}, {}, | ||
} | ||
|
||
valMapper.SetValidatorUpdateSet(s) | ||
s = valMapper.GetValidatorUpdateSet() | ||
require.Equal(t, 2, len(s)) | ||
|
||
valMapper.ClearValidatorUpdateSet() | ||
|
||
s = valMapper.GetValidatorUpdateSet() | ||
require.Equal(t, 0, len(s)) | ||
|
||
addr, _ := valMapper.GetLastBlockProposer() | ||
require.Equal(t, true, addr.Empty()) | ||
|
||
addr = types.Address{12, 20, 32} | ||
valMapper.SetLastBlockProposer(addr) | ||
|
||
addr, _ = valMapper.GetLastBlockProposer() | ||
require.Equal(t, false, addr.Empty()) | ||
|
||
} |