Commit db90235d authored by yukang's avatar yukang Committed by vipwzw

Add some hints for debug and analysis

parent ca7e1885
...@@ -49,7 +49,7 @@ func (client *client) CreateGenesisBlock(newblock *types.Block) error { ...@@ -49,7 +49,7 @@ func (client *client) CreateGenesisBlock(newblock *types.Block) error {
func (client *client) SyncBlocks() { func (client *client) SyncBlocks() {
client.syncInit() client.syncInit()
var isSyncCaughtUp = false isSyncCaughtUp := false
for { for {
//获取同步状态,在需要同步的情况下执行同步 //获取同步状态,在需要同步的情况下执行同步
curSyncCaughtState, err := client.syncBlocksIfNeed() curSyncCaughtState, err := client.syncBlocksIfNeed()
...@@ -80,50 +80,50 @@ func (client *client) SyncBlocks() { ...@@ -80,50 +80,50 @@ func (client *client) SyncBlocks() {
} }
//获取每一轮可执行状态 //获取每一轮可执行状态
func (client *client) getNextAction() (NextActionType,*types.Block,*pt.ParaLocalDbBlock,error) { func (client *client) getNextAction() (NextActionType,*types.Block,*pt.ParaLocalDbBlock,int64,error) {
lastBlock, err := client.getLastBlockInfo() lastBlock, err := client.getLastBlockInfo()
if err != nil { if err != nil {
//取已执行最新区块发生错误,不做任何操作 //取已执行最新区块发生错误,不做任何操作
return NextActionKeep,nil,nil,err return NextActionKeep,nil,nil,-1,err
} }
lastLocalHeight, err := client.getLastLocalHeight() lastLocalHeight, err := client.getLastLocalHeight()
if err != nil { if err != nil {
//取db中最新高度区块发生错误,不做任何操作 //取db中最新高度区块发生错误,不做任何操作
return NextActionKeep,nil,nil,err return NextActionKeep,nil,nil,lastLocalHeight,err
} }
if lastLocalHeight <= 0 { if lastLocalHeight <= 0 {
//db中最新高度为0,不做任何操作(创世区块) //db中最新高度为0,不做任何操作(创世区块)
return NextActionKeep,nil,nil,err return NextActionKeep,nil,nil,lastLocalHeight,err
} else if lastLocalHeight < lastBlock.Height { } else if lastLocalHeight < lastBlock.Height {
//db中最新区块高度小于已执行最新区块高度,回滚 //db中最新区块高度小于已执行最新区块高度,回滚
return NextActionRollback,lastBlock,nil,err return NextActionRollback,lastBlock,nil,lastLocalHeight,err
} else if lastLocalHeight == lastBlock.Height { } else if lastLocalHeight == lastBlock.Height {
localBlock, err := client.getLocalBlockByHeight(lastBlock.Height) localBlock, err := client.getLocalBlockByHeight(lastBlock.Height)
if err != nil { if err != nil {
//取db中指定高度区块发生错误,不做任何操作 //取db中指定高度区块发生错误,不做任何操作
return NextActionKeep,nil,nil,err return NextActionKeep,nil,nil,lastLocalHeight,err
} }
if common.ToHex(localBlock.MainHash) == common.ToHex(lastBlock.MainHash) { if common.ToHex(localBlock.MainHash) == common.ToHex(lastBlock.MainHash) {
//db中最新区块高度等于已执行最新区块高度并且hash相同,不做任何操作(已保持同步状态) //db中最新区块高度等于已执行最新区块高度并且hash相同,不做任何操作(已保持同步状态)
return NextActionKeep,nil,nil,err return NextActionKeep,nil,nil,lastLocalHeight,err
} else { } else {
//db中最新区块高度等于已执行最新区块高度并且hash不同,回滚 //db中最新区块高度等于已执行最新区块高度并且hash不同,回滚
return NextActionRollback,lastBlock,nil,err return NextActionRollback,lastBlock,nil,lastLocalHeight,err
} }
} else { } else {
localBlock, err := client.getLocalBlockByHeight(lastBlock.Height+1) localBlock, err := client.getLocalBlockByHeight(lastBlock.Height+1)
if err != nil { if err != nil {
//取db中后一高度区块发生错误,不做任何操作 //取db中后一高度区块发生错误,不做任何操作
return NextActionKeep,nil,nil,err return NextActionKeep,nil,nil,lastLocalHeight,err
} }
if common.ToHex(localBlock.ParentMainHash) != common.ToHex(lastBlock.MainHash) { if common.ToHex(localBlock.ParentMainHash) != common.ToHex(lastBlock.MainHash) {
//db中后一高度区块的父hash不等于已执行最新区块的hash,回滚 //db中后一高度区块的父hash不等于已执行最新区块的hash,回滚
return NextActionRollback,lastBlock,nil,err return NextActionRollback,lastBlock,nil,lastLocalHeight,err
} else { } else {
//db中后一高度区块的父hash等于已执行最新区块的hash,执行区块创建 //db中后一高度区块的父hash等于已执行最新区块的hash,执行区块创建
return NextActionAdd,lastBlock,localBlock,err return NextActionAdd,lastBlock,localBlock,lastLocalHeight,err
} }
} }
} }
...@@ -132,7 +132,7 @@ func (client *client) getNextAction() (NextActionType,*types.Block,*pt.ParaLocal ...@@ -132,7 +132,7 @@ func (client *client) getNextAction() (NextActionType,*types.Block,*pt.ParaLocal
//返回参数 //返回参数
//bool 是否已完成同步 //bool 是否已完成同步
func (client *client) syncBlocksIfNeed() (bool,error) { func (client *client) syncBlocksIfNeed() (bool,error) {
nextAction, lastBlock, localBlock, err := client.getNextAction() nextAction, lastBlock, localBlock,lastLocalHeight, err := client.getNextAction()
if err != nil { if err != nil {
return false,err return false,err
} }
...@@ -140,11 +140,15 @@ func (client *client) syncBlocksIfNeed() (bool,error) { ...@@ -140,11 +140,15 @@ func (client *client) syncBlocksIfNeed() (bool,error) {
switch nextAction { switch nextAction {
case NextActionAdd: case NextActionAdd:
//1 db中后一高度区块的父hash等于已执行最新区块的hash //1 db中后一高度区块的父hash等于已执行最新区块的hash
plog.Info("Para sync add block",
"lastBlock.Height",lastBlock.Height,"lastLocalHeight",lastLocalHeight)
return false,client.addBlock(lastBlock, localBlock) return false,client.addBlock(lastBlock, localBlock)
case NextActionRollback: case NextActionRollback:
//1 db中最新区块高度小于已执行最新区块高度 //1 db中最新区块高度小于已执行最新区块高度
//2 db中最新区块高度等于已执行最新区块高度并且hash不同 //2 db中最新区块高度等于已执行最新区块高度并且hash不同
//3 db中后一高度区块的父hash不等于已执行最新区块的hash //3 db中后一高度区块的父hash不等于已执行最新区块的hash
plog.Info("Para sync rollback block",
"lastBlock.Height",lastBlock.Height,"lastLocalHeight",lastLocalHeight)
return false,client.rollbackBlock(lastBlock) return false,client.rollbackBlock(lastBlock)
default: //NextActionKeep default: //NextActionKeep
//1 已完成同步,没有需要同步的块 //1 已完成同步,没有需要同步的块
...@@ -159,7 +163,7 @@ func (client *client) delLocalBlocks(startHeight int64,endHeight int64) error { ...@@ -159,7 +163,7 @@ func (client *client) delLocalBlocks(startHeight int64,endHeight int64) error {
return errors.New("startHeight > endHeight,can't clear local blocks") return errors.New("startHeight > endHeight,can't clear local blocks")
} }
var index = startHeight index := startHeight
set := &types.LocalDBSet{} set := &types.LocalDBSet{}
for { for {
if index > endHeight { if index > endHeight {
...@@ -177,6 +181,8 @@ func (client *client) delLocalBlocks(startHeight int64,endHeight int64) error { ...@@ -177,6 +181,8 @@ func (client *client) delLocalBlocks(startHeight int64,endHeight int64) error {
kv := &types.KeyValue{Key: key, Value: types.Encode(&types.Int64{Data: endHeight+1})} kv := &types.KeyValue{Key: key, Value: types.Encode(&types.Int64{Data: endHeight+1})}
set.KV = append(set.KV, kv) set.KV = append(set.KV, kv)
plog.Info("Para sync clear local blocks", "startHeight:",startHeight,"endHeight:",endHeight)
return client.setLocalDb(set) return client.setLocalDb(set)
} }
...@@ -228,7 +234,7 @@ func (client *client) clearLocalOldBlocks() (bool,error) { ...@@ -228,7 +234,7 @@ func (client *client) clearLocalOldBlocks() (bool,error) {
return false,err return false,err
} }
var canDelCount = lastLocalHeight - firstLocalHeight - localCacheCount + 1 canDelCount := lastLocalHeight - firstLocalHeight - localCacheCount + 1
if canDelCount <= 0 { if canDelCount <= 0 {
return false,nil return false,nil
} }
...@@ -378,7 +384,7 @@ func (client *client) initLocalChangeState() { ...@@ -378,7 +384,7 @@ func (client *client) initLocalChangeState() {
//获取当前是否有新的下载到来,获取一次,并马上把状态设置为没有新通知 //获取当前是否有新的下载到来,获取一次,并马上把状态设置为没有新通知
//此函数原则上只限于此线程单元使用 //此函数原则上只限于此线程单元使用
func (client *client) getAndFlipLocalChangeStateIfNeed() bool { func (client *client) getAndFlipLocalChangeStateIfNeed() bool {
var hasLocalChange = atomic.LoadInt32(&client.localChangeAtom) == 1 hasLocalChange := atomic.LoadInt32(&client.localChangeAtom) == 1
if hasLocalChange { if hasLocalChange {
atomic.StoreInt32(&client.localChangeAtom,0) atomic.StoreInt32(&client.localChangeAtom,0)
} }
......
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