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

Support forkid in ENR #5897

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libethereum/BlockChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,9 @@ class BlockChain
/// Change the chain start block.
void setChainStartBlockNumber(unsigned _number);

FixedHash<4> forkHash() const { return {}; }
unsigned nextForkBlockNumber() const { return 0; }

private:
static h256 chunkId(unsigned _level, unsigned _index) { return h256(_index * 0xff + _level); }

Expand Down
7 changes: 7 additions & 0 deletions libp2p/Host.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,13 @@ class Host: public Worker
return m_nodeTable ? m_nodeTable->hostENR() : m_restoredENR;
}

void updateForkID(FixedHash<4> const& _forkHash, uint64_t _forkNext)
{
Guard l(x_nodeTable);
if (m_nodeTable)
m_nodeTable->updateENRForkID(_forkHash, _forkNext);
}

/// Apply function to each session
void forEachPeer(
std::string const& _capabilityName, std::function<bool(NodeID const&)> _f) const;
Expand Down
1 change: 1 addition & 0 deletions libp2p/NodeTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ shared_ptr<NodeEntry> NodeTable::handlePong(
newUdpEndpoint.address(), newUdpEndpoint.port(), m_hostNodeEndpoint.tcpPort()};
{
Guard l(m_hostENRMutex);
// TODO include m_hostForkHash and m_hostForkNext in ENR
m_hostENR =
IdentitySchemeV4::updateENR(m_hostENR, m_secret, m_hostNodeEndpoint.address(),
m_hostNodeEndpoint.tcpPort(), m_hostNodeEndpoint.udpPort());
Expand Down
19 changes: 19 additions & 0 deletions libp2p/NodeTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,23 @@ class NodeTable : UDPSocketEvents

void cancelTimer(std::shared_ptr<ba::steady_timer> _timer);

void updateENRForkID(FixedHash<4> const& _forkHash, uint64_t _forkNext)
{
if (_forkHash != m_hostForkHash && _forkNext != m_hostForkNext)
{
m_hostForkHash = _forkHash;
m_hostForkNext = _forkNext;

{
Guard l(m_hostENRMutex);
// TODO include forkHash and forkNext in ENR
m_hostENR = IdentitySchemeV4::updateENR(m_hostENR, m_secret, m_hostNodeEndpoint.address(),
m_hostNodeEndpoint.tcpPort(), m_hostNodeEndpoint.udpPort());
}
clog(VerbosityInfo, "net") << "ENR updated: " << m_hostENR;
}
}

// protected only for derived classes in tests
protected:
/**
Expand Down Expand Up @@ -339,6 +356,8 @@ class NodeTable : UDPSocketEvents
bi::address const m_hostStaticIP;
// Dynamically updated host endpoint
NodeIPEndpoint m_hostNodeEndpoint;
FixedHash<4> m_hostForkHash;
uint64_t m_hostForkNext = 0;
ENR m_hostENR;
mutable Mutex m_hostENRMutex;
Secret m_secret; ///< This nodes secret key.
Expand Down
9 changes: 9 additions & 0 deletions libwebthree/WebThree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ WebThreeDirect::WebThreeDirect(std::string const& _clientVersion,
m_ethereum.reset(new eth::Client(_params, (int)_params.networkID, m_net,
shared_ptr<GasPricer>(), _dbPath, _snapshotPath, _we));

m_chainChangedHandler =
m_ethereum->setOnChainChanged([this](h256s const&, h256s const&) { onChainChanged(); });

m_ethereum->startWorking();
const auto* buildinfo = aleth_get_buildinfo();
m_ethereum->setExtraData(rlpList(0, string{buildinfo->project_version}.substr(0, 5) + "++" +
Expand Down Expand Up @@ -105,3 +108,9 @@ void WebThreeDirect::addPeer(NodeSpec const& _s, PeerType _t)
m_net.addPeer(_s, _t);
}

void WebThreeDirect::onChainChanged()
{
const auto forkHash = m_ethereum->blockChain().forkHash();
const auto forkNext = m_ethereum->blockChain().nextForkBlockNumber();
m_net.updateForkID(forkHash, forkNext);
}
4 changes: 4 additions & 0 deletions libwebthree/WebThree.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,15 @@ class WebThreeDirect: public NetworkFace
bool isNetworkStarted() const override { return m_net.isStarted(); }

private:
void onChainChanged();

std::string m_clientVersion; ///< Our end-application client's name/version.

std::unique_ptr<eth::Client> m_ethereum; ///< Client for Ethereum ("eth") protocol.

p2p::Host m_net; ///< Should run in background and send us events when blocks found and allow us to send blocks as required.

eth::Handler<h256s const&, h256s const&> m_chainChangedHandler;
};


Expand Down