| 267 | } |
| 268 | |
| 269 | func (ds *DbftService) InitializeConsensus(viewNum byte) error { |
| 270 | log.Debug("[InitializeConsensus] Start InitializeConsensus.") |
| 271 | log.Debug("[InitializeConsensus] viewNum: ", viewNum) |
| 272 | |
| 273 | if viewNum == 0 { |
| 274 | ds.context.Reset(ds.Account) |
| 275 | } else { |
| 276 | if ds.context.State.HasFlag(BlockGenerated) { |
| 277 | return nil |
| 278 | } |
| 279 | ds.context.ChangeView(viewNum) |
| 280 | } |
| 281 | |
| 282 | if ds.context.BookkeeperIndex < 0 { |
| 283 | log.Info("You aren't bookkeeper") |
| 284 | return nil |
| 285 | } |
| 286 | |
| 287 | if ds.context.BookkeeperIndex == int(ds.context.PrimaryIndex) { |
| 288 | |
| 289 | //primary peer |
| 290 | ds.context.State |= Primary |
| 291 | ds.timerHeight = ds.context.Height |
| 292 | ds.timeView = viewNum |
| 293 | span := time.Now().Sub(ds.blockReceivedTime) |
| 294 | if span > genesis.GenBlockTime { |
| 295 | //TODO: double check the is the stop necessary |
| 296 | ds.timer.Stop() |
| 297 | ds.timer.Reset(0) |
| 298 | //go ds.Timeout() |
| 299 | } else { |
| 300 | ds.timer.Stop() |
| 301 | ds.timer.Reset(genesis.GenBlockTime - span) |
| 302 | } |
| 303 | } else { |
| 304 | //backup peer |
| 305 | ds.context.State = Backup |
| 306 | ds.timerHeight = ds.context.Height |
| 307 | ds.timeView = viewNum |
| 308 | |
| 309 | ds.timer.Stop() |
| 310 | ds.timer.Reset(genesis.GenBlockTime << (viewNum + 1)) |
| 311 | } |
| 312 | return nil |
| 313 | } |
| 314 | |
| 315 | func (ds *DbftService) LocalNodeNewInventory(v interface{}) { |
| 316 | log.Debug() |