Pull all recent changes to 1.8.21 tag

This commit is contained in:
Dmitry 2019-02-22 14:51:07 +02:00 committed by Dmitry Shulyak
parent 8583eb2f37
commit 5d829f89f8
7 changed files with 145 additions and 161 deletions

4
Gopkg.lock generated
View File

@ -99,7 +99,7 @@
revision = "935e0e8a636ca4ba70b713f3e38a19e1b77739e8"
[[projects]]
digest = "1:11fb76e8d45f1b8e6812a61ea031e976afae1e21aa5f2aeac94322cbdaab31fc"
digest = "1:e4000f701f06027e58e6ecb5e1b9cebd1d31ed32187d0c036ca0f9bfba580c32"
name = "github.com/ethereum/go-ethereum"
packages = [
".",
@ -168,7 +168,7 @@
"trie",
]
pruneopts = "T"
revision = "5c51e27be022a4c5056791060884eb1cd6d6d3d4"
revision = "8bca0916296164f0c2e6fd178b7735aa8af581f5"
source = "github.com/status-im/go-ethereum"
version = "v1.8.21"

View File

@ -1,119 +0,0 @@
diff --git a/p2p/peer.go b/p2p/peer.go
index af019d0..cfd63af 100644
--- a/p2p/peer.go
+++ b/p2p/peer.go
@@ -23,6 +23,7 @@ import (
"net"
"sort"
"sync"
+ "sync/atomic"
"time"
"github.com/ethereum/go-ethereum/common/mclock"
@@ -44,7 +45,10 @@ const (
snappyProtocolVersion = 5
- pingInterval = 15 * time.Second
+ pingInterval = 1 * time.Second
+ // watchdogInterval intentionally lower than ping interval.
+ // this way we reduce potential flaky window size.
+ watchdogInterval = 200 * time.Millisecond
)
const (
@@ -106,6 +110,7 @@ type Peer struct {
log log.Logger
created mclock.AbsTime
+ flaky int32
wg sync.WaitGroup
protoErr chan error
closed chan struct{}
@@ -125,6 +130,11 @@ func NewPeer(id enode.ID, name string, caps []Cap) *Peer {
return peer
}
+// IsFlaky returns true if there was no incoming traffic recently.
+func (p *Peer) IsFlaky() bool {
+ return atomic.LoadInt32(&p.flaky) == 1
+}
+
// ID returns the node's public key.
func (p *Peer) ID() enode.ID {
return p.rw.node.ID()
@@ -201,8 +211,10 @@ func (p *Peer) run() (remoteRequested bool, err error) {
readErr = make(chan error, 1)
reason DiscReason // sent to the peer
)
- p.wg.Add(2)
- go p.readLoop(readErr)
+ p.wg.Add(3)
+ reads := make(chan struct{}, 10) // channel for reads
+ go p.readLoop(readErr, reads)
+ go p.watchdogLoop(reads)
go p.pingLoop()
// Start all protocol handlers.
@@ -262,7 +274,24 @@ func (p *Peer) pingLoop() {
}
}
-func (p *Peer) readLoop(errc chan<- error) {
+func (p *Peer) watchdogLoop(reads <-chan struct{}) {
+ defer p.wg.Done()
+ hb := time.NewTimer(watchdogInterval)
+ defer hb.Stop()
+ for {
+ select {
+ case <-reads:
+ atomic.StoreInt32(&p.flaky, 0)
+ case <-hb.C:
+ atomic.StoreInt32(&p.flaky, 1)
+ case <-p.closed:
+ return
+ }
+ hb.Reset(watchdogInterval)
+ }
+}
+
+func (p *Peer) readLoop(errc chan<- error, reads chan<- struct{}) {
defer p.wg.Done()
for {
msg, err := p.rw.ReadMsg()
@@ -275,6 +304,7 @@ func (p *Peer) readLoop(errc chan<- error) {
errc <- err
return
}
+ reads <- struct{}{}
}
}
diff --git a/p2p/server.go b/p2p/server.go
index 40db758..8546b02 100644
--- a/p2p/server.go
+++ b/p2p/server.go
@@ -49,7 +49,7 @@ const (
// Maximum time allowed for reading a complete message.
// This is effectively the amount of time a connection can be idle.
- frameReadTimeout = 30 * time.Second
+ frameReadTimeout = 10 * time.Second
// Maximum amount of time allowed for writing a complete message.
frameWriteTimeout = 20 * time.Second
diff --git a/whisper/whisperv6/peer.go b/whisper/whisperv6/peer.go
index eb17d2d..2b7687e 100644
--- a/whisper/whisperv6/peer.go
+++ b/whisper/whisperv6/peer.go
@@ -195,6 +195,10 @@ func (peer *Peer) expire() {
// broadcast iterates over the collection of envelopes and transmits yet unknown
// ones over the network.
func (peer *Peer) broadcast() error {
+ if peer.peer.IsFlaky() {
+ log.Trace("Waiting for a peer to restore communication", "ID", peer.peer.ID())
+ return nil
+ }
envelopes := peer.host.Envelopes()
bundle := make([]*Envelope, 0, len(envelopes))
for _, envelope := range envelopes {

View File

@ -0,0 +1,62 @@
diff --git a/node/api.go b/node/api.go
index 4a434843e..bb1c01119 100644
--- a/node/api.go
+++ b/node/api.go
@@ -75,6 +75,24 @@ func (api *PrivateAdminAPI) RemovePeer(url string) (bool, error) {
return true, nil
}
+// DeletePeer disconnects and deletes forcefully a remote node.
+func (api *PrivateAdminAPI) DeletePeer(url string) (bool, error) {
+ // Make sure the server is running, fail otherwise
+ server := api.node.Server()
+ if server == nil {
+ return false, ErrNodeStopped
+ }
+ // Try to remove the url as a static peer and return
+ node, err := enode.ParseV4(url)
+ if err != nil {
+ return false, fmt.Errorf("invalid enode: %v", err)
+ }
+ if err := server.DeletePeer(node); err != nil {
+ return false, err
+ }
+ return true, nil
+}
+
// AddTrustedPeer allows a remote node to always connect, even if slots are full
func (api *PrivateAdminAPI) AddTrustedPeer(url string) (bool, error) {
// Make sure the server is running, fail otherwise
diff --git a/p2p/server.go b/p2p/server.go
index b8c69bad8..715ab0872 100644
--- a/p2p/server.go
+++ b/p2p/server.go
@@ -327,6 +327,28 @@ func (srv *Server) RemovePeer(node *enode.Node) {
}
}
+// DeletePeer deletes the given node forcefully.
+func (srv *Server) DeletePeer(node *enode.Node) error {
+ var peer *Peer
+ for _, p := range srv.Peers() {
+ if p.ID() == node.ID() {
+ peer = p
+ break
+ }
+ }
+
+ if peer == nil {
+ return errors.New("peer not found")
+ }
+
+ select {
+ case srv.delpeer <- peerDrop{peer, errors.New("forced delete"), true}:
+ case <-srv.quit:
+ }
+
+ return nil
+}
+
// AddTrustedPeer adds the given node to a reserved whitelist which allows the
// node to always connect, even if the slot are full.
func (srv *Server) AddTrustedPeer(node *enode.Node) {

View File

@ -75,6 +75,24 @@ func (api *PrivateAdminAPI) RemovePeer(url string) (bool, error) {
return true, nil
}
// DeletePeer disconnects and deletes forcefully a remote node.
func (api *PrivateAdminAPI) DeletePeer(url string) (bool, error) {
// Make sure the server is running, fail otherwise
server := api.node.Server()
if server == nil {
return false, ErrNodeStopped
}
// Try to remove the url as a static peer and return
node, err := enode.ParseV4(url)
if err != nil {
return false, fmt.Errorf("invalid enode: %v", err)
}
if err := server.DeletePeer(node); err != nil {
return false, err
}
return true, nil
}
// AddTrustedPeer allows a remote node to always connect, even if slots are full
func (api *PrivateAdminAPI) AddTrustedPeer(url string) (bool, error) {
// Make sure the server is running, fail otherwise

View File

@ -23,7 +23,6 @@ import (
"net"
"sort"
"sync"
"sync/atomic"
"time"
"github.com/ethereum/go-ethereum/common/mclock"
@ -45,10 +44,7 @@ const (
snappyProtocolVersion = 5
pingInterval = 1 * time.Second
// watchdogInterval intentionally lower than ping interval.
// this way we reduce potential flaky window size.
watchdogInterval = 200 * time.Millisecond
pingInterval = 15 * time.Second
)
const (
@ -110,7 +106,6 @@ type Peer struct {
log log.Logger
created mclock.AbsTime
flaky int32
wg sync.WaitGroup
protoErr chan error
closed chan struct{}
@ -130,11 +125,6 @@ func NewPeer(id enode.ID, name string, caps []Cap) *Peer {
return peer
}
// IsFlaky returns true if there was no incoming traffic recently.
func (p *Peer) IsFlaky() bool {
return atomic.LoadInt32(&p.flaky) == 1
}
// ID returns the node's public key.
func (p *Peer) ID() enode.ID {
return p.rw.node.ID()
@ -211,10 +201,8 @@ func (p *Peer) run() (remoteRequested bool, err error) {
readErr = make(chan error, 1)
reason DiscReason // sent to the peer
)
p.wg.Add(3)
reads := make(chan struct{}, 10) // channel for reads
go p.readLoop(readErr, reads)
go p.watchdogLoop(reads)
p.wg.Add(2)
go p.readLoop(readErr)
go p.pingLoop()
// Start all protocol handlers.
@ -252,10 +240,37 @@ loop:
close(p.closed)
p.rw.close(reason)
p.wg.Wait()
if shutdownErr := p.shutdownWithTimeout(5 * time.Second); shutdownErr != nil {
p.Log().Error("Timeout while waiting for the peer to shut down", "err", err)
}
return remoteRequested, err
}
func (p *Peer) shutdownWithTimeout(timeout time.Duration) error {
/* A watchdog for the waitGroup (p.wg)
* When a peer shuts down we give 5 seconds for all protocols to finish,
* but we continue the process anyway (and print a warning message).
* Otherwise, this peer will be stuck in the list of peers (`admin_peers`)
* and won't be able to reconnect until the app is restarted.
* */
c := make(chan struct{})
go func() {
p.wg.Wait()
close(c)
}()
select {
case <-c:
p.Log().Debug("Peer stopped successfully")
return nil
case <-time.After(timeout):
return errors.New("WATCHDOG_ENGAGED. A few goroutines leaked.")
}
}
func (p *Peer) pingLoop() {
ping := time.NewTimer(pingInterval)
defer p.wg.Done()
@ -274,24 +289,7 @@ func (p *Peer) pingLoop() {
}
}
func (p *Peer) watchdogLoop(reads <-chan struct{}) {
defer p.wg.Done()
hb := time.NewTimer(watchdogInterval)
defer hb.Stop()
for {
select {
case <-reads:
atomic.StoreInt32(&p.flaky, 0)
case <-hb.C:
atomic.StoreInt32(&p.flaky, 1)
case <-p.closed:
return
}
hb.Reset(watchdogInterval)
}
}
func (p *Peer) readLoop(errc chan<- error, reads chan<- struct{}) {
func (p *Peer) readLoop(errc chan<- error) {
defer p.wg.Done()
for {
msg, err := p.rw.ReadMsg()
@ -304,7 +302,6 @@ func (p *Peer) readLoop(errc chan<- error, reads chan<- struct{}) {
errc <- err
return
}
reads <- struct{}{}
}
}

View File

@ -52,7 +52,7 @@ const (
// Maximum time allowed for reading a complete message.
// This is effectively the amount of time a connection can be idle.
frameReadTimeout = 10 * time.Second
frameReadTimeout = 30 * time.Second
// Maximum amount of time allowed for writing a complete message.
frameWriteTimeout = 20 * time.Second
@ -327,6 +327,36 @@ func (srv *Server) RemovePeer(node *enode.Node) {
}
}
// DeletePeer deletes the given node forcefully.
func (srv *Server) DeletePeer(node *enode.Node) error {
peerIDStr := node.ID().String()
srv.log.Info("DeletePeer called", "peerID", peerIDStr)
var peer *Peer
for _, p := range srv.Peers() {
if p.ID() == node.ID() {
peer = p
break
}
}
if peer == nil {
err := errors.New("peer not found")
srv.log.Info("DeletePeer failed to match a peer", "peerID", peerIDStr, "err", err)
return err
}
select {
case srv.delpeer <- peerDrop{peer, errors.New("forced delete"), true}:
case <-srv.quit:
}
srv.log.Info("DeletePeer passed the request to delpeer channel", "peerID", peerIDStr)
return nil
}
// AddTrustedPeer adds the given node to a reserved whitelist which allows the
// node to always connect, even if the slot are full.
func (srv *Server) AddTrustedPeer(node *enode.Node) {

View File

@ -195,10 +195,6 @@ func (peer *Peer) expire() {
// broadcast iterates over the collection of envelopes and transmits yet unknown
// ones over the network.
func (peer *Peer) broadcast() error {
if peer.peer.IsFlaky() {
log.Trace("Waiting for a peer to restore communication", "ID", peer.peer.ID())
return nil
}
envelopes := peer.host.Envelopes()
bundle := make([]*Envelope, 0, len(envelopes))
for _, envelope := range envelopes {