github.com/annchain/OG@v0.0.9/plugin/annsensus/adapter_annsensus.go (about)

     1  package annsensus
     2  
     3  import (
     4  	"errors"
     5  	"github.com/annchain/OG/consensus/annsensus"
     6  	"github.com/annchain/OG/message"
     7  )
     8  
     9  type AnnsensusMessageUnmarshaller struct {
    10  }
    11  
    12  func (a *AnnsensusMessageUnmarshaller) Unmarshal(
    13  	messageType annsensus.AnnsensusMessageType, message []byte) (
    14  	outMsg annsensus.AnnsensusMessage, err error) {
    15  	switch messageType {
    16  	case annsensus.AnnsensusMessageTypeBftPlain:
    17  		m := &annsensus.AnnsensusMessageBftPlain{}
    18  		_, err = m.UnmarshalMsg(message)
    19  		outMsg = m
    20  	case annsensus.AnnsensusMessageTypeBftSigned:
    21  		m := &annsensus.AnnsensusMessageBftSigned{}
    22  		_, err = m.UnmarshalMsg(message)
    23  		outMsg = m
    24  	case annsensus.AnnsensusMessageTypeBftEncrypted:
    25  		m := &annsensus.AnnsensusMessageBftEncrypted{}
    26  		_, err = m.UnmarshalMsg(message)
    27  		outMsg = m
    28  	case annsensus.AnnsensusMessageTypeDkgPlain:
    29  		m := &annsensus.AnnsensusMessageDkgPlain{}
    30  		_, err = m.UnmarshalMsg(message)
    31  		outMsg = m
    32  	case annsensus.AnnsensusMessageTypeDkgSigned:
    33  		m := &annsensus.AnnsensusMessageDkgSigned{}
    34  		_, err = m.UnmarshalMsg(message)
    35  		outMsg = m
    36  	case annsensus.AnnsensusMessageTypeDkgEncrypted:
    37  		m := &annsensus.AnnsensusMessageDkgEncrypted{}
    38  		_, err = m.UnmarshalMsg(message)
    39  		outMsg = m
    40  	default:
    41  		err = errors.New("message type of Annsensus not supported")
    42  	}
    43  
    44  	return
    45  }
    46  
    47  type DefaultAnnsensusMessageAdapter struct {
    48  	unmarshaller AnnsensusMessageUnmarshaller
    49  }
    50  
    51  func (d *DefaultAnnsensusMessageAdapter) AdaptGeneralMessage(incomingMsg message.GeneralMessage) (annMessage annsensus.AnnsensusMessage, err error) {
    52  	mssageType := incomingMsg.GetType()
    53  	if mssageType != MessageTypeAnnsensus {
    54  		err = errors.New("SimpleAnnsensusAdapter received a message of an unsupported type")
    55  		return
    56  	}
    57  	gma := incomingMsg.(*GeneralMessageAnnsensus)
    58  
    59  	return d.unmarshaller.Unmarshal(gma.InnerMessageType, gma.InnerMessage)
    60  }
    61  
    62  func (d *DefaultAnnsensusMessageAdapter) AdaptGeneralPeer(gnrPeer message.GeneralPeer) (annsensus.AnnsensusPeer, error) {
    63  	return annsensus.AnnsensusPeer{
    64  		Id:             gnrPeer.Id,
    65  		PublicKey:      gnrPeer.PublicKey,
    66  		Address:        gnrPeer.Address,
    67  		PublicKeyBytes: gnrPeer.PublicKeyBytes,
    68  	}, nil
    69  }
    70  
    71  func (d *DefaultAnnsensusMessageAdapter) AdaptAnnsensusMessage(outgoingMsg annsensus.AnnsensusMessage) (msg message.GeneralMessage, err error) {
    72  	msg = &GeneralMessageAnnsensus{
    73  		InnerMessageType: outgoingMsg.GetType(),
    74  		InnerMessage:     outgoingMsg.GetBytes(),
    75  	}
    76  	return
    77  }
    78  
    79  func (d *DefaultAnnsensusMessageAdapter) AdaptAnnsensusPeer(annPeer annsensus.AnnsensusPeer) (message.GeneralPeer, error) {
    80  	return message.GeneralPeer{
    81  		Id:             annPeer.Id,
    82  		PublicKey:      annPeer.PublicKey,
    83  		Address:        annPeer.Address,
    84  		PublicKeyBytes: annPeer.PublicKeyBytes,
    85  	}, nil
    86  }
    87