aboutsummaryrefslogtreecommitdiffstats
path: root/test/libsolidity/SolidityOptimizer.cpp
diff options
context:
space:
mode:
authorchriseth <c@ethdev.com>2016-12-01 23:02:54 +0800
committerchriseth <c@ethdev.com>2016-12-01 23:04:47 +0800
commit5098e1eb15678859d1bd5e9172184d6525e03863 (patch)
tree9069fcc823ca4b27b6add0c7278f1086923eb36e /test/libsolidity/SolidityOptimizer.cpp
parentf1907bbb12ce6a65d781bee1e1faebd6cde261bd (diff)
downloaddexon-solidity-5098e1eb15678859d1bd5e9172184d6525e03863.tar.gz
dexon-solidity-5098e1eb15678859d1bd5e9172184d6525e03863.tar.zst
dexon-solidity-5098e1eb15678859d1bd5e9172184d6525e03863.zip
Count instructions without metadata hash.
Diffstat (limited to 'test/libsolidity/SolidityOptimizer.cpp')
-rw-r--r--test/libsolidity/SolidityOptimizer.cpp32
1 files changed, 22 insertions, 10 deletions
diff --git a/test/libsolidity/SolidityOptimizer.cpp b/test/libsolidity/SolidityOptimizer.cpp
index 123dfe86..2e2e0c6c 100644
--- a/test/libsolidity/SolidityOptimizer.cpp
+++ b/test/libsolidity/SolidityOptimizer.cpp
@@ -79,17 +79,13 @@ public:
bytes nonOptimizedBytecode = compileAndRunWithOptimizer(_sourceCode, _value, _contractName, false);
m_nonOptimizedContract = m_contractAddress;
bytes optimizedBytecode = compileAndRunWithOptimizer(_sourceCode, _value, _contractName, true);
- size_t nonOptimizedSize = 0;
- solidity::eachInstruction(nonOptimizedBytecode, [&](Instruction, u256 const&) {
- nonOptimizedSize++;
- });
- size_t optimizedSize = 0;
- solidity::eachInstruction(optimizedBytecode, [&](Instruction, u256 const&) {
- optimizedSize++;
- });
+ size_t nonOptimizedSize = numInstructions(nonOptimizedBytecode);
+ size_t optimizedSize = numInstructions(optimizedBytecode);
BOOST_CHECK_MESSAGE(
- nonOptimizedSize > optimizedSize,
- "Optimizer did not reduce bytecode size."
+ optimizedSize < nonOptimizedSize,
+ string("Optimizer did not reduce bytecode size. Non-optimized size: ") +
+ std::to_string(nonOptimizedSize) + " - optimized size: " +
+ std::to_string(optimizedSize)
);
m_optimizedContract = m_contractAddress;
}
@@ -173,6 +169,22 @@ public:
}
protected:
+ /// @returns the number of intructions in the given bytecode, not taking the metadata hash
+ /// into account.
+ size_t numInstructions(bytes const& _bytecode)
+ {
+ BOOST_REQUIRE(_bytecode.size() > 5);
+ size_t metadataSize = (_bytecode[_bytecode.size() - 2] << 8) + _bytecode[_bytecode.size() - 1];
+ BOOST_REQUIRE_MESSAGE(metadataSize == 0x29, "Invalid metadata size");
+ BOOST_REQUIRE(_bytecode.size() >= metadataSize + 2);
+ bytes realCode = bytes(_bytecode.begin(), _bytecode.end() - metadataSize - 2);
+ size_t instructions = 0;
+ solidity::eachInstruction(realCode, [&](Instruction, u256 const&) {
+ instructions++;
+ });
+ return instructions;
+ }
+
Address m_optimizedContract;
Address m_nonOptimizedContract;
};