Refactor and improve the SHAMap code:

This commit combines a number of cleanups, targeting both the
code structure and the code logic. Large changes include:

 - Using more strongly-typed classes for SHAMap nodes, instead of relying
   on runtime-time detection of class types. This change saves 16 bytes
   of memory per node.
 - Improving the interface of SHAMap::addGiveItem and SHAMap::addItem to
   avoid the need for passing two bool arguments.
 - Documenting the "copy-on-write" semantics that SHAMap uses to
   efficiently track changes in individual nodes.
 - Removing unused code and simplifying several APIs.
 - Improving function naming.
This commit is contained in:
Nik Bougalis
2020-11-13 23:30:43 -08:00
parent 5def79e93c
commit 1bb294afbc
49 changed files with 1641 additions and 1378 deletions

View File

@@ -283,15 +283,14 @@ class DatabaseShard_test : public TestBase
}
// Store the state map
auto visitAcc = [&](SHAMapAbstractNode& node) {
auto visitAcc = [&](SHAMapTreeNode const& node) {
Serializer s;
node.serializeWithPrefix(s);
db.store(
node.getType() == SHAMapAbstractNode::TNType::tnINNER
? hotUNKNOWN
: hotACCOUNT_NODE,
node.getType() == SHAMapNodeType::tnINNER ? hotUNKNOWN
: hotACCOUNT_NODE,
std::move(s.modData()),
node.getNodeHash().as_uint256(),
node.getHash().as_uint256(),
ledger.info().seq);
return true;
};
@@ -311,15 +310,14 @@ class DatabaseShard_test : public TestBase
}
// Store the transaction map
auto visitTx = [&](SHAMapAbstractNode& node) {
auto visitTx = [&](SHAMapTreeNode& node) {
Serializer s;
node.serializeWithPrefix(s);
db.store(
node.getType() == SHAMapAbstractNode::TNType::tnINNER
? hotUNKNOWN
: hotTRANSACTION_NODE,
node.getType() == SHAMapNodeType::tnINNER ? hotUNKNOWN
: hotTRANSACTION_NODE,
std::move(s.modData()),
node.getNodeHash().as_uint256(),
node.getHash().as_uint256(),
ledger.info().seq);
return true;
};
@@ -357,20 +355,19 @@ class DatabaseShard_test : public TestBase
LedgerFill{*fetched, LedgerFill::full | LedgerFill::binary}));
// walk shamap and validate each node
auto fcompAcc = [&](SHAMapAbstractNode& node) -> bool {
auto fcompAcc = [&](SHAMapTreeNode& node) -> bool {
Serializer s;
node.serializeWithPrefix(s);
auto nSrc{NodeObject::createObject(
node.getType() == SHAMapAbstractNode::TNType::tnINNER
? hotUNKNOWN
: hotACCOUNT_NODE,
node.getType() == SHAMapNodeType::tnINNER ? hotUNKNOWN
: hotACCOUNT_NODE,
std::move(s.modData()),
node.getNodeHash().as_uint256())};
node.getHash().as_uint256())};
if (!BEAST_EXPECT(nSrc))
return false;
auto nDst = db.fetchNodeObject(
node.getNodeHash().as_uint256(), ledger.info().seq);
node.getHash().as_uint256(), ledger.info().seq);
if (!BEAST_EXPECT(nDst))
return false;
@@ -381,20 +378,19 @@ class DatabaseShard_test : public TestBase
if (ledger.stateMap().getHash().isNonZero())
ledger.stateMap().snapShot(false)->visitNodes(fcompAcc);
auto fcompTx = [&](SHAMapAbstractNode& node) -> bool {
auto fcompTx = [&](SHAMapTreeNode& node) -> bool {
Serializer s;
node.serializeWithPrefix(s);
auto nSrc{NodeObject::createObject(
node.getType() == SHAMapAbstractNode::TNType::tnINNER
? hotUNKNOWN
: hotTRANSACTION_NODE,
node.getType() == SHAMapNodeType::tnINNER ? hotUNKNOWN
: hotTRANSACTION_NODE,
std::move(s.modData()),
node.getNodeHash().as_uint256())};
node.getHash().as_uint256())};
if (!BEAST_EXPECT(nSrc))
return false;
auto nDst = db.fetchNodeObject(
node.getNodeHash().as_uint256(), ledger.info().seq);
node.getHash().as_uint256(), ledger.info().seq);
if (!BEAST_EXPECT(nDst))
return false;