Skip to content

Commit 5794f98

Browse files
committed
fixup! [libevmasm] Add support to import evm assembly json.
1 parent b9ba67f commit 5794f98

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
@@ -38,6 +38,7 @@
3838
#include <libsolutil/StringUtils.h>
3939

4040
#include <range/v3/algorithm/any_of.hpp>
41+
#include <range/v3/view/drop_exactly.hpp>
4142
#include <range/v3/view/enumerate.hpp>
4243

4344
#include <fstream>
@@ -618,40 +619,24 @@ std::pair<std::shared_ptr<Assembly>, std::vector<std::string>> Assembly::fromJSO
618619
solThrow(AssemblyImportException, "The value of key '" + dataItemID + "' inside '.data' is neither a hex-string nor an object.");
619620
}
620621
}
622+
621623
if (_level == 0)
622-
result->updatePaths();
624+
result->encodeAllPossibleSubPathsInAssemblyTree();
625+
623626
return std::make_pair(result, _level == 0 ? parsedSourceList : std::vector<std::string>{});
624627
}
625628

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

654-
++subId;
639+
m_subs[_pathFromRoot.back()]->encodeAllPossibleSubPathsInAssemblyTree(_pathFromRoot, _assembliesOnPath);
655640
}
656641
}
657642

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)