Skip to content

Commit

Permalink
Merge pull request #665 from axic/feature/accept-ether
Browse files Browse the repository at this point in the history
BREAKING: Add payable modifier
  • Loading branch information
chriseth authored Sep 6, 2016
2 parents 171c748 + dff9633 commit f687635
Show file tree
Hide file tree
Showing 17 changed files with 434 additions and 85 deletions.
28 changes: 16 additions & 12 deletions libsolidity/analysis/TypeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ void TypeChecker::checkContractIllegalOverrides(ContractDefinition const& _contr
if (
overriding->visibility() != function->visibility() ||
overriding->isDeclaredConst() != function->isDeclaredConst() ||
overriding->isPayable() != function->isPayable() ||
overridingType != functionType
)
typeError(overriding->location(), "Override changes extended function signature.");
Expand Down Expand Up @@ -348,7 +349,7 @@ void TypeChecker::endVisit(InheritanceSpecifier const& _inheritance)
typeError(_inheritance.location(), "Libraries cannot be inherited from.");

auto const& arguments = _inheritance.arguments();
TypePointers parameterTypes = ContractType(*base).constructorType()->parameterTypes();
TypePointers parameterTypes = ContractType(*base).newExpressionType()->parameterTypes();
if (!arguments.empty() && parameterTypes.size() != arguments.size())
{
typeError(
Expand Down Expand Up @@ -416,6 +417,15 @@ bool TypeChecker::visit(StructDefinition const& _struct)
bool TypeChecker::visit(FunctionDefinition const& _function)
{
bool isLibraryFunction = dynamic_cast<ContractDefinition const&>(*_function.scope()).isLibrary();
if (_function.isPayable())
{
if (isLibraryFunction)
typeError(_function.location(), "Library functions cannot be payable.");
if (!_function.isConstructor() && !_function.name().empty() && !_function.isPartOfExternalInterface())
typeError(_function.location(), "Internal functions cannot be payable.");
if (_function.isDeclaredConst())
typeError(_function.location(), "Functions cannot be constant and payable at the same time.");
}
for (ASTPointer<VariableDeclaration> const& var: _function.parameters() + _function.returnParameters())
{
if (!type(*var)->canLiveOutsideStorage())
Expand Down Expand Up @@ -1256,15 +1266,7 @@ void TypeChecker::endVisit(NewExpression const& _newExpression)
"Circular reference for contract creation (cannot create instance of derived or same contract)."
);

auto contractType = make_shared<ContractType>(*contract);
TypePointers parameterTypes = contractType->constructorType()->parameterTypes();
_newExpression.annotation().type = make_shared<FunctionType>(
parameterTypes,
TypePointers{contractType},
strings(),
strings(),
FunctionType::Location::Creation
);
_newExpression.annotation().type = FunctionType::newExpressionType(*contract);
}
else if (type->category() == Type::Category::Array)
{
Expand Down Expand Up @@ -1328,14 +1330,16 @@ bool TypeChecker::visit(MemberAccess const& _memberAccess)
fatalTypeError(
_memberAccess.location(),
"Member \"" + memberName + "\" not found or not visible "
"after argument-dependent lookup in " + exprType->toString()
"after argument-dependent lookup in " + exprType->toString() +
(memberName == "value" ? " - did you forget the \"payable\" modifier?" : "")
);
}
else if (possibleMembers.size() > 1)
fatalTypeError(
_memberAccess.location(),
"Member \"" + memberName + "\" not unique "
"after argument-dependent lookup in " + exprType->toString()
"after argument-dependent lookup in " + exprType->toString() +
(memberName == "value" ? " - did you forget the \"payable\" modifier?" : "")
);

auto& annotation = _memberAccess.annotation();
Expand Down
4 changes: 4 additions & 0 deletions libsolidity/ast/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -540,13 +540,15 @@ class FunctionDefinition: public CallableDeclaration, public Documented, public
bool _isDeclaredConst,
std::vector<ASTPointer<ModifierInvocation>> const& _modifiers,
ASTPointer<ParameterList> const& _returnParameters,
bool _isPayable,
ASTPointer<Block> const& _body
):
CallableDeclaration(_location, _name, _visibility, _parameters, _returnParameters),
Documented(_documentation),
ImplementationOptional(_body != nullptr),
m_isConstructor(_isConstructor),
m_isDeclaredConst(_isDeclaredConst),
m_isPayable(_isPayable),
m_functionModifiers(_modifiers),
m_body(_body)
{}
Expand All @@ -556,6 +558,7 @@ class FunctionDefinition: public CallableDeclaration, public Documented, public

bool isConstructor() const { return m_isConstructor; }
bool isDeclaredConst() const { return m_isDeclaredConst; }
bool isPayable() const { return m_isPayable; }
std::vector<ASTPointer<ModifierInvocation>> const& modifiers() const { return m_functionModifiers; }
std::vector<ASTPointer<VariableDeclaration>> const& returnParameters() const { return m_returnParameters->parameters(); }
Block const& body() const { return *m_body; }
Expand All @@ -578,6 +581,7 @@ class FunctionDefinition: public CallableDeclaration, public Documented, public
private:
bool m_isConstructor;
bool m_isDeclaredConst;
bool m_isPayable;
std::vector<ASTPointer<ModifierInvocation>> m_functionModifiers;
ASTPointer<Block> m_body;
};
Expand Down
92 changes: 66 additions & 26 deletions libsolidity/ast/Types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,8 @@ MemberList::MemberMap IntegerType::nativeMembers(ContractDefinition const*) cons
if (isAddress())
return {
{"balance", make_shared<IntegerType >(256)},
{"call", make_shared<FunctionType>(strings(), strings{"bool"}, FunctionType::Location::Bare, true)},
{"callcode", make_shared<FunctionType>(strings(), strings{"bool"}, FunctionType::Location::BareCallCode, true)},
{"call", make_shared<FunctionType>(strings(), strings{"bool"}, FunctionType::Location::Bare, true, false, true)},
{"callcode", make_shared<FunctionType>(strings(), strings{"bool"}, FunctionType::Location::BareCallCode, true, false, true)},
{"delegatecall", make_shared<FunctionType>(strings(), strings{"bool"}, FunctionType::Location::BareDelegateCall, true)},
{"send", make_shared<FunctionType>(strings{"uint"}, strings{"bool"}, FunctionType::Location::Send)}
};
Expand Down Expand Up @@ -1329,16 +1329,10 @@ MemberList::MemberMap ContractType::nativeMembers(ContractDefinition const*) con
return members;
}

shared_ptr<FunctionType const> const& ContractType::constructorType() const
shared_ptr<FunctionType const> const& ContractType::newExpressionType() const
{
if (!m_constructorType)
{
FunctionDefinition const* constructor = m_contract.constructor();
if (constructor)
m_constructorType = make_shared<FunctionType>(*constructor);
else
m_constructorType = make_shared<FunctionType>(TypePointers(), TypePointers());
}
m_constructorType = FunctionType::newExpressionType(m_contract);
return m_constructorType;
}

Expand Down Expand Up @@ -1653,6 +1647,7 @@ TypePointer TupleType::closestTemporaryType(TypePointer const& _targetType) cons
FunctionType::FunctionType(FunctionDefinition const& _function, bool _isInternal):
m_location(_isInternal ? Location::Internal : Location::External),
m_isConstant(_function.isDeclaredConst()),
m_isPayable(_function.isPayable()),
m_declaration(&_function)
{
TypePointers params;
Expand Down Expand Up @@ -1737,7 +1732,7 @@ FunctionType::FunctionType(VariableDeclaration const& _varDecl):
swap(retParamNames, m_returnParameterNames);
}

FunctionType::FunctionType(const EventDefinition& _event):
FunctionType::FunctionType(EventDefinition const& _event):
m_location(Location::Event), m_isConstant(true), m_declaration(&_event)
{
TypePointers params;
Expand All @@ -1753,6 +1748,35 @@ FunctionType::FunctionType(const EventDefinition& _event):
swap(paramNames, m_parameterNames);
}

FunctionTypePointer FunctionType::newExpressionType(ContractDefinition const& _contract)
{
FunctionDefinition const* constructor = _contract.constructor();
TypePointers parameters;
strings parameterNames;
bool payable = false;

if (constructor)
{
for (ASTPointer<VariableDeclaration> const& var: constructor->parameters())
{
parameterNames.push_back(var->name());
parameters.push_back(var->annotation().type);
}
payable = constructor->isPayable();
}
return make_shared<FunctionType>(
parameters,
TypePointers{make_shared<ContractType>(_contract)},
parameterNames,
strings{""},
Location::Creation,
false,
nullptr,
false,
payable
);
}

vector<string> FunctionType::parameterNames() const
{
if (!bound())
Expand Down Expand Up @@ -1871,7 +1895,12 @@ FunctionTypePointer FunctionType::interfaceFunctionType() const
if (variable && retParamTypes.empty())
return FunctionTypePointer();

return make_shared<FunctionType>(paramTypes, retParamTypes, m_parameterNames, m_returnParameterNames, m_location, m_arbitraryParameters);
return make_shared<FunctionType>(
paramTypes, retParamTypes,
m_parameterNames, m_returnParameterNames,
m_location, m_arbitraryParameters,
m_declaration, m_isConstant, m_isPayable
);
}

MemberList::MemberMap FunctionType::nativeMembers(ContractDefinition const*) const
Expand All @@ -1889,20 +1918,25 @@ MemberList::MemberMap FunctionType::nativeMembers(ContractDefinition const*) con
{
MemberList::MemberMap members;
if (m_location != Location::BareDelegateCall && m_location != Location::DelegateCall)
members.push_back(MemberList::Member(
"value",
make_shared<FunctionType>(
parseElementaryTypeVector({"uint"}),
TypePointers{copyAndSetGasOrValue(false, true)},
strings(),
strings(),
Location::SetValue,
false,
nullptr,
m_gasSet,
m_valueSet
)
));
{
if (m_isPayable)
members.push_back(MemberList::Member(
"value",
make_shared<FunctionType>(
parseElementaryTypeVector({"uint"}),
TypePointers{copyAndSetGasOrValue(false, true)},
strings(),
strings(),
Location::SetValue,
false,
nullptr,
false,
false,
m_gasSet,
m_valueSet
)
));
}
if (m_location != Location::Creation)
members.push_back(MemberList::Member(
"gas",
Expand All @@ -1914,6 +1948,8 @@ MemberList::MemberMap FunctionType::nativeMembers(ContractDefinition const*) con
Location::SetGas,
false,
nullptr,
false,
false,
m_gasSet,
m_valueSet
)
Expand Down Expand Up @@ -2019,6 +2055,8 @@ TypePointer FunctionType::copyAndSetGasOrValue(bool _setGas, bool _setValue) con
m_location,
m_arbitraryParameters,
m_declaration,
m_isConstant,
m_isPayable,
m_gasSet || _setGas,
m_valueSet || _setValue,
m_bound
Expand Down Expand Up @@ -2064,6 +2102,8 @@ FunctionTypePointer FunctionType::asMemberFunction(bool _inLibrary, bool _bound)
location,
m_arbitraryParameters,
m_declaration,
m_isConstant,
m_isPayable,
m_gasSet,
m_valueSet,
_bound
Expand Down
26 changes: 21 additions & 5 deletions libsolidity/ast/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -640,9 +640,8 @@ class ContractType: public Type
bool isSuper() const { return m_super; }
ContractDefinition const& contractDefinition() const { return m_contract; }

/// Returns the function type of the constructor. Note that the location part of the function type
/// is not used, as this type cannot be the type of a variable or expression.
FunctionTypePointer const& constructorType() const;
/// Returns the function type of the constructor modified to return an object of the contract's type.
FunctionTypePointer const& newExpressionType() const;

/// @returns the identifier of the function with the given name or Invalid256 if such a name does
/// not exist.
Expand Down Expand Up @@ -820,21 +819,32 @@ class FunctionType: public Type
explicit FunctionType(VariableDeclaration const& _varDecl);
/// Creates the function type of an event.
explicit FunctionType(EventDefinition const& _event);
/// Function type constructor to be used for a plain type (not derived from a declaration).
FunctionType(
strings const& _parameterTypes,
strings const& _returnParameterTypes,
Location _location = Location::Internal,
bool _arbitraryParameters = false
bool _arbitraryParameters = false,
bool _constant = false,
bool _payable = false
): FunctionType(
parseElementaryTypeVector(_parameterTypes),
parseElementaryTypeVector(_returnParameterTypes),
strings(),
strings(),
_location,
_arbitraryParameters
_arbitraryParameters,
nullptr,
_constant,
_payable
)
{
}

/// @returns the type of the "new Contract" function, i.e. basically the constructor.
static FunctionTypePointer newExpressionType(ContractDefinition const& _contract);

/// Detailed constructor, use with care.
FunctionType(
TypePointers const& _parameterTypes,
TypePointers const& _returnParameterTypes,
Expand All @@ -843,6 +853,8 @@ class FunctionType: public Type
Location _location = Location::Internal,
bool _arbitraryParameters = false,
Declaration const* _declaration = nullptr,
bool _isConstant = false,
bool _isPayable = false,
bool _gasSet = false,
bool _valueSet = false,
bool _bound = false
Expand All @@ -856,6 +868,8 @@ class FunctionType: public Type
m_gasSet(_gasSet),
m_valueSet(_valueSet),
m_bound(_bound),
m_isConstant(_isConstant),
m_isPayable(_isPayable),
m_declaration(_declaration)
{}

Expand Down Expand Up @@ -905,6 +919,7 @@ class FunctionType: public Type
}
bool hasDeclaration() const { return !!m_declaration; }
bool isConstant() const { return m_isConstant; }
bool isPayable() const { return m_isPayable; }
/// @return A shared pointer of an ASTString.
/// Can contain a nullptr in which case indicates absence of documentation
ASTPointer<ASTString> documentation() const;
Expand Down Expand Up @@ -942,6 +957,7 @@ class FunctionType: public Type
bool const m_valueSet = false; ///< true iff the value to be sent is on the stack
bool const m_bound = false; ///< true iff the function is called as arg1.fun(arg2, ..., argn)
bool m_isConstant = false;
bool m_isPayable = false;
Declaration const* m_declaration = nullptr;
};

Expand Down
14 changes: 14 additions & 0 deletions libsolidity/codegen/ContractCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ void ContractCompiler::appendFunctionSelector(ContractDefinition const& _contrac
m_context << notFound;
if (fallback)
{
if (!fallback->isPayable())
{
// Throw if function is not payable but call contained ether.
m_context << Instruction::CALLVALUE;
m_context.appendConditionalJumpTo(m_context.errorTag());
}
eth::AssemblyItem returnTag = m_context.pushNewTag();
fallback->accept(*this);
m_context << returnTag;
Expand All @@ -255,7 +261,15 @@ void ContractCompiler::appendFunctionSelector(ContractDefinition const& _contrac
FunctionTypePointer const& functionType = it.second;
solAssert(functionType->hasDeclaration(), "");
CompilerContext::LocationSetter locationSetter(m_context, functionType->declaration());

m_context << callDataUnpackerEntryPoints.at(it.first);
if (!functionType->isPayable())
{
// Throw if function is not payable but call contained ether.
m_context << Instruction::CALLVALUE;
m_context.appendConditionalJumpTo(m_context.errorTag());
}

eth::AssemblyItem returnTag = m_context.pushNewTag();
m_context << CompilerUtils::dataStartOffset;
appendCalldataUnpacker(functionType->parameterTypes());
Expand Down
2 changes: 2 additions & 0 deletions libsolidity/codegen/ExpressionCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,8 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
Location::Bare,
false,
nullptr,
false,
false,
true,
true
),
Expand Down
2 changes: 2 additions & 0 deletions libsolidity/interface/InterfaceHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ string InterfaceHandler::abiInterface(ContractDefinition const& _contractDef)
method["type"] = "function";
method["name"] = it.second->declaration().name();
method["constant"] = it.second->isConstant();
method["payable"] = it.second->isPayable();
method["inputs"] = populateParameters(
externalFunctionType->parameterNames(),
externalFunctionType->parameterTypeNames(_contractDef.isLibrary())
Expand Down Expand Up @@ -81,6 +82,7 @@ string InterfaceHandler::abiInterface(ContractDefinition const& _contractDef)
Json::Value method;
method["type"] = "fallback";
method["constant"] = externalFunctionType->isConstant();
method["payable"] = externalFunctionType->isPayable();
abi.append(method);
}
for (auto const& it: _contractDef.interfaceEvents())
Expand Down
Loading

0 comments on commit f687635

Please sign in to comment.