Skip to content

Commit

Permalink
add vote relevant instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
neo hong committed Jul 14, 2021
1 parent 78ceea1 commit c666397
Show file tree
Hide file tree
Showing 12 changed files with 418 additions and 38 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ include(EthPolicy)
eth_policy()

# project name and version should be set after cmake_policy CMP0048
set(PROJECT_VERSION "0.6.12")
set(PROJECT_VERSION "0.6.13")
# OSX target needed in order to support std::visit
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.14")
project(solidity VERSION ${PROJECT_VERSION} LANGUAGES C CXX)
Expand Down
6 changes: 6 additions & 0 deletions libevmasm/GasMeter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ GasMeter::GasConsumption GasMeter::estimateMax(AssemblyItem const& _item, bool _
case Instruction::NATIVEFREEZEEXPIRETIME:
gas = runGas(Instruction::NATIVEFREEZEEXPIRETIME);
break;
case Instruction::NATIVEVOTE:
gas = runGas(Instruction::NATIVEVOTE);
break;
case Instruction::NATIVEWITHDRAWREWARD:
gas = runGas(Instruction::NATIVEWITHDRAWREWARD);
break;
case Instruction::CHAINID:
gas = runGas(Instruction::CHAINID);
break;
Expand Down
4 changes: 4 additions & 0 deletions libevmasm/Instruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ std::map<std::string, Instruction> const solidity::evmasm::c_instructions =
{ "NATIVEFREEZE", Instruction::NATIVEFREEZE },
{ "NATIVEUNFREEZE", Instruction::NATIVEUNFREEZE },
{ "NATIVEFREEZEEXPIRETIME", Instruction::NATIVEFREEZEEXPIRETIME },
{ "NATIVEVOTE", Instruction::NATIVEVOTE },
{ "NATIVEWITHDRAWREWARD", Instruction::NATIVEWITHDRAWREWARD },
{ "CALLER", Instruction::CALLER },
{ "CALLVALUE", Instruction::CALLVALUE },
{ "CALLTOKENVALUE", Instruction::CALLTOKENVALUE },
Expand Down Expand Up @@ -223,6 +225,8 @@ static std::map<Instruction, InstructionInfo> const c_instructionInfo =
{ Instruction::NATIVEFREEZE, { "NATIVEFREEZE", 0, 3, 1, true, Tier::Ext } },
{ Instruction::NATIVEUNFREEZE, { "NATIVEUNFREEZE", 0, 2, 1, true, Tier::Ext } },
{ Instruction::NATIVEFREEZEEXPIRETIME, { "NATIVEFREEZEEXPIRETIME", 0, 2, 1, true, Tier::Ext } },
{ Instruction::NATIVEVOTE, { "NATIVEVOTE", 0, 4, 1, true, Tier::Ext } },
{ Instruction::NATIVEWITHDRAWREWARD, { "NATIVEWITHDRAWREWARD", 0, 0, 1, true, Tier::Ext } },
{ Instruction::CALLER, { "CALLER", 0, 0, 1, false, Tier::Base } },
{ Instruction::CALLVALUE, { "CALLVALUE", 0, 0, 1, false, Tier::Base } },
{ Instruction::CALLTOKENVALUE, { "CALLTOKENVALUE", 0, 0, 1, false, Tier::Base } },
Expand Down
2 changes: 2 additions & 0 deletions libevmasm/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ enum class Instruction: uint8_t
NATIVEFREEZE,
NATIVEUNFREEZE,
NATIVEFREEZEEXPIRETIME,
NATIVEVOTE,
NATIVEWITHDRAWREWARD,

CREATE = 0xf0, ///< create a new account with associated code
CALL, ///< message-call into an account
Expand Down
6 changes: 6 additions & 0 deletions libevmasm/SemanticInformation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ bool SemanticInformation::isDeterministic(AssemblyItem const& _item)
case Instruction::NATIVEFREEZE:
case Instruction::NATIVEUNFREEZE:
case Instruction::NATIVEFREEZEEXPIRETIME:
case Instruction::NATIVEVOTE:
case Instruction::NATIVEWITHDRAWREWARD:
case Instruction::SELFBALANCE: // depends on previous calls
case Instruction::EXTCODESIZE:
case Instruction::EXTCODEHASH:
Expand Down Expand Up @@ -307,6 +309,8 @@ bool SemanticInformation::invalidInPureFunctions(Instruction _instruction)
case Instruction::NATIVEFREEZE:
case Instruction::NATIVEUNFREEZE:
case Instruction::NATIVEFREEZEEXPIRETIME:
case Instruction::NATIVEVOTE:
case Instruction::NATIVEWITHDRAWREWARD:
case Instruction::ORIGIN:
case Instruction::CALLER:
case Instruction::CALLVALUE:
Expand Down Expand Up @@ -354,6 +358,8 @@ bool SemanticInformation::invalidInViewFunctions(Instruction _instruction)
case Instruction::NATIVEFREEZE:
case Instruction::NATIVEUNFREEZE:
case Instruction::NATIVEFREEZEEXPIRETIME:
case Instruction::NATIVEVOTE:
case Instruction::NATIVEWITHDRAWREWARD:
return true;
default:
break;
Expand Down
193 changes: 189 additions & 4 deletions libsolidity/analysis/GlobalContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ int magicVariableToID(std::string const& _name)
else if (_name == "freeze") return -35;
else if (_name == "unfreeze") return -36;
else if (_name == "freezeExpireTime") return -37;
else if (_name == "withdrawreward") return -38;
else if (_name == "vote") return -39;
else if (_name == "rewardBalance") return -40;
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
solAssert(false, "Unknown magic variable: \"" + _name + "\".");
}
Expand Down Expand Up @@ -109,9 +116,10 @@ inline vector<shared_ptr<MagicVariableDeclaration const>> constructMagicVariable
magicVarDecl("sha256", TypeProvider::function(strings{"bytes memory"}, strings{"bytes32"}, FunctionType::Kind::SHA256, false, StateMutability::Pure)),
magicVarDecl("sha3", TypeProvider::function(strings{"bytes memory"}, strings{"bytes32"}, FunctionType::Kind::KECCAK256, false, StateMutability::Pure)),
magicVarDecl("suicide", TypeProvider::function(strings{"address payable"}, strings{}, FunctionType::Kind::Selfdestruct)),
magicVarDecl("freeze", TypeProvider::function(strings{"uint", "uint"}, strings{"bool"}, FunctionType::Kind::Freeze, false, StateMutability::NonPayable)),
magicVarDecl("unfreeze", TypeProvider::function(strings{"uint"}, strings{"bool"}, FunctionType::Kind::Unfreeze, false, StateMutability::NonPayable)),
magicVarDecl("freezeExpireTime", TypeProvider::function(strings{"uint"}, strings{"uint"}, FunctionType::Kind::FreezeExpireTime, false, StateMutability::NonPayable)),
magicVarDecl("freeze", TypeProvider::function(strings{"uint", "uint"}, strings{"bool"}, FunctionType::Kind::Freeze, false, StateMutability::NonPayable)),
magicVarDecl("unfreeze", TypeProvider::function(strings{"uint"}, strings{"bool"}, FunctionType::Kind::Unfreeze, false, StateMutability::NonPayable)),
magicVarDecl("freezeExpireTime", TypeProvider::function(strings{"uint"}, strings{"uint"}, FunctionType::Kind::FreezeExpireTime, false, StateMutability::NonPayable)),
magicVarDecl("withdrawreward", TypeProvider::function(strings{}, strings{"uint"}, FunctionType::Kind::WithdrawReward)),
magicVarDecl("tx", TypeProvider::magic(MagicType::Kind::Transaction)),
// Accepts a MagicType that can be any contract type or an Integer type and returns a
// MagicType. The TypeChecker handles the correctness of the input and output types.
Expand All @@ -133,6 +141,12 @@ inline vector<shared_ptr<MagicVariableDeclaration const>> constructMagicVariable
addVerifyBurnProofMethod();
addVerifyTransferProofMethod();
addPedersenHashMethod();
addVoteMethod();
addRewardBalanceMethod();
addIsSRCandidateMethod();
addVoteCountMethod();
addTotalVoteCountMethod();
addTotalReceivedVoteCountMethod();
}

void GlobalContext::addVerifyMintProofMethod() {
Expand Down Expand Up @@ -384,7 +398,178 @@ inline vector<shared_ptr<MagicVariableDeclaration const>> constructMagicVariable
));
}

void GlobalContext::setCurrentContract(ContractDefinition const& _contract)
void GlobalContext::addVoteMethod() {
// bool vote(address[] memory addresses, unit256[] tronpowerlist)
TypePointers parameterTypes;

parameterTypes.push_back(TypeProvider::array(DataLocation::Memory, TypeProvider::address()));
parameterTypes.push_back(TypeProvider::array(DataLocation::Memory, TypeProvider::uint256()));

TypePointers returnParameterTypes;
returnParameterTypes.push_back(TypeProvider::boolean());
strings parameterNames;
parameterNames.push_back("srList");
parameterNames.push_back("tronpowerList");
strings returnParameterNames;
returnParameterNames.push_back("ok");

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

void GlobalContext::addRewardBalanceMethod() {
// uint rewardBalance()
TypePointers parameterTypes;
TypePointers returnParameterTypes;
returnParameterTypes.push_back(TypeProvider::uint256());
strings parameterNames;
strings returnParameterNames;
returnParameterNames.push_back("result");

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

void GlobalContext::addIsSRCandidateMethod() {
// bool isSrCandidate(address)
TypePointers parameterTypes;
parameterTypes.push_back(TypeProvider::address());

TypePointers returnParameterTypes;
returnParameterTypes.push_back(TypeProvider::boolean());
strings parameterNames;
parameterNames.push_back("address");
strings returnParameterNames;
returnParameterNames.push_back("ok");

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

void GlobalContext::addVoteCountMethod() {
// uint voteCount(address, address)
TypePointers parameterTypes;
parameterTypes.push_back(TypeProvider::address());
parameterTypes.push_back(TypeProvider::address());

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

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

void GlobalContext::addTotalVoteCountMethod() {
// uint totalVoteCount(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("totalVoteCount"), "totalVoteCount", TypeProvider::function(
parameterTypes,
returnParameterTypes,
parameterNames,
returnParameterNames,
FunctionType::Kind::totalVoteCount,
false,
StateMutability::View,
nullptr,
false,
false,
false,
false)
));
}

void GlobalContext::addTotalReceivedVoteCountMethod() {
// uint totalReceivedVoteCount(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("totalReceivedVoteCount"), "totalReceivedVoteCount", TypeProvider::function(
parameterTypes,
returnParameterTypes,
parameterNames,
returnParameterNames,
FunctionType::Kind::totalReceivedVoteCount,
false,
StateMutability::View,
nullptr,
false,
false,
false,
false)
));
}


void GlobalContext::setCurrentContract(ContractDefinition const& _contract)
{
m_currentContract = &_contract;
}
Expand Down
6 changes: 6 additions & 0 deletions libsolidity/analysis/GlobalContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ class GlobalContext: private boost::noncopyable
void addVerifyTransferProofMethod();
void addVerifyMintProofMethod();
void addPedersenHashMethod();
void addVoteMethod();
void addRewardBalanceMethod();
void addIsSRCandidateMethod();
void addVoteCountMethod();
void addTotalVoteCountMethod();
void addTotalReceivedVoteCountMethod();
};

}
12 changes: 12 additions & 0 deletions libsolidity/ast/Types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3045,6 +3045,13 @@ string FunctionType::richIdentifier() const
case Kind::Freeze: id += "freeze"; break;
case Kind::Unfreeze: id += "unfreeze"; break;
case Kind::FreezeExpireTime: id += "freezeExpireTime"; break;
case Kind::vote: id += "vote"; break;
case Kind::WithdrawReward: id += "withdrawreward"; break;
case Kind::rewardBalance: id += "rewardBalance"; break;
case Kind::isSrCandidate: id += "isSrCandidate"; break;
case Kind::voteCount: id += "voteCount"; break;
case Kind::totalVoteCount: id += "totalVoteCount"; break;
case Kind::totalReceivedVoteCount: id += "totalReceivedVoteCount"; break;
}
id += "_" + stateMutabilityToString(m_stateMutability);
id += identifierList(m_parameterTypes) + "returns" + identifierList(m_returnParameterTypes);
Expand Down Expand Up @@ -3515,6 +3522,11 @@ bool FunctionType::isBareCall() const
case Kind::verifyTransferProof:
case Kind::verifyMintProof:
case Kind::pedersenHash:
case Kind::rewardBalance:
case Kind::isSrCandidate:
case Kind::voteCount:
case Kind::totalVoteCount:
case Kind::totalReceivedVoteCount:
case Kind::SHA256:
case Kind::RIPEMD160:
return true;
Expand Down
17 changes: 12 additions & 5 deletions libsolidity/ast/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -1110,11 +1110,18 @@ class FunctionType: public Type
RIPEMD160, ///< CALL to special contract for ripemd160
verifyBurnProof,///< CALL to special contract for verifyBurnProof which is used for shielded transaction for TRC-20
verifyTransferProof,///< CALL to special contract for verifyTransferProof which is used for shielded transaction for TRC-20
verifyMintProof,//< CALL to special contract for verifyMintProof which is used for shielded transaction for TRC-20
pedersenHash,//< CALL to special contract for verifyMintProof which is used for shielded transaction for TRC-20
Freeze,//< CALL to freeze balance
Unfreeze,//< CALL to unfreeze balance
FreezeExpireTime,// < CALL to freeze expire time
verifyMintProof,/// < CALL to special contract for verifyMintProof which is used for shielded transaction for TRC-20
pedersenHash,/// < CALL to special contract for verifyMintProof which is used for shielded transaction for TRC-20
Freeze,/// < CALL to freeze balance
Unfreeze,/// < CALL to unfreeze balance
FreezeExpireTime,/// < CALL to freeze expire time
vote,///< CALL to vote witness
WithdrawReward,///< CALL to withdrawReward to address
rewardBalance, ///< get reward balance
isSrCandidate, ///< Judge witness whether or not
voteCount, ///< get vote count
totalVoteCount, /// get total vote count
totalReceivedVoteCount, /// get total received vote count
Log0,
Log1,
Log2,
Expand Down
Loading

0 comments on commit c666397

Please sign in to comment.