github.com/annchain/OG@v0.0.9/consensus/annsensus/adapter_dkg.go (about) 1 package annsensus 2 3 import ( 4 "errors" 5 "github.com/annchain/OG/consensus/dkg" 6 "github.com/annchain/OG/og/account" 7 ) 8 9 type ProxyDkgPeerCommunicator struct { 10 dkgMessageAdapter DkgMessageAdapter 11 annsensusOutgoing AnnsensusPeerCommunicatorOutgoing 12 pipe chan *dkg.DkgMessageEvent 13 } 14 15 func NewProxyDkgPeerCommunicator( 16 dkgMessageAdapter DkgMessageAdapter, 17 annsensusCommunicator AnnsensusPeerCommunicatorOutgoing) *ProxyDkgPeerCommunicator { 18 return &ProxyDkgPeerCommunicator{ 19 dkgMessageAdapter: dkgMessageAdapter, 20 annsensusOutgoing: annsensusCommunicator, 21 pipe: make(chan *dkg.DkgMessageEvent), 22 } 23 } 24 25 func (p *ProxyDkgPeerCommunicator) Broadcast(msg dkg.DkgMessage, peers []dkg.DkgPeer) { 26 annsensusMessage, err := p.dkgMessageAdapter.AdaptDkgMessage(msg) 27 if err != nil { 28 panic("adapt should never fail") 29 } 30 // adapt the interface so that the request can be handled by annsensus 31 annsensusPeers := make([]AnnsensusPeer, len(peers)) 32 for i, peer := range peers { 33 adaptedValue, err := p.dkgMessageAdapter.AdaptDkgPeer(peer) 34 if err != nil { 35 panic("adapt should never fail") 36 } 37 annsensusPeers[i] = adaptedValue 38 } 39 40 p.annsensusOutgoing.Broadcast(annsensusMessage, annsensusPeers) 41 } 42 43 func (p *ProxyDkgPeerCommunicator) Unicast(msg dkg.DkgMessage, peer dkg.DkgPeer) { 44 // adapt the interface so that the request can be handled by annsensus 45 annsensusMessage, err := p.dkgMessageAdapter.AdaptDkgMessage(msg) 46 if err != nil { 47 panic("adapt should never fail") 48 } 49 annsensusPeer, err := p.dkgMessageAdapter.AdaptDkgPeer(peer) 50 if err != nil { 51 panic("adapt should never fail") 52 } 53 p.annsensusOutgoing.Unicast(annsensusMessage, annsensusPeer) 54 } 55 56 func (p *ProxyDkgPeerCommunicator) GetPipeOut() chan *dkg.DkgMessageEvent { 57 // the channel to be consumed by the downstream. 58 return p.pipe 59 } 60 61 func (p *ProxyDkgPeerCommunicator) GetPipeIn() chan *dkg.DkgMessageEvent { 62 // the channel to be fed by other peers 63 return p.pipe 64 } 65 66 func (p *ProxyDkgPeerCommunicator) Run() { 67 // nothing to do 68 return 69 } 70 71 72 type DkgMessageUnmarshaller struct { 73 } 74 75 func (b DkgMessageUnmarshaller) Unmarshal(messageType dkg.DkgMessageType, message []byte) (outMsg dkg.DkgMessage, err error) { 76 switch messageType { 77 case dkg.DkgMessageTypeDeal: 78 m := &dkg.MessageDkgDeal{} 79 _, err = m.UnmarshalMsg(message) 80 outMsg = m 81 case dkg.DkgMessageTypeDealResponse: 82 m := &dkg.MessageDkgDealResponse{} 83 _, err = m.UnmarshalMsg(message) 84 outMsg = m 85 case dkg.DkgMessageTypeGenesisPublicKey: 86 m := &dkg.MessageDkgGenesisPublicKey{} 87 _, err = m.UnmarshalMsg(message) 88 outMsg = m 89 case dkg.DkgMessageTypeSigSets: 90 m := &dkg.MessageDkgSigSets{} 91 _, err = m.UnmarshalMsg(message) 92 outMsg = m 93 default: 94 err = errors.New("message type of Dkg not supported") 95 } 96 return 97 } 98 99 // TrustfulDkgAdapter signs and validate messages using pubkey/privkey given by DKG/BLS 100 type TrustfulDkgAdapter struct { 101 signatureProvider account.SignatureProvider 102 termProvider TermIdProvider 103 dkgMessageUnmarshaller *DkgMessageUnmarshaller 104 } 105 106 func (r *TrustfulDkgAdapter) AdaptDkgMessage(outgoingMsg dkg.DkgMessage) (msg AnnsensusMessage, err error) { 107 signed := r.Sign(outgoingMsg) 108 msg = &signed 109 return 110 } 111 112 func (r *TrustfulDkgAdapter) Sign(message dkg.DkgMessage) AnnsensusMessageDkgSigned { 113 publicKey, signature := r.signatureProvider.Sign(message.SignatureTargets()) 114 signed := AnnsensusMessageDkgSigned{ 115 InnerMessageType: uint16(message.GetType()), 116 InnerMessage: message.SignatureTargets(), 117 Signature: signature, 118 PublicKey: publicKey, 119 } 120 //SessionId: partner.CurrentTerm(), 121 //PublicKey: account.PublicKey.KeyBytes, 122 return signed 123 } 124 125 func NewTrustfulDkgAdapter() *TrustfulDkgAdapter { 126 return &TrustfulDkgAdapter{} 127 } 128 129 func (b *TrustfulDkgAdapter) AdaptAnnsensusMessage(incomingMsg AnnsensusMessage) (msg dkg.DkgMessage, err error) { // Only allows SignedOgPartnerMessage 130 panic("not implemented yet") 131 } 132 133 type PlainDkgAdapter struct { 134 DkgMessageUnmarshaller *DkgMessageUnmarshaller 135 } 136 137 func (p PlainDkgAdapter) AdaptAnnsensusPeer(annPeer AnnsensusPeer) (dkg.DkgPeer, error) { 138 return dkg.DkgPeer{ 139 Id: annPeer.Id, 140 PublicKey: annPeer.PublicKey, 141 Address: annPeer.Address, 142 PublicKeyBytes: annPeer.PublicKeyBytes, 143 }, nil 144 } 145 146 func (p PlainDkgAdapter) AdaptDkgPeer(dkgPeer dkg.DkgPeer) (AnnsensusPeer, error) { 147 return AnnsensusPeer{ 148 Id: dkgPeer.Id, 149 PublicKey: dkgPeer.PublicKey, 150 Address: dkgPeer.Address, 151 PublicKeyBytes: dkgPeer.PublicKeyBytes, 152 }, nil 153 } 154 155 func (p PlainDkgAdapter) AdaptAnnsensusMessage(incomingMsg AnnsensusMessage) (msg dkg.DkgMessage, err error) { 156 if incomingMsg.GetType() != AnnsensusMessageTypeDkgSigned { 157 err = errors.New("PlainDkgAdapter received a message of an unsupported type") 158 return 159 } 160 iMsg := incomingMsg.(*AnnsensusMessageDkgSigned) 161 innerMessageType := dkg.DkgMessageType(iMsg.InnerMessageType) 162 163 switch innerMessageType { 164 case dkg.DkgMessageTypeDeal: 165 fallthrough 166 case dkg.DkgMessageTypeDealResponse: 167 fallthrough 168 case dkg.DkgMessageTypeSigSets: 169 fallthrough 170 case dkg.DkgMessageTypeGenesisPublicKey: 171 msg, err = p.DkgMessageUnmarshaller.Unmarshal(innerMessageType, iMsg.InnerMessage) 172 default: 173 err = errors.New("PlainDkgAdapter received a message of an unsupported inner type") 174 } 175 return 176 } 177 178 func (p PlainDkgAdapter) AdaptDkgMessage(outgoingMsg dkg.DkgMessage) (adaptedMessage AnnsensusMessage, err error) { 179 var msgBytes []byte 180 switch outgoingMsg.GetType() { 181 case dkg.DkgMessageTypeDeal: 182 omsg := outgoingMsg.(*dkg.MessageDkgDeal) 183 msgBytes, err = omsg.MarshalMsg(nil) 184 if err != nil { 185 return 186 } 187 adaptedMessage = &AnnsensusMessageDkgPlain{ 188 InnerMessageType: uint16(omsg.GetType()), 189 InnerMessage: msgBytes, 190 } 191 case dkg.DkgMessageTypeDealResponse: 192 omsg := outgoingMsg.(*dkg.MessageDkgDealResponse) 193 msgBytes, err = omsg.MarshalMsg(nil) 194 if err != nil { 195 return 196 } 197 adaptedMessage = &AnnsensusMessageDkgPlain{ 198 InnerMessageType: uint16(omsg.GetType()), 199 InnerMessage: msgBytes, 200 } 201 case dkg.DkgMessageTypeGenesisPublicKey: 202 omsg := outgoingMsg.(*dkg.MessageDkgGenesisPublicKey) 203 msgBytes, err = omsg.MarshalMsg(nil) 204 if err != nil { 205 return 206 } 207 adaptedMessage = &AnnsensusMessageDkgPlain{ 208 InnerMessageType: uint16(omsg.GetType()), 209 InnerMessage: msgBytes, 210 } 211 case dkg.DkgMessageTypeSigSets: 212 omsg := outgoingMsg.(*dkg.MessageDkgSigSets) 213 msgBytes, err = omsg.MarshalMsg(nil) 214 if err != nil { 215 return 216 } 217 adaptedMessage = &AnnsensusMessageDkgPlain{ 218 InnerMessageType: uint16(omsg.GetType()), 219 InnerMessage: msgBytes, 220 } 221 default: 222 err = errors.New("PlainDkgAdapter received a message of an unsupported type") 223 } 224 return 225 }