Skip to content

Commit e293bca

Browse files
cameelr0qs
authored andcommitted
fixup! [libevmasm] Add support to import evm assembly json.
1 parent 4120fce commit e293bca

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

libevmasm/Assembly.cpp

Lines changed: 26 additions & 27 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,7 +505,11 @@ 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
{
513514
solRequire(_json.isObject(), AssemblyImportException, "Supplied JSON is not an object.");
514515
static std::set<std::string> const validMembers{".code", ".data", ".auxdata", "sourceList"};
@@ -528,7 +529,8 @@ std::pair<std::shared_ptr<Assembly>, std::vector<std::string>> Assembly::fromJSO
528529
for (Json::Value const& sourceListItem: _json["sourceList"])
529530
solRequire(sourceListItem.isString(), AssemblyImportException, "The 'sourceList' array contains an item that is not a string.");
530531
}
531-
} else
532+
}
533+
else
532534
solRequire(
533535
!_json.isMember("sourceList"),
534536
AssemblyImportException,
@@ -549,16 +551,16 @@ std::pair<std::shared_ptr<Assembly>, std::vector<std::string>> Assembly::fromJSO
549551
);
550552
sourceList.emplace_back(it.asString());
551553
}
552-
} else
554+
}
555+
else
553556
sourceList = _sourceList;
554557

555558
result->importAssemblyItemsFromJSON(_json[".code"], sourceList);
556559
if (_json[".auxdata"])
557560
{
558561
solRequire(_json[".auxdata"].isString(), AssemblyImportException, "Optional member '.auxdata' is not a string.");
559-
bytes auxdata{fromHex(_json[".auxdata"].asString())};
560-
solRequire(!auxdata.empty(), AssemblyImportException, "Optional member '.auxdata' is not a valid hexadecimal string.");
561-
result->m_auxiliaryData = auxdata;
562+
result->m_auxiliaryData = fromHex(_json[".auxdata"].asString());
563+
solRequire(!result->m_auxiliaryData.empty(), AssemblyImportException, "Optional member '.auxdata' is not a valid hexadecimal string.");
562564
}
563565

564566
if (_json.isMember(".data"))
@@ -572,14 +574,11 @@ std::pair<std::shared_ptr<Assembly>, std::vector<std::string>> Assembly::fromJSO
572574
Json::Value const& code = data[dataItemID];
573575
if (code.isString())
574576
{
575-
if (!code.asString().empty())
576-
{
577-
bytes data_value{fromHex(code.asString())};
578-
solRequire(
579-
!data_value.empty(),
580-
AssemblyImportException,
581-
"The value for key '" + dataItemID + "' inside '.data' is not a valid hexadecimal string.");
582-
}
577+
solRequire(
578+
code.asString().empty() || !fromHex(code.asString()).empty(),
579+
AssemblyImportException,
580+
"The value for key '" + dataItemID + "' inside '.data' is not a valid hexadecimal string."
581+
);
583582
result->m_data[h256(fromHex(dataItemID))] = fromHex(code.asString());
584583
}
585584
else if (code.isObject())
@@ -603,7 +602,7 @@ std::pair<std::shared_ptr<Assembly>, std::vector<std::string>> Assembly::fromJSO
603602
void Assembly::updatePaths(std::vector<Assembly*> const& _parents, std::vector<size_t> const& _absolutePathFromRoot)
604603
{
605604
size_t subId = 0;
606-
for (std::shared_ptr<Assembly> assembly: this->m_subs)
605+
for (std::shared_ptr<Assembly> assembly: m_subs)
607606
{
608607
std::vector<Assembly*> parents{_parents};
609608
parents.push_back(this);

0 commit comments

Comments
 (0)