Skip to content

Commit f50ed59

Browse files
cameelr0qs
authored andcommitted
fixup! [libevmasm] Add support to import evm assembly json.
1 parent 80b96e0 commit f50ed59

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>
@@ -612,40 +613,24 @@ std::pair<std::shared_ptr<Assembly>, std::vector<std::string>> Assembly::fromJSO
612613
solThrow(AssemblyImportException, "The value of key '" + dataItemID + "' inside '.data' is neither a hex string nor an object.");
613614
}
614615
}
616+
615617
if (_level == 0)
616-
result->updatePaths();
618+
result->encodeAllPossibleSubPathsInAssemblyTree();
619+
617620
return std::make_pair(result, _level == 0 ? parsedSourceList : std::vector<std::string>{});
618621
}
619622

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

648-
++subId;
633+
m_subs[_pathFromRoot.back()]->encodeAllPossibleSubPathsInAssemblyTree(_pathFromRoot, _assembliesOnPath);
649634
}
650635
}
651636

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)