/
consensus.go
57 lines (44 loc) · 1.06 KB
/
consensus.go
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
package ibft
import (
"context"
"sync"
"github.com/0xPolygon/go-ibft/core"
)
// IBFTConsensus is a convenience wrapper for the go-ibft package
type IBFTConsensus struct {
*core.IBFT
wg sync.WaitGroup
cancelSequence context.CancelFunc
}
func newIBFT(
logger core.Logger,
backend core.Backend,
transport core.Transport,
) *IBFTConsensus {
return &IBFTConsensus{
IBFT: core.NewIBFT(logger, backend, transport),
wg: sync.WaitGroup{},
}
}
// runSequence starts the underlying consensus mechanism for the given height.
// It may be called by a single thread at any given time
func (c *IBFTConsensus) runSequence(height uint64) <-chan struct{} {
done := make(chan struct{})
ctx, cancel := context.WithCancel(context.Background())
c.cancelSequence = cancel
c.wg.Add(1)
go func() {
defer func() {
cancel()
c.wg.Done()
close(done)
}()
c.RunSequence(ctx, height)
}()
return done
}
// stopSequence terminates the running IBFT sequence gracefully and waits for it to return
func (c *IBFTConsensus) stopSequence() {
c.cancelSequence()
c.wg.Wait()
}