This repository was 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
Implement ReputationManager #581
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package reputationmanager | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
"github.com/AndreasBriese/bbloom" | ||
blocks "github.com/ipfs/go-block-format" | ||
"github.com/ipfs/go-cid" | ||
) | ||
|
||
type recentWantList struct { | ||
params *recentWantListParams | ||
oldWants bbloom.Bloom | ||
newWants bbloom.Bloom | ||
lk sync.RWMutex | ||
} | ||
|
||
type recentWantListParams struct { | ||
numEntries float64 | ||
wrongPositives float64 | ||
refreshInterval time.Duration | ||
} | ||
|
||
func newRecentWantList(params *recentWantListParams) *recentWantList { | ||
r := &recentWantList{ | ||
params: params, | ||
oldWants: bbloom.New(params.numEntries, params.wrongPositives), | ||
newWants: bbloom.New(params.numEntries, params.wrongPositives), | ||
} | ||
|
||
return r | ||
} | ||
|
||
func (r *recentWantList) loop(ctx context.Context) { | ||
ticker := time.NewTicker(r.params.refreshInterval) | ||
defer ticker.Stop() | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case <-ticker.C: | ||
r.swapWantLists() | ||
} | ||
} | ||
} | ||
|
||
func (r *recentWantList) swapWantLists() { | ||
r.lk.Lock() | ||
defer r.lk.Unlock() | ||
|
||
r.oldWants = r.newWants | ||
r.newWants = bbloom.New(r.params.numEntries, r.params.wrongPositives) | ||
} | ||
|
||
func (r *recentWantList) addWants(cids []cid.Cid) { | ||
r.lk.Lock() | ||
defer r.lk.Unlock() | ||
|
||
for _, c := range cids { | ||
r.newWants.Add(c.Bytes()) | ||
} | ||
} | ||
|
||
func (r *recentWantList) splitRecentlyWanted(blks []blocks.Block, presences []cid.Cid) (wantedBlks []blocks.Block, unwantedBlks []blocks.Block, wantedPresences []cid.Cid, unwantedPresences []cid.Cid) { | ||
r.lk.RLock() | ||
defer r.lk.RUnlock() | ||
|
||
for _, blk := range blks { | ||
entry := blk.Cid().Bytes() | ||
if r.newWants.Has(entry) || r.oldWants.Has(entry) { | ||
wantedBlks = append(wantedBlks, blk) | ||
} else { | ||
unwantedBlks = append(unwantedBlks, blk) | ||
} | ||
} | ||
|
||
for _, presence := range presences { | ||
entry := presence.Bytes() | ||
if r.newWants.Has(entry) || r.oldWants.Has(entry) { | ||
wantedPresences = append(wantedPresences, presence) | ||
} else { | ||
unwantedPresences = append(unwantedPresences, presence) | ||
} | ||
} | ||
|
||
return wantedBlks, unwantedBlks, wantedPresences, unwantedPresences | ||
} |
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,98 @@ | ||||||||
package reputationmanager | ||||||||
|
||||||||
import ( | ||||||||
"context" | ||||||||
"time" | ||||||||
|
||||||||
blocks "github.com/ipfs/go-block-format" | ||||||||
"github.com/ipfs/go-cid" | ||||||||
peer "github.com/libp2p/go-libp2p-peer" | ||||||||
) | ||||||||
|
||||||||
type ReputationManager struct { | ||||||||
rwl *recentWantList | ||||||||
scoreKeeper ScoreKeeper | ||||||||
params *ReputationManagerParams | ||||||||
thresholds *ReputationManagerThresholds | ||||||||
} | ||||||||
|
||||||||
type ReputationManagerParams struct { | ||||||||
RetainScore time.Duration | ||||||||
ScoreRefreshInterval time.Duration | ||||||||
} | ||||||||
|
||||||||
type ReputationManagerThresholds struct { | ||||||||
GrayListThreshold float64 | ||||||||
} | ||||||||
|
||||||||
type ScoreKeeper interface { | ||||||||
Score(p peer.ID) float64 | ||||||||
Update(p peer.ID, wantedBlks, unwantedBlks []blocks.Block, wantedPresences, unwantedPresences []cid.Cid) | ||||||||
PeerConnected(p peer.ID) | ||||||||
PeerDisconnected(p peer.ID) | ||||||||
} | ||||||||
|
||||||||
func NewReputationManager(params *ReputationManagerParams, thresholds *ReputationManagerThresholds, scoreKeeper ScoreKeeper) *ReputationManager { | ||||||||
rm := &ReputationManager{ | ||||||||
params: params, | ||||||||
thresholds: thresholds, | ||||||||
scoreKeeper: scoreKeeper, | ||||||||
// TODO: make these params configurable | ||||||||
rwl: newRecentWantList(&recentWantListParams{ | ||||||||
numEntries: float64(1 << 12), | ||||||||
wrongPositives: float64(0.01), | ||||||||
refreshInterval: 2 * time.Minute, | ||||||||
}), | ||||||||
} | ||||||||
|
||||||||
return rm | ||||||||
} | ||||||||
|
||||||||
func (r *ReputationManager) AcceptFrom(pid peer.ID) bool { | ||||||||
if r == nil { | ||||||||
return true | ||||||||
} | ||||||||
|
||||||||
return r.scoreKeeper.Score(pid) > r.thresholds.GrayListThreshold | ||||||||
} | ||||||||
|
||||||||
func (r *ReputationManager) AddWants(cids []cid.Cid) { | ||||||||
if r == nil { | ||||||||
return | ||||||||
} | ||||||||
|
||||||||
r.rwl.addWants(cids) | ||||||||
} | ||||||||
|
||||||||
func (r *ReputationManager) ReceivedFrom(pid peer.ID, blks []blocks.Block, presences []cid.Cid) { | ||||||||
if r == nil { | ||||||||
return | ||||||||
} | ||||||||
|
||||||||
wantedBlks, unwantedBlks, wantedPresences, unwantedPresences := r.rwl.splitRecentlyWanted(blks, presences) | ||||||||
r.scoreKeeper.Update(pid, wantedBlks, unwantedBlks, wantedPresences, unwantedPresences) | ||||||||
} | ||||||||
|
||||||||
func (r *ReputationManager) PeerConnected(pid peer.ID) { | ||||||||
if r == nil { | ||||||||
return | ||||||||
} | ||||||||
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 don't like this, if I call this incorrectly I want to know about it so I can fix the bug. (fail fast & fail loudly) Lines 175 to 177 in bc4fd4a
(I know tracing is virtual so we couldn't do the same thing you did here but anyway) Applies to this in all other methods. |
||||||||
|
||||||||
r.scoreKeeper.PeerConnected(pid) | ||||||||
} | ||||||||
|
||||||||
func (r *ReputationManager) PeerDisconnected(pid peer.ID) { | ||||||||
if r == nil { | ||||||||
return | ||||||||
} | ||||||||
|
||||||||
r.scoreKeeper.PeerDisconnected(pid) | ||||||||
} | ||||||||
|
||||||||
func (r *ReputationManager) Start(ctx context.Context) { | ||||||||
if r == nil { | ||||||||
return | ||||||||
} | ||||||||
|
||||||||
go r.rwl.loop(ctx) | ||||||||
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. You already used a goroutine earlier (I don't mind where you start it, just once not twice). |
||||||||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
AFAIT it's just fancy counters, sounds like a thing you could do with locks.
Is one more goroutine really needed ?