From aed6c22318dcc763342d434b2b85e125d1d02610 Mon Sep 17 00:00:00 2001 From: Harikrishnan Mulackal Date: Tue, 5 May 2020 16:34:01 +0530 Subject: [PATCH] solidity-upgrade can now change now to block.timestamp --- docs/070-breaking-changes.rst | 1 + docs/using-the-compiler.rst | 6 ++++++ tools/solidityUpgrade/SourceTransform.h | 18 ++++++++++++++++++ tools/solidityUpgrade/SourceUpgrade.cpp | 2 ++ tools/solidityUpgrade/SourceUpgrade.h | 8 ++++++-- tools/solidityUpgrade/Upgrade070.cpp | 19 +++++++++++++++++++ tools/solidityUpgrade/Upgrade070.h | 9 +++++++++ 7 files changed, 61 insertions(+), 2 deletions(-) diff --git a/docs/070-breaking-changes.rst b/docs/070-breaking-changes.rst index 1d8dd695d..f273dcd0b 100644 --- a/docs/070-breaking-changes.rst +++ b/docs/070-breaking-changes.rst @@ -15,3 +15,4 @@ This section gives detailed instructions on how to update prior code for every b * Change ``f.value(...)()`` to ``f{value: ...}()``. Similarly ``(new C).value(...)()`` to ``(new C){value: ...}()`` and ``f.gas(...)()`` to ``f{gas: ...}()``. +* Change ``now`` to ``block.timestamp``. diff --git a/docs/using-the-compiler.rst b/docs/using-the-compiler.rst index e409323fa..08f817670 100644 --- a/docs/using-the-compiler.rst +++ b/docs/using-the-compiler.rst @@ -580,6 +580,12 @@ Available upgrade modules | | | ``f{gas: ..., value: ...}()`` and | | | | ``(new C){value: ...}()``. | +-----------------+---------+--------------------------------------------------+ +| ``now`` | 0.7.0 | The ``now`` keyword is deprecated. Use | +| | | ``block.timestamp`` instead. | +| | | | +| | | | +| | | | ++-----------------+---------+--------------------------------------------------+ Please read :doc:`0.5.0 release notes <050-breaking-changes>`, :doc:`0.6.0 release notes <060-breaking-changes>` and diff --git a/tools/solidityUpgrade/SourceTransform.h b/tools/solidityUpgrade/SourceTransform.h index b9250ae6a..200c20e5b 100644 --- a/tools/solidityUpgrade/SourceTransform.h +++ b/tools/solidityUpgrade/SourceTransform.h @@ -267,6 +267,24 @@ public: return ""; } + + static std::string nowUpdate(langutil::SourceLocation const& _location) + { + std::regex nowRegex{"now"}; + + if (regex_search(_location.text(), nowRegex)) + { + return regex_replace(_location.text(), nowRegex, "block.timestamp"); + } + else + solAssert( + false, + LocationHelper() << "Could not fix: " << _location.text() << " at " << _location << + "\nNeeds to be fixed manually." + ); + + return ""; + } }; } diff --git a/tools/solidityUpgrade/SourceUpgrade.cpp b/tools/solidityUpgrade/SourceUpgrade.cpp index 04f83eeee..6fffc074d 100644 --- a/tools/solidityUpgrade/SourceUpgrade.cpp +++ b/tools/solidityUpgrade/SourceUpgrade.cpp @@ -196,6 +196,8 @@ Allowed options)", m_suite.activateModule(Module::VirtualFunction); else if (module == "dotsyntax") m_suite.activateModule(Module::DotSyntax); + else if (module == "now") + m_suite.activateModule(Module::NowKeyword); else { error() << "Unknown upgrade module \"" + module + "\"" << endl; diff --git a/tools/solidityUpgrade/SourceUpgrade.h b/tools/solidityUpgrade/SourceUpgrade.h index 9fb7a5ee2..8b82b310e 100644 --- a/tools/solidityUpgrade/SourceUpgrade.h +++ b/tools/solidityUpgrade/SourceUpgrade.h @@ -58,7 +58,8 @@ private: AbstractContract, OverridingFunction, VirtualFunction, - DotSyntax + DotSyntax, + NowKeyword }; /// Upgrade suite that hosts all available modules. @@ -84,6 +85,8 @@ private: /// Solidity 0.7.0 if (isActivated(Module::DotSyntax)) DotSyntax{m_changes}.analyze(_sourceUnit); + if (isActivated(Module::NowKeyword)) + NowKeyword{m_changes}.analyze(_sourceUnit); } void activateModule(Module _module) { m_modules.insert(_module); } @@ -103,7 +106,8 @@ private: Module::AbstractContract, Module::OverridingFunction, Module::VirtualFunction, - Module::DotSyntax + Module::DotSyntax, + Module::NowKeyword }; }; diff --git a/tools/solidityUpgrade/Upgrade070.cpp b/tools/solidityUpgrade/Upgrade070.cpp index c38ae2b97..8e921ce11 100644 --- a/tools/solidityUpgrade/Upgrade070.cpp +++ b/tools/solidityUpgrade/Upgrade070.cpp @@ -40,3 +40,22 @@ void DotSyntax::endVisit(FunctionCall const& _functionCall) ); } } + +void NowKeyword::endVisit(Identifier const& _identifier) +{ + IdentifierAnnotation& annotation = _identifier.annotation(); + + if (MagicVariableDeclaration const* magicVar + = dynamic_cast(annotation.referencedDeclaration)) + { + if (magicVar->type()->category() == Type::Category::Integer) + { + solAssert(_identifier.name() == "now", ""); + m_changes.emplace_back( + UpgradeChange::Level::Safe, + _identifier.location(), + SourceTransform::nowUpdate(_identifier.location()) + ); + } + } +} diff --git a/tools/solidityUpgrade/Upgrade070.h b/tools/solidityUpgrade/Upgrade070.h index 29d689f97..bfa07ce51 100644 --- a/tools/solidityUpgrade/Upgrade070.h +++ b/tools/solidityUpgrade/Upgrade070.h @@ -32,4 +32,13 @@ private: void endVisit(frontend::FunctionCall const& _expression) override; }; +class NowKeyword: public AnalysisUpgrade +{ +public: + using AnalysisUpgrade::AnalysisUpgrade; + void analyze(frontend::SourceUnit const& _sourceUnit) { _sourceUnit.accept(*this); } +private: + void endVisit(frontend::Identifier const& _expression) override; +}; + }