github.com/annchain/OG@v0.0.9/consensus/dkg/message.go (about)

     1  package dkg
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/annchain/OG/common/byteutil"
     6  	dkg "github.com/annchain/kyber/v3/share/dkg/pedersen"
     7  )
     8  
     9  //go:generate msgp
    10  
    11  //msgp:tuple DkgMessageType
    12  type DkgMessageType uint16
    13  
    14  const (
    15  	DkgMessageTypeDeal DkgMessageType = iota + 200
    16  	DkgMessageTypeDealResponse
    17  	DkgMessageTypeSigSets
    18  	DkgMessageTypeGenesisPublicKey
    19  )
    20  
    21  func (m DkgMessageType) String() string {
    22  	switch m {
    23  	case DkgMessageTypeDeal:
    24  		return "DkgMessageTypeDeal"
    25  	case DkgMessageTypeDealResponse:
    26  		return "DkgMessageTypeDealResponse"
    27  	case DkgMessageTypeSigSets:
    28  		return "DkgMessageTypeSigSets"
    29  	case DkgMessageTypeGenesisPublicKey:
    30  		return "DkgMessageTypeGenesisPublicKey"
    31  	default:
    32  		return "DkgUnknown"
    33  	}
    34  }
    35  
    36  type Signable interface {
    37  	SignatureTargets() []byte
    38  }
    39  
    40  type DkgMessage interface {
    41  	Signable
    42  	GetType() DkgMessageType
    43  	ProvideHeight() uint64
    44  	String() string
    45  }
    46  
    47  type DkgMessageEvent struct {
    48  	Message DkgMessage
    49  	Peer    DkgPeer
    50  }
    51  
    52  //msgp:tuple DkgBasicInfo
    53  type DkgBasicInfo struct {
    54  	TermId uint32
    55  	Height uint64
    56  	//PublicKey PublicKeyMarshallable
    57  }
    58  
    59  func (d *DkgBasicInfo) ProvideHeight() uint64 {
    60  	return d.Height
    61  }
    62  
    63  //msgp:tuple MessageDkgGenesisPublicKey
    64  type MessageDkgGenesisPublicKey struct {
    65  	DkgBasicInfo
    66  	PublicKeyBytes []byte
    67  	//PublicKey    []byte
    68  	//Signature    []byte
    69  }
    70  
    71  func (z *MessageDkgGenesisPublicKey) GetType() DkgMessageType {
    72  	return DkgMessageTypeGenesisPublicKey
    73  }
    74  
    75  func (z *MessageDkgGenesisPublicKey) String() string {
    76  	return "DkgMessageTypeGenesisPublicKey"
    77  }
    78  
    79  func (z *MessageDkgGenesisPublicKey) SignatureTargets() []byte {
    80  	return z.PublicKeyBytes
    81  }
    82  
    83  //msgp:tuple MessageDkgSigSets
    84  type MessageDkgSigSets struct {
    85  	DkgBasicInfo
    86  	PkBls []byte
    87  }
    88  
    89  func (z *MessageDkgSigSets) GetType() DkgMessageType {
    90  	return DkgMessageTypeSigSets
    91  }
    92  
    93  func (z *MessageDkgSigSets) String() string {
    94  	return "DkgMessageTypeSigSets"
    95  }
    96  
    97  func (m *MessageDkgSigSets) SignatureTargets() []byte {
    98  	w := byteutil.NewBinaryWriter()
    99  	w.Write(m.PkBls)
   100  	return w.Bytes()
   101  }
   102  
   103  //msgp:tuple MessageDkgDeal
   104  type MessageDkgDeal struct {
   105  	DkgBasicInfo
   106  	//Id   uint32
   107  	Data []byte
   108  }
   109  
   110  func (z *MessageDkgDeal) GetType() DkgMessageType {
   111  	return DkgMessageTypeDeal
   112  }
   113  
   114  func (m *MessageDkgDeal) GetDeal() (*dkg.Deal, error) {
   115  	var d dkg.Deal
   116  	_, err := d.UnmarshalMsg(m.Data)
   117  	return &d, err
   118  }
   119  
   120  func (m *MessageDkgDeal) SignatureTargets() []byte {
   121  	w := byteutil.NewBinaryWriter()
   122  	d := m.Data
   123  	//w.Write(d, m.Id)
   124  	w.Write(d)
   125  	return w.Bytes()
   126  }
   127  
   128  func (m MessageDkgDeal) String() string {
   129  	//var pkstr string
   130  	//if len(m.PublicKey) > 10 {
   131  	//	pkstr = hexutil.Encode(m.PublicKey[:5])
   132  	//}
   133  	return "dkg " + fmt.Sprintf("len %d  height %d", len(m.Data), m.Height) //  + " pk-" + pkstr
   134  }
   135  
   136  //msgp:tuple MessageDkgDealResponse
   137  type MessageDkgDealResponse struct {
   138  	DkgBasicInfo
   139  	//MyIndex   uint32
   140  	Data []byte
   141  	//PublicKey []byte
   142  	//Signature []byte
   143  	//SessionId uint64
   144  }
   145  
   146  func (m MessageDkgDealResponse) GetType() DkgMessageType {
   147  	return DkgMessageTypeDealResponse
   148  }
   149  
   150  func (m MessageDkgDealResponse) String() string {
   151  	//var pkstr string
   152  	//if len(m.PublicKey) > 10 {
   153  	//	pkstr = hexutil.Encode(m.PublicKey[:5])
   154  	//}
   155  	return "dkgresponse " //+ fmt.Sprintf(" pk %s , len %d  tid %d", hexutil.Encode(m.PublicKey), len(m.Data), m.TermId) // + " pk-" + pkstr
   156  }
   157  
   158  func (m *MessageDkgDealResponse) SignatureTargets() []byte {
   159  	d := m.Data
   160  	w := byteutil.NewBinaryWriter()
   161  	//w.Write(d, m.Id)
   162  	w.Write(d)
   163  	return w.Bytes()
   164  }
   165  
   166  func (m *MessageDkgDealResponse) GetResponse() (response *dkg.Response, err error) {
   167  	var d dkg.Response
   168  	_, err = d.UnmarshalMsg(m.Data)
   169  	return &d, err
   170  }