提交 e6498b37 作者: Jeromy

fix issue with blocks not being trimmed properly and being too large to be sent over the network

上级 f5295885
...@@ -220,6 +220,8 @@ func (bs *BitSwap) peerWantsBlock(p *peer.Peer, want string) { ...@@ -220,6 +220,8 @@ func (bs *BitSwap) peerWantsBlock(p *peer.Peer, want string) {
} }
bs.SendBlock(p, bblk) bs.SendBlock(p, bblk)
ledg.SentBytes(len(blk)) ledg.SentBytes(len(blk))
} else {
u.DOut("Decided not to send block.")
} }
} }
...@@ -248,7 +250,7 @@ func (bs *BitSwap) GetLedger(p *peer.Peer) *Ledger { ...@@ -248,7 +250,7 @@ func (bs *BitSwap) GetLedger(p *peer.Peer) *Ledger {
} }
l = new(Ledger) l = new(Ledger)
l.Strategy = StandardStrategy l.Strategy = bs.strategy
l.Partner = p l.Partner = p
bs.partners[p.Key()] = l bs.partners[p.Key()] = l
return l return l
......
...@@ -75,6 +75,7 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) { ...@@ -75,6 +75,7 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
swap.SetStrategy(bitswap.YesManStrategy)
} }
bs, err := bserv.NewBlockService(d, swap) bs, err := bserv.NewBlockService(d, swap)
......
...@@ -24,7 +24,7 @@ func SplitterBySize(n int) BlockSplitter { ...@@ -24,7 +24,7 @@ func SplitterBySize(n int) BlockSplitter {
return return
} }
if nread < n { if nread < n {
chunk = chunk[:n] chunk = chunk[:nread]
} }
out <- chunk out <- chunk
} }
......
...@@ -13,6 +13,8 @@ import ( ...@@ -13,6 +13,8 @@ import (
// ChanBuffer is the size of the buffer in the Conn Chan // ChanBuffer is the size of the buffer in the Conn Chan
const ChanBuffer = 10 const ChanBuffer = 10
const MaxMessageSize = 1 << 19
// Conn represents a connection to another Peer (IPFS Node). // Conn represents a connection to another Peer (IPFS Node).
type Conn struct { type Conn struct {
Peer *peer.Peer Peer *peer.Peer
...@@ -66,13 +68,14 @@ func newConnChans(c *Conn) error { ...@@ -66,13 +68,14 @@ func newConnChans(c *Conn) error {
c.Closed = make(chan bool, 1) c.Closed = make(chan bool, 1)
go c.Outgoing.WriteTo(c.Conn) go c.Outgoing.WriteTo(c.Conn)
go c.Incoming.ReadFrom(c.Conn, 1<<12) go c.Incoming.ReadFrom(c.Conn, MaxMessageSize)
return nil return nil
} }
// Close closes the connection, and associated channels. // Close closes the connection, and associated channels.
func (s *Conn) Close() error { func (s *Conn) Close() error {
u.DOut("Closing Conn.\n")
if s.Conn == nil { if s.Conn == nil {
return fmt.Errorf("Already closed") // already closed return fmt.Errorf("Already closed") // already closed
} }
......
...@@ -279,6 +279,10 @@ func (s *Swarm) fanOut() { ...@@ -279,6 +279,10 @@ func (s *Swarm) fanOut() {
return return
} }
if len(msg.Data) > MaxMessageSize {
s.Error(fmt.Errorf("Exceeded max message size! (tried to send len = %d)", len(msg.Data)))
}
s.connsLock.RLock() s.connsLock.RLock()
conn, found := s.conns[msg.Peer.Key()] conn, found := s.conns[msg.Peer.Key()]
s.connsLock.RUnlock() s.connsLock.RUnlock()
...@@ -459,6 +463,7 @@ func (s *Swarm) ConnectNew(addr *ma.Multiaddr) (*peer.Peer, error) { ...@@ -459,6 +463,7 @@ func (s *Swarm) ConnectNew(addr *ma.Multiaddr) (*peer.Peer, error) {
// Removes a given peer from the swarm and closes connections to it // Removes a given peer from the swarm and closes connections to it
func (s *Swarm) Drop(p *peer.Peer) error { func (s *Swarm) Drop(p *peer.Peer) error {
u.DOut("Dropping peer: [%s]\n", p.ID.Pretty())
s.connsLock.RLock() s.connsLock.RLock()
conn, found := s.conns[u.Key(p.ID)] conn, found := s.conns[u.Key(p.ID)]
s.connsLock.RUnlock() s.connsLock.RUnlock()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论