Merge pull request #856 from chriseth/sol_modifiers

Function modifiers.
This commit is contained in:
Gav Wood 2015-01-26 15:01:25 -08:00
commit dd87fbccd5
4 changed files with 256 additions and 1 deletions

View File

@ -1650,6 +1650,127 @@ BOOST_AUTO_TEST_CASE(constructor_argument_overriding)
BOOST_CHECK(callContractFunction("getA()") == encodeArgs(3));
}
BOOST_AUTO_TEST_CASE(function_modifier)
{
char const* sourceCode = R"(
contract C {
function getOne() nonFree returns (uint r) { return 1; }
modifier nonFree { if (msg.value > 0) _ }
}
)";
compileAndRun(sourceCode);
BOOST_CHECK(callContractFunction("getOne()") == encodeArgs(0));
BOOST_CHECK(callContractFunctionWithValue("getOne()", 1) == encodeArgs(1));
}
BOOST_AUTO_TEST_CASE(function_modifier_local_variables)
{
char const* sourceCode = R"(
contract C {
modifier mod1 { var a = 1; var b = 2; _ }
modifier mod2(bool a) { if (a) return; else _ }
function f(bool a) mod1 mod2(a) returns (uint r) { return 3; }
}
)";
compileAndRun(sourceCode);
BOOST_CHECK(callContractFunction("f(bool)", true) == encodeArgs(0));
BOOST_CHECK(callContractFunction("f(bool)", false) == encodeArgs(3));
}
BOOST_AUTO_TEST_CASE(function_modifier_loop)
{
char const* sourceCode = R"(
contract C {
modifier repeat(uint count) { for (var i = 0; i < count; ++i) _ }
function f() repeat(10) returns (uint r) { r += 1; }
}
)";
compileAndRun(sourceCode);
BOOST_CHECK(callContractFunction("f()") == encodeArgs(10));
}
BOOST_AUTO_TEST_CASE(function_modifier_multi_invocation)
{
char const* sourceCode = R"(
contract C {
modifier repeat(bool twice) { if (twice) _ _ }
function f(bool twice) repeat(twice) returns (uint r) { r += 1; }
}
)";
compileAndRun(sourceCode);
BOOST_CHECK(callContractFunction("f(bool)", false) == encodeArgs(1));
BOOST_CHECK(callContractFunction("f(bool)", true) == encodeArgs(2));
}
BOOST_AUTO_TEST_CASE(function_modifier_multi_with_return)
{
// Here, the explicit return prevents the second execution
char const* sourceCode = R"(
contract C {
modifier repeat(bool twice) { if (twice) _ _ }
function f(bool twice) repeat(twice) returns (uint r) { r += 1; return r; }
}
)";
compileAndRun(sourceCode);
BOOST_CHECK(callContractFunction("f(bool)", false) == encodeArgs(1));
BOOST_CHECK(callContractFunction("f(bool)", true) == encodeArgs(1));
}
BOOST_AUTO_TEST_CASE(function_modifier_overriding)
{
char const* sourceCode = R"(
contract A {
function f() mod returns (bool r) { return true; }
modifier mod { _ }
}
contract C is A {
modifier mod { }
}
)";
compileAndRun(sourceCode);
BOOST_CHECK(callContractFunction("f()") == encodeArgs(false));
}
BOOST_AUTO_TEST_CASE(function_modifier_calling_functions_in_creation_context)
{
char const* sourceCode = R"(
contract A {
uint data;
function A() mod1 { f1(); }
function f1() mod2 { data |= 0x1; }
function f2() { data |= 0x20; }
function f3() { }
modifier mod1 { f2(); _ }
modifier mod2 { f3(); }
function getData() returns (uint r) { return data; }
}
contract C is A {
modifier mod1 { f4(); _ }
function f3() { data |= 0x300; }
function f4() { data |= 0x4000; }
}
)";
compileAndRun(sourceCode);
BOOST_CHECK(callContractFunction("getData()") == encodeArgs(0x4300));
}
BOOST_AUTO_TEST_CASE(function_modifier_for_constructor)
{
char const* sourceCode = R"(
contract A {
uint data;
function A() mod1 { data |= 2; }
modifier mod1 { data |= 1; _ }
function getData() returns (uint r) { return data; }
}
contract C is A {
modifier mod1 { data |= 4; _ }
}
)";
compileAndRun(sourceCode);
BOOST_CHECK(callContractFunction("getData()") == encodeArgs(4 | 2));
}
BOOST_AUTO_TEST_SUITE_END()
}

View File

@ -118,8 +118,11 @@ bytes compileFirstExpression(const string& _sourceCode, vector<vector<string>> _
CompilerContext context;
for (vector<string> const& function: _functions)
context.addFunction(dynamic_cast<FunctionDefinition const&>(resolveDeclaration(function, resolver)));
unsigned parametersSize = _localVariables.size(); // assume they are all one slot on the stack
context.adjustStackOffset(parametersSize);
for (vector<string> const& variable: _localVariables)
context.addVariable(dynamic_cast<VariableDeclaration const&>(resolveDeclaration(variable, resolver)));
context.addVariable(dynamic_cast<VariableDeclaration const&>(resolveDeclaration(variable, resolver)),
parametersSize--);
ExpressionCompiler::compileExpression(context, *extractor.getExpression());

View File

@ -479,6 +479,7 @@ BOOST_AUTO_TEST_CASE(implicit_derived_to_base_conversion)
)";
BOOST_CHECK_NO_THROW(parseTextAndResolveNames(text));
}
BOOST_AUTO_TEST_CASE(implicit_base_to_derived_conversion)
{
char const* text = R"(
@ -490,6 +491,99 @@ BOOST_AUTO_TEST_CASE(implicit_base_to_derived_conversion)
BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
}
BOOST_AUTO_TEST_CASE(function_modifier_invocation)
{
char const* text = R"(
contract B {
function f() mod1(2, true) mod2("0123456") { }
modifier mod1(uint a, bool b) { if (b) _ }
modifier mod2(string7 a) { while (a == "1234567") _ }
}
)";
BOOST_CHECK_NO_THROW(parseTextAndResolveNames(text));
}
BOOST_AUTO_TEST_CASE(invalid_function_modifier_type)
{
char const* text = R"(
contract B {
function f() mod1(true) { }
modifier mod1(uint a) { if (a > 0) _ }
}
)";
BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
}
BOOST_AUTO_TEST_CASE(function_modifier_invocation_parameters)
{
char const* text = R"(
contract B {
function f(uint8 a) mod1(a, true) mod2(r) returns (string7 r) { }
modifier mod1(uint a, bool b) { if (b) _ }
modifier mod2(string7 a) { while (a == "1234567") _ }
}
)";
BOOST_CHECK_NO_THROW(parseTextAndResolveNames(text));
}
BOOST_AUTO_TEST_CASE(function_modifier_invocation_local_variables)
{
char const* text = R"(
contract B {
function f() mod(x) { uint x = 7; }
modifier mod(uint a) { if (a > 0) _ }
}
)";
BOOST_CHECK_NO_THROW(parseTextAndResolveNames(text));
}
BOOST_AUTO_TEST_CASE(legal_modifier_override)
{
char const* text = R"(
contract A { modifier mod(uint a) {} }
contract B is A { modifier mod(uint a) {} }
)";
BOOST_CHECK_NO_THROW(parseTextAndResolveNames(text));
}
BOOST_AUTO_TEST_CASE(illegal_modifier_override)
{
char const* text = R"(
contract A { modifier mod(uint a) {} }
contract B is A { modifier mod(uint8 a) {} }
)";
BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
}
BOOST_AUTO_TEST_CASE(modifier_overrides_function)
{
char const* text = R"(
contract A { modifier mod(uint a) {} }
contract B is A { function mod(uint a) {} }
)";
BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
}
BOOST_AUTO_TEST_CASE(function_overrides_modifier)
{
char const* text = R"(
contract A { function mod(uint a) {} }
contract B is A { modifier mod(uint a) {} }
)";
BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
}
BOOST_AUTO_TEST_CASE(modifier_returns_value)
{
char const* text = R"(
contract A {
function f(uint a) mod(2) returns (uint r) {}
modifier mod(uint a) { return 7; }
}
)";
BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
}
BOOST_AUTO_TEST_SUITE_END()
}

View File

@ -540,6 +540,43 @@ BOOST_AUTO_TEST_CASE(contract_multiple_inheritance_with_arguments)
BOOST_CHECK_NO_THROW(parseText(text));
}
BOOST_AUTO_TEST_CASE(placeholder_in_function_context)
{
char const* text = "contract c {\n"
" function fun() returns (uint r) {\n"
" var _ = 8;\n"
" return _ + 1;"
" }\n"
"}\n";
BOOST_CHECK_NO_THROW(parseText(text));
}
BOOST_AUTO_TEST_CASE(modifier)
{
char const* text = "contract c {\n"
" modifier mod { if (msg.sender == 0) _ }\n"
"}\n";
BOOST_CHECK_NO_THROW(parseText(text));
}
BOOST_AUTO_TEST_CASE(modifier_arguments)
{
char const* text = "contract c {\n"
" modifier mod(uint a) { if (msg.sender == a) _ }\n"
"}\n";
BOOST_CHECK_NO_THROW(parseText(text));
}
BOOST_AUTO_TEST_CASE(modifier_invocation)
{
char const* text = "contract c {\n"
" modifier mod1(uint a) { if (msg.sender == a) _ }\n"
" modifier mod2 { if (msg.sender == 2) _ }\n"
" function f() mod1(7) mod2 { }\n"
"}\n";
BOOST_CHECK_NO_THROW(parseText(text));
}
BOOST_AUTO_TEST_SUITE_END()
}