Commit b0d1ca85 authored by vipwzw's avatar vipwzw

auto ci

parent 493afe81
...@@ -6,13 +6,10 @@ package para ...@@ -6,13 +6,10 @@ package para
import ( import (
"fmt" "fmt"
) )
func calcTitleHeightKey(title string, height int64) []byte {
return []byte(fmt.Sprintf("TH-%s-%d", title, height))
func calcTitleHeightKey(title string,height int64) []byte {
return []byte(fmt.Sprintf("TH-%s-%d", title,height))
} }
func calcTitleLastHeightKey(title string) []byte { func calcTitleLastHeightKey(title string) []byte {
......
...@@ -262,7 +262,7 @@ func (client *client) GetStartSeq(height int64) (int64, []byte) { ...@@ -262,7 +262,7 @@ func (client *client) GetStartSeq(height int64) (int64, []byte) {
hint.Stop() hint.Stop()
plog.Info(fmt.Sprintf("lastHeight more than %d blocks after startHeight", minBlockNum), "lastHeight", lastHeight, "startHeight", height) plog.Info(fmt.Sprintf("lastHeight more than %d blocks after startHeight", minBlockNum), "lastHeight", lastHeight, "startHeight", height)
seq, hash, err := client.GetSeqByHeightOnMainChain(height-1) seq, hash, err := client.GetSeqByHeightOnMainChain(height - 1)
if err != nil { if err != nil {
panic(err) panic(err)
} }
...@@ -395,7 +395,7 @@ func (client *client) GetHashByHeightOnMainChain(height int64) ([]byte, error) { ...@@ -395,7 +395,7 @@ func (client *client) GetHashByHeightOnMainChain(height int64) ([]byte, error) {
func (client *client) GetSeqByHashOnMainChain(hash []byte) (int64, error) { func (client *client) GetSeqByHashOnMainChain(hash []byte) (int64, error) {
seq, err := client.grpcClient.GetSequenceByHash(context.Background(), &types.ReqHash{Hash: hash}) seq, err := client.grpcClient.GetSequenceByHash(context.Background(), &types.ReqHash{Hash: hash})
if err != nil { if err != nil {
plog.Error("GetSeqByHashOnMainChain", "Error", err.Error(),"hash",hex.EncodeToString(hash)) plog.Error("GetSeqByHashOnMainChain", "Error", err.Error(), "hash", hex.EncodeToString(hash))
return -1, err return -1, err
} }
//the reflect checked in grpcHandle //the reflect checked in grpcHandle
......
...@@ -5,22 +5,19 @@ ...@@ -5,22 +5,19 @@
package para package para
import ( import (
"time"
"errors" "errors"
"time"
"encoding/hex"
"github.com/33cn/chain33/common" "github.com/33cn/chain33/common"
paracross "github.com/33cn/plugin/plugin/dapp/paracross/types"
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
"encoding/hex" paracross "github.com/33cn/plugin/plugin/dapp/paracross/types"
) )
func (client *client) setLocalBlock(set *types.LocalDBSet) error {
func (client *client) setLocalBlock(set *types.LocalDBSet) (error) {
//如果追赶上主链了,则落盘 //如果追赶上主链了,则落盘
if client.isCaughtUp{ if client.isCaughtUp {
set.Txid = 1 set.Txid = 1
} }
...@@ -33,24 +30,23 @@ func (client *client) setLocalBlock(set *types.LocalDBSet) (error) { ...@@ -33,24 +30,23 @@ func (client *client) setLocalBlock(set *types.LocalDBSet) (error) {
if err != nil { if err != nil {
return err return err
} }
if resp.GetData().(*types.Reply).IsOk{ if resp.GetData().(*types.Reply).IsOk {
return nil return nil
} }
return errors.New(string(resp.GetData().(*types.Reply).GetMsg())) return errors.New(string(resp.GetData().(*types.Reply).GetMsg()))
} }
func (client *client) addLocalBlock(height int64, block *paracross.ParaLocalDbBlock) error {
func (client *client) addLocalBlock(height int64, block *paracross.ParaLocalDbBlock) (error) {
set := &types.LocalDBSet{} set := &types.LocalDBSet{}
key := calcTitleHeightKey(types.GetTitle(),height) key := calcTitleHeightKey(types.GetTitle(), height)
kv := &types.KeyValue{Key: key, Value: types.Encode(block)} kv := &types.KeyValue{Key: key, Value: types.Encode(block)}
set.KV = append(set.KV,kv) set.KV = append(set.KV, kv)
//两个key原子操作 //两个key原子操作
key = calcTitleLastHeightKey(types.GetTitle()) key = calcTitleLastHeightKey(types.GetTitle())
kv = &types.KeyValue{Key: key, Value: types.Encode(&types.Int64{Data: height})} kv = &types.KeyValue{Key: key, Value: types.Encode(&types.Int64{Data: height})}
set.KV = append(set.KV,kv) set.KV = append(set.KV, kv)
return client.setLocalBlock(set) return client.setLocalBlock(set)
} }
...@@ -68,18 +64,16 @@ func (client *client) createLocalBlock(lastBlock *paracross.ParaLocalDbBlock, tx ...@@ -68,18 +64,16 @@ func (client *client) createLocalBlock(lastBlock *paracross.ParaLocalDbBlock, tx
return client.addLocalBlock(newblock.Height, &newblock) return client.addLocalBlock(newblock.Height, &newblock)
} }
func (client *client) delLocalBlock(height int64) error {
func (client *client) delLocalBlock(height int64) (error) {
set := &types.LocalDBSet{} set := &types.LocalDBSet{}
key := calcTitleHeightKey(types.GetTitle(),height) key := calcTitleHeightKey(types.GetTitle(), height)
kv := &types.KeyValue{Key: key, Value: nil} kv := &types.KeyValue{Key: key, Value: nil}
set.KV = append(set.KV,kv) set.KV = append(set.KV, kv)
//两个key原子操作 //两个key原子操作
key = calcTitleLastHeightKey(types.GetTitle()) key = calcTitleLastHeightKey(types.GetTitle())
kv = &types.KeyValue{Key: key, Value: types.Encode(&types.Int64{Data: height-1})} kv = &types.KeyValue{Key: key, Value: types.Encode(&types.Int64{Data: height - 1})}
set.KV = append(set.KV,kv) set.KV = append(set.KV, kv)
return client.setLocalBlock(set) return client.setLocalBlock(set)
} }
...@@ -89,81 +83,76 @@ func (client *client) removeLocalBlocks(minHeight int64) error { ...@@ -89,81 +83,76 @@ func (client *client) removeLocalBlocks(minHeight int64) error {
set := &types.LocalDBSet{} set := &types.LocalDBSet{}
key := calcTitleLastHeightKey(types.GetTitle()) key := calcTitleLastHeightKey(types.GetTitle())
if minHeight >0{ if minHeight > 0 {
kv := &types.KeyValue{Key: key, Value: types.Encode(&types.Int64{Data: minHeight})} kv := &types.KeyValue{Key: key, Value: types.Encode(&types.Int64{Data: minHeight})}
set.KV = append(set.KV,kv) set.KV = append(set.KV, kv)
}else { } else {
kv := &types.KeyValue{Key: key, Value: nil} kv := &types.KeyValue{Key: key, Value: nil}
set.KV = append(set.KV,kv) set.KV = append(set.KV, kv)
} }
return client.setLocalBlock(set) return client.setLocalBlock(set)
} }
func (client *client) getFromLocalDb(set *types.LocalDBGet, count int) ([][]byte, error) { func (client *client) getFromLocalDb(set *types.LocalDBGet, count int) ([][]byte, error) {
msg := client.GetQueueClient().NewMessage("blockchain", types.EventGetValueByKey, set) msg := client.GetQueueClient().NewMessage("blockchain", types.EventGetValueByKey, set)
err := client.GetQueueClient().Send(msg, true) err := client.GetQueueClient().Send(msg, true)
if err != nil { if err != nil {
return nil,err return nil, err
} }
resp, err := client.GetQueueClient().Wait(msg) resp, err := client.GetQueueClient().Wait(msg)
if err != nil { if err != nil {
return nil,err return nil, err
} }
reply := resp.GetData().(*types.LocalReplyValue) reply := resp.GetData().(*types.LocalReplyValue)
if len(reply.Values) != count{ if len(reply.Values) != count {
plog.Error("Parachain getFromLocalDb count not match", "expert", count,"real",len(reply.Values)) plog.Error("Parachain getFromLocalDb count not match", "expert", count, "real", len(reply.Values))
return nil, types.ErrInvalidParam return nil, types.ErrInvalidParam
} }
return reply.Values,nil return reply.Values, nil
} }
func (client *client) getLastLocalHeight() (int64, error) { func (client *client) getLastLocalHeight() (int64, error) {
key := calcTitleLastHeightKey(types.GetTitle()) key := calcTitleLastHeightKey(types.GetTitle())
set := &types.LocalDBGet{Keys:[][]byte{key}} set := &types.LocalDBGet{Keys: [][]byte{key}}
value,err := client.getFromLocalDb(set,len(set.Keys)) value, err := client.getFromLocalDb(set, len(set.Keys))
if err != nil{ if err != nil {
return -1, err return -1, err
} }
if value[0] == nil{ if value[0] == nil {
return -1, types.ErrNotFound return -1, types.ErrNotFound
} }
height := &types.Int64{} height := &types.Int64{}
err = types.Decode(value[0],height) err = types.Decode(value[0], height)
if err != nil{ if err != nil {
return -1,err return -1, err
} }
return height.Data,nil return height.Data, nil
} }
func (client *client) getLocalBlockByHeight(height int64) (*paracross.ParaLocalDbBlock, error) { func (client *client) getLocalBlockByHeight(height int64) (*paracross.ParaLocalDbBlock, error) {
key := calcTitleHeightKey(types.GetTitle(),height) key := calcTitleHeightKey(types.GetTitle(), height)
set := &types.LocalDBGet{Keys:[][]byte{key}} set := &types.LocalDBGet{Keys: [][]byte{key}}
value,err := client.getFromLocalDb(set,len(set.Keys)) value, err := client.getFromLocalDb(set, len(set.Keys))
if err != nil{ if err != nil {
return nil, err return nil, err
} }
if value[0] == nil{ if value[0] == nil {
return nil, types.ErrNotFound return nil, types.ErrNotFound
} }
var block paracross.ParaLocalDbBlock var block paracross.ParaLocalDbBlock
err = types.Decode(value[0],&block) err = types.Decode(value[0], &block)
if err != nil{ if err != nil {
return nil,err return nil, err
} }
return &block,nil return &block, nil
} }
...@@ -182,45 +171,44 @@ func (client *client) getLocalBlockInfoByHeight(height int64) (int64, []byte, er ...@@ -182,45 +171,44 @@ func (client *client) getLocalBlockInfoByHeight(height int64) (int64, []byte, er
} }
func (client *client) setLocalBlockByChainBlock(chainBlock *types.Block) (error) { func (client *client) setLocalBlockByChainBlock(chainBlock *types.Block) error {
//根据匹配上的chainblock,设置当前localdb block //根据匹配上的chainblock,设置当前localdb block
localBlock := &paracross.ParaLocalDbBlock{ localBlock := &paracross.ParaLocalDbBlock{
Height:chainBlock.Height, Height: chainBlock.Height,
MainHeight:chainBlock.MainHeight, MainHeight: chainBlock.MainHeight,
MainHash:chainBlock.MainHash, MainHash: chainBlock.MainHash,
BlockTime:chainBlock.BlockTime, BlockTime: chainBlock.BlockTime,
} }
return client.addLocalBlock(localBlock.Height,localBlock) return client.addLocalBlock(localBlock.Height, localBlock)
} }
//如果localdb里面没有信息,就从chain block返回,至少有创世区块,然后进入循环匹配切换场景 //如果localdb里面没有信息,就从chain block返回,至少有创世区块,然后进入循环匹配切换场景
func (client *client) getLastLocalBlockInfo() (int64, []byte, error) { func (client *client) getLastLocalBlockInfo() (int64, []byte, error) {
height,err := client.getLastLocalHeight() height, err := client.getLastLocalHeight()
if err == nil{ if err == nil {
mainSeq,mainHash,err := client.getLocalBlockInfoByHeight(height) mainSeq, mainHash, err := client.getLocalBlockInfoByHeight(height)
if err == nil{ if err == nil {
return mainSeq, mainHash,nil return mainSeq, mainHash, nil
} }
} }
mainSeq,chainBlock,err := client.getLastBlockMainInfo() mainSeq, chainBlock, err := client.getLastBlockMainInfo()
if err != nil{ if err != nil {
return -2,nil,err return -2, nil, err
} }
err = client.setLocalBlockByChainBlock(chainBlock) err = client.setLocalBlockByChainBlock(chainBlock)
if err != nil{ if err != nil {
return -2,nil,err return -2, nil, err
} }
return mainSeq,chainBlock.MainHash,nil return mainSeq, chainBlock.MainHash, nil
} }
func (client *client) getLastDbBlock() (*paracross.ParaLocalDbBlock, error) { func (client *client) getLastDbBlock() (*paracross.ParaLocalDbBlock, error) {
height,err := client.getLastLocalHeight() height, err := client.getLastLocalHeight()
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -228,7 +216,6 @@ func (client *client) getLastDbBlock() (*paracross.ParaLocalDbBlock, error) { ...@@ -228,7 +216,6 @@ func (client *client) getLastDbBlock() (*paracross.ParaLocalDbBlock, error) {
return client.getLocalBlockByHeight(height) return client.getLocalBlockByHeight(height)
} }
func (client *client) reqChainMatchedBlock(startHeight int64) (int64, *types.Block, error) { func (client *client) reqChainMatchedBlock(startHeight int64) (int64, *types.Block, error) {
lastBlock, err := client.RequestLastBlock() lastBlock, err := client.RequestLastBlock()
if err != nil { if err != nil {
...@@ -240,7 +227,7 @@ func (client *client) reqChainMatchedBlock(startHeight int64) (int64, *types.Blo ...@@ -240,7 +227,7 @@ func (client *client) reqChainMatchedBlock(startHeight int64) (int64, *types.Blo
return client.syncFromGenesisBlock() return client.syncFromGenesisBlock()
} }
if startHeight == 0 || startHeight > lastBlock.Height{ if startHeight == 0 || startHeight > lastBlock.Height {
startHeight = lastBlock.Height startHeight = lastBlock.Height
} }
...@@ -265,7 +252,7 @@ func (client *client) reqChainMatchedBlock(startHeight int64) (int64, *types.Blo ...@@ -265,7 +252,7 @@ func (client *client) reqChainMatchedBlock(startHeight int64) (int64, *types.Blo
if height == 1 { if height == 1 {
plog.Error("switchHashMatchedBlock search to height=1 not found", "lastBlockHeight", lastBlock.Height, plog.Error("switchHashMatchedBlock search to height=1 not found", "lastBlockHeight", lastBlock.Height,
"height1 mainHash", hex.EncodeToString(block.MainHash)) "height1 mainHash", hex.EncodeToString(block.MainHash))
return client.syncFromGenesisBlock() return client.syncFromGenesisBlock()
} }
continue continue
...@@ -279,22 +266,22 @@ func (client *client) reqChainMatchedBlock(startHeight int64) (int64, *types.Blo ...@@ -279,22 +266,22 @@ func (client *client) reqChainMatchedBlock(startHeight int64) (int64, *types.Blo
} }
func (client *client) switchChainMatchedBlock(startHeight int64) (int64, []byte, error) { func (client *client) switchChainMatchedBlock(startHeight int64) (int64, []byte, error) {
mainSeq, chainBlock,err := client.reqChainMatchedBlock(startHeight) mainSeq, chainBlock, err := client.reqChainMatchedBlock(startHeight)
if err != nil{ if err != nil {
return -2,nil,err return -2, nil, err
} }
err = client.setLocalBlockByChainBlock(chainBlock) err = client.setLocalBlockByChainBlock(chainBlock)
if err != nil{ if err != nil {
return -2,nil,err return -2, nil, err
} }
return mainSeq,chainBlock.MainHash,nil return mainSeq, chainBlock.MainHash, nil
} }
// search base on para block but not last MainBlockHash, last MainBlockHash can not back tracing // search base on para block but not last MainBlockHash, last MainBlockHash can not back tracing
func (client *client) switchLocalHashMatchedBlock(currSeq int64) (int64, []byte, error) { func (client *client) switchLocalHashMatchedBlock(currSeq int64) (int64, []byte, error) {
lastBlock, err := client.getLastDbBlock() lastBlock, err := client.getLastDbBlock()
if err != nil { if err != nil {
if err == types.ErrNotFound{ if err == types.ErrNotFound {
//TODO 或者通知执行层去切换 //TODO 或者通知执行层去切换
return client.switchChainMatchedBlock(0) return client.switchChainMatchedBlock(0)
} }
...@@ -302,14 +289,13 @@ func (client *client) switchLocalHashMatchedBlock(currSeq int64) (int64, []byte, ...@@ -302,14 +289,13 @@ func (client *client) switchLocalHashMatchedBlock(currSeq int64) (int64, []byte,
return -2, nil, err return -2, nil, err
} }
for height := lastBlock.Height; height > 0; height-- {
for height := lastBlock.Height; height > 0 ; height-- {
block, err := client.getLocalBlockByHeight(height) block, err := client.getLocalBlockByHeight(height)
if err != nil { if err != nil {
if err == types.ErrNotFound{ if err == types.ErrNotFound {
plog.Error("switchLocalHashMatchedBlock search not found", "lastBlockHeight", height) plog.Error("switchLocalHashMatchedBlock search not found", "lastBlockHeight", height)
err = client.removeLocalBlocks(height) err = client.removeLocalBlocks(height)
if err != nil{ if err != nil {
return -2, nil, err return -2, nil, err
} }
return client.switchChainMatchedBlock(height) return client.switchChainMatchedBlock(height)
...@@ -337,22 +323,20 @@ func (client *client) switchLocalHashMatchedBlock(currSeq int64) (int64, []byte, ...@@ -337,22 +323,20 @@ func (client *client) switchLocalHashMatchedBlock(currSeq int64) (int64, []byte,
return -2, nil, paracross.ErrParaCurHashNotMatch return -2, nil, paracross.ErrParaCurHashNotMatch
} }
func (client *client) downloadBlocks() { func (client *client) downloadBlocks() {
lastSeq, lastSeqMainHash, err := client.getLastLocalBlockInfo() lastSeq, lastSeqMainHash, err := client.getLastLocalBlockInfo()
if err != nil { if err != nil {
plog.Error("Parachain CreateBlock getLastLocalBlockInfo fail", "err", err.Error()) plog.Error("Parachain CreateBlock getLastLocalBlockInfo fail", "err", err.Error())
return return
} }
currSeq := lastSeq+1 currSeq := lastSeq + 1
for { for {
txs, mainBlock, err := client.RequestTx(currSeq, lastSeqMainHash) txs, mainBlock, err := client.RequestTx(currSeq, lastSeqMainHash)
if err != nil { if err != nil {
if err == paracross.ErrParaCurHashNotMatch { if err == paracross.ErrParaCurHashNotMatch {
preSeq, preSeqMainHash, err := client.switchLocalHashMatchedBlock(currSeq) preSeq, preSeqMainHash, err := client.switchLocalHashMatchedBlock(currSeq)
if err == nil { if err == nil {
currSeq = preSeq+1 currSeq = preSeq + 1
lastSeqMainHash = preSeqMainHash lastSeqMainHash = preSeqMainHash
continue continue
} }
...@@ -368,13 +352,13 @@ func (client *client) downloadBlocks() { ...@@ -368,13 +352,13 @@ func (client *client) downloadBlocks() {
} }
lastBlock, err := client.getLastDbBlock() lastBlock, err := client.getLastDbBlock()
if err != nil && err != types.ErrNotFound{ if err != nil && err != types.ErrNotFound {
plog.Error("Parachain getLastDbBlock", "err", err) plog.Error("Parachain getLastDbBlock", "err", err)
time.Sleep(time.Second) time.Sleep(time.Second)
continue continue
} }
plog.Info("Parachain process block", "curSeq", currSeq,"lastBlockHeight", lastBlock.Height, plog.Info("Parachain process block", "curSeq", currSeq, "lastBlockHeight", lastBlock.Height,
"currSeqMainHeight", lastSeqMainHeight, "currSeqMainHash", common.ToHex(lastSeqMainHash), "currSeqMainHeight", lastSeqMainHeight, "currSeqMainHash", common.ToHex(lastSeqMainHash),
"lastBlockMainHeight", lastBlock.MainHeight, "lastBlockMainHash", common.ToHex(lastBlock.MainHash), "seqTy", mainBlock.Seq.Type) "lastBlockMainHeight", lastBlock.MainHeight, "lastBlockMainHash", common.ToHex(lastBlock.MainHash), "seqTy", mainBlock.Seq.Type)
...@@ -401,8 +385,8 @@ func (client *client) downloadBlocks() { ...@@ -401,8 +385,8 @@ func (client *client) downloadBlocks() {
err = types.ErrInvalidParam err = types.ErrInvalidParam
} }
if err != nil{ if err != nil {
plog.Error("para DownloadBlocks", "type",mainBlock.Seq.Type,"err",err.Error()) plog.Error("para DownloadBlocks", "type", mainBlock.Seq.Type, "err", err.Error())
time.Sleep(time.Second) time.Sleep(time.Second)
continue continue
} }
......
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