Commit eaf1c68c authored by mdj33's avatar mdj33 Committed by vipwzw

fix ut

parent 822d8a13
...@@ -284,38 +284,3 @@ func TestCheckEmptyInterval(t *testing.T) { ...@@ -284,38 +284,3 @@ func TestCheckEmptyInterval(t *testing.T) {
assert.Equal(t, nil, err) assert.Equal(t, nil, err)
} }
//func TestCheckSelfConsensEnable(t *testing.T) {
// int1 := &paraSelfConsEnable{BlockHeight: 0, Enable: false}
// int2 := &paraSelfConsEnable{BlockHeight: 10, Enable: true}
// int3 := &paraSelfConsEnable{BlockHeight: 5, Enable: false}
//
// ints := []*paraSelfConsEnable{int1, int2, int3}
// err := checkSelfConsensEnable(ints)
// assert.Equal(t, types.ErrInvalidParam, err)
//
// int3.BlockHeight = 15
// err = checkSelfConsensEnable(ints)
// assert.Nil(t, err)
//}
//func TestGetSelfConsEnableStatus(t *testing.T) {
// int1 := &paraSelfConsEnable{BlockHeight: 0, Enable: false}
// int2 := &paraSelfConsEnable{BlockHeight: 10, Enable: true}
// int3 := &paraSelfConsEnable{BlockHeight: 15, Enable: false}
//
// para := new(client)
// para.subCfg = new(subConfig)
// para.subCfg.SelfConsensusEnable = []*paraSelfConsEnable{int1, int2, int3}
// selfConf := para.getSelfConsEnableStatus(5)
// assert.Equal(t, int64(0), selfConf.BlockHeight)
// assert.Equal(t, false, selfConf.Enable)
//
// selfConf = para.getSelfConsEnableStatus(10)
// assert.Equal(t, int64(10), selfConf.BlockHeight)
// assert.Equal(t, true, selfConf.Enable)
//
// selfConf = para.getSelfConsEnableStatus(16)
// assert.Equal(t, int64(15), selfConf.BlockHeight)
// assert.Equal(t, false, selfConf.Enable)
//}
...@@ -751,7 +751,6 @@ func (client *commitMsgClient) getSelfConsensusStatus() (*pt.ParacrossStatus, er ...@@ -751,7 +751,6 @@ func (client *commitMsgClient) getSelfConsensusStatus() (*pt.ParacrossStatus, er
plog.Error("getSelfConsensusStatus nok") plog.Error("getSelfConsensusStatus nok")
return nil, types.ErrInvalidParam return nil, types.ErrInvalidParam
} }
plog.Info("getSelfConsensusStatus ", "height", block.Height, "stageHeight", stage.BlockHeight, "enable", stage.Enable)
if stage.Enable == pt.ParaConfigYes { if stage.Enable == pt.ParaConfigYes {
//从本地查询共识高度 //从本地查询共识高度
ret, err := client.paraClient.GetAPI().QueryChain(&types.ChainExecutor{ ret, err := client.paraClient.GetAPI().QueryChain(&types.ChainExecutor{
......
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package para
import (
"testing"
_ "github.com/33cn/chain33/system"
"github.com/stretchr/testify/assert"
)
func TestIsSelfConsEnable(t *testing.T) {
commitCli := new(commitMsgClient)
enable := commitCli.isSelfConsEnable(0)
assert.Equal(t, false, enable)
s1 := &paraSelfConsEnable{startHeight: 10, endHeight: 20}
s2 := &paraSelfConsEnable{startHeight: 30, endHeight: 40}
commitCli.selfConsEnableList = append(commitCli.selfConsEnableList, s1)
commitCli.selfConsEnableList = append(commitCli.selfConsEnableList, s2)
enable = commitCli.isSelfConsEnable(10)
assert.Equal(t, true, enable)
enable = commitCli.isSelfConsEnable(21)
assert.Equal(t, false, enable)
enable = commitCli.isSelfConsEnable(30)
assert.Equal(t, true, enable)
}
...@@ -77,6 +77,9 @@ func createParaTestInstance(t *testing.T, q queue.Queue) *client { ...@@ -77,6 +77,9 @@ func createParaTestInstance(t *testing.T, q queue.Queue) *client {
maxSyncErrCount: 100, maxSyncErrCount: 100,
} }
para.commitMsgClient = &commitMsgClient{
paraClient: para,
}
return para return para
} }
......
...@@ -82,45 +82,45 @@ func TestCalcCommitMsgTxs(t *testing.T) { ...@@ -82,45 +82,45 @@ func TestCalcCommitMsgTxs(t *testing.T) {
} }
func TestGetConsensusStatus(t *testing.T) { //func TestGetConsensusStatus(t *testing.T) {
chain33Cfg := types.NewChain33Config(testnode.DefaultConfig) // chain33Cfg := types.NewChain33Config(testnode.DefaultConfig)
//
api := new(apimocks.QueueProtocolAPI) // api := new(apimocks.QueueProtocolAPI)
api.On("GetConfig", mock.Anything).Return(chain33Cfg, nil) // api.On("GetConfig", mock.Anything).Return(chain33Cfg, nil)
para := &client{BaseClient: &drivers.BaseClient{}} // para := &client{BaseClient: &drivers.BaseClient{}}
//
para.subCfg = new(subConfig) // para.subCfg = new(subConfig)
grpcClient := &typesmocks.Chain33Client{} // grpcClient := &typesmocks.Chain33Client{}
//grpcClient.On("GetFork", mock.Anything, &types.ReqKey{Key: []byte("ForkBlockHash")}).Return(&types.Int64{Data: 1}, errors.New("err")).Once() // //grpcClient.On("GetFork", mock.Anything, &types.ReqKey{Key: []byte("ForkBlockHash")}).Return(&types.Int64{Data: 1}, errors.New("err")).Once()
para.grpcClient = grpcClient // para.grpcClient = grpcClient
//
client := &commitMsgClient{ // client := &commitMsgClient{
paraClient: para, // paraClient: para,
} // }
para.commitMsgClient = client // para.commitMsgClient = client
//
block := &types.Block{ // block := &types.Block{
Height: 1, // Height: 1,
MainHeight: 10, // MainHeight: 10,
} // }
getMockLastBlock(para, block) // getMockLastBlock(para, block)
//
status := &pt.ParacrossStatus{ // status := &pt.ParacrossStatus{
Height: 1, // Height: 1,
} // }
//
api.On("QueryChain", mock.Anything, mock.Anything, mock.Anything).Return(status, nil).Once() // api.On("QueryChain", mock.Anything, mock.Anything, mock.Anything).Return(status, nil).Once()
detail := &types.BlockDetail{Block: block} // detail := &types.BlockDetail{Block: block}
details := &types.BlockDetails{Items: []*types.BlockDetail{detail}} // details := &types.BlockDetails{Items: []*types.BlockDetail{detail}}
//
api.On("GetBlocks", mock.Anything).Return(details, nil).Once() // api.On("GetBlocks", mock.Anything).Return(details, nil).Once()
//
para.SetAPI(api) // para.SetAPI(api)
ret, err := client.getSelfConsensusStatus() // ret, err := client.getSelfConsensusStatus()
//
assert.Nil(t, err) // assert.Nil(t, err)
assert.Equal(t, int64(1), ret.Height) // assert.Equal(t, int64(1), ret.Height)
} //}
func TestSendCommitMsg(t *testing.T) { func TestSendCommitMsg(t *testing.T) {
cfg := types.NewChain33Config(testnode.DefaultConfig) cfg := types.NewChain33Config(testnode.DefaultConfig)
......
...@@ -220,7 +220,7 @@ function paracross_ListTitles() { ...@@ -220,7 +220,7 @@ function paracross_ListTitles() {
local ok local ok
local main_ip=${UNIT_HTTP//8901/8801} local main_ip=${UNIT_HTTP//8901/8801}
resp=$(curl -ksd '{"method":"paracross.ListTitles","params":[]}' ${main_ip}) resp=$(curl -ksd '{"method":"paracross.ListTitles","params":[]}' ${main_ip})
echo $resp echo "$resp"
ok=$(jq '(.error|not) and (.result| [has("titles"),true])' <<<"$resp") ok=$(jq '(.error|not) and (.result| [has("titles"),true])' <<<"$resp")
[ "$ok" == true ] [ "$ok" == true ]
local rst=$? local rst=$?
......
...@@ -126,6 +126,17 @@ func (suite *CommitTestSuite) SetupSuite() { ...@@ -126,6 +126,17 @@ func (suite *CommitTestSuite) SetupSuite() {
} }
assert.Equal(suite.T(), value, types.Encode(nodeValue)) assert.Equal(suite.T(), value, types.Encode(nodeValue))
stageKey := calcParaSelfConsStagesKey()
stage := &pt.SelfConsensStage{BlockHeight: 0, Enable: pt.ParaConfigYes}
stages := &pt.SelfConsensStages{Items: []*pt.SelfConsensStage{stage}}
suite.stateDB.Set(stageKey, types.Encode(stages))
value, err = suite.stateDB.Get(stageKey)
if err != nil {
suite.T().Error("get setup stages failed", err)
return
}
assert.Equal(suite.T(), value, types.Encode(stages))
// setup state title 'test' height is 9 // setup state title 'test' height is 9
var titleStatus pt.ParacrossStatus var titleStatus pt.ParacrossStatus
titleStatus.Title = Title titleStatus.Title = Title
...@@ -452,15 +463,39 @@ func (s *VoteTestSuite) TestFilterTxsForPara() { ...@@ -452,15 +463,39 @@ func (s *VoteTestSuite) TestFilterTxsForPara() {
type VoteTestSuite struct { type VoteTestSuite struct {
suite.Suite suite.Suite
stateDB dbm.KV
localDB *dbmock.KVDB
exec *Paracross exec *Paracross
} }
func (s *VoteTestSuite) SetupSuite() { func (suite *VoteTestSuite) SetupSuite() {
//para_init(Title) //para_init(Title)
s.exec = newParacross().(*Paracross) suite.exec = newParacross().(*Paracross)
api := new(apimock.QueueProtocolAPI) api := new(apimock.QueueProtocolAPI)
api.On("GetConfig", mock.Anything).Return(chain33TestCfg, nil) api.On("GetConfig", mock.Anything).Return(chain33TestCfg, nil)
s.exec.SetAPI(api) suite.exec.SetAPI(api)
suite.stateDB, _ = dbm.NewGoMemDB("state", "state", 1024)
// memdb 不支持KVDB接口, 等测试完Exec , 再扩展 memdb
//suite.localDB, _ = dbm.NewGoMemDB("local", "local", 1024)
suite.localDB = new(dbmock.KVDB)
suite.exec.SetLocalDB(suite.localDB)
suite.exec.SetStateDB(suite.stateDB)
suite.exec.SetEnv(0, 0, 0)
stageKey := calcParaSelfConsStagesKey()
stage := &pt.SelfConsensStage{BlockHeight: 0, Enable: pt.ParaConfigYes}
stages := &pt.SelfConsensStages{Items: []*pt.SelfConsensStage{stage}}
suite.stateDB.Set(stageKey, types.Encode(stages))
value, err := suite.stateDB.Get(stageKey)
if err != nil {
suite.T().Error("get setup stages failed", err)
return
}
assert.Equal(suite.T(), value, types.Encode(stages))
} }
func (s *VoteTestSuite) TestVoteTx() { func (s *VoteTestSuite) TestVoteTx() {
......
...@@ -285,7 +285,7 @@ func (a *action) stageVote(config *pt.ConfigVoteInfo) (*types.Receipt, error) { ...@@ -285,7 +285,7 @@ func (a *action) stageVote(config *pt.ConfigVoteInfo) (*types.Receipt, error) {
} }
//剔除已退出nodegroup的addr的投票 //剔除已退出nodegroup的addr的投票
updateVotes(stat.Votes, nodes) stat.Votes = updateVotes(stat.Votes, nodes)
most, vote := getMostVote(stat.Votes) most, vote := getMostVote(stat.Votes)
if !isCommitDone(nodes, most) { if !isCommitDone(nodes, most) {
......
...@@ -432,15 +432,15 @@ func (a *action) superManagerVoteProc(title string) error { ...@@ -432,15 +432,15 @@ func (a *action) superManagerVoteProc(title string) error {
return nil return nil
} }
func updateVotes(stat *pt.ParaNodeVoteDetail, nodes map[string]struct{}) { func updateVotes(in *pt.ParaNodeVoteDetail, nodes map[string]struct{}) *pt.ParaNodeVoteDetail {
votes := &pt.ParaNodeVoteDetail{} votes := &pt.ParaNodeVoteDetail{}
for i, addr := range stat.Addrs { for i, addr := range in.Addrs {
if _, ok := nodes[addr]; ok { if _, ok := nodes[addr]; ok {
votes.Addrs = append(votes.Addrs, addr) votes.Addrs = append(votes.Addrs, addr)
votes.Votes = append(votes.Votes, stat.Votes[i]) votes.Votes = append(votes.Votes, in.Votes[i])
} }
} }
stat = votes return votes
} }
//由于propasal id 和quit id分开,quit id不知道对应addr proposal id的coinfrozen信息,需要维护一个围绕addr的数据库结构信息 //由于propasal id 和quit id分开,quit id不知道对应addr proposal id的coinfrozen信息,需要维护一个围绕addr的数据库结构信息
...@@ -539,7 +539,7 @@ func (a *action) nodeVote(config *pt.ParaNodeAddrConfig) (*types.Receipt, error) ...@@ -539,7 +539,7 @@ func (a *action) nodeVote(config *pt.ParaNodeAddrConfig) (*types.Receipt, error)
} }
//剔除已退出nodegroup的addr的投票 //剔除已退出nodegroup的addr的投票
updateVotes(stat.Votes, nodes) stat.Votes = updateVotes(stat.Votes, nodes)
most, vote := getMostVote(stat.Votes) most, vote := getMostVote(stat.Votes)
if !isCommitDone(nodes, most) { if !isCommitDone(nodes, most) {
......
...@@ -354,7 +354,7 @@ func TestUpdateVotes(t *testing.T) { ...@@ -354,7 +354,7 @@ func TestUpdateVotes(t *testing.T) {
nodes["BB"] = struct{}{} nodes["BB"] = struct{}{}
nodes["CC"] = struct{}{} nodes["CC"] = struct{}{}
updateVotes(stat.Votes, nodes) stat.Votes = updateVotes(stat.Votes, nodes)
assert.Equal(t, []string{"BB", "CC"}, stat.Votes.Addrs) assert.Equal(t, []string{"BB", "CC"}, stat.Votes.Addrs)
assert.Equal(t, []string{"no", "no"}, stat.Votes.Votes) assert.Equal(t, []string{"no", "no"}, stat.Votes.Votes)
} }
......
...@@ -123,18 +123,11 @@ genesisAmount=100000000 ...@@ -123,18 +123,11 @@ genesisAmount=100000000
mainBlockHashForkHeight=1 mainBlockHashForkHeight=1
mainForkParacrossCommitTx=1 mainForkParacrossCommitTx=1
mainLoopCheckCommitTxDoneForkHeight=11 mainLoopCheckCommitTxDoneForkHeight=11
selfConsensEnablePreContract=["0-1000"]
[[consensus.sub.para.emptyBlockInterval]] [[consensus.sub.para.emptyBlockInterval]]
blockHeight=0 blockHeight=0
interval=2 interval=2
[[consensus.sub.para.selfConsensusEnable]]
blockHeight=0
enable=true
[[consensus.sub.para.selfConsensusEnable]]
blockHeight=20
enable=false
[[consensus.sub.para.selfConsensusEnable]]
blockHeight=40
enable=true
[store] [store]
name="mavl" name="mavl"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment