Skip to content

Commit 4af2272

Browse files
committed
fixup! [libevmasm] Add support to import evm assembly json.
1 parent 041d05c commit 4af2272

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

libevmasm/Assembly.cpp

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
#include <libsolutil/JSON.h>
3838
#include <libsolutil/StringUtils.h>
3939

40+
#include <fmt/format.h>
41+
4042
#include <range/v3/algorithm/any_of.hpp>
4143
#include <range/v3/view/enumerate.hpp>
4244

@@ -101,24 +103,19 @@ AssemblyItem Assembly::createAssemblyItemFromJSON(Json::Value const& _json, std:
101103
solRequire(
102104
validMembers.count(member),
103105
AssemblyImportException,
104-
"Unknown member '" + member + "'. Valid members are: " +
105-
solidity::util::joinHumanReadable(validMembers, ", ") + "."
106+
fmt::format(
107+
"Unknown member '{}'. Valid members are: {}.",
108+
member,
109+
solidity::util::joinHumanReadable(validMembers, ", ")
110+
)
106111
);
107112
solRequire(isOfType<std::string>(_json["name"]), AssemblyImportException, "Member 'name' missing or not of type string.");
108113
solRequire(isOfTypeIfExists<int>(_json, "begin"), AssemblyImportException, "Optional member 'begin' not of type int.");
109114
solRequire(isOfTypeIfExists<int>(_json, "end"), AssemblyImportException, "Optional member 'end' not of type int.");
110115
solRequire(isOfTypeIfExists<int>(_json, "source"), AssemblyImportException, "Optional member 'source' not of type int.");
111116
solRequire(isOfTypeIfExists<std::string>(_json, "value"), AssemblyImportException, "Optional member 'value' not of type string.");
112-
solRequire(
113-
isOfTypeIfExists<int>(_json, "modifierDepth"),
114-
AssemblyImportException,
115-
"Optional member 'modifierDepth' not of type int."
116-
);
117-
solRequire(
118-
isOfTypeIfExists<std::string>(_json, "jumpType"),
119-
AssemblyImportException,
120-
"Optional member 'jumpType' not of type string."
121-
);
117+
solRequire(isOfTypeIfExists<int>(_json, "modifierDepth"), AssemblyImportException, "Optional member 'modifierDepth' not of type int.");
118+
solRequire(isOfTypeIfExists<std::string>(_json, "jumpType"), AssemblyImportException, "Optional member 'jumpType' not of type string.");
122119

123120
std::string name = get<std::string>(_json["name"]);
124121
solRequire(!name.empty(), AssemblyImportException, "Member 'name' is empty.");
@@ -508,9 +505,13 @@ Json::Value Assembly::assemblyJSON(std::map<std::string, unsigned> const& _sourc
508505
return root;
509506
}
510507

511-
std::pair<std::shared_ptr<Assembly>, std::vector<std::string>> Assembly::fromJSON(Json::Value const& _json, std::vector<std::string> const& _sourceList, size_t _level)
508+
std::pair<std::shared_ptr<Assembly>, std::vector<std::string>> Assembly::fromJSON(
509+
Json::Value const& _json,
510+
std::vector<std::string> const& _sourceList,
511+
size_t _level
512+
)
512513
{
513-
auto result = std::make_shared<Assembly>(langutil::EVMVersion(), _level == 0, "");
514+
auto result = std::make_shared<Assembly>(EVMVersion{}, _level == 0 /* _creation */, "" /* _name */);
514515
if (_json.isNull())
515516
return std::make_pair(result, std::vector<std::string>());
516517

@@ -532,7 +533,8 @@ std::pair<std::shared_ptr<Assembly>, std::vector<std::string>> Assembly::fromJSO
532533
for (Json::Value const& sourceListItem: _json["sourceList"])
533534
solRequire(sourceListItem.isString(), AssemblyImportException, "The 'sourceList' array contains an item that is not a string.");
534535
}
535-
} else
536+
}
537+
else
536538
solRequire(
537539
!_json.isMember("sourceList"),
538540
AssemblyImportException,
@@ -552,16 +554,16 @@ std::pair<std::shared_ptr<Assembly>, std::vector<std::string>> Assembly::fromJSO
552554
);
553555
sourceList.emplace_back(it.asString());
554556
}
555-
} else
557+
}
558+
else
556559
sourceList = _sourceList;
557560

558561
result->importAssemblyItemsFromJSON(_json[".code"], sourceList);
559562
if (_json[".auxdata"])
560563
{
561564
solRequire(_json[".auxdata"].isString(), AssemblyImportException, "Optional member '.auxdata' is not a string.");
562-
bytes auxdata{fromHex(_json[".auxdata"].asString())};
563-
solRequire(!auxdata.empty(), AssemblyImportException, "Optional member '.auxdata' is not a valid hexadecimal string.");
564-
result->m_auxiliaryData = auxdata;
565+
result->m_auxiliaryData = fromHex(_json[".auxdata"].asString());
566+
solRequire(!result->m_auxiliaryData.empty(), AssemblyImportException, "Optional member '.auxdata' is not a valid hexadecimal string.");
565567
}
566568

567569
if (_json.isMember(".data"))
@@ -575,14 +577,11 @@ std::pair<std::shared_ptr<Assembly>, std::vector<std::string>> Assembly::fromJSO
575577
Json::Value const& code = data[dataItemID];
576578
if (code.isString())
577579
{
578-
if (!code.asString().empty())
579-
{
580-
bytes data_value{fromHex(code.asString())};
581-
solRequire(
582-
!data_value.empty(),
583-
AssemblyImportException,
584-
"Member '.data' contains a value for '" + dataItemID + "' that is not a valid hexadecimal string.");
585-
}
580+
solRequire(
581+
code.asString().empty() || !fromHex(code.asString()).empty(),
582+
AssemblyImportException,
583+
"Member '.data' contains a value for '" + dataItemID + "' that is not a valid hexadecimal string."
584+
);
586585
result->m_data[h256(fromHex(dataItemID))] = fromHex(code.asString());
587586
}
588587
else if (code.isObject())
@@ -606,7 +605,7 @@ std::pair<std::shared_ptr<Assembly>, std::vector<std::string>> Assembly::fromJSO
606605
void Assembly::updatePaths(std::vector<Assembly*> const& _parents, std::vector<size_t> const& _absolutePathFromRoot)
607606
{
608607
size_t subId = 0;
609-
for (std::shared_ptr<Assembly> assembly: this->m_subs)
608+
for (std::shared_ptr<Assembly> assembly: m_subs)
610609
{
611610
std::vector<Assembly*> parents{_parents};
612611
parents.push_back(this);

0 commit comments

Comments
 (0)