Skip to content

Commit

Permalink
[Lity] Add rand built-in function to get an uint random numbe…
Browse files Browse the repository at this point in the history
…r from the virtual machiine.

- Add a new opcode ``rand`` at 0xf9.
- Update syntax highlight for ``rand``.
  • Loading branch information
hydai committed Feb 27, 2019
1 parent 5e14982 commit 4f90b5f
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Features:

Language Features:
* General: Add a new modifier ``freegas``, which allows developers to use contract balance to pay the transaction fee for the users.
* General: Add a new built-in function ``rand`` to get an ``uint`` random number from the virtual machine.

### Lity 1.2.6 (2018-12-07)

Expand Down
2 changes: 1 addition & 1 deletion docs/utils/LityLexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def type_names_mn(prefix, sizerangem, sizerangen):
(r'push\b', Name.Function),

# built-in functions and/or precompiles
(r'(addmod|ecrecover|keccak256|mulmod|ripemd160|sha256|sha3|eni|isValidator|schedule)\b',
(r'(addmod|ecrecover|keccak256|mulmod|ripemd160|sha256|sha3|eni|isValidator|freegas|rand|schedule)\b',
Name.Function),

# everything else is a var/function name
Expand Down
2 changes: 2 additions & 0 deletions libevmasm/Instruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const std::map<std::string, Instruction> dev::solidity::c_instructions =
{ "SFDIV", Instruction::SFDIV },
{ "ENI", Instruction::ENI },
{ "ISVALIDATOR", Instruction::ISVALIDATOR },
{ "RAND", Instruction::RAND },
{ "FREEGAS", Instruction::FREEGAS },
{ "ADDRESS", Instruction::ADDRESS },
{ "BALANCE", Instruction::BALANCE },
Expand Down Expand Up @@ -219,6 +220,7 @@ static const std::map<Instruction, InstructionInfo> c_instructionInfo =
{ Instruction::SFDIV, { "SFDIV", 0, 3, 1, false, Tier::Mid } },
{ Instruction::ENI, { "ENI", 0, 3, 1, true, Tier::Special } },
{ Instruction::ISVALIDATOR, { "ISVALIDATOR", 0, 1, 1, false, Tier::VeryLow } },
{ Instruction::RAND, { "RAND", 0, 0, 1, false, Tier::Low } },
{ Instruction::FREEGAS, { "FREEGAS", 0, 0, 0, false, Tier::VeryLow } },
{ Instruction::ADDRESS, { "ADDRESS", 0, 0, 1, false, Tier::Base } },
{ Instruction::BALANCE, { "BALANCE", 0, 1, 1, false, Tier::Balance } },
Expand Down
1 change: 1 addition & 0 deletions libevmasm/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ enum class Instruction: uint8_t
ENI,
ISVALIDATOR = 0xf6,
FREEGAS = 0xf8,
RAND = 0xf9,
STATICCALL = 0xfa, ///< like CALL but disallow state modifications
CREATE2 = 0xfb, ///< create new account with associated code at address `sha3(sender + salt + sha3(init code)) % 2**160`

Expand Down
1 change: 1 addition & 0 deletions libsolidity/analysis/GlobalContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ m_magicVariables(vector<shared_ptr<MagicVariableDeclaration const>>{
make_shared<MagicVariableDeclaration>("msg", make_shared<MagicType>(MagicType::Kind::Message)),
make_shared<MagicVariableDeclaration>("mulmod", make_shared<FunctionType>(strings{"uint256", "uint256", "uint256"}, strings{"uint256"}, FunctionType::Kind::MulMod, FunctionType::SpecialModifier::Default, false, StateMutability::Pure)),
make_shared<MagicVariableDeclaration>("now", make_shared<IntegerType>(256)),
make_shared<MagicVariableDeclaration>("rand", make_shared<FunctionType>(strings(), strings{"uint256"}, FunctionType::Kind::Rand, FunctionType::SpecialModifier::Default, true, StateMutability::Pure)),
make_shared<MagicVariableDeclaration>("require", make_shared<FunctionType>(strings{"bool"}, strings{}, FunctionType::Kind::Require, FunctionType::SpecialModifier::Default, false, StateMutability::Pure)),
make_shared<MagicVariableDeclaration>("require", make_shared<FunctionType>(strings{"bool", "string memory"}, strings{}, FunctionType::Kind::Require, FunctionType::SpecialModifier::Default, false, StateMutability::Pure)),
make_shared<MagicVariableDeclaration>("revert", make_shared<FunctionType>(strings(), strings(), FunctionType::Kind::Revert, FunctionType::SpecialModifier::Default, false, StateMutability::Pure)),
Expand Down
2 changes: 2 additions & 0 deletions libsolidity/ast/Types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2590,6 +2590,7 @@ string FunctionType::richIdentifier() const
case Kind::ABIEncodeWithSignature: id += "abiencodewithsignature"; break;
case Kind::ENI: id += "eni"; break;
case Kind::IsValidator: id += "isvalidator"; break;
case Kind::Rand: id += "rand"; break;
default: solAssert(false, "Unknown function location."); break;
}
switch (m_specialModifier)
Expand Down Expand Up @@ -2969,6 +2970,7 @@ bool FunctionType::isPure() const
m_kind == Kind::ABIEncodeWithSelector ||
m_kind == Kind::ABIEncodeWithSignature ||
m_kind == Kind::IsValidator ||
m_kind == Kind::Rand ||
m_kind == Kind::ENI;
}

Expand Down
5 changes: 3 additions & 2 deletions libsolidity/ast/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -930,8 +930,9 @@ class FunctionType: public Type
ABIEncodeWithSelector,
ABIEncodeWithSignature,
GasLeft, ///< gasleft()
ENI,
IsValidator ///< isValidator()
ENI, ///< eni()
IsValidator, ///< isValidator()
Rand ///< rand()
};

virtual Category category() const override { return Category::Function; }
Expand Down
3 changes: 3 additions & 0 deletions libsolidity/codegen/ExpressionCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,9 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
case FunctionType::Kind::GasLeft:
m_context << Instruction::GAS;
break;
case FunctionType::Kind::Rand:
m_context << Instruction::RAND;
break;
case FunctionType::Kind::IsValidator:
arguments.front()->accept(*this);
utils().convertType(*arguments.front()->annotation().type, *function.parameterTypes().front(), true);
Expand Down
2 changes: 2 additions & 0 deletions libsolidity/interface/EVMVersion.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class EVMVersion:
bool hasStaticCall() const { return *this >= byzantium(); }
bool hasENI() const {return *this == lity(); }
bool hasIsValidator() const {return *this == lity(); }
bool hasFreeGas() const {return *this == lity(); }
bool hasRand() const {return *this == lity(); }
bool hasBitwiseShifting() const { return *this >= constantinople(); }

/// Whether we have to retain the costs for the call opcode itself (false),
Expand Down

0 comments on commit 4f90b5f

Please sign in to comment.