add error variables for p2p/host

no retry on newstream error
reduce number of retry

Signed-off-by: Leo Chen <leo@harmony.one>
pull/399/head
Leo Chen 6 years ago
parent 22b47dafc0
commit 65d3e56e67
  1. 9
      p2p/errors.go
  2. 6
      p2p/host/hostv2/hostv2.go
  3. 8
      p2p/host/message.go
  4. 6
      p2p/host/message_test.go

@ -0,0 +1,9 @@
package p2p
import "errors"
// Error of host package
var (
ErrNewStream = errors.New("[HOST]: new stream error")
ErrMsgWrite = errors.New("[HOST]: send message write error")
)

@ -7,6 +7,7 @@ import (
"github.com/ethereum/go-ethereum/log"
"github.com/harmony-one/harmony/p2p"
libp2p "github.com/libp2p/go-libp2p"
p2p_crypto "github.com/libp2p/go-libp2p-crypto"
p2p_host "github.com/libp2p/go-libp2p-host"
@ -186,12 +187,11 @@ func (host *HostV2) SendMessage(p p2p.Peer, message []byte) error {
if err != nil {
logger.Error("NewStream() failed", "peerID", p.PeerID,
"protocolID", ProtocolID, "error", err)
return fmt.Errorf("NewStream(%v, %v) failed: %v", p.PeerID,
ProtocolID, err)
return p2p.ErrNewStream
}
if nw, err := s.Write(message); err != nil {
logger.Error("Write() failed", "error", err)
return fmt.Errorf("Write() failed: %v", err)
return p2p.ErrMsgWrite
} else if nw < len(message) {
logger.Error("Short Write()", "expected", len(message), "actual", nw)
return io.ErrShortWrite

@ -83,10 +83,12 @@ func SelectMyPeers(peers []p2p.Peer, min int, max int) []p2p.Peer {
func send(h p2p.Host, peer p2p.Peer, message []byte, lostPeer chan p2p.Peer) {
// Add attack code here.
//attack.GetInstance().Run()
backoff := p2p.NewExpBackoff(150*time.Millisecond, 5*time.Second, 2)
backoff := p2p.NewExpBackoff(250*time.Millisecond, 5*time.Second, 2)
for trial := 0; trial < 10; trial++ {
if err := h.SendMessage(peer, message); err == nil {
for trial := 0; trial < 3; trial++ {
err := h.SendMessage(peer, message)
// No need to retry if new stream error or no error
if err == nil || err == p2p.ErrNewStream {
if trial > 0 {
log.Warn("retry send", "rety", trial)
}

@ -8,7 +8,7 @@ import (
"github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/p2p"
"github.com/harmony-one/harmony/p2p/host/hostv2"
"github.com/harmony-one/harmony/p2p/p2pimpl"
peer "github.com/libp2p/go-libp2p-peer"
multiaddr "github.com/multiformats/go-multiaddr"
@ -21,7 +21,7 @@ func TestSendMessage(test *testing.T) {
priKey1, pubKey1, _ := utils.GenKeyP2P(peer1.IP, peer1.Port)
peerID1, _ := peer.IDFromPublicKey(pubKey1)
peer1.PeerID = peerID1
host1 := hostv2.New(&peer1, priKey1)
host1, _ := p2pimpl.NewHost(&peer1, priKey1)
peer2 := p2p.Peer{IP: "127.0.0.1", Port: "9001"}
selfAddr2, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/0.0.0.0/tcp/%s", peer2.Port))
@ -29,7 +29,7 @@ func TestSendMessage(test *testing.T) {
priKey2, pubKey2, _ := utils.GenKeyP2P(peer2.IP, peer2.Port)
peerID2, _ := peer.IDFromPublicKey(pubKey2)
peer2.PeerID = peerID2
host2 := hostv2.New(&peer2, priKey2)
host2, _ := p2pimpl.NewHost(&peer2, priKey2)
msg := []byte{0x00, 0x01, 0x02, 0x03, 0x04}
if err := host1.AddPeer(&peer2); err != nil {

Loading…
Cancel
Save