Skip to content

Commit

Permalink
add usedVoteCount instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
neo hong committed Jul 19, 2021
1 parent c666397 commit 42e6972
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 15 deletions.
42 changes: 36 additions & 6 deletions libsolidity/analysis/GlobalContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ int magicVariableToID(std::string const& _name)
else if (_name == "isSrCandidate") return -41;
else if (_name == "voteCount") return -42;
else if (_name == "totalVoteCount") return -43;
else if (_name == "totalReceivedVoteCount") return -44;
else if (_name == "receivedVoteCount") return -44;
else if (_name == "usedVoteCount") return -45;
else
solAssert(false, "Unknown magic variable: \"" + _name + "\".");
}
Expand Down Expand Up @@ -146,7 +147,8 @@ inline vector<shared_ptr<MagicVariableDeclaration const>> constructMagicVariable
addIsSRCandidateMethod();
addVoteCountMethod();
addTotalVoteCountMethod();
addTotalReceivedVoteCountMethod();
addReceivedVoteCountMethod();
addUsedVoteCountMethod();
}

void GlobalContext::addVerifyMintProofMethod() {
Expand Down Expand Up @@ -540,8 +542,8 @@ inline vector<shared_ptr<MagicVariableDeclaration const>> constructMagicVariable
));
}

void GlobalContext::addTotalReceivedVoteCountMethod() {
// uint totalReceivedVoteCount(address)
void GlobalContext::addReceivedVoteCountMethod() {
// uint receivedVoteCount(address)
TypePointers parameterTypes;
parameterTypes.push_back(TypeProvider::address());

Expand All @@ -552,12 +554,12 @@ inline vector<shared_ptr<MagicVariableDeclaration const>> constructMagicVariable
strings returnParameterNames;
returnParameterNames.push_back("result");

m_magicVariables.push_back(make_shared<MagicVariableDeclaration>(magicVariableToID("totalReceivedVoteCount"), "totalReceivedVoteCount", TypeProvider::function(
m_magicVariables.push_back(make_shared<MagicVariableDeclaration>(magicVariableToID("receivedVoteCount"), "receivedVoteCount", TypeProvider::function(
parameterTypes,
returnParameterTypes,
parameterNames,
returnParameterNames,
FunctionType::Kind::totalReceivedVoteCount,
FunctionType::Kind::receivedVoteCount,
false,
StateMutability::View,
nullptr,
Expand All @@ -568,6 +570,34 @@ inline vector<shared_ptr<MagicVariableDeclaration const>> constructMagicVariable
));
}

void GlobalContext::addUsedVoteCountMethod() {
// uint usedVoteCount(address)
TypePointers parameterTypes;
parameterTypes.push_back(TypeProvider::address());

TypePointers returnParameterTypes;
returnParameterTypes.push_back(TypeProvider::uint256());
strings parameterNames;
parameterNames.push_back("address");
strings returnParameterNames;
returnParameterNames.push_back("result");

m_magicVariables.push_back(make_shared<MagicVariableDeclaration>(magicVariableToID("usedVoteCount"), "usedVoteCount", TypeProvider::function(
parameterTypes,
returnParameterTypes,
parameterNames,
returnParameterNames,
FunctionType::Kind::usedVoteCount,
false,
StateMutability::View,
nullptr,
false,
false,
false,
false)
));
}


void GlobalContext::setCurrentContract(ContractDefinition const& _contract)
{
Expand Down
3 changes: 2 additions & 1 deletion libsolidity/analysis/GlobalContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ class GlobalContext: private boost::noncopyable
void addIsSRCandidateMethod();
void addVoteCountMethod();
void addTotalVoteCountMethod();
void addTotalReceivedVoteCountMethod();
void addReceivedVoteCountMethod();
void addUsedVoteCountMethod();
};

}
6 changes: 4 additions & 2 deletions libsolidity/ast/Types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3051,7 +3051,8 @@ string FunctionType::richIdentifier() const
case Kind::isSrCandidate: id += "isSrCandidate"; break;
case Kind::voteCount: id += "voteCount"; break;
case Kind::totalVoteCount: id += "totalVoteCount"; break;
case Kind::totalReceivedVoteCount: id += "totalReceivedVoteCount"; break;
case Kind::receivedVoteCount: id += "receivedVoteCount"; break;
case Kind::usedVoteCount: id += "usedVoteCount"; break;
}
id += "_" + stateMutabilityToString(m_stateMutability);
id += identifierList(m_parameterTypes) + "returns" + identifierList(m_returnParameterTypes);
Expand Down Expand Up @@ -3526,7 +3527,8 @@ bool FunctionType::isBareCall() const
case Kind::isSrCandidate:
case Kind::voteCount:
case Kind::totalVoteCount:
case Kind::totalReceivedVoteCount:
case Kind::receivedVoteCount:
case Kind::usedVoteCount:
case Kind::SHA256:
case Kind::RIPEMD160:
return true;
Expand Down
3 changes: 2 additions & 1 deletion libsolidity/ast/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,8 @@ class FunctionType: public Type
isSrCandidate, ///< Judge witness whether or not
voteCount, ///< get vote count
totalVoteCount, /// get total vote count
totalReceivedVoteCount, /// get total received vote count
receivedVoteCount, /// get total received vote count
usedVoteCount, /// get total used vote count
Log0,
Log1,
Log2,
Expand Down
14 changes: 9 additions & 5 deletions libsolidity/codegen/ExpressionCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,8 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
case FunctionType::Kind::isSrCandidate:
case FunctionType::Kind::voteCount:
case FunctionType::Kind::totalVoteCount:
case FunctionType::Kind::totalReceivedVoteCount:
case FunctionType::Kind::receivedVoteCount:
case FunctionType::Kind::usedVoteCount:
{
_functionCall.expression().accept(*this);
static map<FunctionType::Kind, u256> const contractAddresses{
Expand All @@ -1005,8 +1006,9 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
{FunctionType::Kind::rewardBalance, 16777221},
{FunctionType::Kind::isSrCandidate, 16777222},
{FunctionType::Kind::voteCount, 16777223},
{FunctionType::Kind::totalVoteCount, 16777224},
{FunctionType::Kind::totalReceivedVoteCount, 16777225}
{FunctionType::Kind::usedVoteCount, 16777224},
{FunctionType::Kind::receivedVoteCount, 16777225},
{FunctionType::Kind::totalVoteCount, 16777226}
};
m_context << contractAddresses.at(function.kind());
for (unsigned i = function.sizeOnStack(); i > 0; --i)
Expand Down Expand Up @@ -1524,7 +1526,8 @@ bool ExpressionCompiler::visit(MemberAccess const& _memberAccess)
case FunctionType::Kind::isSrCandidate:
case FunctionType::Kind::voteCount:
case FunctionType::Kind::totalVoteCount:
case FunctionType::Kind::totalReceivedVoteCount:
case FunctionType::Kind::receivedVoteCount:
case FunctionType::Kind::usedVoteCount:
case FunctionType::Kind::SHA256:
case FunctionType::Kind::RIPEMD160:
default:
Expand Down Expand Up @@ -2495,7 +2498,8 @@ void ExpressionCompiler::appendExternalFunctionCall(
|| _functionType.kind() == FunctionType::Kind::isSrCandidate
|| _functionType.kind() == FunctionType::Kind::voteCount
|| _functionType.kind() == FunctionType::Kind::totalVoteCount
|| _functionType.kind() == FunctionType::Kind::totalReceivedVoteCount
|| _functionType.kind() == FunctionType::Kind::receivedVoteCount
|| _functionType.kind() == FunctionType::Kind::usedVoteCount
)
// This would be the only combination of padding and in-place encoding,
// but all parameters of ecrecover are value types anyway.
Expand Down

0 comments on commit 42e6972

Please sign in to comment.