Cache hash computations.

This commit is contained in:
chriseth 2018-12-18 16:38:25 +01:00
parent a51a8368aa
commit bd52beedff
2 changed files with 25 additions and 8 deletions

View File

@ -619,6 +619,22 @@ tuple<int, int, int, int> CompilerStack::positionFromSourceLocation(SourceLocati
return make_tuple(++startLine, ++startColumn, ++endLine, ++endColumn); return make_tuple(++startLine, ++startColumn, ++endLine, ++endColumn);
} }
h256 const& CompilerStack::Source::keccak256() const
{
if (keccak256HashCached == h256{})
keccak256HashCached = dev::keccak256(scanner->source());
return keccak256HashCached;
}
h256 const& CompilerStack::Source::swarmHash() const
{
if (swarmHashCached == h256{})
swarmHashCached = dev::swarmHash(scanner->source());
return swarmHashCached;
}
StringMap CompilerStack::loadMissingSources(SourceUnit const& _ast, std::string const& _sourcePath) StringMap CompilerStack::loadMissingSources(SourceUnit const& _ast, std::string const& _sourcePath)
{ {
solAssert(m_stackState < ParsingSuccessful, ""); solAssert(m_stackState < ParsingSuccessful, "");
@ -860,16 +876,13 @@ string CompilerStack::createMetadata(Contract const& _contract) const
continue; continue;
solAssert(s.second.scanner, "Scanner not available"); solAssert(s.second.scanner, "Scanner not available");
meta["sources"][s.first]["keccak256"] = meta["sources"][s.first]["keccak256"] = "0x" + toHex(s.second.keccak256().asBytes());
"0x" + toHex(dev::keccak256(s.second.scanner->source()).asBytes());
if (m_metadataLiteralSources) if (m_metadataLiteralSources)
meta["sources"][s.first]["content"] = s.second.scanner->source(); meta["sources"][s.first]["content"] = s.second.scanner->source();
else else
{ {
meta["sources"][s.first]["urls"] = Json::arrayValue; meta["sources"][s.first]["urls"] = Json::arrayValue;
meta["sources"][s.first]["urls"].append( meta["sources"][s.first]["urls"].append("bzzr://" + toHex(s.second.swarmHash().asBytes()));
"bzzr://" + toHex(dev::swarmHash(s.second.scanner->source()).asBytes())
);
} }
} }
meta["settings"]["optimizer"]["enabled"] = m_optimize; meta["settings"]["optimizer"]["enabled"] = m_optimize;
@ -896,7 +909,7 @@ string CompilerStack::createMetadata(Contract const& _contract) const
return jsonCompactPrint(meta); return jsonCompactPrint(meta);
} }
bytes CompilerStack::createCBORMetadata(string _metadata, bool _experimentalMode) bytes CompilerStack::createCBORMetadata(string const& _metadata, bool _experimentalMode)
{ {
bytes cborEncodedHash = bytes cborEncodedHash =
// CBOR-encoding of the key "bzzr0" // CBOR-encoding of the key "bzzr0"

View File

@ -261,7 +261,11 @@ private:
std::shared_ptr<langutil::Scanner> scanner; std::shared_ptr<langutil::Scanner> scanner;
std::shared_ptr<SourceUnit> ast; std::shared_ptr<SourceUnit> ast;
bool isLibrary = false; bool isLibrary = false;
void reset() { scanner.reset(); ast.reset(); } h256 mutable keccak256HashCached;
h256 mutable swarmHashCached;
void reset() { *this = Source(); }
h256 const& keccak256() const;
h256 const& swarmHash() const;
}; };
/// The state per contract. Filled gradually during compilation. /// The state per contract. Filled gradually during compilation.
@ -315,7 +319,7 @@ private:
std::string createMetadata(Contract const& _contract) const; std::string createMetadata(Contract const& _contract) const;
/// @returns the metadata CBOR for the given serialised metadata JSON. /// @returns the metadata CBOR for the given serialised metadata JSON.
static bytes createCBORMetadata(std::string _metadata, bool _experimentalMode); static bytes createCBORMetadata(std::string const& _metadata, bool _experimentalMode);
/// @returns the computer source mapping string. /// @returns the computer source mapping string.
std::string computeSourceMapping(eth::AssemblyItems const& _items) const; std::string computeSourceMapping(eth::AssemblyItems const& _items) const;