github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/neatptc/protocol.go (about)

     1  package neatptc
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"math/big"
     7  
     8  	"github.com/neatlab/neatio/chain/core"
     9  	"github.com/neatlab/neatio/chain/core/types"
    10  	"github.com/neatlab/neatio/utilities/common"
    11  	"github.com/neatlab/neatio/utilities/event"
    12  	"github.com/neatlab/neatio/utilities/rlp"
    13  )
    14  
    15  const (
    16  	intprotocol63 = 63
    17  	intprotocol64 = 64
    18  	intprotocol65 = 65
    19  )
    20  
    21  const protocolName = "neatptc"
    22  
    23  var ProtocolVersions = []uint{intprotocol65, intprotocol64, intprotocol63}
    24  
    25  var protocolLengths = map[uint]uint64{intprotocol65: 17, intprotocol64: 17, intprotocol63: 17}
    26  
    27  const ProtocolMaxMsgSize = 10 * 1024 * 1024
    28  
    29  const (
    30  	StatusMsg          = 0x00
    31  	NewBlockHashesMsg  = 0x01
    32  	TxMsg              = 0x02
    33  	GetBlockHeadersMsg = 0x03
    34  	BlockHeadersMsg    = 0x04
    35  	GetBlockBodiesMsg  = 0x05
    36  	BlockBodiesMsg     = 0x06
    37  	NewBlockMsg        = 0x07
    38  
    39  	GetNodeDataMsg = 0x0d
    40  	NodeDataMsg    = 0x0e
    41  	GetReceiptsMsg = 0x0f
    42  	ReceiptsMsg    = 0x10
    43  
    44  	TX3ProofDataMsg = 0x18
    45  
    46  	GetPreImagesMsg = 0x19
    47  	PreImagesMsg    = 0x1a
    48  	TrieNodeDataMsg = 0x1b
    49  )
    50  
    51  type errCode int
    52  
    53  const (
    54  	ErrMsgTooLarge = iota
    55  	ErrDecode
    56  	ErrInvalidMsgCode
    57  	ErrProtocolVersionMismatch
    58  	ErrNetworkIdMismatch
    59  	ErrGenesisBlockMismatch
    60  	ErrNoStatusMsg
    61  	ErrExtraStatusMsg
    62  	ErrSuspendedPeer
    63  	ErrTX3ValidateFail
    64  )
    65  
    66  func (e errCode) String() string {
    67  	return errorToString[int(e)]
    68  }
    69  
    70  var errorToString = map[int]string{
    71  	ErrMsgTooLarge:             "Message too long",
    72  	ErrDecode:                  "Invalid message",
    73  	ErrInvalidMsgCode:          "Invalid message code",
    74  	ErrProtocolVersionMismatch: "Protocol version mismatch",
    75  	ErrNetworkIdMismatch:       "NetworkId mismatch",
    76  	ErrGenesisBlockMismatch:    "Genesis block mismatch",
    77  	ErrNoStatusMsg:             "No status message",
    78  	ErrExtraStatusMsg:          "Extra status message",
    79  	ErrSuspendedPeer:           "Suspended peer",
    80  	ErrTX3ValidateFail:         "TX3 validate fail",
    81  }
    82  
    83  type txPool interface {
    84  	AddRemotes([]*types.Transaction) []error
    85  
    86  	Pending() (map[common.Address]types.Transactions, error)
    87  
    88  	SubscribeTxPreEvent(chan<- core.TxPreEvent) event.Subscription
    89  }
    90  
    91  type statusData struct {
    92  	ProtocolVersion uint32
    93  	NetworkId       uint64
    94  	TD              *big.Int
    95  	CurrentBlock    common.Hash
    96  	GenesisBlock    common.Hash
    97  }
    98  
    99  type newBlockHashesData []struct {
   100  	Hash   common.Hash
   101  	Number uint64
   102  }
   103  
   104  type getBlockHeadersData struct {
   105  	Origin  hashOrNumber
   106  	Amount  uint64
   107  	Skip    uint64
   108  	Reverse bool
   109  }
   110  
   111  type hashOrNumber struct {
   112  	Hash   common.Hash
   113  	Number uint64
   114  }
   115  
   116  func (hn *hashOrNumber) EncodeRLP(w io.Writer) error {
   117  	if hn.Hash == (common.Hash{}) {
   118  		return rlp.Encode(w, hn.Number)
   119  	}
   120  	if hn.Number != 0 {
   121  		return fmt.Errorf("both origin hash (%x) and number (%d) provided", hn.Hash, hn.Number)
   122  	}
   123  	return rlp.Encode(w, hn.Hash)
   124  }
   125  
   126  func (hn *hashOrNumber) DecodeRLP(s *rlp.Stream) error {
   127  	_, size, _ := s.Kind()
   128  	origin, err := s.Raw()
   129  	if err == nil {
   130  		switch {
   131  		case size == 32:
   132  			err = rlp.DecodeBytes(origin, &hn.Hash)
   133  		case size <= 8:
   134  			err = rlp.DecodeBytes(origin, &hn.Number)
   135  		default:
   136  			err = fmt.Errorf("invalid input size %d for origin", size)
   137  		}
   138  	}
   139  	return err
   140  }
   141  
   142  type newBlockData struct {
   143  	Block *types.Block
   144  	TD    *big.Int
   145  }
   146  
   147  type blockBody struct {
   148  	Transactions []*types.Transaction
   149  	Uncles       []*types.Header
   150  }
   151  
   152  type blockBodiesData []*blockBody