This repository has been archived by the owner on May 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Event driven AutoNAT #98
Draft
aarshkshah1992
wants to merge
4
commits into
master
Choose a base branch
from
feat/support-multiple-addrs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package addrset | ||
|
||
import ( | ||
ma "github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
// AddrState describes the state of a dialable addresses as seen by AutoNAT. | ||
type AddrState int | ||
|
||
const ( | ||
// StateUnknown is used for new entries. | ||
StateUnknown AddrState = iota | ||
// UnconfirmedListenUnknown applies to interface listen addresses that have unknown reachability. | ||
UnconfirmedListenUnknown | ||
|
||
// UnconfirmedExternalUnknown applies to external observed addresses with unknown reachability. | ||
UnconfirmedExternalUnknown | ||
|
||
// Private is applied to addresses that have been deemed to have private reachability by AutoNAT. | ||
Private | ||
|
||
// Confirmed is applied to peers that have been confirmed to be dialable by AutoNAT. | ||
Confirmed | ||
) | ||
|
||
// DialAddrsSet maintains the state of a dialable addresses as seen by AutoNAT. | ||
// The state is a set of multi-addresses, each labeled with a AddrState. | ||
type DialAddrsSet struct { | ||
// all known addresses | ||
all []dialAddrState | ||
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'd consider just using a map keyed on |
||
} | ||
|
||
type dialAddrState struct { | ||
addr ma.Multiaddr | ||
state AddrState | ||
} | ||
|
||
// DialAddrsSet creates a new empty set of dial addresses. | ||
func NewDialAddrsSet() *DialAddrsSet { | ||
return &DialAddrsSet{ | ||
all: []dialAddrState{}, | ||
} | ||
} | ||
|
||
func (ds *DialAddrsSet) find(a ma.Multiaddr) int { | ||
for i := range ds.all { | ||
if ds.all[i].addr.Equal(a) { | ||
return i | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
// TryAdd adds the address a to the address set. | ||
// If the address is already present, no action is taken. | ||
// Otherwise, the address is added with state set to StateUnknown. | ||
// TryAdd returns true if the address was not already present. | ||
func (ds *DialAddrsSet) TryAdd(a ma.Multiaddr) bool { | ||
if ds.find(a) >= 0 { | ||
return false | ||
} else { | ||
ds.all = append(ds.all, | ||
dialAddrState{addr: a, state: StateUnknown}) | ||
return true | ||
} | ||
} | ||
|
||
// SetState sets the state of address a to s. | ||
// If a is not in the address set, SetState panics. | ||
func (ds *DialAddrsSet) SetState(a ma.Multiaddr, s AddrState) { | ||
ds.all[ds.find(a)].state = s | ||
} | ||
|
||
// GetState returns the state of address a. | ||
// If a is not in the address set, GetState panics. | ||
func (ds *DialAddrsSet) GetState(a ma.Multiaddr) AddrState { | ||
return ds.all[ds.find(a)].state | ||
} | ||
|
||
// GetAddrsInState returns multiaddrs which are in one of the given states | ||
func (ds *DialAddrsSet) GetAddrsInStates(states ...AddrState) []ma.Multiaddr { | ||
var addrs []ma.Multiaddr | ||
for _, a := range ds.all { | ||
for _, s := range states { | ||
if a.state == s { | ||
addrs = append(addrs, a.addr) | ||
} | ||
} | ||
} | ||
|
||
return addrs | ||
} | ||
|
||
// AssertStateAndRemove aserts that address a is in the given state s (panics if is NOT). | ||
// It then remove the address from the address set. | ||
// AssertStateAndRemove will panic if a is NOT in the address set. | ||
func (ds *DialAddrsSet) AssertStateAndRemove(a ma.Multiaddr, s AddrState) { | ||
index := ds.find(a) | ||
|
||
if ds.all[index].state != s { | ||
panic("unexpected state") | ||
} | ||
|
||
ds.all[index] = ds.all[len(ds.all)-1] | ||
ds.all = ds.all[:len(ds.all)-1] | ||
} |
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,9 @@ | ||
package addrset | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestAddrsSet(t *testing.T) { | ||
|
||
} |
Oops, something went wrong.
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.
can we keep these states this with the network reach-ability ones?
More generally, it seems that there are two axes here that this enum can maybe track separately: those being *source and *confidence in reachability