Skip to content
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

Merged
merged 20 commits into from
Apr 17, 2020

Conversation

acud
Copy link
Member

@acud acud commented Apr 15, 2020

Imports Distance functionality from swarm to resolve certain ambiguities regarding which peer to route to when using only po function.

as part of #48

@acud acud assigned jmozah and acud Apr 15, 2020
@acud acud requested a review from janos April 15, 2020 11:05
@acud acud added the in progress ongoing development , hold out with review label Apr 15, 2020
@acud acud requested a review from nolash April 16, 2020 10:18
@acud acud added ready for review The PR is ready to be reviewed and removed in progress ongoing development , hold out with review labels Apr 16, 2020

// 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DistanceBytes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick

// 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.. from a

return ProxCmp(a, y, x), nil
}

// ProxCmp compares the distances x->a and y->a
Copy link
Contributor

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.

Copy link
Member Author

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)

Copy link
Member Author

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....

{
x: MustParseHexAddress("9100000000000000000000000000000000000000000000000000000000000000").Bytes(),
y: MustParseHexAddress("8200000000000000000000000000000000000000000000000000000000000000").Bytes(),
z: MustParseHexAddress("1200000000000000000000000000000000000000000000000000000000000000").Bytes(),
Copy link
Contributor

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

Copy link
Member Author

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

@@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Papers, please


cpeer := d.base // start checking closest from _self_
for _, peer := range connectedPeers {
switch d := swarm.ProxCmp(addr.Bytes(), cpeer.Bytes(), peer.Address.Bytes()); d {
Copy link
Contributor

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.

}
}

// check if node is actually the closest one to the chunk
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check if self...?

Copy link
Member Author

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

topology.Narcissus

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean topology.LoneGunman

@@ -186,6 +186,83 @@ func TestAddPeer(t *testing.T) {
})
}

// TestSyncPeer tests that SyncPeer method returns closest connected peer
Copy link
Contributor

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 {
Copy link
Contributor

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?

Copy link
Member Author

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

Comment on lines 12 to 13
// 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
Copy link
Member

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.

@acud acud requested review from janos and nolash April 16, 2020 15:45
@acud
Copy link
Member Author

acud commented Apr 16, 2020

@janos i pulled the closestPeer functionality in full.go to a separate function, as this will be needed for ChunkPeer method too.

// do nothing
case -1:
// current peer is closer
closest = peer
Copy link
Member Author

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?

Copy link
Member

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
Copy link
Member

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.

distanceCmpTests = []distanceCmpTest{
{
a: MustParseHexAddress("9100000000000000000000000000000000000000000000000000000000000000").Bytes(), // 10010001
x: MustParseHexAddress("8200000000000000000000000000000000000000000000000000000000000000").Bytes(), // 10000010 xor(91,82) = 9
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

= 19?

{
a: MustParseHexAddress("9100000000000000000000000000000000000000000000000000000000000000").Bytes(), // 10010001
x: MustParseHexAddress("8200000000000000000000000000000000000000000000000000000000000000").Bytes(), // 10000010 xor(91,82) = 9
y: MustParseHexAddress("1200000000000000000000000000000000000000000000000000000000000000").Bytes(), // 00010010 xor(91,12) = 87
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

= 3?

Copy link
Contributor

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 {
Copy link
Contributor

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",
Copy link
Contributor

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?

Copy link
Member Author

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?

@@ -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
Copy link
Contributor

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) {
Copy link
Contributor

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.

@acud acud requested a review from nolash April 17, 2020 11:50
@acud acud merged commit 7e40e57 into master Apr 17, 2020
@acud acud mentioned this pull request Apr 17, 2020
@acud acud deleted the upload-to-closest branch September 26, 2020 10:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready for review The PR is ready to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants