Browse Source

More on tree.

cl-refactor
Gav Wood 11 years ago
parent
commit
00f0bdb92e
  1. 29
      libethereum/BlockChain.cpp
  2. 4
      libethereum/BlockChain.h
  3. 9
      libethereum/BlockInfo.cpp
  4. 2
      libethereum/BlockInfo.h

29
libethereum/BlockChain.cpp

@ -38,6 +38,35 @@ BlockChain::~BlockChain()
void BlockChain::import(bytes const& _block) void BlockChain::import(bytes const& _block)
{ {
BlockInfo bi;
try
{
bi.populate(_block, 0);
auto newHash = sha256(_block);
// Check block doesn't already exist first!
if (m_numberAndParent.count(newHash))
return;
// Work out its number as the parent's number + 1
auto it = m_numberAndParent.find(bi.parentHash);
if (it == m_numberAndParent.end())
// We don't know the parent (yet) - discard for now. It'll get resent to us if we find out about its ancestry later on.
return;
bi.number = it->second.first + 1;
// Insert into DB
m_numberAndParent[newHash] = make_pair(bi.number, bi.parentHash);
m_children.insert(make_pair(bi.parentHash, newHash));
// TODO: put _block onto disk and load into cache.
// This might be the new last block; count back through ancestors to common shared ancestor and compare to current.
}
catch (...)
{
// Exit silently on exception(?)
return;
}
} }
bytesConstRef BlockChain::block(u256 _hash) const bytesConstRef BlockChain::block(u256 _hash) const

4
libethereum/BlockChain.h

@ -48,7 +48,7 @@ public:
BlockChain(); BlockChain();
~BlockChain(); ~BlockChain();
/// (Potentiall) renders invalid existing bytesConstRef returned by lastBlock. /// (Potentially) renders invalid existing bytesConstRef returned by lastBlock.
/// To be called from main loop every 100ms or so. /// To be called from main loop every 100ms or so.
void process(); void process();
@ -78,5 +78,3 @@ private:
}; };
} }

9
libethereum/BlockInfo.cpp

@ -73,18 +73,23 @@ void BlockInfo::populate(bytesConstRef _block, u256 _number)
} }
} }
void BlockInfo::verify(bytesConstRef _block, u256 _number) void BlockInfo::verify(bytesConstRef _block, u256 _number, u256 _parentHash)
{ {
populate(_block, _number); populate(_block, _number);
RLP root(_block); RLP root(_block);
if (root[0][0].toInt<u256>() != _parentHash)
throw InvalidParentHash();
if (sha256Transactions != sha256(root[1].data())) if (sha256Transactions != sha256(root[1].data()))
throw InvalidTransactionsHash(); throw InvalidTransactionsHash();
if (sha256Uncles != sha256(root[2].data())) if (sha256Uncles != sha256(root[2].data()))
throw InvalidUnclesHash(); throw InvalidUnclesHash();
// TODO: check timestamp. // TODO: check timestamp after previous timestamp.
// TODO: check parent's hash
// TODO: check difficulty against timestamp. // TODO: check difficulty against timestamp.
// TODO: check proof of work. // TODO: check proof of work.

2
libethereum/BlockInfo.h

@ -47,7 +47,7 @@ public:
static BlockInfo const& genesis() { if (!s_genesis) (s_genesis = new BlockInfo)->populateGenesis(); return *s_genesis; } static BlockInfo const& genesis() { if (!s_genesis) (s_genesis = new BlockInfo)->populateGenesis(); return *s_genesis; }
void populate(bytesConstRef _block, u256 _number); void populate(bytesConstRef _block, u256 _number);
void verify(bytesConstRef _block, u256 _number); void verify(bytesConstRef _block, u256 _number, u256 _parentHash);
static bytes createGenesisBlock(); static bytes createGenesisBlock();

Loading…
Cancel
Save