Commit b35f406b authored by mdj33's avatar mdj33 Committed by vipwzw

remove float calculate

parent cedd7c81
...@@ -83,6 +83,7 @@ type subConfig struct { ...@@ -83,6 +83,7 @@ type subConfig struct {
MainBlockHashForkHeight int64 `json:"mainBlockHashForkHeight,omitempty"` MainBlockHashForkHeight int64 `json:"mainBlockHashForkHeight,omitempty"`
MainParaSelfConsensusForkHeight int64 `json:"mainParaSelfConsensusForkHeight,omitempty"` MainParaSelfConsensusForkHeight int64 `json:"mainParaSelfConsensusForkHeight,omitempty"`
MainForkParacrossCommitTx int64 `json:"mainForkParacrossCommitTx,omitempty"` MainForkParacrossCommitTx int64 `json:"mainForkParacrossCommitTx,omitempty"`
WaitConsensStopTimes uint32 `json:"waitConsensStopTimes,omitempty"`
} }
// New function to init paracross env // New function to init paracross env
...@@ -155,9 +156,16 @@ func New(cfg *types.Consensus, sub []byte) queue.Module { ...@@ -155,9 +156,16 @@ func New(cfg *types.Consensus, sub []byte) queue.Module {
} }
waitBlocks = subcfg.WaitBlocks4CommitMsg waitBlocks = subcfg.WaitBlocks4CommitMsg
} }
waitConsensTimes := uint32(30) //30*10s = 5min
if subcfg.WaitConsensStopTimes > 0 {
waitConsensTimes = subcfg.WaitConsensStopTimes
}
para.commitMsgClient = &commitMsgClient{ para.commitMsgClient = &commitMsgClient{
paraClient: para, paraClient: para,
waitMainBlocks: waitBlocks, waitMainBlocks: waitBlocks,
waitConsensStopTimes: waitConsensTimes,
commitMsgNotify: make(chan int64, 1), commitMsgNotify: make(chan int64, 1),
delMsgNotify: make(chan int64, 1), delMsgNotify: make(chan int64, 1),
mainBlockAdd: make(chan *types.BlockDetail, 1), mainBlockAdd: make(chan *types.BlockDetail, 1),
......
...@@ -27,6 +27,7 @@ var ( ...@@ -27,6 +27,7 @@ var (
type commitMsgClient struct { type commitMsgClient struct {
paraClient *client paraClient *client
waitMainBlocks int32 //等待平行链共识消息在主链上链并成功的块数,超出会重发共识消息,最小是2 waitMainBlocks int32 //等待平行链共识消息在主链上链并成功的块数,超出会重发共识消息,最小是2
waitConsensStopTimes uint32 //共识高度低于完成高度, reset高度重发等待的次数
commitMsgNotify chan int64 commitMsgNotify chan int64
delMsgNotify chan int64 delMsgNotify chan int64
mainBlockAdd chan *types.BlockDetail mainBlockAdd chan *types.BlockDetail
...@@ -47,12 +48,12 @@ func (client *commitMsgClient) handler() { ...@@ -47,12 +48,12 @@ func (client *commitMsgClient) handler() {
var isRollback bool var isRollback bool
var notification []int64 //记录每次系统重启后 min and current height var notification []int64 //记录每次系统重启后 min and current height
var finishHeight int64 = -1 var finishHeight int64 = -1
var consensHeight int64 = -1
var sendingHeight int64 //当前发送的最大高度 var sendingHeight int64 //当前发送的最大高度
var sendingMsgs []*pt.ParacrossNodeStatus var sendingMsgs []*pt.ParacrossNodeStatus
var readTick <-chan time.Time var readTick <-chan time.Time
var ticker *time.Ticker var ticker *time.Ticker
var lastAuthAccountIn bool var lastAuthAccountIn bool
var consensStopTimes uint32
client.paraClient.wg.Add(1) client.paraClient.wg.Add(1)
consensusCh := make(chan *commitConsensRsp, 1) consensusCh := make(chan *commitConsensRsp, 1)
...@@ -163,7 +164,7 @@ out: ...@@ -163,7 +164,7 @@ out:
//获取正在共识的高度,同步有两层意思,一个是主链跟其他节点完成了同步,另一个是当前平行链节点的高度追赶上了共识高度 //获取正在共识的高度,同步有两层意思,一个是主链跟其他节点完成了同步,另一个是当前平行链节点的高度追赶上了共识高度
//一般来说高度增长从小到大: notifiy[0] -- selfConsensusHeight(mainHeight) -- finishHeight -- sendingHeight -- notify[1] //一般来说高度增长从小到大: notifiy[0] -- selfConsensusHeight(mainHeight) -- finishHeight -- sendingHeight -- notify[1]
case rsp := <-consensusCh: case rsp := <-consensusCh:
consensHeight = rsp.status.Height consensHeight := rsp.status.Height
plog.Info("para consensus rcv", "notify", notification, "sending", len(sendingMsgs), plog.Info("para consensus rcv", "notify", notification, "sending", len(sendingMsgs),
"consensHeight", rsp.status.Height, "finishHeight", finishHeight, "authIn", rsp.authAccountIn, "sync", isSync, "miner", readTick != nil) "consensHeight", rsp.status.Height, "finishHeight", finishHeight, "authIn", rsp.authAccountIn, "sync", isSync, "miner", readTick != nil)
plog.Debug("para consensus rcv", "consensBlockHash", common.ToHex(rsp.status.BlockHash)) plog.Debug("para consensus rcv", "consensBlockHash", common.ToHex(rsp.status.BlockHash))
...@@ -186,15 +187,23 @@ out: ...@@ -186,15 +187,23 @@ out:
// 共识高度追赶上完成高度之后再发,不然继续发浪费手续费 // 共识高度追赶上完成高度之后再发,不然继续发浪费手续费
if finishHeight > consensHeight { if finishHeight > consensHeight {
if consensStopTimes < client.waitConsensStopTimes {
isSync = false isSync = false
consensStopTimes++
continue
}
//reset finishHeight to consensHeight and resent
finishHeight = consensHeight
} }
//未共识过的小于当前共识高度的区块,可以不参与共识, 如果是新节点,一直等到同步的区块达到了共识高度,才设置同步参与共识 //未共识过的小于当前共识高度的区块,可以不参与共识, 如果是新节点,一直等到同步的区块达到了共识高度,才设置同步参与共识
//在某些特殊场景下,比如平行链连接的主链节点分叉后又恢复,主链的共识高度低于分叉高度时候,主链上形成共识空洞,需要从共识高度重新发送而不是分叉高度 //在某些特殊场景下,比如平行链连接的主链节点分叉后又恢复,主链的共识高度低于分叉高度时候,主链上形成共识空洞,需要从共识高度重新发送而不是分叉高度
//共识高度和分叉高度不一致其中一个原因是共识交易组里面某个高度分叉了,分叉的主链节点执行成功,而其他主链节点执行失败,共识高度停留在交易组最小高度-1 //共识高度和分叉高度不一致其中一个原因是共识交易组里面某个高度分叉了,分叉的主链节点执行成功,而其他主链节点执行失败,共识高度停留在交易组最小高度-1
//而分叉高度是交易组里面的某个高度 //而分叉高度是交易组里面的某个高度
if finishHeight < consensHeight { if finishHeight <= consensHeight {
finishHeight = consensHeight finishHeight = consensHeight
consensStopTimes = 0
} }
//系统每次重启都有检查一次共识,如果共识高度落后于系统起来后完成的第一个高度或最小高度,说明可能有共识空洞,需要重发 //系统每次重启都有检查一次共识,如果共识高度落后于系统起来后完成的第一个高度或最小高度,说明可能有共识空洞,需要重发
...@@ -222,8 +231,6 @@ out: ...@@ -222,8 +231,6 @@ out:
readTick = ticker.C readTick = ticker.C
plog.Info("para consensus start mining") plog.Info("para consensus start mining")
//钱包开启后,从共识高度重新开始发送,在需要重发共识时候,不需要重启设备
finishHeight = consensHeight
} }
case <-client.quit: case <-client.quit:
......
...@@ -118,8 +118,9 @@ func checkCommitInfo(commit *pt.ParacrossCommitAction) error { ...@@ -118,8 +118,9 @@ func checkCommitInfo(commit *pt.ParacrossCommitAction) error {
return nil return nil
} }
//区块链中不能使用float类型
func isCommitDone(nodes map[string]struct{}, mostSame int) bool { func isCommitDone(nodes map[string]struct{}, mostSame int) bool {
return float32(mostSame) > float32(len(nodes))*float32(2)/float32(3) return 3*mostSame > 2*len(nodes)
} }
func makeCommitReceipt(addr string, commit *pt.ParacrossCommitAction, prev, current *pt.ParacrossHeightStatus) *types.Receipt { func makeCommitReceipt(addr string, commit *pt.ParacrossCommitAction, prev, current *pt.ParacrossHeightStatus) *types.Receipt {
...@@ -273,6 +274,7 @@ func getDappForkHeight(forkKey string) int64 { ...@@ -273,6 +274,7 @@ func getDappForkHeight(forkKey string) int64 {
} else { } else {
forkHeight = types.GetDappFork(pt.ParaX, forkKey) forkHeight = types.GetDappFork(pt.ParaX, forkKey)
// CI特殊处理,主链是local,fork都是0,平行链有些配置项需要设置为非0,不然获取到的高度为MaxHeight
if types.IsLocal() { if types.IsLocal() {
switch forkKey { switch forkKey {
case pt.ForkCommitTx: case pt.ForkCommitTx:
...@@ -696,7 +698,7 @@ func (a *action) commitTxDoneByStat(stat *pt.ParacrossHeightStatus, titleStatus ...@@ -696,7 +698,7 @@ func (a *action) commitTxDoneByStat(stat *pt.ParacrossHeightStatus, titleStatus
//平行链自共识无缝切换条件:1,平行链没有共识过,2:commit高度是大于自共识分叉高度且上一次共识的主链高度小于自共识分叉高度,保证只运行一次, //平行链自共识无缝切换条件:1,平行链没有共识过,2:commit高度是大于自共识分叉高度且上一次共识的主链高度小于自共识分叉高度,保证只运行一次,
// 这样在主链没有共识空洞前提下,平行链允许有条件的共识跳跃 // 这样在主链没有共识空洞前提下,平行链允许有条件的共识跳跃
func (a *action) isParaSelfConsensSwitch(stat *pt.ParacrossHeightStatus, titleStatus *pt.ParacrossStatus) (bool, error) { func (a *action) isParaSelfConsensSwitch(commit *pt.ParacrossHeightStatus, titleStatus *pt.ParacrossStatus) (bool, error) {
if !types.IsPara() { if !types.IsPara() {
return false, nil return false, nil
} }
...@@ -708,7 +710,7 @@ func (a *action) isParaSelfConsensSwitch(stat *pt.ParacrossHeightStatus, titleSt ...@@ -708,7 +710,7 @@ func (a *action) isParaSelfConsensSwitch(stat *pt.ParacrossHeightStatus, titleSt
selfConsensForkHeight := getDappForkHeight(pt.ParaSelfConsensForkHeight) selfConsensForkHeight := getDappForkHeight(pt.ParaSelfConsensForkHeight)
lastStatusMainHeight := int64(-1) lastStatusMainHeight := int64(-1)
if titleStatus.Height > -1 { if titleStatus.Height > -1 {
s, err := getTitleHeight(a.db, calcTitleHeightKey(stat.Title, titleStatus.Height)) s, err := getTitleHeight(a.db, calcTitleHeightKey(commit.Title, titleStatus.Height))
if err != nil { if err != nil {
clog.Error("paracross.Commit isParaSelfConsensSwitch getTitleHeight failed", "err", err.Error()) clog.Error("paracross.Commit isParaSelfConsensSwitch getTitleHeight failed", "err", err.Error())
return false, err return false, err
...@@ -716,7 +718,7 @@ func (a *action) isParaSelfConsensSwitch(stat *pt.ParacrossHeightStatus, titleSt ...@@ -716,7 +718,7 @@ func (a *action) isParaSelfConsensSwitch(stat *pt.ParacrossHeightStatus, titleSt
lastStatusMainHeight = s.MainHeight lastStatusMainHeight = s.MainHeight
} }
return stat.MainHeight > selfConsensForkHeight && lastStatusMainHeight < selfConsensForkHeight, nil return commit.MainHeight > selfConsensForkHeight && lastStatusMainHeight < selfConsensForkHeight, 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