Added some more contract processing code

This commit is contained in:
Bhargava Shastry 2020-04-11 23:39:59 +02:00
parent 515939670d
commit f63552f4ca
3 changed files with 54 additions and 2 deletions

View File

@ -476,6 +476,47 @@ interface <programName><?inheritance> is <baseNames></inheritance> {
.render();
}
bool SolContract::validTest() const
{
return m_contractFunctionMap.size() > 0;
}
tuple<string, string, string> SolContract::pseudoRandomTest()
{
solAssert(m_contractFunctionMap.size() > 0, "Sol proto adaptor: Empty contract map");
string chosenContractName{};
string chosenFunctionName{};
string expectedOutput{};
unsigned numFunctions = m_contractFunctionMap.size();
unsigned contractIdx = randomNumber() % numFunctions;
unsigned functionIdx = 0;
unsigned mapIdx = 0;
for (auto &e: m_contractFunctionMap)
{
if (contractIdx == mapIdx)
{
chosenContractName = e.first;
functionIdx = random() % e.second.size();
unsigned functionMapIdx = 0;
for (auto &f: e.second)
{
if (functionIdx == functionMapIdx)
{
chosenFunctionName = f.first;
expectedOutput = f.second;
break;
}
functionMapIdx++;
}
break;
}
mapIdx++;
}
solAssert(m_contractFunctionMap.count(chosenContractName), "Sol proto adaptor: Invalid contract chosen");
solAssert(m_contractFunctionMap[chosenContractName].count(chosenFunctionName), "Sol proto adaptor: Invalid contract function chosen");
return tuple(chosenContractName, chosenFunctionName, expectedOutput);
}
void SolContract::overrideHelper()
{

View File

@ -251,7 +251,7 @@ struct SolContract
void addBases(Contract const& _contract);
void addOverrides();
void overrideHelper();
bool validTest();
bool validTest() const;
std::tuple<std::string, std::string, std::string> pseudoRandomTest();
unsigned randomNumber() const
@ -296,6 +296,9 @@ struct SolContract
std::vector<std::shared_ptr<SolContractFunction>> m_contractFunctions;
std::vector<std::shared_ptr<SolBaseContract>> m_baseContracts;
std::vector<std::shared_ptr<CFunctionOverride>> m_overriddenFunctions;
/// Maps non abstract contract name to list of publicly exposed function name
/// and their expected output
std::map<std::string, std::map<std::string, std::string>> m_contractFunctionMap;
std::shared_ptr<SolRandomNumGenerator> m_prng;
};

View File

@ -629,7 +629,15 @@ string ProtoConverter::visit(Contract const& _contract)
openProgramScope(&_contract);
try {
auto contract = SolContract(_contract, programName(&_contract), m_randomGen);
return contract.str();
if (contract.validTest())
{
m_contractTests.push_back(contract.pseudoRandomTest());
return contract.str();
}
// There is no point in generating a contract that can not provide
// a valid test case, so we simply bail.
else
return "";
}
catch (langutil::FuzzerError const&)
{