Commit 4593674c authored by mdj33's avatar mdj33 Committed by vipwzw

support main consens height jump

parent 508227d0
......@@ -88,6 +88,7 @@ type subConfig struct {
MaxSyncErrCount int32 `json:"maxSyncErrCount,omitempty"`
BatchFetchSeqEnable uint32 `json:"batchFetchSeqEnable,omitempty"`
BatchFetchSeqNum int64 `json:"batchFetchSeqNum,omitempty"`
ParaConsensStartHeight int64 `json:"paraConsensStartHeight,omitempty"`
}
// New function to init paracross env
......@@ -178,9 +179,14 @@ func New(cfg *types.Consensus, sub []byte) queue.Module {
waitConsensStopTimes: waitConsensTimes,
consensHeight: -2,
sendingHeight: -1,
consensStartHeight: -1,
resetCh: make(chan interface{}, 1),
quit: make(chan struct{}),
}
//note:只有在主链LoopCheckCommitTxDoneForkHeight之后才支持设置ParaConsensStartHeight
if subcfg.ParaConsensStartHeight > 0 {
para.commitMsgClient.consensStartHeight = subcfg.ParaConsensStartHeight - 1
}
para.blockSyncClient = &BlockSyncClient{
paraClient: para,
......
......@@ -41,6 +41,7 @@ type commitMsgClient struct {
chainHeight int64
sendingHeight int64
consensHeight int64
consensStartHeight int64
authAccountIn bool
isRollBack int32
checkTxCommitTimes int32
......@@ -148,12 +149,18 @@ func (client *commitMsgClient) sendCommitTx() {
defer client.mutex.Unlock()
consensHeight := client.getConsensusHeight()
//只有从未共识过,才可以设置从初始起始高度跳跃
if consensHeight == -1 && consensHeight < client.consensStartHeight {
consensHeight = client.consensStartHeight
}
chainHeight := atomic.LoadInt64(&client.chainHeight)
sendingHeight := client.sendingHeight
isSync := client.isSync()
plog.Info("para commitMsg---status", "chainHeight", chainHeight, "sendingHeight", sendingHeight,
"consensHeight", consensHeight, "isSendingTx", client.isSendingCommitMsg(), "sync", client.isSync())
"consensHeight", consensHeight, "isSendingTx", client.isSendingCommitMsg(), "sync", isSync)
if client.isSendingCommitMsg() || !client.isSync() {
if client.isSendingCommitMsg() || !isSync {
return
}
......@@ -470,9 +477,8 @@ out:
case tx = <-client.sendMsgCh:
err = client.sendCommitTxOut(tx)
if err != nil && err == types.ErrTxFeeTooLow {
feeRate, err := client.GetProperFeeRate()
err := client.GetProperFeeRate()
if err == nil {
atomic.StoreInt64(&client.txFeeRate, feeRate)
client.resetNotify()
}
continue
......@@ -666,15 +672,11 @@ out:
selfHeight = selfStatus.Height
}
var feeRate int64
if client.paraClient.authAccount != "" {
feeRate, err = client.GetProperFeeRate()
if err == nil {
atomic.StoreInt64(&client.txFeeRate, feeRate)
}
client.GetProperFeeRate()
}
plog.Info("para consensusHeight", "mainHeight", status.Height, "selfHeight", selfHeight, "feeRate", feeRate)
plog.Info("para consensusHeight", "mainHeight", status.Height, "selfHeight", selfHeight)
}
}
......@@ -682,18 +684,19 @@ out:
client.paraClient.wg.Done()
}
func (client *commitMsgClient) GetProperFeeRate() (int64, error) {
func (client *commitMsgClient) GetProperFeeRate() error {
feeRate, err := client.paraClient.grpcClient.GetProperFee(context.Background(), &types.ReqProperFee{})
if err != nil {
plog.Error("para commit.GetProperFee", "err", err.Error())
return -1, err
return err
}
if feeRate == nil {
plog.Error("para commit.GetProperFee return nil")
return -1, types.ErrInvalidParam
return types.ErrInvalidParam
}
return feeRate.ProperFee, nil
atomic.StoreInt64(&client.txFeeRate, feeRate.ProperFee)
return nil
}
func (client *commitMsgClient) getSelfConsensusStatus() (*pt.ParacrossStatus, error) {
......
......@@ -455,11 +455,11 @@ func (a *action) Commit(commit *pt.ParacrossCommitAction) (*types.Receipt, error
if commit.Status.Height > titleStatus.Height+1 {
saveTitleHeight(a.db, calcTitleHeightKey(commit.Status.Title, commit.Status.Height), stat)
//平行链由主链共识无缝切换,即接收第一个收到的高度,可以不从0开始
paraSwitch, err := a.isParaSelfConsensSwitch(stat, titleStatus)
allowJump, err := a.isAllowConsensJump(stat, titleStatus)
if err != nil {
return nil, err
}
if !paraSwitch {
if !allowJump {
return receipt, nil
}
}
......@@ -689,13 +689,20 @@ func (a *action) commitTxDoneByStat(stat *pt.ParacrossHeightStatus, titleStatus
return receipt, nil
}
//平行链自共识无缝切换条件:1,平行链没有共识过,2:commit高度是大于自共识分叉高度且上一次共识的主链高度小于自共识分叉高度,保证只运行一次,
// 这样在主链没有共识空洞前提下,平行链允许有条件的共识跳跃
func (a *action) isParaSelfConsensSwitch(commit *pt.ParacrossHeightStatus, titleStatus *pt.ParacrossStatus) (bool, error) {
if !types.IsPara() {
return false, nil
//主链共识跳跃条件: 仅支持主链共识初始高度为-1,也就是没有共识过,共识过不允许再跳跃
func (a *action) isAllowMainConsensJump(commit *pt.ParacrossHeightStatus, titleStatus *pt.ParacrossStatus) (bool, error) {
if types.IsDappFork(a.exec.GetMainHeight(), pt.ParaX, pt.ForkLoopCheckCommitTxDone) {
if titleStatus.Height == -1 {
return true, nil
}
}
return false, nil
}
//平行链自共识无缝切换条件:1,平行链没有共识过,2:commit高度是大于自共识分叉高度且上一次共识的主链高度小于自共识分叉高度,保证只运行一次,
// 这样在主链没有共识空洞前提下,平行链允许有条件的共识跳跃
func (a *action) isAllowParaConsensJump(commit *pt.ParacrossHeightStatus, titleStatus *pt.ParacrossStatus) (bool, error) {
if titleStatus.Height == -1 {
return true, nil
}
......@@ -705,7 +712,7 @@ func (a *action) isParaSelfConsensSwitch(commit *pt.ParacrossHeightStatus, title
if titleStatus.Height > -1 {
s, err := getTitleHeight(a.db, calcTitleHeightKey(commit.Title, titleStatus.Height))
if err != nil {
clog.Error("paracross.Commit isParaSelfConsensSwitch getTitleHeight failed", "err", err.Error())
clog.Error("paracross.Commit isAllowConsensJump getTitleHeight failed", "err", err.Error())
return false, err
}
lastStatusMainHeight = s.MainHeight
......@@ -715,6 +722,14 @@ func (a *action) isParaSelfConsensSwitch(commit *pt.ParacrossHeightStatus, title
}
func (a *action) isAllowConsensJump(commit *pt.ParacrossHeightStatus, titleStatus *pt.ParacrossStatus) (bool, error) {
if types.IsPara() {
return a.isAllowParaConsensJump(commit, titleStatus)
}
return a.isAllowMainConsensJump(commit, titleStatus)
}
func (a *action) execCrossTx(tx *types.TransactionDetail, crossTxHash []byte) (*types.Receipt, error) {
if !bytes.HasSuffix(tx.Tx.Execer, []byte(pt.ParaX)) {
return nil, 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