Browse Source

Move to agreed network format.

cl-refactor
Gav Wood 11 years ago
parent
commit
1320449dc9
  1. 41
      libethereum/PeerNetwork.cpp

41
libethereum/PeerNetwork.cpp

@ -68,11 +68,10 @@ bool PeerSession::interpret(RLP const& _r)
// Grab their block chain off them. // Grab their block chain off them.
{ {
unsigned count = std::min<unsigned>(256, m_server->m_chain->details(m_server->m_latestBlockSent).number);
RLPStream s; RLPStream s;
prep(s).appendList(3); prep(s).appendList(2 + count);
s << (uint)GetChain; s << (uint)GetChain;
unsigned count = std::min<unsigned>(256, m_server->m_chain->details(m_server->m_latestBlockSent).number);
s.appendList(count);
auto h = m_server->m_chain->details(m_server->m_latestBlockSent).parent; auto h = m_server->m_chain->details(m_server->m_latestBlockSent).parent;
for (unsigned i = 0; i < count; ++i, h = m_server->m_chain->details(h).parent) for (unsigned i = 0; i < count; ++i, h = m_server->m_chain->details(h).parent)
s << h; s << h;
@ -100,44 +99,42 @@ bool PeerSession::interpret(RLP const& _r)
cout << std::setw(2) << m_socket.native_handle() << " | GetPeers" << endl; cout << std::setw(2) << m_socket.native_handle() << " | GetPeers" << endl;
std::vector<bi::tcp::endpoint> peers = m_server->potentialPeers(); std::vector<bi::tcp::endpoint> peers = m_server->potentialPeers();
RLPStream s; RLPStream s;
prep(s).appendList(2); prep(s).appendList(peers.size() + 1);
s << (uint)Peers; s << (uint)Peers;
s.appendList(peers.size());
for (auto i: peers) for (auto i: peers)
s.appendList(2) << i.address().to_v4().to_bytes() << i.port(); s.appendList(2) << i.address().to_v4().to_bytes() << i.port();
sealAndSend(s); sealAndSend(s);
break; break;
} }
case Peers: case Peers:
cout << std::setw(2) << m_socket.native_handle() << " | Peers (" << dec << _r[1].itemCount() << " entries)" << endl; cout << std::setw(2) << m_socket.native_handle() << " | Peers (" << dec << (_r.itemCount() - 1) << " entries)" << endl;
for (auto i: _r[1]) for (unsigned i = 1; i < _r.itemCount(); ++i)
{ {
auto ep = bi::tcp::endpoint(bi::address_v4(i[0].toArray<byte, 4>()), i[1].toInt<short>()); auto ep = bi::tcp::endpoint(bi::address_v4(_r[i][0].toArray<byte, 4>()), _r[i][1].toInt<short>());
m_server->m_incomingPeers.push_back(ep); m_server->m_incomingPeers.push_back(ep);
cout << "New peer: " << ep << endl; cout << "New peer: " << ep << endl;
} }
break; break;
case Transactions: case Transactions:
cout << std::setw(2) << m_socket.native_handle() << " | Transactions (" << dec << _r[1].itemCount() << " entries)" << endl; cout << std::setw(2) << m_socket.native_handle() << " | Transactions (" << dec << (_r.itemCount() - 1) << " entries)" << endl;
for (auto i: _r[1]) for (unsigned i = 1; i < _r.itemCount(); ++i)
{ {
m_server->m_incomingTransactions.push_back(i.data().toBytes()); m_server->m_incomingTransactions.push_back(_r[i].data().toBytes());
m_knownTransactions.insert(sha3(i.data())); m_knownTransactions.insert(sha3(_r[i].data()));
} }
break; break;
case Blocks: case Blocks:
cout << std::setw(2) << m_socket.native_handle() << " | Blocks (" << dec << _r[1].itemCount() << " entries)" << endl; cout << std::setw(2) << m_socket.native_handle() << " | Blocks (" << dec << (_r.itemCount() - 1) << " entries)" << endl;
for (auto i: _r[1]) for (unsigned i = 1; i < _r.itemCount(); ++i)
{ {
m_server->m_incomingBlocks.push_back(i.data().toBytes()); m_server->m_incomingBlocks.push_back(_r[i].data().toBytes());
m_knownBlocks.insert(sha3(i.data())); m_knownBlocks.insert(sha3(_r[i].data()));
} }
if (_r[1].itemCount()) // we received some - check if there's any more if (_r[1].itemCount()) // we received some - check if there's any more
{ {
RLPStream s; RLPStream s;
prep(s).appendList(3); prep(s).appendList(3);
s << (uint)GetChain; s << (uint)GetChain;
s.appendList(1);
s << sha3(_r[1][0].data()); s << sha3(_r[1][0].data());
s << 256; s << 256;
sealAndSend(s); sealAndSend(s);
@ -171,8 +168,7 @@ bool PeerSession::interpret(RLP const& _r)
// cout << latest << " - " << parent << endl; // cout << latest << " - " << parent << endl;
prep(s); prep(s);
s.appendList(2) << (uint)Blocks; s.appendList(1 + count) << (uint)Blocks;
s.appendList(count);
uint endNumber = m_server->m_chain->details(parent).number; uint endNumber = m_server->m_chain->details(parent).number;
uint startNumber = endNumber + count; uint startNumber = endNumber + count;
// cout << "Sending " << dec << count << " blocks from " << startNumber << " to " << endNumber << endl; // cout << "Sending " << dec << count << " blocks from " << startNumber << " to " << endNumber << endl;
@ -220,15 +216,14 @@ bool PeerSession::interpret(RLP const& _r)
cout << std::setw(2) << m_socket.native_handle() << " | NotInChain (" << noGood << ")" << endl; cout << std::setw(2) << m_socket.native_handle() << " | NotInChain (" << noGood << ")" << endl;
if (noGood != m_server->m_chain->genesisHash()) if (noGood != m_server->m_chain->genesisHash())
{ {
unsigned count = std::min<unsigned>(256, m_server->m_chain->details(noGood).number);
RLPStream s; RLPStream s;
prep(s).appendList(3); prep(s).appendList(2 + count);
s << (uint)GetChain; s << (uint)GetChain;
unsigned count = std::min<unsigned>(256, m_server->m_chain->details(noGood).number);
s.appendList(count);
auto h = m_server->m_chain->details(noGood).parent; auto h = m_server->m_chain->details(noGood).parent;
for (unsigned i = 0; i < count; ++i, h = m_server->m_chain->details(h).parent) for (unsigned i = 0; i < count; ++i, h = m_server->m_chain->details(h).parent)
s << h; s << h;
s << 2048; s << 256;
sealAndSend(s); sealAndSend(s);
} }
// else our peer obviously knows nothing if they're unable to give the descendents of the genesis! // else our peer obviously knows nothing if they're unable to give the descendents of the genesis!

Loading…
Cancel
Save