@@ -25,6 +25,7 @@ import (
2525
2626 mapset "github.com/deckarep/golang-set"
2727 "github.com/ethereum/go-ethereum/common"
28+ "github.com/ethereum/go-ethereum/core/forkid"
2829 "github.com/ethereum/go-ethereum/core/types"
2930 "github.com/ethereum/go-ethereum/p2p"
3031 "github.com/ethereum/go-ethereum/rlp"
@@ -353,22 +354,46 @@ func (p *peer) RequestReceipts(hashes []common.Hash) error {
353354
354355// Handshake executes the eth protocol handshake, negotiating version number,
355356// network IDs, difficulties, head and genesis blocks.
356- func (p * peer ) Handshake (network uint64 , td * big.Int , head common.Hash , genesis common.Hash ) error {
357+ func (p * peer ) Handshake (network uint64 , td * big.Int , head common.Hash , genesis common.Hash , forkID forkid. ID , forkFilter forkid. Filter ) error {
357358 // Send out own handshake in a new thread
358359 errc := make (chan error , 2 )
359- var status statusData // safe to read after two values have been received from errc
360360
361+ var (
362+ status63 statusData63 // safe to read after two values have been received from errc
363+ status statusData // safe to read after two values have been received from errc
364+ )
361365 go func () {
362- errc <- p2p .Send (p .rw , StatusMsg , & statusData {
363- ProtocolVersion : uint32 (p .version ),
364- NetworkId : network ,
365- TD : td ,
366- CurrentBlock : head ,
367- GenesisBlock : genesis ,
368- })
366+ switch {
367+ case p .version == eth63 :
368+ errc <- p2p .Send (p .rw , StatusMsg , & statusData63 {
369+ ProtocolVersion : uint32 (p .version ),
370+ NetworkId : network ,
371+ TD : td ,
372+ CurrentBlock : head ,
373+ GenesisBlock : genesis ,
374+ })
375+ case p .version == eth64 :
376+ errc <- p2p .Send (p .rw , StatusMsg , & statusData {
377+ ProtocolVersion : uint32 (p .version ),
378+ NetworkID : network ,
379+ TD : td ,
380+ Head : head ,
381+ Genesis : genesis ,
382+ ForkID : forkID ,
383+ })
384+ default :
385+ panic (fmt .Sprintf ("unsupported eth protocol version: %d" , p .version ))
386+ }
369387 }()
370388 go func () {
371- errc <- p .readStatus (network , & status , genesis )
389+ switch {
390+ case p .version == eth63 :
391+ errc <- p .readStatusLegacy (network , & status63 , genesis )
392+ case p .version == eth64 :
393+ errc <- p .readStatus (network , & status , genesis , forkFilter )
394+ default :
395+ panic (fmt .Sprintf ("unsupported eth protocol version: %d" , p .version ))
396+ }
372397 }()
373398 timeout := time .NewTimer (handshakeTimeout )
374399 defer timeout .Stop ()
@@ -382,11 +407,18 @@ func (p *peer) Handshake(network uint64, td *big.Int, head common.Hash, genesis
382407 return p2p .DiscReadTimeout
383408 }
384409 }
385- p .td , p .head = status .TD , status .CurrentBlock
410+ switch {
411+ case p .version == eth63 :
412+ p .td , p .head = status63 .TD , status63 .CurrentBlock
413+ case p .version == eth64 :
414+ p .td , p .head = status .TD , status .Head
415+ default :
416+ panic (fmt .Sprintf ("unsupported eth protocol version: %d" , p .version ))
417+ }
386418 return nil
387419}
388420
389- func (p * peer ) readStatus (network uint64 , status * statusData , genesis common.Hash ) ( err error ) {
421+ func (p * peer ) readStatusLegacy (network uint64 , status * statusData63 , genesis common.Hash ) error {
390422 msg , err := p .rw .ReadMsg ()
391423 if err != nil {
392424 return err
@@ -402,17 +434,47 @@ func (p *peer) readStatus(network uint64, status *statusData, genesis common.Has
402434 return errResp (ErrDecode , "msg %v: %v" , msg , err )
403435 }
404436 if status .GenesisBlock != genesis {
405- return errResp (ErrGenesisBlockMismatch , "%x (!= %x)" , status .GenesisBlock [:8 ], genesis [:8 ])
437+ return errResp (ErrGenesisMismatch , "%x (!= %x)" , status .GenesisBlock [:8 ], genesis [:8 ])
406438 }
407439 if status .NetworkId != network {
408- return errResp (ErrNetworkIdMismatch , "%d (!= %d)" , status .NetworkId , network )
440+ return errResp (ErrNetworkIDMismatch , "%d (!= %d)" , status .NetworkId , network )
409441 }
410442 if int (status .ProtocolVersion ) != p .version {
411443 return errResp (ErrProtocolVersionMismatch , "%d (!= %d)" , status .ProtocolVersion , p .version )
412444 }
413445 return nil
414446}
415447
448+ func (p * peer ) readStatus (network uint64 , status * statusData , genesis common.Hash , forkFilter forkid.Filter ) error {
449+ msg , err := p .rw .ReadMsg ()
450+ if err != nil {
451+ return err
452+ }
453+ if msg .Code != StatusMsg {
454+ return errResp (ErrNoStatusMsg , "first msg has code %x (!= %x)" , msg .Code , StatusMsg )
455+ }
456+ if msg .Size > protocolMaxMsgSize {
457+ return errResp (ErrMsgTooLarge , "%v > %v" , msg .Size , protocolMaxMsgSize )
458+ }
459+ // Decode the handshake and make sure everything matches
460+ if err := msg .Decode (& status ); err != nil {
461+ return errResp (ErrDecode , "msg %v: %v" , msg , err )
462+ }
463+ if status .NetworkID != network {
464+ return errResp (ErrNetworkIDMismatch , "%d (!= %d)" , status .NetworkID , network )
465+ }
466+ if int (status .ProtocolVersion ) != p .version {
467+ return errResp (ErrProtocolVersionMismatch , "%d (!= %d)" , status .ProtocolVersion , p .version )
468+ }
469+ if status .Genesis != genesis {
470+ return errResp (ErrGenesisMismatch , "%x (!= %x)" , status .Genesis , genesis )
471+ }
472+ if err := forkFilter (status .ForkID ); err != nil {
473+ return errResp (ErrForkIDRejected , "%v" , err )
474+ }
475+ return nil
476+ }
477+
416478// String implements fmt.Stringer.
417479func (p * peer ) String () string {
418480 return fmt .Sprintf ("Peer %s [%s]" , p .id ,
0 commit comments