github.com/annchain/OG@v0.0.9/poc/tendermint/types.go (about)

     1  package tendermint
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  )
     7  
     8  type StepType int
     9  
    10  const (
    11  	StepTypePropose StepType = iota
    12  	StepTypePreVote
    13  	StepTypePreCommit
    14  )
    15  
    16  func (m StepType) String() string {
    17  	switch m {
    18  	case StepTypePropose:
    19  		return "Propose"
    20  	case StepTypePreVote:
    21  		return "PreVote"
    22  	case StepTypePreCommit:
    23  		return "PreCommit"
    24  	default:
    25  		return "Unknown"
    26  	}
    27  }
    28  
    29  func (m StepType) IsAfter(o StepType) bool {
    30  	return m > o
    31  }
    32  
    33  type MessageType int
    34  
    35  func (m MessageType) String() string {
    36  	switch m {
    37  	case MessageTypeProposal:
    38  		return "Proposal"
    39  	case MessageTypePreVote:
    40  		return "PreVote"
    41  	case MessageTypePreCommit:
    42  		return "PreCommit"
    43  	default:
    44  		return "Unknown"
    45  	}
    46  }
    47  
    48  const (
    49  	MessageTypeProposal MessageType = iota
    50  	MessageTypePreVote
    51  	MessageTypePreCommit
    52  )
    53  
    54  const (
    55  	TimeoutPropose   = time.Duration(10) * time.Second
    56  	TimeoutPreVote   = time.Duration(10) * time.Second
    57  	TimeoutPreCommit = time.Duration(10) * time.Second
    58  )
    59  
    60  type ValueIdMatchType int
    61  
    62  const (
    63  	MatchTypeAny ValueIdMatchType = iota
    64  	MatchTypeByValue
    65  	MatchTypeNil
    66  )
    67  
    68  type Message struct {
    69  	Type    MessageType
    70  	Payload interface{}
    71  }
    72  
    73  func (m *Message) String() string {
    74  	return fmt.Sprintf("%s %+v", m.Type.String(), m.Payload)
    75  }
    76  
    77  type Proposal interface {
    78  	Equal(Proposal) bool
    79  	GetId() string
    80  }
    81  
    82  type StringProposal string
    83  
    84  func (s StringProposal) Equal(o Proposal) bool {
    85  	v, ok := o.(StringProposal)
    86  	if !ok {
    87  		return false
    88  	}
    89  	return s == v
    90  }
    91  
    92  func (s StringProposal) GetId() string {
    93  	return string(s)
    94  }
    95  
    96  type BasicMessage struct {
    97  	SourceId    int
    98  	HeightRound HeightRound
    99  }
   100  type MessageProposal struct {
   101  	BasicMessage
   102  	Value      Proposal
   103  	ValidRound int
   104  }
   105  type MessageCommonVote struct {
   106  	BasicMessage
   107  	Idv string // ID of the proposal, usually be the hash of the proposal
   108  }
   109  
   110  type ChangeStateEvent struct {
   111  	NewStepType StepType
   112  	HeightRound HeightRound
   113  }
   114  
   115  type TendermintContext struct {
   116  	HeightRound HeightRound
   117  	StepType    StepType
   118  }
   119  
   120  func (t *TendermintContext) Equal(w WaiterContext) bool {
   121  	v, ok := w.(*TendermintContext)
   122  	if !ok {
   123  		return false
   124  	}
   125  	return t.HeightRound == v.HeightRound && t.StepType == v.StepType
   126  }
   127  
   128  func (t *TendermintContext) IsAfter(w WaiterContext) bool {
   129  	v, ok := w.(*TendermintContext)
   130  	if !ok {
   131  		return false
   132  	}
   133  	return t.HeightRound.IsAfter(v.HeightRound) || (t.HeightRound == v.HeightRound && t.StepType.IsAfter(v.StepType))
   134  }