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

ENR record for host #5537

Merged
merged 13 commits into from
Apr 10, 2019
Merged

ENR record for host #5537

merged 13 commits into from
Apr 10, 2019

Conversation

gumb0
Copy link
Member

@gumb0 gumb0 commented Mar 28, 2019

https://eips.ethereum.org/EIPS/eip-778

This creates ENR record for the host at program start, filling values based on --public-ip and --listen CLI parameters.

For example aleth --public-ip 13.74.189.147 --listen 30303 creates ENR like

[<signature>, 0, "id", "v4", "ip", 0xD4ABD93, "sec256k1", <compressed public key>, "tcp", 30303, "udp", 30303]

(sequence number is always 0 now, updating ENR will be added in later PRs)

At program exit ENR is serialized into network.rlp file and restored at the next program start.

cc @fjl

@codecov-io
Copy link

codecov-io commented Apr 2, 2019

Codecov Report

❗ No coverage uploaded for pull request base (master@dbbfd5b). Click here to learn what that means.
The diff coverage is 94.41%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master    #5537   +/-   ##
=========================================
  Coverage          ?   62.14%           
=========================================
  Files             ?      347           
  Lines             ?    28965           
  Branches          ?     3281           
=========================================
  Hits              ?    18000           
  Misses            ?     9787           
  Partials          ?     1178

@gumb0 gumb0 force-pushed the enr branch 3 times, most recently from cbb2735 to 8740d15 Compare April 4, 2019 13:58
@gumb0 gumb0 removed the in progress label Apr 4, 2019
@gumb0
Copy link
Member Author

gumb0 commented Apr 4, 2019

@chfast @halfalicious I think this is ready for review.

It's based on #5542 and should be rebased when it's merged.

Also I need to add a CHANGELOG item here, but maybe we merge this after 1.6.0 release?

@halfalicious
Copy link
Contributor

The failed tests might just be unreliable, see #5544

@gumb0
Copy link
Member Author

gumb0 commented Apr 8, 2019

Rebased.

libp2p/ENR.cpp Outdated
#include "ENR.h"
#include <libdevcore/SHA3.h>

static std::string const c_keyID = "id";
Copy link
Member

Choose a reason for hiding this comment

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

I don't think it is great to have global dynamically constructed objects.

https://godbolt.org/z/qZwVxl

Copy link
Member Author

Choose a reason for hiding this comment

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

Because of run-time initialization cost? Is it better to have dynamic allocation each time this code is run? (string is probably short-string-optimized, but vector isn't)

Copy link
Member

Choose a reason for hiding this comment

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

Yes, to avoid dynamic initialization during startup.

This can land, but I'm leaving it to consider changes.

libp2p/ENR.cpp Outdated Show resolved Hide resolved
Co-Authored-By: gumb0 <andrei@ethereum.org>
libp2p/ENR.cpp Outdated
constexpr char c_keyTCP[] = "tcp";
constexpr char c_keyUDP[] = "udp";
constexpr char c_IDV4[] = "v4";
constexpr size_t c_ENRMaxSize = 300;
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be nice to call out that this is in bytes (preferably by renaming the variable)

@halfalicious
Copy link
Contributor

I noticed you created the tests using Gtest...is the goal to switch everything over to Gtest eventually?

Copy link
Contributor

@halfalicious halfalicious left a comment

Choose a reason for hiding this comment

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

Didn't get a chance to look at the test code yet but if they all pass then they are presumably okay.

libp2p/ENR.cpp Show resolved Hide resolved
libp2p/ENR.cpp Outdated Show resolved Hide resolved
libp2p/ENR.cpp Outdated

// read key-values into vector first, to check the order
std::vector<std::pair<std::string const, bytes>> keyValues;
for (size_t i = 2; i < _rlp.itemCount(); i+= 2)
Copy link
Contributor

Choose a reason for hiding this comment

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

Technically the loop termination check should be i < _rlp.itemCount() - 1 (since you're increasing i by 2 on each loop iteration)

Copy link
Member Author

Choose a reason for hiding this comment

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

count - 1 looks dangerous for unsigned type (well in this case it shouldn't underflow though)

Also _rlp[i + 1] in the body loop should throw for out of bounds and not lead to UB.

Copy link
Contributor

@halfalicious halfalicious Apr 10, 2019

Choose a reason for hiding this comment

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

I think _rlp[i + 1] would only throw if it happened to point to protected memory (unless you have something like full page heap enabled) and I think the chances of this are low.

Copy link
Member Author

@gumb0 gumb0 Apr 10, 2019

Choose a reason for hiding this comment

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

I think you confuse it with arrays/vectors.

Actually I was not right - RLP::operator[] will not throw but return empty RLP for out of bounds - I forget this all the time. So in this case it will end up with key => "" if last value is absent, which is not ideal, but it's not an UB, I think I'll merge it like this for now.

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've added some comments to RLP::operator[] which is not very straight-forward a791c5b

libp2p/ENR.cpp Outdated Show resolved Hide resolved
libp2p/ENR.cpp Outdated
}

ENR::ENR(uint64_t _seq, std::map<std::string, bytes> const& _keyValues, SignFunction _signFunction)
: m_seq(_seq), m_map(_keyValues), m_signature(_signFunction(dev::ref(content())))
Copy link
Contributor

Choose a reason for hiding this comment

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

{} initializers?

libp2p/Host.cpp Show resolved Hide resolved
libp2p/Host.cpp Show resolved Hide resolved
libp2p/Host.cpp Outdated Show resolved Hide resolved
m_lastPing(chrono::steady_clock::time_point::min()),
m_capabilityHost(createCapabilityHost(*this))
{
cnetnote << "Id: " << id();
cnetnote << "ENR: " << m_enr;
Copy link
Contributor

Choose a reason for hiding this comment

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

In the interest of consistency should we call enr() here? (since we call id() rather than m_alias.pub())

libp2p/Host.cpp Outdated Show resolved Hide resolved
@gumb0
Copy link
Member Author

gumb0 commented Apr 9, 2019

I noticed you created the tests using Gtest...is the goal to switch everything over to Gtest eventually?

Yes, all unit tests.

Copy link
Contributor

@halfalicious halfalicious left a comment

Choose a reason for hiding this comment

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

Approved, only small issue is the loop iteration but I don't think that's a blocker

libp2p/ENR.cpp Show resolved Hide resolved
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants