Skip to content

Commit 8e2cdc0

Browse files
committed
fixup! [libevmasm] Add support to import evm assembly json.
1 parent ef30e28 commit 8e2cdc0

File tree

2 files changed

+13
-28
lines changed

2 files changed

+13
-28
lines changed

libevmasm/Assembly.cpp

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <fmt/format.h>
4141

4242
#include <range/v3/algorithm/any_of.hpp>
43+
#include <range/v3/view/drop_exactly.hpp>
4344
#include <range/v3/view/enumerate.hpp>
4445

4546
#include <fstream>
@@ -615,40 +616,24 @@ std::pair<std::shared_ptr<Assembly>, std::vector<std::string>> Assembly::fromJSO
615616
solThrow(AssemblyImportException, "The value of key '" + dataItemID + "' inside '.data' is neither a hex-string nor an object.");
616617
}
617618
}
619+
618620
if (_level == 0)
619-
result->updatePaths();
621+
result->encodeAllPossibleSubPathsInAssemblyTree();
622+
620623
return std::make_pair(result, _level == 0 ? parsedSourceList : std::vector<std::string>{});
621624
}
622625

623-
void Assembly::updatePaths(std::vector<Assembly*> const& _parents, std::vector<size_t> const& _absolutePathFromRoot)
626+
void Assembly::encodeAllPossibleSubPathsInAssemblyTree(std::vector<size_t> _pathFromRoot, std::vector<Assembly*> _assembliesOnPath)
624627
{
625-
size_t subId = 0;
626-
for (std::shared_ptr<Assembly> assembly: m_subs)
628+
_assembliesOnPath.push_back(this);
629+
for (_pathFromRoot.push_back(0); _pathFromRoot.back() < m_subs.size(); ++_pathFromRoot.back())
627630
{
628-
std::vector<Assembly*> parents{_parents};
629-
parents.push_back(this);
630-
631-
std::vector<size_t> absolutePathFromRoot{_absolutePathFromRoot};
632-
absolutePathFromRoot.emplace_back(subId);
633-
634-
int pindex = 0;
635-
for (Assembly* parent: parents)
636-
{
637-
if (pindex == 0)
638-
parent->encodeSubPath(absolutePathFromRoot);
639-
else
640-
{
641-
std::vector<size_t> relativePath{absolutePathFromRoot};
642-
for (int i = 0; i < pindex; ++i)
643-
relativePath.erase(relativePath.begin());
644-
parent->encodeSubPath(relativePath);
645-
}
646-
++pindex;
647-
}
648-
649-
assembly->updatePaths(parents, absolutePathFromRoot);
631+
for (size_t distanceFromRoot = 0; distanceFromRoot < _assembliesOnPath.size(); ++distanceFromRoot)
632+
_assembliesOnPath[distanceFromRoot]->encodeSubPath(
633+
_pathFromRoot | ranges::views::drop_exactly(distanceFromRoot) | ranges::to<std::vector>
634+
);
650635

651-
++subId;
636+
m_subs[_pathFromRoot.back()]->encodeAllPossibleSubPathsInAssemblyTree(_pathFromRoot, _assembliesOnPath);
652637
}
653638
}
654639

libevmasm/Assembly.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ class Assembly
201201

202202
Assembly const* subAssemblyById(size_t _subId) const;
203203

204-
void updatePaths(std::vector<Assembly *> const& _parents = {}, std::vector<size_t> const& _absolutePathFromRoot = {});
204+
void encodeAllPossibleSubPathsInAssemblyTree(std::vector<size_t> _pathFromRoot = {}, std::vector<Assembly*> _assembliesOnPath = {});
205205

206206
protected:
207207
/// 0 is reserved for exception

0 commit comments

Comments
 (0)