github.com/bigzoro/my_simplechain@v0.0.0-20240315012955-8ad0a2a29bb9/consensus/hotstuff/service.go (about)

     1  package hotstuff
     2  
     3  import (
     4  	"github.com/bigzoro/my_simplechain/consensus"
     5  	"github.com/bigzoro/my_simplechain/p2p"
     6  	"github.com/bigzoro/my_simplechain/rpc"
     7  )
     8  
     9  const (
    10  	// Hotstuff protocol version
    11  	protocolVersion uint = 1
    12  
    13  	// Maximum number of hotstuff message codes
    14  	protocolLength uint64 = 10
    15  )
    16  
    17  type protocol interface {
    18  	Handle(*p2p.Peer, p2p.MsgReadWriter, consensus.ChainReader) error
    19  
    20  	Close() error
    21  }
    22  
    23  type Service struct {
    24  	chain    consensus.ChainReader
    25  	protocol protocol
    26  }
    27  
    28  func (s *Service) Protocols() []p2p.Protocol {
    29  	return []p2p.Protocol{
    30  		{
    31  			Name:    "hotstuff",
    32  			Version: protocolVersion,
    33  			Length:  protocolLength,
    34  			Run: func(peer *p2p.Peer, rw p2p.MsgReadWriter) error {
    35  				return s.protocol.Handle(peer, rw, s.chain)
    36  			},
    37  		},
    38  	}
    39  }
    40  
    41  func (s *Service) APIs() []rpc.API {
    42  	return nil
    43  }
    44  
    45  func (s *Service) Start(server *p2p.Server) error {
    46  	return nil
    47  }
    48  
    49  func (s *Service) Stop() error {
    50  	return s.protocol.Close()
    51  }