Commit 493afe81 authored by mdj33's avatar mdj33 Committed by vipwzw

para download improve

parent 7fdd37ad
// 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 (
"fmt"
)
func calcTitleHeightKey(title string,height int64) []byte {
return []byte(fmt.Sprintf("TH-%s-%d", title,height))
}
func calcTitleLastHeightKey(title string) []byte {
return []byte(fmt.Sprintf("LH-%s", title))
}
...@@ -222,7 +222,7 @@ func (client *client) InitBlock() { ...@@ -222,7 +222,7 @@ func (client *client) InitBlock() {
tx := client.CreateGenesisTx() tx := client.CreateGenesisTx()
newblock.Txs = tx newblock.Txs = tx
newblock.TxHash = merkle.CalcMerkleRoot(newblock.Txs) newblock.TxHash = merkle.CalcMerkleRoot(newblock.Txs)
client.WriteBlock(zeroHash[:], newblock, startSeq-1) client.WriteBlock(zeroHash[:], newblock, startSeq)
} else { } else {
client.SetCurrentBlock(block) client.SetCurrentBlock(block)
} }
...@@ -234,8 +234,8 @@ func (client *client) InitBlock() { ...@@ -234,8 +234,8 @@ func (client *client) InitBlock() {
// GetStartSeq get startSeq in mainchain // GetStartSeq get startSeq in mainchain
func (client *client) GetStartSeq(height int64) (int64, []byte) { func (client *client) GetStartSeq(height int64) (int64, []byte) {
if height == 0 { if height <= 0 {
return 0, nil panic(fmt.Sprintf("startHeight(%d) should be more than 0 in mainchain", height))
} }
lastHeight, err := client.GetLastHeightOnMainChain() lastHeight, err := client.GetLastHeightOnMainChain()
...@@ -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) seq, hash, err := client.GetSeqByHeightOnMainChain(height-1)
if err != nil { if err != nil {
panic(err) panic(err)
} }
...@@ -320,43 +320,29 @@ func (client *client) GetBlockByHeight(height int64) (*types.Block, error) { ...@@ -320,43 +320,29 @@ func (client *client) GetBlockByHeight(height int64) (*types.Block, error) {
return blockDetails.Items[0].Block, nil return blockDetails.Items[0].Block, nil
} }
// 获取上一个平行链对应主链seq,hash信息 // 获取当前平行链block对应主链seq,hash信息
// 对于平行链创世区块特殊场景: // 对于云端主链节点,创世区块记录seq在不同主链节点上差异很大,通过记录的主链hash获取真实seq使用
// 1,创世区块seq从-1开始,也就是从主链0高度同步区块,主链seq从0开始,平行链对seq=0的区块校验时候做特殊处理,不校验parentHash func (client *client) getLastBlockMainInfo() (int64, *types.Block, error) {
// 2,创世区块seq不是-1, 也就是从主链seq=n高度同步区块,此时创世区块记录了起始高度对应的主链hash,通过hash获取当前seq,然后创世区块需要倒退一个seq,lastSeq=n-1, lastBlock, err := client.getLastBlockInfo()
// 因为对于云端主链节点,创世区块记录seq在不同主链节点上差异很大,通过记录的主链hash获取的真实seq-1来使用,主链hash使用对应区块的parenthash做校验目的
func (client *client) getLastBlockMainInfo() (int64, []byte, error) {
lastSeq, lastBlock, err := client.getLastBlockInfo()
if err != nil { if err != nil {
return -2, nil, err return -2, nil, err
} }
if lastBlock.Height == 0 && lastSeq > -1 { mainSeq, err := client.GetSeqByHashOnMainChain(lastBlock.MainHash)
mainBlock, err := client.GetBlockOnMainByHash(lastBlock.MainHash) if err != nil {
if err != nil { return client.reqChainMatchedBlock(0)
return -2, nil, err
}
mainSeq, err := client.GetSeqByHashOnMainChain(lastBlock.MainHash)
if err != nil {
return -2, nil, err
}
return mainSeq - 1, mainBlock.ParentHash, nil
} }
return lastSeq, lastBlock.MainHash, nil return mainSeq, lastBlock, nil
} }
func (client *client) getLastBlockInfo() (int64, *types.Block, error) { func (client *client) getLastBlockInfo() (*types.Block, error) {
lastBlock, err := client.RequestLastBlock() lastBlock, err := client.RequestLastBlock()
if err != nil { if err != nil {
plog.Error("Parachain RequestLastBlock fail", "err", err) plog.Error("Parachain RequestLastBlock fail", "err", err)
return -2, nil, err return nil, err
}
blockedSeq, err := client.GetBlockedSeq(lastBlock.Hash())
if err != nil {
plog.Error("Parachain GetBlockedSeq fail", "err", err)
return -2, nil, err
} }
return blockedSeq, lastBlock, nil
return lastBlock, nil
} }
func (client *client) GetForkHeightOnMainChain(key string) (int64, error) { func (client *client) GetForkHeightOnMainChain(key string) (int64, error) {
...@@ -409,7 +395,7 @@ func (client *client) GetHashByHeightOnMainChain(height int64) ([]byte, error) { ...@@ -409,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()) 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
...@@ -498,18 +484,18 @@ func (client *client) RequestTx(currSeq int64, preMainBlockHash []byte) ([]*type ...@@ -498,18 +484,18 @@ func (client *client) RequestTx(currSeq int64, preMainBlockHash []byte) ([]*type
//genesis block scenario, new main node's blockHash as preMainHash, genesis sequence+1 as currSeq //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 // for genesis seq=-1 scenario, mainHash not care, as the 0 seq instead of -1
// not seq=-1 scenario, mainHash needed // not seq=-1 scenario, mainHash needed
func (client *client) syncFromGenesisBlock() (int64, []byte, error) { func (client *client) syncFromGenesisBlock() (int64, *types.Block, error) {
lastSeq, lastMainHash, err := client.getLastBlockMainInfo() lastSeq, lastBlock, err := client.getLastBlockMainInfo()
if err != nil { if err != nil {
plog.Error("Parachain getLastBlockInfo fail", "err", err) plog.Error("Parachain getLastBlockInfo fail", "err", err)
return -2, nil, err return -2, nil, err
} }
plog.Info("syncFromGenesisBlock sync from height 0") plog.Info("syncFromGenesisBlock sync from height 0")
return lastSeq + 1, lastMainHash, nil return lastSeq, lastBlock, 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) switchHashMatchedBlock(currSeq int64) (int64, []byte, error) { func (client *client) switchHashMatchedBlock(currSeq int64) (int64, *types.Block, error) {
lastBlock, err := client.RequestLastBlock() lastBlock, err := client.RequestLastBlock()
if err != nil { if err != nil {
plog.Error("Parachain RequestLastBlock fail", "err", err) plog.Error("Parachain RequestLastBlock fail", "err", err)
...@@ -559,7 +545,7 @@ func (client *client) switchHashMatchedBlock(currSeq int64) (int64, []byte, erro ...@@ -559,7 +545,7 @@ func (client *client) switchHashMatchedBlock(currSeq int64) (int64, []byte, erro
plog.Info("switchHashMatchedBlock succ", "currHeight", height, "initHeight", lastBlock.Height, plog.Info("switchHashMatchedBlock succ", "currHeight", height, "initHeight", lastBlock.Height,
"new currSeq", mainSeq+1, "new preMainBlockHash", hex.EncodeToString(block.MainHash)) "new currSeq", mainSeq+1, "new preMainBlockHash", hex.EncodeToString(block.MainHash))
return mainSeq + 1, block.MainHash, nil return mainSeq, block, nil
} }
return -2, nil, paracross.ErrParaCurHashNotMatch return -2, nil, paracross.ErrParaCurHashNotMatch
} }
...@@ -615,15 +601,17 @@ func (client *client) CreateBlock() { ...@@ -615,15 +601,17 @@ func (client *client) CreateBlock() {
txs, blockOnMain, err := client.RequestTx(currSeq, lastSeqMainHash) txs, blockOnMain, err := client.RequestTx(currSeq, lastSeqMainHash)
if err != nil { if err != nil {
incSeqFlag = false
if err == paracross.ErrParaCurHashNotMatch { if err == paracross.ErrParaCurHashNotMatch {
newSeq, newSeqMainHash, err := client.switchHashMatchedBlock(currSeq) newSeq, newSeqMainHash, err := client.switchHashMatchedBlock(currSeq)
if err == nil { if err == nil {
incSeqFlag = true
currSeq = newSeq currSeq = newSeq
lastSeqMainHash = newSeqMainHash lastSeqMainHash = newSeqMainHash
continue continue
} }
} }
incSeqFlag = false
time.Sleep(time.Second * time.Duration(blockSec)) time.Sleep(time.Second * time.Duration(blockSec))
continue continue
} }
...@@ -634,7 +622,7 @@ func (client *client) CreateBlock() { ...@@ -634,7 +622,7 @@ func (client *client) CreateBlock() {
lastSeqMainHash = blockOnMain.Detail.Block.ParentHash lastSeqMainHash = blockOnMain.Detail.Block.ParentHash
} }
lastBlockSeq, lastBlock, err := client.getLastBlockInfo() lastBlock, err := client.getLastBlockInfo()
if err != nil { if err != nil {
plog.Error("Parachain getLastBlockInfo fail", "err", err) plog.Error("Parachain getLastBlockInfo fail", "err", err)
time.Sleep(time.Second) time.Sleep(time.Second)
...@@ -642,7 +630,7 @@ func (client *client) CreateBlock() { ...@@ -642,7 +630,7 @@ func (client *client) CreateBlock() {
} }
plog.Info("Parachain process block", "lastSeq", lastSeq, "curSeq", currSeq, plog.Info("Parachain process block", "lastSeq", lastSeq, "curSeq", currSeq,
"lastBlockHeight", lastBlock.Height, "lastBlockSeq", lastBlockSeq, "lastBlockHeight", lastBlock.Height,
"currSeqMainHeight", lastSeqMainHeight, "currSeqMainHash", common.ToHex(lastSeqMainHash), "currSeqMainHeight", lastSeqMainHeight, "currSeqMainHash", common.ToHex(lastSeqMainHash),
"lastBlockMainHeight", lastBlock.MainHeight, "lastBlockMainHash", common.ToHex(lastBlock.MainHash), "seqTy", blockOnMain.Seq.Type) "lastBlockMainHeight", lastBlock.MainHeight, "lastBlockMainHash", common.ToHex(lastBlock.MainHash), "seqTy", blockOnMain.Seq.Type)
......
This diff is collapsed.
...@@ -301,6 +301,14 @@ message ParacrossAsset { ...@@ -301,6 +301,14 @@ message ParacrossAsset {
bool success = 23; bool success = 23;
} }
message ParaLocalDbBlock {
int64 height = 1;
bytes mainHash = 2;
int64 mainHeight = 3;
bytes parentMainHash = 4;
int64 blockTime = 5;
repeated Transaction txs = 6;
}
service paracross { service paracross {
......
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