-
Notifications
You must be signed in to change notification settings - Fork 337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[bee #48] upload syncs to closest - add SyncPeer method #97
Conversation
|
||
// DistanceRaw returns the distance between address x and address y in big-endian binary format using the distance metric defined in the swarm specfication | ||
// Fails if not all addresses are of equal length | ||
func DistanceRaw(x, y []byte) ([]byte, error) { |
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.
DistanceBytes
?
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.
nitpick
pkg/swarm/distance.go
Outdated
// DistanceCmp compares x and y to a in terms of the distance metric defined in the swarm specfication | ||
// it returns: | ||
// 1 if x is closer to a than y | ||
// 0 if x and y are equally far apart from (this means that x and y are the same address) |
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.
.. from a
pkg/swarm/distance.go
Outdated
return ProxCmp(a, y, x), nil | ||
} | ||
|
||
// ProxCmp compares the distances x->a and y->a |
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.
It is not clear why you need both of these. It seems that all the DistanceCmp
adds is a length check. If so, perhaps this function should not be exported.
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.
unrelated, but the original implementer chose to swap x
and y
when calling ProxCmp
...(L47)
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.
i hope no one used this....
pkg/swarm/distance_test.go
Outdated
{ | ||
x: MustParseHexAddress("9100000000000000000000000000000000000000000000000000000000000000").Bytes(), | ||
y: MustParseHexAddress("8200000000000000000000000000000000000000000000000000000000000000").Bytes(), | ||
z: MustParseHexAddress("1200000000000000000000000000000000000000000000000000000000000000").Bytes(), |
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.
Why not use same letters as in implementation for clarity? z
-> y
a
-> x
x
-> y
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.
you tell me... its your implementation :D
pkg/topology/full/full.go
Outdated
@@ -130,6 +133,34 @@ func (d *Driver) ChunkPeer(addr swarm.Address) (peerAddr swarm.Address, err erro | |||
return swarm.Address{}, topology.ErrNotFound | |||
} | |||
|
|||
func (d *Driver) SyncPeer(addr swarm.Address) (peerAddr swarm.Address, err error) { |
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.
Papers, please
pkg/topology/full/full.go
Outdated
|
||
cpeer := d.base // start checking closest from _self_ | ||
for _, peer := range connectedPeers { | ||
switch d := swarm.ProxCmp(addr.Bytes(), cpeer.Bytes(), peer.Address.Bytes()); d { |
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.
I find it more readable to split such in two lines. Feel free to ignore this message.
pkg/topology/full/full.go
Outdated
} | ||
} | ||
|
||
// check if node is actually the closest one to the chunk |
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.
check if self...?
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.
i can english
|
||
// check if node is actually the closest one to the chunk | ||
if cpeer.Equal(d.base) { | ||
return swarm.Address{}, topology.ErrWantSelf |
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.
topology.Narcissus
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.
you mean topology.LoneGunman
pkg/topology/full/full_test.go
Outdated
@@ -186,6 +186,83 @@ func TestAddPeer(t *testing.T) { | |||
}) | |||
} | |||
|
|||
// TestSyncPeer tests that SyncPeer method returns closest connected peer |
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.
... for any given chunk
@@ -25,3 +27,7 @@ type PeerAdder interface { | |||
type ChunkPeerer interface { | |||
ChunkPeer(addr swarm.Address) (peerAddr swarm.Address, err error) | |||
} | |||
|
|||
type SyncPeerer interface { |
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.
"Peerer
" ... is that even a word?
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.
go convention, let's not get into this
pkg/swarm/distance.go
Outdated
// Distance returns the distance between address x and address y as a (comparable) big integer using the distance metric defined in the swarm specification | ||
// Fails if not all addresses are of equal length |
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.
Comments with full sentences.
Applies to all comments in this package.
@janos i pulled the |
// do nothing | ||
case -1: | ||
// current peer is closer | ||
closest = peer |
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.
@janos do i need to shadow peer
on L156
as a result?
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.
I do not think that it is needed.
// do nothing | ||
case -1: | ||
// current peer is closer | ||
closest = peer |
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.
I do not think that it is needed.
pkg/swarm/distance_test.go
Outdated
distanceCmpTests = []distanceCmpTest{ | ||
{ | ||
a: MustParseHexAddress("9100000000000000000000000000000000000000000000000000000000000000").Bytes(), // 10010001 | ||
x: MustParseHexAddress("8200000000000000000000000000000000000000000000000000000000000000").Bytes(), // 10000010 xor(91,82) = 9 |
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.
= 19?
pkg/swarm/distance_test.go
Outdated
{ | ||
a: MustParseHexAddress("9100000000000000000000000000000000000000000000000000000000000000").Bytes(), // 10010001 | ||
x: MustParseHexAddress("8200000000000000000000000000000000000000000000000000000000000000").Bytes(), // 10000010 xor(91,82) = 9 | ||
y: MustParseHexAddress("1200000000000000000000000000000000000000000000000000000000000000").Bytes(), // 00010010 xor(91,12) = 87 |
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.
= 3?
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.
sorry 83 of course
dy := y[i] ^ a[i] | ||
if dx == dy { | ||
continue | ||
} else if dx < dy { |
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.
Good catch!
{ | ||
x: MustParseHexAddress("9100000000000000000000000000000000000000000000000000000000000000").Bytes(), | ||
y: MustParseHexAddress("8200000000000000000000000000000000000000000000000000000000000000").Bytes(), | ||
result: "8593944123082061379093159043613555660984881674403010612303492563087302590464", |
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.
Oh, an easily verifiable number. Any reason why we have to run these testes with 32 byte ints?
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.
you mean why we don't?
pkg/topology/full/full_test.go
Outdated
@@ -186,33 +186,19 @@ func TestAddPeer(t *testing.T) { | |||
}) | |||
} | |||
|
|||
// TestSyncPeer tests that SyncPeer method returns closest connected peer | |||
// TestSyncPeer tests that SyncPeer method returns closest connected peer to a given chunk |
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.
period
return closestPeer(addr, d.base, overlays) | ||
} | ||
|
||
func closestPeer(addr, self swarm.Address, peers []swarm.Address) (swarm.Address, error) { |
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.
let's add a comment just to be nice.
Imports Distance functionality from swarm to resolve certain ambiguities regarding which peer to route to when using only
po
function.as part of #48