github.com/phillinzzz/newBsc@v1.1.6/eth/protocols/diff/protocol.go (about)

     1  // Copyright 2020 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package diff
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  
    23  	"golang.org/x/crypto/sha3"
    24  
    25  	"github.com/phillinzzz/newBsc/common"
    26  	"github.com/phillinzzz/newBsc/core/types"
    27  	"github.com/phillinzzz/newBsc/rlp"
    28  )
    29  
    30  // Constants to match up protocol versions and messages
    31  const (
    32  	Diff1 = 1
    33  )
    34  
    35  // ProtocolName is the official short name of the `diff` protocol used during
    36  // devp2p capability negotiation.
    37  const ProtocolName = "diff"
    38  
    39  // ProtocolVersions are the supported versions of the `diff` protocol (first
    40  // is primary).
    41  var ProtocolVersions = []uint{Diff1}
    42  
    43  // protocolLengths are the number of implemented message corresponding to
    44  // different protocol versions.
    45  var protocolLengths = map[uint]uint64{Diff1: 4}
    46  
    47  // maxMessageSize is the maximum cap on the size of a protocol message.
    48  const maxMessageSize = 10 * 1024 * 1024
    49  
    50  const (
    51  	DiffCapMsg       = 0x00
    52  	GetDiffLayerMsg  = 0x01
    53  	DiffLayerMsg     = 0x02
    54  	FullDiffLayerMsg = 0x03
    55  )
    56  
    57  var defaultExtra = []byte{0x00}
    58  
    59  var (
    60  	errMsgTooLarge    = errors.New("message too long")
    61  	errDecode         = errors.New("invalid message")
    62  	errInvalidMsgCode = errors.New("invalid message code")
    63  	errUnexpectedMsg  = errors.New("unexpected message code")
    64  	errNoCapMsg       = errors.New("miss cap message during handshake")
    65  )
    66  
    67  // Packet represents a p2p message in the `diff` protocol.
    68  type Packet interface {
    69  	Name() string // Name returns a string corresponding to the message type.
    70  	Kind() byte   // Kind returns the message type.
    71  }
    72  
    73  type GetDiffLayersPacket struct {
    74  	RequestId   uint64
    75  	BlockHashes []common.Hash
    76  }
    77  
    78  func (p *DiffLayersPacket) Unpack() ([]*types.DiffLayer, error) {
    79  	diffLayers := make([]*types.DiffLayer, 0, len(*p))
    80  	hasher := sha3.NewLegacyKeccak256()
    81  	for _, rawData := range *p {
    82  		var diff types.DiffLayer
    83  		err := rlp.DecodeBytes(rawData, &diff)
    84  		if err != nil {
    85  			return nil, fmt.Errorf("%w: diff layer %v", errDecode, err)
    86  		}
    87  		diffLayers = append(diffLayers, &diff)
    88  		_, err = hasher.Write(rawData)
    89  		if err != nil {
    90  			return nil, err
    91  		}
    92  		var diffHash common.Hash
    93  		hasher.Sum(diffHash[:0])
    94  		hasher.Reset()
    95  		diff.DiffHash = diffHash
    96  	}
    97  	return diffLayers, nil
    98  }
    99  
   100  type DiffCapPacket struct {
   101  	DiffSync bool
   102  	Extra    rlp.RawValue // for extension
   103  }
   104  
   105  type DiffLayersPacket []rlp.RawValue
   106  
   107  type FullDiffLayersPacket struct {
   108  	RequestId uint64
   109  	DiffLayersPacket
   110  }
   111  
   112  func (*GetDiffLayersPacket) Name() string { return "GetDiffLayers" }
   113  func (*GetDiffLayersPacket) Kind() byte   { return GetDiffLayerMsg }
   114  
   115  func (*DiffLayersPacket) Name() string { return "DiffLayers" }
   116  func (*DiffLayersPacket) Kind() byte   { return DiffLayerMsg }
   117  
   118  func (*FullDiffLayersPacket) Name() string { return "FullDiffLayers" }
   119  func (*FullDiffLayersPacket) Kind() byte   { return FullDiffLayerMsg }
   120  
   121  func (*DiffCapPacket) Name() string { return "DiffCap" }
   122  func (*DiffCapPacket) Kind() byte   { return DiffCapMsg }