github.com/annchain/OG@v0.0.9/consensus_interface/model.go (about)

     1  package consensus_interface
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/annchain/commongo/hexutil"
     6  	"strconv"
     7  )
     8  
     9  //go:generate msgp
    10  
    11  type HotStuffMessageType int
    12  
    13  const HotStuffMessageTypeRoot = 100
    14  
    15  const (
    16  	HotStuffMessageTypeProposal HotStuffMessageType = iota + 100
    17  	HotStuffMessageTypeVote
    18  	HotStuffMessageTypeTimeout
    19  	HotStuffMessageTypeString
    20  )
    21  
    22  func (m HotStuffMessageType) String() string {
    23  	switch m {
    24  	case HotStuffMessageTypeProposal:
    25  		return "HotStuffProposal"
    26  	case HotStuffMessageTypeVote:
    27  		return "HotStuffVote"
    28  	case HotStuffMessageTypeTimeout:
    29  		return "HotStuffTimeout"
    30  	//case LocalTimeout:
    31  	//	return "LocalTimeout"
    32  	case HotStuffMessageTypeString:
    33  		return "HotStuffString"
    34  	default:
    35  		return "Unknown Message " + strconv.Itoa(int(m))
    36  	}
    37  }
    38  
    39  //msgp Block
    40  type Block struct {
    41  	Round    int64  // the round that generated this proposal
    42  	Payload  string // proposed transactions
    43  	ParentQC *QC    // qc for parent block
    44  	Id       string // unique digest of round, payload and parent_qc.id
    45  }
    46  
    47  func (b Block) String() string {
    48  	return fmt.Sprintf("[Block round=%d payload=%s parentQC=%s id=%s]", b.Round, b.Payload, b.ParentQC, b.Id)
    49  }
    50  
    51  // HotStuffSignedMessage is for transportation.
    52  //msgp HotStuffSignedMessage
    53  type HotStuffSignedMessage struct {
    54  	HotStuffMessageType int    // what ContentByte is (one of HotStuffMessageType).
    55  	ContentBytes        []byte // this Byte will be recovered to implementation of Content interface
    56  	SenderMemberId      string // member id of the sender
    57  	Signature           []byte
    58  }
    59  
    60  func (z *HotStuffSignedMessage) ToBytes() []byte {
    61  	b, err := z.MarshalMsg(nil)
    62  	if err != nil {
    63  		panic(err)
    64  	}
    65  	return b
    66  }
    67  
    68  func (z *HotStuffSignedMessage) FromBytes(bts []byte) error {
    69  	_, err := z.UnmarshalMsg(bts)
    70  	if err != nil {
    71  		return err
    72  	}
    73  	return nil
    74  }
    75  
    76  func (z *HotStuffSignedMessage) GetTypeValue() int {
    77  	return HotStuffMessageTypeRoot
    78  }
    79  
    80  func (z *HotStuffSignedMessage) String() string {
    81  	return fmt.Sprintf("WM: Type=%s SenderMemberId=%s ContentBytes=%s",
    82  		HotStuffMessageType(z.HotStuffMessageType).String(),
    83  		z.SenderMemberId,
    84  		hexutil.ToBriefHex(z.ContentBytes, 100))
    85  }
    86  
    87  //msgp Signature
    88  type Signature []byte
    89  
    90  //msgp JointSignature
    91  type JointSignature []byte
    92  
    93  //msgp ContentString
    94  // it is a dummy content type for debugging
    95  type ContentString struct {
    96  	Content string
    97  }
    98  
    99  func (z *ContentString) ToBytes() []byte {
   100  	b, err := z.MarshalMsg(nil)
   101  	if err != nil {
   102  		panic(err)
   103  	}
   104  	return b
   105  }
   106  
   107  func (z *ContentString) FromBytes(bts []byte) error {
   108  	_, err := z.UnmarshalMsg(bts)
   109  	if err != nil {
   110  		return err
   111  	}
   112  	return nil
   113  }
   114  
   115  func (z *ContentString) String() string {
   116  	return z.Content
   117  }
   118  
   119  func (z *ContentString) SignatureTarget() []byte {
   120  	return z.ToBytes()
   121  }
   122  
   123  //msgp ContentProposal
   124  type ContentProposal struct {
   125  	Proposal Block
   126  	TC       *TC
   127  }
   128  
   129  func (z *ContentProposal) ToBytes() []byte {
   130  	b, err := z.MarshalMsg(nil)
   131  	if err != nil {
   132  		panic(err)
   133  	}
   134  	return b
   135  }
   136  
   137  func (z *ContentProposal) FromBytes(bts []byte) error {
   138  	_, err := z.UnmarshalMsg(bts)
   139  	if err != nil {
   140  		return err
   141  	}
   142  	return nil
   143  }
   144  
   145  func (z *ContentProposal) String() string {
   146  	return fmt.Sprintf("[CProposal: p=%s tc=%s]", z.Proposal, z.TC)
   147  }
   148  
   149  func (z *ContentProposal) SignatureTarget() []byte {
   150  	return z.ToBytes()
   151  }
   152  
   153  //msgp ContentTimeout
   154  type ContentTimeout struct {
   155  	Round  int64
   156  	HighQC *QC
   157  	TC     *TC
   158  }
   159  
   160  func (z *ContentTimeout) SignatureTarget() []byte {
   161  	return z.ToBytes()
   162  }
   163  
   164  func (z *ContentTimeout) ToBytes() []byte {
   165  	b, err := z.MarshalMsg(nil)
   166  	if err != nil {
   167  		panic(err)
   168  	}
   169  	return b
   170  }
   171  
   172  func (z *ContentTimeout) FromBytes(bts []byte) error {
   173  	_, err := z.UnmarshalMsg(bts)
   174  	if err != nil {
   175  		return err
   176  	}
   177  	return nil
   178  }
   179  
   180  func (z *ContentTimeout) String() string {
   181  	return fmt.Sprintf("[CTimeout: round=%d highQC=%s tc=%s]", z.Round, z.HighQC, z.TC)
   182  }
   183  
   184  //msgp ContentVote
   185  type ContentVote struct {
   186  	VoteInfo         VoteInfo
   187  	LedgerCommitInfo LedgerCommitInfo
   188  	QC               *QC
   189  	TC               *TC
   190  }
   191  
   192  func (z *ContentVote) ToBytes() []byte {
   193  	b, err := z.MarshalMsg(nil)
   194  	if err != nil {
   195  		panic(err)
   196  	}
   197  	return b
   198  }
   199  
   200  func (z *ContentVote) FromBytes(bts []byte) error {
   201  	_, err := z.UnmarshalMsg(bts)
   202  	if err != nil {
   203  		return err
   204  	}
   205  	return nil
   206  }
   207  
   208  func (z *ContentVote) String() string {
   209  	return fmt.Sprintf("[CVote: voteinfo=%s ledgercinfo=%s qc=%s tc=%s]", z.VoteInfo, z.LedgerCommitInfo, z.QC, z.TC)
   210  }
   211  
   212  func (z *ContentVote) SignatureTarget() []byte {
   213  	return z.ToBytes()
   214  }
   215  
   216  //msgp QC
   217  type QC struct {
   218  	VoteData       VoteInfo
   219  	JointSignature JointSignature
   220  }
   221  
   222  func (q *QC) String() string {
   223  	if q == nil {
   224  		return "nil"
   225  	}
   226  	return fmt.Sprintf("[QC: voteInfo=%s sigs=%d]", q.VoteData, len(q.JointSignature))
   227  }
   228  
   229  //msgp TC
   230  type TC struct {
   231  	Round          int64 // round of the block
   232  	JointSignature JointSignature
   233  }
   234  
   235  func (t *TC) String() string {
   236  	if t == nil {
   237  		return "nil"
   238  	}
   239  	return fmt.Sprintf("[TC: round=%d, sigs=%d]", t.Round, len(t.JointSignature))
   240  }
   241  
   242  //msgp VoteInfo
   243  type VoteInfo struct {
   244  	Id               string // Id of the block
   245  	Round            int64  // round of the block
   246  	ParentId         string // Id of the parent
   247  	ParentRound      int64  // round of the parent
   248  	GrandParentId    string // Id of the grandParent
   249  	GrandParentRound int64  // round of the grandParent
   250  	ExecStateId      string // speculated execution state
   251  }
   252  
   253  func (i VoteInfo) String() string {
   254  	return fmt.Sprintf("<%s %d> <%s %d> <%s %d> %s", i.Id, i.Round, i.ParentId, i.ParentRound, i.GrandParentId, i.GrandParentRound, i.ExecStateId)
   255  }
   256  
   257  func (i VoteInfo) GetHashContent() string {
   258  	return fmt.Sprintf("%s %d %s %d %s %d %s", i.Id, i.Round, i.ParentId, i.ParentRound, i.GrandParentId, i.GrandParentRound, i.ExecStateId)
   259  }
   260  
   261  //msgp LedgerCommitInfo
   262  type LedgerCommitInfo struct {
   263  	CommitStateId string // nil if no commit happens when this vote is aggregated to QC. Usually the merkle root
   264  	VoteInfoHash  string // hash of VoteMsg.voteInfo
   265  }
   266  
   267  func (l LedgerCommitInfo) String() string {
   268  	return fmt.Sprintf("[LedgerCommitInfo: commitStateId %s VoteInfoHash %s]", l.CommitStateId, l.VoteInfoHash)
   269  }
   270  
   271  func (l LedgerCommitInfo) GetHashContent() string {
   272  	return fmt.Sprintf("%s %s", l.CommitStateId, l.VoteInfoHash)
   273  }