Commit 8a3a2058 authored by mdj33's avatar mdj33 Committed by vipwzw

add ut more

parent 36ce6853
......@@ -435,7 +435,7 @@ func (client *client) RequestTx(currSeq int64, preMainBlockHash []byte) ([]*type
return nil, nil, -1, err
}
//genesis block with seq=-1 not check
//genesis block start with seq=-1 not check
if currSeq == 0 ||
(bytes.Equal(preMainBlockHash, blockDetail.Block.ParentHash) && seqTy == addAct) ||
(bytes.Equal(preMainBlockHash, blockDetail.Block.Hash()) && seqTy == delAct) {
......@@ -473,6 +473,9 @@ func (client *client) RequestTx(currSeq int64, preMainBlockHash []byte) ([]*type
return nil, nil, -1, paracross.ErrParaCurHashNotMatch
}
//genesis block scenario, new main node's blockHash as preMainHash, genesis sequence+1 as currSeq
// for genesis seq=-1 scenario, mainHash not care, as the 0 seq instead of -1
// not seq=-1 scenario, mainHash needed
func (client *client) syncFromGenesisBlock() (int64, []byte, error) {
lastSeq, _, lastSeqMainHash, _, err := client.getLastBlockInfo()
if err != nil {
......@@ -484,13 +487,13 @@ func (client *client) syncFromGenesisBlock() (int64, []byte, error) {
}
// search base on para block but not last MainBlockHash, last MainBlockHash can not back tracing
func (client *client) switchHashMatchedBlock(currSeq int64, preMainBlockHash []byte) (int64, []byte, error) {
func (client *client) switchHashMatchedBlock(currSeq int64) (int64, []byte, error) {
lastBlock, err := client.RequestLastBlock()
if err != nil {
plog.Error("Parachain RequestLastBlock fail", "err", err)
return -2, nil, err
}
//genesis block scenario, get new main node's blockHash as preMainHash, genesis sequence as currSeq
if lastBlock.Height == 0 {
return client.syncFromGenesisBlock()
}
......@@ -535,7 +538,8 @@ func (client *client) switchHashMatchedBlock(currSeq int64, preMainBlockHash []b
return currSeq, nil, nil
}
plog.Info("switchHashMatchedBlock succ", "currHeight", height, "initHeight", lastBlock.Height, "set new currSeq", currSeq, "new preMainBlockHash", common.Bytes2Hex(preMainBlockHash))
plog.Info("switchHashMatchedBlock succ", "currHeight", height, "initHeight", lastBlock.Height,
"new currSeq", mainSeq+1, "new preMainBlockHash", common.Bytes2Hex(miner.MainBlockHash))
return mainSeq + 1, miner.MainBlockHash, nil
}
return -2, nil, paracross.ErrParaCurHashNotMatch
......@@ -592,7 +596,7 @@ func (client *client) CreateBlock() {
txs, blockOnMain, seqTy, err := client.RequestTx(currSeq, lastSeqMainHash)
if err != nil {
if err == paracross.ErrParaCurHashNotMatch {
newSeq, newSeqMainHash, err := client.switchHashMatchedBlock(currSeq, lastSeqMainHash)
newSeq, newSeqMainHash, err := client.switchHashMatchedBlock(currSeq)
if err == nil {
currSeq = newSeq
lastSeqMainHash = newSeqMainHash
......
......@@ -115,6 +115,52 @@ func (s *suiteParaClient) initEnv(cfg *types.Config, sub *types.ConfigSubModule)
}
func (s *suiteParaClient) TestRun_Test() {
//s.testGetBlock()
lastBlock, err := s.para.RequestLastBlock()
if err != nil {
plog.Error("para test", "err", err.Error())
}
plog.Info("para test---------1", "last height", lastBlock.Height)
s.para.createBlock(lastBlock, nil, 0, getMainBlock(2, lastBlock.BlockTime+1))
lastBlock, err = s.para.RequestLastBlock()
if err != nil {
plog.Error("para test--2", "err", err.Error())
}
plog.Info("para test---------", "last height", lastBlock.Height)
s.para.createBlock(lastBlock, nil, 1, getMainBlock(3, lastBlock.BlockTime+1))
time.Sleep(time.Second * 1)
s.testRunGetMinerTxInfo()
s.testRunRmvBlock()
}
func (s *suiteParaClient) testRunGetMinerTxInfo() {
lastBlock, err := s.para.RequestLastBlock()
s.Nil(err)
plog.Info("para test testRunGetMinerTxInfo", "last height", lastBlock.Height)
s.True(lastBlock.Height > 1)
status, err := getMinerTxInfo(lastBlock)
s.Nil(err)
s.Equal(int64(3), status.MainBlockHeight)
}
func (s *suiteParaClient) testRunRmvBlock() {
lastBlock, err := s.para.RequestLastBlock()
s.Nil(err)
plog.Info("para test testRunGetMinerTxInfo", "last height", lastBlock.Height)
s.True(lastBlock.Height > 1)
s.para.removeBlocks(1)
lastBlock, err = s.para.RequestLastBlock()
s.Nil(err)
plog.Info("para test testRunGetMinerTxInfo", "last height", lastBlock.Height)
s.Equal(int64(1), lastBlock.Height)
}
func (s *suiteParaClient) SetupSuite() {
s.initEnv(types.InitCfg("../../../plugin/dapp/paracross/cmd/build/chain33.para.test.toml"))
}
......@@ -136,31 +182,4 @@ func (s *suiteParaClient) TearDownSuite() {
}
//func newMockParaNode() *testnode.Chain33Mock {
// //_, sub := testnode.GetDefaultConfig()
// //cfg.Consensus.Minerstart = false
// cfg, sub := types.InitCfg("../../../plugin/dapp/paracross/cmd/build/chain33.para.test.toml")
// cfg.Consensus.StartHeight=0
// mock33 := testnode.NewWithConfig(cfg, sub, nil)
// return mock33
//}
//
//func TestSwitchHashMatchedBlock(t *testing.T) {
// mockPara := newMockParaNode()
// defer mockPara.Close()
// mockPara.WaitHeight(0)
// block := mockPara.GetBlock(0)
// assert.Equal(t, block.Height, int64(0))
//
// //consens:=mockPara.GetCfg().Consensus
// //
// //paraCli := New(mockPara.GetCfg().Consensus,nil).(*client)
// ////paraCli.BaseClient.SetQueueClient(mock33.GetClient())
// //paraCli.SetQueueClient(mockPara.GetClient())
// var currSeq int64
// var preMainBlockHash []byte
// currSeq=2
// cs := mockPara.GetConsensClient().(*client)
// cs.switchHashMatchedBlock(&currSeq,&preMainBlockHash)
// assert.Equal(t,1,currSeq)
//}
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