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

     1  package og
     2  
     3  import (
     4  	general_communication "github.com/annchain/OG/communication"
     5  	"github.com/annchain/OG/debug/debuglog"
     6  	"github.com/annchain/OG/eventbus"
     7  	general_message "github.com/annchain/OG/message"
     8  	"github.com/annchain/OG/ogcore"
     9  	"github.com/annchain/OG/ogcore/communication"
    10  	"github.com/annchain/OG/ogcore/events"
    11  	"github.com/annchain/OG/ogcore/message"
    12  	"github.com/annchain/OG/ogcore/pool"
    13  	"github.com/sirupsen/logrus"
    14  )
    15  
    16  var supportedMessageTypes = []general_message.GeneralMessageType{
    17  	MessageTypeOg,
    18  }
    19  
    20  type OgPlugin struct {
    21  	messageHandler general_communication.GeneralMessageEventHandler
    22  	OgPartner      *ogcore.OgPartner
    23  	Communicator   *ProxyOgPeerCommunicator
    24  	TxBuffer       *pool.TxBuffer
    25  }
    26  
    27  func NewOgPlugin() *OgPlugin {
    28  	config := ogcore.OgProcessorConfig{}
    29  	ogMessageAdapter := &DefaultOgMessageAdapter{
    30  		unmarshaller: OgMessageUnmarshaller{},
    31  	}
    32  
    33  	communicator := &ProxyOgPeerCommunicator{
    34  		OgMessageAdapter: ogMessageAdapter,
    35  		GeneralOutgoing:  nil, // place for p2p peer outgoing (set later)
    36  	}
    37  	communicator.InitDefault()
    38  
    39  	ogCore := &ogcore.OgCore{
    40  		NodeLogger: debuglog.NodeLogger{
    41  			Logger: logrus.StandardLogger(),
    42  		},
    43  		EventBus: nil,
    44  	}
    45  
    46  	ogPartner := &ogcore.OgPartner{
    47  		NodeLogger: debuglog.NodeLogger{
    48  			Logger: logrus.StandardLogger(),
    49  		},
    50  		Config:         config,
    51  		PeerOutgoing:   communicator,
    52  		PeerIncoming:   communicator,
    53  		EventBus:       nil,
    54  		StatusProvider: ogCore,
    55  		OgCore:         ogCore,
    56  	}
    57  	ogPartner.InitDefault()
    58  
    59  	return &OgPlugin{
    60  		messageHandler: &OgGeneralMessageHandler{
    61  			OgPartner:        ogPartner,
    62  			OgMessageAdapter: ogMessageAdapter,
    63  		},
    64  		OgPartner:    ogPartner,
    65  		Communicator: communicator,
    66  	}
    67  }
    68  
    69  func (o *OgPlugin) SupportedEventHandlers() []eventbus.EventHandlerRegisterInfo {
    70  	return []eventbus.EventHandlerRegisterInfo{
    71  		{
    72  			Type:    events.TxReceivedEventType,
    73  			Handler: o.TxBuffer,
    74  		},
    75  		{
    76  			Type:    events.SequencerReceivedEventType,
    77  			Handler: o.TxBuffer,
    78  		},
    79  	}
    80  }
    81  
    82  func (o *OgPlugin) SetOutgoing(outgoing general_communication.GeneralPeerCommunicatorOutgoing) {
    83  	o.Communicator.GeneralOutgoing = outgoing
    84  }
    85  
    86  func (o *OgPlugin) Start() {
    87  	o.OgPartner.Start()
    88  }
    89  
    90  func (o *OgPlugin) Stop() {
    91  	o.OgPartner.Stop()
    92  }
    93  
    94  func (o *OgPlugin) SupportedMessageTypes() []general_message.GeneralMessageType {
    95  	return supportedMessageTypes
    96  }
    97  
    98  func (o *OgPlugin) GetMessageEventHandler() general_communication.GeneralMessageEventHandler {
    99  	return o.messageHandler
   100  }
   101  
   102  type OgMessageAdapter interface {
   103  	AdaptGeneralMessage(incomingMsg general_message.GeneralMessage) (ogMessage message.OgMessage, err error)
   104  	AdaptGeneralPeer(gnrPeer *general_message.GeneralPeer) (communication.OgPeer, error)
   105  	AdaptOgMessage(outgoingMsg message.OgMessage) (msg general_message.GeneralMessage, err error)
   106  	AdaptOgPeer(annPeer *communication.OgPeer) (general_message.GeneralPeer, error)
   107  }