Commit 16d7818b authored by caopingcp's avatar caopingcp Committed by vipwzw

calc startSeq in para

parent 039dc644
...@@ -33,6 +33,8 @@ const ( ...@@ -33,6 +33,8 @@ const (
delAct int64 = 2 //reference blockstore.go, del para block action delAct int64 = 2 //reference blockstore.go, del para block action
paraCrossTxCount = 2 //current only support 2 txs for cross paraCrossTxCount = 2 //current only support 2 txs for cross
minBlockNum = 6 //min block number startHeight before lastHeight in mainchain
) )
var ( var (
...@@ -40,7 +42,6 @@ var ( ...@@ -40,7 +42,6 @@ var (
grpcSite = "localhost:8802" grpcSite = "localhost:8802"
genesisBlockTime int64 = 1514533390 genesisBlockTime int64 = 1514533390
startHeight int64 //parachain sync from startHeight in mainchain startHeight int64 //parachain sync from startHeight in mainchain
searchSeq int64 //start sequence to search startHeight in mainchain
blockSec int64 = 5 //write block interval, second blockSec int64 = 5 //write block interval, second
emptyBlockInterval int64 = 4 //write empty block every interval blocks in mainchain emptyBlockInterval int64 = 4 //write empty block every interval blocks in mainchain
zeroHash [32]byte zeroHash [32]byte
...@@ -75,7 +76,6 @@ func New(cfg *types.Consensus, sub []byte) queue.Module { ...@@ -75,7 +76,6 @@ func New(cfg *types.Consensus, sub []byte) queue.Module {
} }
if cfg.StartHeight > 0 { if cfg.StartHeight > 0 {
startHeight = cfg.StartHeight startHeight = cfg.StartHeight
searchSeq = calcSearchseq(cfg.StartHeight)
} }
if cfg.WriteBlockSeconds > 0 { if cfg.WriteBlockSeconds > 0 {
blockSec = cfg.WriteBlockSeconds blockSec = cfg.WriteBlockSeconds
...@@ -139,13 +139,6 @@ func New(cfg *types.Consensus, sub []byte) queue.Module { ...@@ -139,13 +139,6 @@ func New(cfg *types.Consensus, sub []byte) queue.Module {
return para return para
} }
func calcSearchseq(height int64) int64 {
if height < 1000 {
return 0
}
return height - 1000
}
//para 不检查任何的交易 //para 不检查任何的交易
func (client *client) CheckBlock(parent *types.Block, current *types.BlockDetail) error { func (client *client) CheckBlock(parent *types.Block, current *types.BlockDetail) error {
err := checkMinerTx(current) err := checkMinerTx(current)
...@@ -179,10 +172,7 @@ func (client *client) InitBlock() { ...@@ -179,10 +172,7 @@ func (client *client) InitBlock() {
} }
if block == nil { if block == nil {
startSeq := int64(0) startSeq := client.GetStartSeq(startHeight)
if searchSeq > 0 {
startSeq = client.GetSeqByHeightOnMain(startHeight, searchSeq)
}
// 创世区块 // 创世区块
newblock := &types.Block{} newblock := &types.Block{}
newblock.Height = 0 newblock.Height = 0
...@@ -191,37 +181,28 @@ func (client *client) InitBlock() { ...@@ -191,37 +181,28 @@ 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-int64(1)) client.WriteBlock(zeroHash[:], newblock, startSeq-1)
} else { } else {
client.SetCurrentBlock(block) client.SetCurrentBlock(block)
} }
} }
func (client *client) GetSeqByHeightOnMain(height int64, originSeq int64) int64 { // GetStartSeq get startSeq in mainchain
lastSeq, err := client.GetLastSeqOnMainChain() func (client *client) GetStartSeq(height int64) int64 {
plog.Info("Searching for the sequence", "heightOnMain", height, "searchSeq", searchSeq, "lastSeq", lastSeq) lastHeight, err := client.GetLastHeightOnMainChain()
if err != nil { if err != nil {
panic(err) panic(err)
} }
hint := time.NewTicker(10 * time.Second) if lastHeight-height < minBlockNum {
defer hint.Stop() panic(fmt.Sprintf("startHeight(%d) less than %d blocks before lastHeight(%d) in mainchain", height, minBlockNum, lastHeight))
for originSeq <= lastSeq { }
select {
case <-hint.C: seq, err := client.GetSeqByHeightOnMainChain(height)
plog.Info("Still Searching......", "searchAtSeq", originSeq, "lastSeq", lastSeq)
default:
blockDetail, seqTy, err := client.GetBlockOnMainBySeq(originSeq)
if err != nil { if err != nil {
panic(err) panic(err)
} }
if blockDetail.Block.Height == height && seqTy == addAct { plog.Info("the start sequence in mainchain", "startHeight", height, "startSeq", seq)
plog.Info("the target sequence in mainchain", "heightOnMain", height, "targetSeq", originSeq) return seq
return originSeq
}
originSeq++
}
}
panic("Main chain has not reached the height currently")
} }
func (client *client) CreateGenesisTx() (ret []*types.Transaction) { func (client *client) CreateGenesisTx() (ret []*types.Transaction) {
...@@ -357,6 +338,15 @@ func (client *client) getLastBlockInfo() (int64, *types.Block, []byte, int64, er ...@@ -357,6 +338,15 @@ func (client *client) getLastBlockInfo() (int64, *types.Block, []byte, int64, er
} }
func (client *client) GetLastHeightOnMainChain() (int64, error) {
header, err := client.grpcClient.GetLastHeader(context.Background(), &types.ReqNil{})
if err != nil {
plog.Error("GetLastHeightOnMainChain", "Error", err.Error())
return -1, err
}
return header.Height, nil
}
func (client *client) GetLastSeqOnMainChain() (int64, error) { func (client *client) GetLastSeqOnMainChain() (int64, error) {
seq, err := client.grpcClient.GetLastBlockSequence(context.Background(), &types.ReqNil{}) seq, err := client.grpcClient.GetLastBlockSequence(context.Background(), &types.ReqNil{})
if err != nil { if err != nil {
...@@ -367,6 +357,24 @@ func (client *client) GetLastSeqOnMainChain() (int64, error) { ...@@ -367,6 +357,24 @@ func (client *client) GetLastSeqOnMainChain() (int64, error) {
return seq.Data, nil return seq.Data, nil
} }
func (client *client) GetSeqByHeightOnMainChain(height int64) (int64, error) {
hash, err := client.GetHashByHeightOnMainChain(height)
if err != nil {
return -1, err
}
seq, err := client.GetSeqByHashOnMainChain(hash)
return seq, err
}
func (client *client) GetHashByHeightOnMainChain(height int64) ([]byte, error) {
reply, err := client.grpcClient.GetBlockHash(context.Background(), &types.ReqInt{Height: height})
if err != nil {
plog.Error("GetHashByHeightOnMainChain", "Error", err.Error())
return nil, err
}
return reply.Hash, nil
}
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 {
......
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