diff options
author | Wei-Ning Huang <w@dexon.org> | 2018-10-18 11:38:05 +0800 |
---|---|---|
committer | Jimmy Hu <jimmy.hu@dexon.org> | 2018-11-09 13:31:14 +0800 |
commit | 433aebb7c91acf6d364acd76f6018dc1b4f847af (patch) | |
tree | 755cb38bb4ec4c873cc86c537200d4cfa1b1b83f | |
parent | 91c1c3ada94f983f71938fba5abb570207acc248 (diff) | |
download | dexon-solidity-433aebb7c91acf6d364acd76f6018dc1b4f847af.tar dexon-solidity-433aebb7c91acf6d364acd76f6018dc1b4f847af.tar.gz dexon-solidity-433aebb7c91acf6d364acd76f6018dc1b4f847af.tar.bz2 dexon-solidity-433aebb7c91acf6d364acd76f6018dc1b4f847af.tar.lz dexon-solidity-433aebb7c91acf6d364acd76f6018dc1b4f847af.tar.xz dexon-solidity-433aebb7c91acf6d364acd76f6018dc1b4f847af.tar.zst dexon-solidity-433aebb7c91acf6d364acd76f6018dc1b4f847af.zip |
Add opcode RAND supportv0.4.25
DEXON has a built-in on chain random oracle that allow one to retrieve a
random variable. Add `rand` solidity variable is introduced to load the
random variable onto the stack.
-rw-r--r-- | libevmasm/Instruction.cpp | 2 | ||||
-rw-r--r-- | libevmasm/Instruction.h | 1 | ||||
-rw-r--r-- | libevmasm/SemanticInformation.cpp | 1 | ||||
-rw-r--r-- | libsolidity/analysis/GlobalContext.cpp | 1 | ||||
-rw-r--r-- | libsolidity/analysis/ViewPureChecker.cpp | 2 | ||||
-rw-r--r-- | libsolidity/codegen/ExpressionCompiler.cpp | 8 |
6 files changed, 13 insertions, 2 deletions
diff --git a/libevmasm/Instruction.cpp b/libevmasm/Instruction.cpp index f9bbad2c..05669feb 100644 --- a/libevmasm/Instruction.cpp +++ b/libevmasm/Instruction.cpp @@ -57,6 +57,7 @@ const std::map<std::string, Instruction> dev::solidity::c_instructions = { "MULMOD", Instruction::MULMOD }, { "SIGNEXTEND", Instruction::SIGNEXTEND }, { "KECCAK256", Instruction::KECCAK256 }, + { "RAND", Instruction::RAND }, { "ADDRESS", Instruction::ADDRESS }, { "BALANCE", Instruction::BALANCE }, { "ORIGIN", Instruction::ORIGIN }, @@ -200,6 +201,7 @@ static const std::map<Instruction, InstructionInfo> c_instructionInfo = { Instruction::MULMOD, { "MULMOD", 0, 3, 1, false, Tier::Mid } }, { Instruction::SIGNEXTEND, { "SIGNEXTEND", 0, 2, 1, false, Tier::Low } }, { Instruction::KECCAK256, { "KECCAK256", 0, 2, 1, true, Tier::Special } }, + { Instruction::RAND, { "RAND", 0, 0, 1, false, Tier::High } }, { Instruction::ADDRESS, { "ADDRESS", 0, 0, 1, false, Tier::Base } }, { Instruction::BALANCE, { "BALANCE", 0, 1, 1, false, Tier::Balance } }, { Instruction::ORIGIN, { "ORIGIN", 0, 0, 1, false, Tier::Base } }, diff --git a/libevmasm/Instruction.h b/libevmasm/Instruction.h index dc116f88..200751a9 100644 --- a/libevmasm/Instruction.h +++ b/libevmasm/Instruction.h @@ -66,6 +66,7 @@ enum class Instruction: uint8_t SAR, ///< bitwise SAR operation KECCAK256 = 0x20, ///< compute KECCAK-256 hash + RAND = 0x2f, ///< load a random value ADDRESS = 0x30, ///< get address of currently executing account BALANCE, ///< get balance of the given account diff --git a/libevmasm/SemanticInformation.cpp b/libevmasm/SemanticInformation.cpp index 71267ee8..13dedd29 100644 --- a/libevmasm/SemanticInformation.cpp +++ b/libevmasm/SemanticInformation.cpp @@ -236,6 +236,7 @@ bool SemanticInformation::invalidInPureFunctions(Instruction _instruction) case Instruction::BLOCKHASH: case Instruction::COINBASE: case Instruction::TIMESTAMP: + case Instruction::RAND: case Instruction::NUMBER: case Instruction::DIFFICULTY: case Instruction::GASLIMIT: diff --git a/libsolidity/analysis/GlobalContext.cpp b/libsolidity/analysis/GlobalContext.cpp index 756bb540..347cf943 100644 --- a/libsolidity/analysis/GlobalContext.cpp +++ b/libsolidity/analysis/GlobalContext.cpp @@ -51,6 +51,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, false, StateMutability::Pure)), make_shared<MagicVariableDeclaration>("now", make_shared<IntegerType>(256)), + make_shared<MagicVariableDeclaration>("rand", make_shared<IntegerType>(256)), make_shared<MagicVariableDeclaration>("require", make_shared<FunctionType>(strings{"bool"}, strings{}, FunctionType::Kind::Require, false, StateMutability::Pure)), make_shared<MagicVariableDeclaration>("require", make_shared<FunctionType>(strings{"bool", "string memory"}, strings{}, FunctionType::Kind::Require, false, StateMutability::Pure)), make_shared<MagicVariableDeclaration>("revert", make_shared<FunctionType>(strings(), strings(), FunctionType::Kind::Revert, false, StateMutability::Pure)), diff --git a/libsolidity/analysis/ViewPureChecker.cpp b/libsolidity/analysis/ViewPureChecker.cpp index d9843012..610ba617 100644 --- a/libsolidity/analysis/ViewPureChecker.cpp +++ b/libsolidity/analysis/ViewPureChecker.cpp @@ -210,7 +210,7 @@ void ViewPureChecker::endVisit(Identifier const& _identifier) mutability = StateMutability::View; break; case Type::Category::Integer: - solAssert(_identifier.name() == "now", ""); + solAssert(_identifier.name() == "now" || _identifier.name() == "rand", ""); mutability = StateMutability::View; break; default: diff --git a/libsolidity/codegen/ExpressionCompiler.cpp b/libsolidity/codegen/ExpressionCompiler.cpp index f38c1e67..0a6ce940 100644 --- a/libsolidity/codegen/ExpressionCompiler.cpp +++ b/libsolidity/codegen/ExpressionCompiler.cpp @@ -1265,6 +1265,8 @@ bool ExpressionCompiler::visit(MemberAccess const& _memberAccess) m_context << Instruction::COINBASE; else if (member == "timestamp") m_context << Instruction::TIMESTAMP; + else if (member == "rand") + m_context << Instruction::RAND; else if (member == "difficulty") m_context << Instruction::DIFFICULTY; else if (member == "number") @@ -1491,7 +1493,11 @@ void ExpressionCompiler::endVisit(Identifier const& _identifier) break; case Type::Category::Integer: // "now" - m_context << Instruction::TIMESTAMP; + if (_identifier.name() == "now") { + m_context << Instruction::TIMESTAMP; + } else if (_identifier.name() == "rand") { + m_context << Instruction::RAND; + } break; default: break; |