github.com/annchain/OG@v0.0.9/consensus/annsensus/term.go (about)

     1  package annsensus
     2  
     3  import (
     4  	"github.com/annchain/OG/consensus/bft"
     5  	"github.com/annchain/OG/consensus/dkg"
     6  	"sync"
     7  )
     8  
     9  type HeightInfoCarrier interface {
    10  	ProvideHeight() uint64
    11  }
    12  
    13  type TermCollection struct {
    14  	contextProvider ConsensusContext
    15  	BftPartner      bft.BftPartner
    16  	DkgPartner      dkg.DkgPartner
    17  	quit            chan bool
    18  	quitWg          sync.WaitGroup
    19  }
    20  
    21  func NewTermCollection(
    22  	contextProvider ConsensusContext,
    23  	bftPartner bft.BftPartner, dkgPartner dkg.DkgPartner) *TermCollection {
    24  	return &TermCollection{
    25  		contextProvider: contextProvider,
    26  		BftPartner:      bftPartner,
    27  		DkgPartner:      dkgPartner,
    28  		quit:            make(chan bool),
    29  		quitWg:          sync.WaitGroup{},
    30  	}
    31  }
    32  
    33  func (tc *TermCollection) Start() {
    34  	// start all operators for this term.
    35  	tc.quitWg.Add(1)
    36  loop:
    37  	for {
    38  		select {
    39  		case <-tc.quit:
    40  			tc.BftPartner.Stop()
    41  			tc.DkgPartner.Stop()
    42  			tc.quitWg.Done()
    43  			break loop
    44  		}
    45  	}
    46  }
    47  
    48  func (tc *TermCollection) Stop() {
    49  	close(tc.quit)
    50  	tc.quitWg.Wait()
    51  }
    52  
    53  
    54  type AnnsensusTermHolder struct {
    55  	// in case of disordered message, cache the terms and the correspondent processors.
    56  	// TODO: wipe it constantly
    57  	termMap      map[uint32]*TermCollection
    58  	termProvider TermIdProvider
    59  	mu           sync.RWMutex
    60  	debugMyId    int
    61  }
    62  
    63  func (b *AnnsensusTermHolder) GetTermById(termId uint32) (msgTerm *TermCollection, ok bool) {
    64  	msgTerm, ok = b.termMap[termId]
    65  	return
    66  }
    67  
    68  func (b *AnnsensusTermHolder) SetTerm(termId uint32, termCollection *TermCollection) {
    69  	b.mu.Lock()
    70  	defer b.mu.Unlock()
    71  	b.termMap[termId] = termCollection
    72  	b.debugMyId = termCollection.contextProvider.GetMyBftId()
    73  }
    74  
    75  func NewAnnsensusTermHolder(termProvider TermIdProvider) *AnnsensusTermHolder {
    76  	return &AnnsensusTermHolder{
    77  		termProvider: termProvider,
    78  		termMap:      make(map[uint32]*TermCollection),
    79  	}
    80  }
    81  
    82  func (b *AnnsensusTermHolder) GetTermByHeight(heightInfoCarrier HeightInfoCarrier) (msgTerm *TermCollection, ok bool) {
    83  	height := heightInfoCarrier.ProvideHeight()
    84  	// judge term
    85  	termId := b.termProvider.HeightTerm(height)
    86  
    87  	b.mu.RLock()
    88  	defer b.mu.RUnlock()
    89  	msgTerm, ok = b.termMap[termId]
    90  	return
    91  }
    92  
    93  func (b *AnnsensusTermHolder) DebugMyId() int {
    94  	return b.debugMyId
    95  }