aboutsummaryrefslogtreecommitdiffstats
path: root/StructuralGasEstimator.cpp
diff options
context:
space:
mode:
authorchriseth <c@ethdev.com>2015-05-06 16:43:59 +0800
committerchriseth <c@ethdev.com>2015-05-06 16:43:59 +0800
commitc3e5fe6a1205068c9b5ded84f8ae96d97f855c1b (patch)
treea1a16e63550143e4dcbec6283be8b2f11710c80f /StructuralGasEstimator.cpp
parent22eaf8ecd80d3bf091826a4c71d07d4f6269ce80 (diff)
downloaddexon-solidity-c3e5fe6a1205068c9b5ded84f8ae96d97f855c1b.tar
dexon-solidity-c3e5fe6a1205068c9b5ded84f8ae96d97f855c1b.tar.gz
dexon-solidity-c3e5fe6a1205068c9b5ded84f8ae96d97f855c1b.tar.bz2
dexon-solidity-c3e5fe6a1205068c9b5ded84f8ae96d97f855c1b.tar.lz
dexon-solidity-c3e5fe6a1205068c9b5ded84f8ae96d97f855c1b.tar.xz
dexon-solidity-c3e5fe6a1205068c9b5ded84f8ae96d97f855c1b.tar.zst
dexon-solidity-c3e5fe6a1205068c9b5ded84f8ae96d97f855c1b.zip
Structural gas estimator.
Diffstat (limited to 'StructuralGasEstimator.cpp')
-rw-r--r--StructuralGasEstimator.cpp108
1 files changed, 108 insertions, 0 deletions
diff --git a/StructuralGasEstimator.cpp b/StructuralGasEstimator.cpp
new file mode 100644
index 00000000..2cf8c2a8
--- /dev/null
+++ b/StructuralGasEstimator.cpp
@@ -0,0 +1,108 @@
+/*
+ This file is part of cpp-ethereum.
+
+ cpp-ethereum is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ cpp-ethereum is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
+*/
+/**
+ * @author Christian <c@ethdev.com>
+ * @date 2015
+ * Gas consumption estimator working alongside the AST.
+ */
+
+#include "StructuralGasEstimator.h"
+#include <map>
+#include <functional>
+#include <libsolidity/AST.h>
+#include <libsolidity/ASTVisitor.h>
+
+using namespace std;
+using namespace dev;
+using namespace dev::eth;
+using namespace dev::solidity;
+
+map<ASTNode const*, GasMeter::GasConsumption[2]> StructuralGasEstimator::performEstimation(
+ AssemblyItems const& _items,
+ vector<ASTNode const*> const& _ast
+)
+{
+ map<SourceLocation, GasMeter::GasConsumption> particularCosts;
+ GasMeter meter;
+
+ for (auto const& item: _items)
+ particularCosts[item.getLocation()] += meter.estimateMax(item);
+
+ map<ASTNode const*, GasMeter::GasConsumption[2]> gasCosts;
+ auto onNode = [&](ASTNode const& _node)
+ {
+ gasCosts[&_node][0] = gasCosts[&_node][1] = particularCosts[_node.getLocation()];
+ return true;
+ };
+ auto onEdge = [&](ASTNode const& _parent, ASTNode const& _child)
+ {
+ gasCosts[&_parent][1] += gasCosts[&_child][1];
+ };
+ ASTReduce folder(onNode, onEdge);
+ for (ASTNode const* ast: _ast)
+ ast->accept(folder);
+
+ return gasCosts;
+}
+
+map<ASTNode const*, GasMeter::GasConsumption> StructuralGasEstimator::breakToStatementLevel(
+ map<ASTNode const*, GasMeter::GasConsumption[2]> const& _gasCosts,
+ vector<ASTNode const*> const& _roots
+)
+{
+ // first pass: statementDepth[node] is the distance from the deepend statement to node
+ // in direction of the tree root (or undefined if not possible)
+ map<ASTNode const*, int> statementDepth;
+ auto onNodeFirstPass = [&](ASTNode const& _node)
+ {
+ if (dynamic_cast<Statement const*>(&_node))
+ statementDepth[&_node] = 0;
+ return true;
+ };
+ auto onEdgeFirstPass = [&](ASTNode const& _parent, ASTNode const& _child)
+ {
+ if (statementDepth.count(&_child))
+ statementDepth[&_parent] = max(statementDepth[&_parent], statementDepth[&_child] + 1);
+ };
+ ASTReduce firstPass(onNodeFirstPass, onEdgeFirstPass);
+ for (ASTNode const* node: _roots)
+ node->accept(firstPass);
+
+ // we use the location of a node if
+ // - its statement depth is 0 or
+ // - its statement depth is undefined but the parent's statement depth is at least 1
+ map<ASTNode const*, GasMeter::GasConsumption> gasCosts;
+ auto onNodeSecondPass = [&](ASTNode const& _node)
+ {
+ return statementDepth.count(&_node);
+ };
+ auto onEdgeSecondPass = [&](ASTNode const& _parent, ASTNode const& _child)
+ {
+ bool useNode = false;
+ if (statementDepth.count(&_child))
+ useNode = statementDepth[&_child] == 0;
+ else
+ useNode = statementDepth.count(&_parent) && statementDepth.at(&_parent) > 0;
+ if (useNode)
+ gasCosts[&_child] = _gasCosts.at(&_child)[1];
+ };
+ ASTReduce secondPass(onNodeSecondPass, onEdgeSecondPass);
+ for (ASTNode const* node: _roots)
+ node->accept(secondPass);
+ // gasCosts should only contain non-overlapping locations
+ return gasCosts;
+}