Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

swarm/network: Use different privatekey for bzz overlay in sim #1304

Closed
wants to merge 21 commits into from

Conversation

nolash
Copy link
Contributor

@nolash nolash commented Mar 19, 2019

Depends on #1302

A swarm node has different values for swarm overlay address and p2p connectivity id. However, in simulations the same value is used. This has in some cases led to confusion.

This PR provides a function to generate a swarm overlay address that is deterministically dependent on the p2p id (more precisely, its private key) but where the resulting value is different. The private key will be available through a respective simulation bucket.

When generating networks using the swarm/network/simulation package, this change is now built in (see swarm/network/simulation/node.go:AddNode). However, when booting from snapshots, it is currently up to the client code defining the node.Service implementations to explicitly choose different values.

An example of the latter is implemented for the swarm/pss/prox_test.go. Corresponding changes for other simulation code will be amended in future PR(s).

swarm/pss/prox_test.go Show resolved Hide resolved
Copy link
Contributor

@holisticode holisticode left a comment

Choose a reason for hiding this comment

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

just a minor thing I believe

p2p/simulations/adapters/exec.go Outdated Show resolved Hide resolved
p2p/simulations/adapters/exec.go Outdated Show resolved Hide resolved
// returns the private key used to generate the bzz key AND the generated bzz key
func BzzKeyFromConfig(conf *adapters.NodeConfig) (*ecdsa.PrivateKey, []byte, error) {
// ecdsa.GenerateKey takes 40 bytes entropy
privKeyBuf := append(crypto.FromECDSA(conf.PrivateKey), []byte{0x62, 0x7a, 0x7a, 0x62, 0x7a, 0x7a, 0x62, 0x7a}...)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you extract []byte{0x62, 0x7a, 0x7a, 0x62, 0x7a, 0x7a, 0x62, 0x7a} into a variable so that we don't have magic constants? What these hex number anyway? I don't know what they mean.

Copy link
Contributor Author

@nolash nolash Mar 20, 2019

Choose a reason for hiding this comment

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

Hint: hex.

And I tend to think that a bit of cryptic easter egg fun in the code raises spirits. You don't agree, perhaps?

Copy link
Contributor

Choose a reason for hiding this comment

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

I see these are hex numbers, but still magic constants. <- 🔥

So, I guess I disagree.
As I feel we already have enough cryptic code to understand. 😉
Let's make fun of solving the already existing puzzles first. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's make fun of solving the already existing puzzles first.

That would be magic.

Copy link
Member

Choose a reason for hiding this comment

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

fun is ok bzz, but why are we doing this again? what was wrong with this?

swarm/network/simulation/simulation.go Show resolved Hide resolved
swarm/pss/prox_test.go Show resolved Hide resolved
swarm/pss/prox_test.go Show resolved Hide resolved
swarm/pss/prox_test.go Outdated Show resolved Hide resolved
swarm/pss/prox_test.go Show resolved Hide resolved
@frncmx
Copy link
Contributor

frncmx commented Mar 20, 2019

Okay. getCmdParams is splitting the test name into params. Well, I disagree with the naming. It does not deal with command line arguments so Cmd is improper. But please, do use table driven tests instead. You don't need string splitting and parsing that way.

Copy link
Member

@zelig zelig left a comment

Choose a reason for hiding this comment

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

minor comments

p2p/simulations/adapters/exec.go Outdated Show resolved Hide resolved
p2p/simulations/adapters/types.go Outdated Show resolved Hide resolved
// returns the private key used to generate the bzz key AND the generated bzz key
func BzzKeyFromConfig(conf *adapters.NodeConfig) (*ecdsa.PrivateKey, []byte, error) {
// ecdsa.GenerateKey takes 40 bytes entropy
privKeyBuf := append(crypto.FromECDSA(conf.PrivateKey), []byte{0x62, 0x7a, 0x7a, 0x62, 0x7a, 0x7a, 0x62, 0x7a}...)
Copy link
Member

Choose a reason for hiding this comment

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

fun is ok bzz, but why are we doing this again? what was wrong with this?

@nolash
Copy link
Contributor Author

nolash commented Mar 21, 2019

fun is ok bzz, but why are we doing this again? what was wrong with this?

@zelig we should not use the same key for p2p and swarm. We should enforce that separation both in binary (which wasn't done either, in fact) and in sims. bzz bzz.

@nolash
Copy link
Contributor Author

nolash commented Mar 21, 2019

@frncmx it's still "in process" ;) but thanks, yes, I will mind the comments.

@frncmx
Copy link
Contributor

frncmx commented Mar 22, 2019

@frncmx did you have any further comments, please? thanks.

After yesterday stand-up I thought that you will address the last remaining comment about BzzKeyFromConfig. That is extracting the magic array into a meaningful variable.

I understand that you think it's a funny Easter egg. Please keep in mind, that you write your code for others to understand and not to entertain them.


After looking at BzzKeyFromConfig() even more I understand even less. So, please try to fix my ignorance.

  • All the other places we generate the bzz key from the public key and not from the private key. Why do we use the private key here?
  • Further you say that ecdsa.GenerateKey() requires entropy as the second parameter is a slice called rand. However we don't really provide entropy but some deterministically crafted data. So the comment is confusing for me.
  • Could we generate the bzzkey as just is done in cmd/swarm/main.go:237 - keys() , i.e., bzzkey := crypto.Keccak256Hash(pubkey).Hex() as that seems simpler?

@nolash
Copy link
Contributor Author

nolash commented Mar 22, 2019

@frncmx

All the other places we generate the bzz key from the public key and not from the private key. Why do we use the private key here?

Did you actually read the description of the PR? Please read again along with ethereum/go-ethereum#19309 . If it is still not clear, I will try to add a more thorough explanation.

Further you say that ecdsa.GenerateKey() requires entropy as the second parameter is a slice called rand. However we don't really provide entropy but some deterministically crafted data. So the comment is confusing for me.

I will add a more precise comment.

Could we generate the bzzkey as just is done in cmd/swarm/main.go:237 - keys() , i.e., bzzkey := crypto.Keccak256Hash(pubkey).Hex() as that seems simpler?

No. In fact, cmd/swarm/main.go should also be changed to use the same function fo generating the key from private key as the others that were changed. Again, please read the description.

Please keep in mind, that you write your code for others to understand and not to entertain them.

I'm not sure if patronizing me is very useful.

@frncmx
Copy link
Contributor

frncmx commented Mar 22, 2019

Did you actually read the description of the PR? Please read again along with ethereum#19309 . If it is still not clear, I will try to add a more thorough explanation.

Yes, I read. To my understanding the description just says that the overlay address and p2p connectivity id should be different in simulations. As it used to be the same before. The description also says we should use the private key, but does not talk about the why.

As I've limited understanding of the code under change; I thought you might fill in the blanks for me. As switching from public key to private seemed like a radical change.

I will add a more precise comment.

Thanks.

No. In fact, cmd/swarm/main.go should also be changed to use the same function fo generating the key from private key as the others that were changed. Again, please read the description.

I read the description again. I might be mistaken, but I did not find the reference that we intended to change the key generation at other places. Also, I'm not sure when do we want to do that? Is that part of this change effort?

I'm not sure if patronizing me is very useful.

I did not mean that. I just don't understand why such a simple thing - like creating a variable called padding - results in a huge push-back from you. As I pointed out I find that code part hard to read and that could be easily fixed by a variable. If such simple changes require the amount of energy (explanations) we are currently putting in, that makes me very uncomfortable while reviewing your changes. In this specific case I feel I way exceeded the reasonable amount of energy spent on that one line. It just seems meaningless. So do as you wish.

@nolash
Copy link
Contributor Author

nolash commented Mar 22, 2019

As switching from public key to private seemed like a radical change.

Ahh, so there is a misunderstanding here. We are not generating it from the private key, still from the public key. But the name of this method is stale, from the point where it was actually returning the bzzkey. I've changed the method name. The only thing this method does is to generate the private key from whose public key the bzzkey will be created.

The pattern with "deterministic randomness" is used several places in our code. I prefer that kind of approach personally.

that makes me very uncomfortable while reviewing your changes.

I don't mean to make you feel uncomfortable. My apologies. But I simply don't agree with you that we can't risk a bit of fun once in awhile. And we have to be able to cope with disagreement, @frncmx . If there is a difference of opinion on something, we leave it to a third party to be the tiebreaker. And the tie was broken. There was no need to push this further.

That said, I've changed the comment description above this line. It should be pretty clear now that the given data is inconsequential, aswell as not pointing out that it's not actual entropy. Thanks for pointing that out.

@nolash nolash force-pushed the enr-bzz-derived-bzzkey branch 4 times, most recently from 8e46e42 to b16ddc7 Compare March 22, 2019 18:30
(amend commit to trigger ci - attempt 5)
@nolash
Copy link
Contributor Author

nolash commented Mar 22, 2019

Merged upstream as ethereum/go-ethereum#19313

@nolash nolash closed this Mar 22, 2019
@frncmx frncmx deleted the enr-bzz-derived-bzzkey branch March 22, 2019 21:12
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants