@@ -29,6 +29,7 @@ import (
2929 "github.com/ethereum/go-ethereum/common"
3030 "github.com/ethereum/go-ethereum/common/mclock"
3131 "github.com/ethereum/go-ethereum/core"
32+ "github.com/ethereum/go-ethereum/core/forkid"
3233 "github.com/ethereum/go-ethereum/core/types"
3334 "github.com/ethereum/go-ethereum/eth"
3435 "github.com/ethereum/go-ethereum/les/flowcontrol"
@@ -246,7 +247,7 @@ func (p *peerCommons) sendReceiveHandshake(sendList keyValueList) (keyValueList,
246247// network IDs, difficulties, head and genesis blocks. Besides the basic handshake
247248// fields, server and client can exchange and resolve some specified fields through
248249// two callback functions.
249- func (p * peerCommons ) handshake (td * big.Int , head common.Hash , headNum uint64 , genesis common.Hash , sendCallback func (* keyValueList ), recvCallback func (keyValueMap ) error ) error {
250+ func (p * peerCommons ) handshake (td * big.Int , head common.Hash , headNum uint64 , genesis common.Hash , forkID forkid. ID , forkFilter forkid. Filter , sendCallback func (* keyValueList ), recvCallback func (keyValueMap ) error ) error {
250251 p .lock .Lock ()
251252 defer p .lock .Unlock ()
252253
@@ -262,6 +263,12 @@ func (p *peerCommons) handshake(td *big.Int, head common.Hash, headNum uint64, g
262263 send = send .add ("headNum" , headNum )
263264 send = send .add ("genesisHash" , genesis )
264265
266+ // If the protocol version is beyond les4, then pass the forkID
267+ // as well. Check http://eips.ethereum.org/EIPS/eip-2124 for more
268+ // spec detail.
269+ if p .version >= lpv4 {
270+ send = send .add ("forkID" , forkID )
271+ }
265272 // Add client-specified or server-specified fields
266273 if sendCallback != nil {
267274 sendCallback (& send )
@@ -295,6 +302,16 @@ func (p *peerCommons) handshake(td *big.Int, head common.Hash, headNum uint64, g
295302 if int (rVersion ) != p .version {
296303 return errResp (ErrProtocolVersionMismatch , "%d (!= %d)" , rVersion , p .version )
297304 }
305+ // Check forkID if the protocol version is beyond the les4
306+ if p .version >= lpv4 {
307+ var forkID forkid.ID
308+ if err := recv .get ("forkID" , & forkID ); err != nil {
309+ return err
310+ }
311+ if err := forkFilter (forkID ); err != nil {
312+ return errResp (ErrForkIDRejected , "%v" , err )
313+ }
314+ }
298315 if recvCallback != nil {
299316 return recvCallback (recv )
300317 }
@@ -561,10 +578,10 @@ func (p *serverPeer) updateHead(hash common.Hash, number uint64, td *big.Int) {
561578
562579// Handshake executes the les protocol handshake, negotiating version number,
563580// network IDs and genesis blocks.
564- func (p * serverPeer ) Handshake (genesis common.Hash ) error {
581+ func (p * serverPeer ) Handshake (genesis common.Hash , forkid forkid. ID , forkFilter forkid. Filter ) error {
565582 // Note: there is no need to share local head with a server but older servers still
566583 // require these fields so we announce zero values.
567- return p .handshake (common .Big0 , common.Hash {}, 0 , genesis , func (lists * keyValueList ) {
584+ return p .handshake (common .Big0 , common.Hash {}, 0 , genesis , forkid , forkFilter , func (lists * keyValueList ) {
568585 // Add some client-specific handshake fields
569586 //
570587 // Enable signed announcement randomly even the server is not trusted.
@@ -944,11 +961,11 @@ func (p *clientPeer) freezeClient() {
944961
945962// Handshake executes the les protocol handshake, negotiating version number,
946963// network IDs, difficulties, head and genesis blocks.
947- func (p * clientPeer ) Handshake (td * big.Int , head common.Hash , headNum uint64 , genesis common.Hash , server * LesServer ) error {
964+ func (p * clientPeer ) Handshake (td * big.Int , head common.Hash , headNum uint64 , genesis common.Hash , forkID forkid. ID , forkFilter forkid. Filter , server * LesServer ) error {
948965 // Note: clientPeer.headInfo should contain the last head announced to the client by us.
949966 // The values announced in the handshake are dummy values for compatibility reasons and should be ignored.
950967 p .headInfo = blockInfo {Hash : head , Number : headNum , Td : td }
951- return p .handshake (td , head , headNum , genesis , func (lists * keyValueList ) {
968+ return p .handshake (td , head , headNum , genesis , forkID , forkFilter , func (lists * keyValueList ) {
952969 // Add some information which services server can offer.
953970 if ! server .config .UltraLightOnlyAnnounce {
954971 * lists = (* lists ).add ("serveHeaders" , nil )
0 commit comments