-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[R4R] add extension in eth protocol handshake to disable tx broadcast #412
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,7 @@ const ( | |
|
||
// Handshake executes the eth protocol handshake, negotiating version number, | ||
// network IDs, difficulties, head and genesis blocks. | ||
func (p *Peer) Handshake(network uint64, td *big.Int, head common.Hash, genesis common.Hash, forkID forkid.ID, forkFilter forkid.Filter) error { | ||
func (p *Peer) Handshake(network uint64, td *big.Int, head common.Hash, genesis common.Hash, forkID forkid.ID, forkFilter forkid.Filter, extension *UpgradeStatusExtension) error { | ||
// Send out own handshake in a new thread | ||
errc := make(chan error, 2) | ||
|
||
|
@@ -68,6 +68,49 @@ func (p *Peer) Handshake(network uint64, td *big.Int, head common.Hash, genesis | |
} | ||
p.td, p.head = status.TD, status.Head | ||
|
||
if p.version >= ETH67 { | ||
var upgradeStatus UpgradeStatusPacket // safe to read after two values have been received from errc | ||
if extension == nil { | ||
extension = &UpgradeStatusExtension{} | ||
} | ||
extensionRaw, err := extension.Encode() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
gopool.Submit(func() { | ||
errc <- p2p.Send(p.rw, UpgradeStatusMsg, &UpgradeStatusPacket{ | ||
Extension: extensionRaw, | ||
}) | ||
}) | ||
gopool.Submit(func() { | ||
errc <- p.readUpgradeStatus(&upgradeStatus) | ||
}) | ||
timeout := time.NewTimer(handshakeTimeout) | ||
defer timeout.Stop() | ||
for i := 0; i < 2; i++ { | ||
select { | ||
case err := <-errc: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you need make sure errc is clear before read from it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if the errc is not empty, the upper code should complain |
||
if err != nil { | ||
return err | ||
} | ||
case <-timeout.C: | ||
return p2p.DiscReadTimeout | ||
} | ||
} | ||
|
||
extension, err := upgradeStatus.GetExtension() | ||
if err != nil { | ||
return err | ||
} | ||
p.statusExtension = extension | ||
|
||
if p.statusExtension.DisablePeerTxBroadcast { | ||
p.Log().Debug("peer does not need broadcast txs, closing broadcast routines") | ||
p.CloseTxBroadcast() | ||
} | ||
} | ||
|
||
// TD at mainnet block #7753254 is 76 bits. If it becomes 100 million times | ||
// larger, it will still fit within 100 bits | ||
if tdlen := p.td.BitLen(); tdlen > 100 { | ||
|
@@ -106,3 +149,20 @@ func (p *Peer) readStatus(network uint64, status *StatusPacket, genesis common.H | |
} | ||
return nil | ||
} | ||
|
||
func (p *Peer) readUpgradeStatus(status *UpgradeStatusPacket) error { | ||
msg, err := p.rw.ReadMsg() | ||
if err != nil { | ||
return err | ||
} | ||
if msg.Code != UpgradeStatusMsg { | ||
return fmt.Errorf("%w: upgrade status msg has code %x (!= %x)", errNoStatusMsg, msg.Code, UpgradeStatusMsg) | ||
} | ||
if msg.Size > maxMessageSize { | ||
return fmt.Errorf("%w: %v > %v", errMsgTooLarge, msg.Size, maxMessageSize) | ||
} | ||
if err := msg.Decode(&status); err != nil { | ||
return fmt.Errorf("%w: message %v: %v", errDecode, msg, err) | ||
} | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just fix the imports