-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathtimer.go
More file actions
75 lines (57 loc) · 2.02 KB
/
timer.go
File metadata and controls
75 lines (57 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2015 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package process
import (
"github.com/FactomProject/btcd/wire"
"time"
)
// BlockTimer is set to sent End-Of-Minute messages to processor
type BlockTimer struct {
nextDBlockHeight uint32
inCtlMsgQueue chan wire.FtmInternalMsg //incoming message queue for factom control messages
}
// Send End-Of-Minute messages to processor for the current open directory block
func (bt *BlockTimer) StartBlockTimer() {
//wait till the end of minute
//the first minute section might be bigger than others. To be improved.
/* t := time.Now()
time.Sleep(time.Duration((60 - t.Second()) * 1000000000))
*/
if directoryBlockInSeconds < 600 {
sleeptime := directoryBlockInSeconds / 10
// Set the start time for the open dir block
dchain.NextBlock.Header.Timestamp = uint32(time.Now().Round(time.Minute).Unix() / 60)
for i := 0; i < 10; i++ {
eomMsg := &wire.MsgInt_EOM{
EOM_Type: wire.END_MINUTE_1 + byte(i),
NextDBlockHeight: bt.nextDBlockHeight,
}
//send the end-of-minute message to processor
bt.inCtlMsgQueue <- eomMsg
time.Sleep(time.Duration(sleeptime * 1000000000))
}
return
}
roundTime := time.Now().Round(time.Minute)
minutesPassed := roundTime.Minute() - (roundTime.Minute()/10)*10
// Set the start time for the open dir block
dchain.NextBlock.Header.Timestamp = uint32(roundTime.Add(time.Duration((0-60*minutesPassed)*1000000000)).Unix() / 60)
for minutesPassed < 10 {
// Sleep till the end of minute
t0 := time.Now()
t0_round := t0.Round(time.Minute)
if t0.Before(t0_round) {
time.Sleep(time.Duration((60 + t0.Second()) * 1000000000))
} else {
time.Sleep(time.Duration((60 - t0.Second()) * 1000000000))
}
eomMsg := &wire.MsgInt_EOM{
EOM_Type: wire.END_MINUTE_1 + byte(minutesPassed),
NextDBlockHeight: bt.nextDBlockHeight,
}
//send the end-of-minute message to processor
bt.inCtlMsgQueue <- eomMsg
minutesPassed++
}
}