Commit 57900caa authored by mdj33's avatar mdj33 Committed by vipwzw

modify some process

parent c37a551b
...@@ -84,9 +84,9 @@ func New(cfg *types.Consensus, sub []byte) queue.Module { ...@@ -84,9 +84,9 @@ func New(cfg *types.Consensus, sub []byte) queue.Module {
emptyBlockInterval = cfg.EmptyBlockInterval emptyBlockInterval = cfg.EmptyBlockInterval
} }
if cfg.searchHashMatchBlockDepth > 0 { //if cfg.searchHashMatchBlockDepth > 0 {
searchHashMatchBlockDepth = cfg.searchHashMatchBlockDepth // searchHashMatchBlockDepth = cfg.searchHashMatchBlockDepth
} //}
pk, err := hex.DecodeString(minerPrivateKey) pk, err := hex.DecodeString(minerPrivateKey)
if err != nil { if err != nil {
...@@ -384,15 +384,6 @@ func (client *client) GetSeqByHashOnMainChain(hash []byte) (int64, error) { ...@@ -384,15 +384,6 @@ func (client *client) GetSeqByHashOnMainChain(hash []byte) (int64, error) {
return seq.Data, nil return seq.Data, nil
} }
func (client *client) GetHashByHeightFromMainChain(height int64) ([]byte, error) {
req := &types.ReqInt{Height: height}
hash, err := client.grpcClient.GetBlockHash(context.Background(), req)
if err != nil {
plog.Error("GetBlocksByHashesFromMainChain", "Error", err.Error())
return nil, err
}
return hash, nil
}
func (client *client) GetBlocksByHashesFromMainChain(hashes [][]byte) (*types.BlockDetails, error) { func (client *client) GetBlocksByHashesFromMainChain(hashes [][]byte) (*types.BlockDetails, error) {
req := &types.ReqHashes{Hashes: hashes} req := &types.ReqHashes{Hashes: hashes}
...@@ -440,7 +431,7 @@ func (client *client) GetBlockOnMainBySeq(seq int64) (*types.BlockDetail, int64, ...@@ -440,7 +431,7 @@ func (client *client) GetBlockOnMainBySeq(seq int64) (*types.BlockDetail, int64,
} }
// preBlockHash to identify the same main node // preBlockHash to identify the same main node
func (client *client) RequestTx(seq *int64, lastBlock *types.Block, preMainBlockHash *[]byte) ([]*types.Transaction, *types.Block, int64, error) { func (client *client) RequestTx(seq *int64, preMainBlockHash *[]byte) ([]*types.Transaction, *types.Block, int64, error) {
plog.Debug("Para consensus RequestTx") plog.Debug("Para consensus RequestTx")
currSeq := *seq currSeq := *seq
lastSeq, err := client.GetLastSeqOnMainChain() lastSeq, err := client.GetLastSeqOnMainChain()
...@@ -453,8 +444,8 @@ func (client *client) RequestTx(seq *int64, lastBlock *types.Block, preMainBlock ...@@ -453,8 +444,8 @@ func (client *client) RequestTx(seq *int64, lastBlock *types.Block, preMainBlock
if err != nil { if err != nil {
return nil, nil, -1, err return nil, nil, -1, err
} }
//genesis block not check //genesis block with seq=-1 not check
if lastBlock.Height == 0 || if currSeq == 0 ||
(bytes.Equal(*preMainBlockHash, blockDetail.Block.ParentHash) && seqTy == addAct) || (bytes.Equal(*preMainBlockHash, blockDetail.Block.ParentHash) && seqTy == addAct) ||
(bytes.Equal(*preMainBlockHash, blockDetail.Block.Hash()) && seqTy == delAct) { (bytes.Equal(*preMainBlockHash, blockDetail.Block.Hash()) && seqTy == delAct) {
...@@ -486,14 +477,18 @@ func (client *client) RequestTx(seq *int64, lastBlock *types.Block, preMainBlock ...@@ -486,14 +477,18 @@ func (client *client) RequestTx(seq *int64, lastBlock *types.Block, preMainBlock
// 1. lastSeq < currSeq-1 // 1. lastSeq < currSeq-1
// 2. lastSeq >= currSeq and seq not consistent or fork case // 2. lastSeq >= currSeq and seq not consistent or fork case
// to search base on para block but not preMainBlock, preMainBlock can not back tracking
// whether found matched block or not, return err to re-requestTx // whether found matched block or not, return err to re-requestTx
client.findHashMatchedBlock(seq, lastBlock, preMainBlockHash) client.findHashMatchedBlock(seq, preMainBlockHash)
return nil, nil, -1, errors.New("hash not matched") return nil, nil, -1, errors.New("hash not matched")
} }
// // search base on para block but not last MainBlockHash, last MainBlockHash can not back tracing
func (client *client) findHashMatchedBlock(currSeq *int64, lastBlock *types.Block, preMainBlockHash *[]byte) { func (client *client) findHashMatchedBlock(currSeq *int64, preMainBlockHash *[]byte) {
lastBlock, err := client.RequestLastBlock()
if err != nil {
plog.Error("Parachain RequestLastBlock fail", "err", err)
return
}
findDepth := searchHashMatchBlockDepth findDepth := searchHashMatchBlockDepth
for height := lastBlock.Height; height > 0 && findDepth > 0; height-- { for height := lastBlock.Height; height > 0 && findDepth > 0; height-- {
block, err := client.GetBlockByHeight(height) block, err := client.GetBlockByHeight(height)
...@@ -515,15 +510,11 @@ func (client *client) findHashMatchedBlock(currSeq *int64, lastBlock *types.Bloc ...@@ -515,15 +510,11 @@ func (client *client) findHashMatchedBlock(currSeq *int64, lastBlock *types.Bloc
continue continue
} }
//try 100 times for remove fail case, if >100, panic to restart system to keep lastblock and sequence unify //remove fail, set the preMainBlockHash to nil, to match nothing, force to search again
for i := 0; i < 100; i++ { err = client.removeBlocks(height)
err = client.removeBlocks(height) if err != nil {
if err == nil { *preMainBlockHash = nil
break return
}
if i >= 100 {
panic("findHashMatchedBlock del blocks fail, restart and retry")
}
} }
*currSeq = mainSeq + 1 *currSeq = mainSeq + 1
...@@ -532,16 +523,44 @@ func (client *client) findHashMatchedBlock(currSeq *int64, lastBlock *types.Bloc ...@@ -532,16 +523,44 @@ func (client *client) findHashMatchedBlock(currSeq *int64, lastBlock *types.Bloc
} }
} }
func (client *client) removeBlocks(endHeight int64) error {
for {
lastBlock, err := client.RequestLastBlock()
if err != nil {
plog.Error("Parachain RequestLastBlock fail", "err", err)
return err
}
if lastBlock.Height == endHeight {
return nil
}
blockedSeq, err := client.GetBlockedSeq(lastBlock.Hash())
if err != nil {
plog.Error("Parachain GetBlockedSeq fail", "err", err)
return err
}
err = client.DelBlock(lastBlock, blockedSeq)
if err != nil {
plog.Error("Parachain GetBlockedSeq fail", "err", err)
return err
}
}
}
//正常情况下,打包交易 //正常情况下,打包交易
func (client *client) CreateBlock() { func (client *client) CreateBlock() {
incSeqFlag := true incSeqFlag := true
//system startup, take the last added block's seq is ok
currSeq, _, lastSeqMainHash, _, err := client.getLastBlockInfo() currSeq, _, lastSeqMainHash, _, err := client.getLastBlockInfo()
if err != nil { if err != nil {
plog.Error("Parachain GetLastSeq fail", "err", err) plog.Error("Parachain GetLastSeq fail", "err", err)
return return
} }
for { for {
lastSeq, lastBlock, _, lastBlockMainHeight, err := client.getLastBlockInfo() //should be lastSeq but not LastBlockSeq as del block case the seq is not equal
lastSeq, err := client.GetLastSeq()
if err != nil { if err != nil {
plog.Error("Parachain GetLastSeq fail", "err", err) plog.Error("Parachain GetLastSeq fail", "err", err)
time.Sleep(time.Second) time.Sleep(time.Second)
...@@ -552,7 +571,7 @@ func (client *client) CreateBlock() { ...@@ -552,7 +571,7 @@ func (client *client) CreateBlock() {
currSeq++ currSeq++
} }
txs, blockOnMain, seqTy, err := client.RequestTx(&currSeq, lastBlock, &lastSeqMainHash) txs, blockOnMain, seqTy, err := client.RequestTx(&currSeq, &lastSeqMainHash)
if err != nil { if err != nil {
incSeqFlag = false incSeqFlag = false
time.Sleep(time.Second) time.Sleep(time.Second)
...@@ -560,6 +579,14 @@ func (client *client) CreateBlock() { ...@@ -560,6 +579,14 @@ func (client *client) CreateBlock() {
} }
lastSeqMainHash = blockOnMain.Hash() lastSeqMainHash = blockOnMain.Hash()
lastSeqMainHeight := blockOnMain.Height lastSeqMainHeight := blockOnMain.Height
_, lastBlock, _, lastBlockMainHeight, err := client.getLastBlockInfo()
if err != nil {
plog.Error("Parachain GetLastSeq fail", "err", err)
time.Sleep(time.Second)
continue
}
plog.Info("Parachain process block", "lastSeq", lastSeq, "curSeq", currSeq, "lastSeqMainHeight", lastSeqMainHeight, "lastBlockMainHeight", lastBlockMainHeight) plog.Info("Parachain process block", "lastSeq", lastSeq, "curSeq", currSeq, "lastSeqMainHeight", lastSeqMainHeight, "lastBlockMainHeight", lastBlockMainHeight)
if seqTy == delAct { if seqTy == delAct {
...@@ -670,29 +697,6 @@ func (client *client) WriteBlock(prev []byte, paraBlock *types.Block, seq int64) ...@@ -670,29 +697,6 @@ func (client *client) WriteBlock(prev []byte, paraBlock *types.Block, seq int64)
return nil return nil
} }
func (client *client) removeBlocks(endHeight int64) error {
for {
lastBlock, err := client.RequestLastBlock()
if err != nil {
plog.Error("Parachain RequestLastBlock fail", "err", err)
return err
}
if lastBlock.Height == endHeight {
return nil
}
blockedSeq, err := client.GetBlockedSeq(lastBlock.Hash())
if err != nil {
plog.Error("Parachain GetBlockedSeq fail", "err", err)
return err
}
err = client.DelBlock(lastBlock, blockedSeq)
if err != nil {
plog.Error("Parachain GetBlockedSeq fail", "err", err)
return err
}
}
}
// 向blockchain删区块 // 向blockchain删区块
func (client *client) DelBlock(block *types.Block, seq int64) error { func (client *client) DelBlock(block *types.Block, seq int64) error {
......
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