This repository has been archived by the owner on Feb 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce a ledger that stores which peers are waiting for a Cid
When receiving a new block (Engine.ReceiveFrom), we shouldn't have to loop over all peers in order to determine if they need this block. Instead, use a map to save which peers are waiting for a give Cid.
- Loading branch information
1 parent
a016d39
commit 99b09fe
Showing
3 changed files
with
104 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package decision | ||
|
||
import ( | ||
"github.com/ipfs/go-cid" | ||
"github.com/libp2p/go-libp2p-core/peer" | ||
) | ||
|
||
type peerLedger struct { | ||
cids map[cid.Cid]map[peer.ID]struct{} | ||
} | ||
|
||
func newPeerLedger() *peerLedger { | ||
return &peerLedger{cids: make(map[cid.Cid]map[peer.ID]struct{})} | ||
} | ||
|
||
func (l *peerLedger) Wants(p peer.ID, k cid.Cid) { | ||
m, ok := l.cids[k] | ||
if !ok { | ||
m = make(map[peer.ID]struct{}) | ||
l.cids[k]=m | ||
} | ||
m[p] = struct{}{} | ||
} | ||
|
||
func (l *peerLedger) CancelWant(p peer.ID, k cid.Cid) { | ||
m, ok := l.cids[k] | ||
if !ok { | ||
return | ||
} | ||
delete(m, p) | ||
if len(m) == 0 { | ||
delete(l.cids, k) | ||
} | ||
} | ||
|
||
func (l *peerLedger) Peers(k cid.Cid) []peer.ID { | ||
m, ok := l.cids[k] | ||
if !ok { | ||
return nil | ||
} | ||
peers := make([]peer.ID, 0, len(m)) | ||
for p := range m { | ||
peers = append(peers, p) | ||
} | ||
return peers | ||
} |