From 799106acfea7b748b61be4faa96ecb192af85fe1 Mon Sep 17 00:00:00 2001 From: Alexander Arlt Date: Thu, 20 May 2021 18:11:29 -0500 Subject: [PATCH] [isoltest] Add account builtin. Co-authored-by: Alex Beregszaszi --- test/ExecutionFramework.h | 6 +++ test/libsolidity/SemanticTest.cpp | 16 +++++-- test/libsolidity/SemanticTest.h | 2 +- .../semanticTests/isoltestTesting/account.sol | 48 +++++++++++++++++++ 4 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 test/libsolidity/semanticTests/isoltestTesting/account.sol diff --git a/test/ExecutionFramework.h b/test/ExecutionFramework.h index 602ba2337..de88679c7 100644 --- a/test/ExecutionFramework.h +++ b/test/ExecutionFramework.h @@ -241,6 +241,12 @@ public: return result; } + util::h160 setAccount(size_t _accountNumber) + { + m_sender = account(_accountNumber); + return m_sender; + } + private: template auto callCppAndEncodeResult(CppFunction const& _cppFunction, Args const&... _arguments) diff --git a/test/libsolidity/SemanticTest.cpp b/test/libsolidity/SemanticTest.cpp index 147349619..3409614cb 100644 --- a/test/libsolidity/SemanticTest.cpp +++ b/test/libsolidity/SemanticTest.cpp @@ -123,7 +123,7 @@ SemanticTest::SemanticTest( } } -map SemanticTest::makeBuiltins() const +map SemanticTest::makeBuiltins() { return { { @@ -151,9 +151,19 @@ map SemanticTest::makeBuiltins() const [this](FunctionCall const& _call) -> optional { soltestAssert(_call.arguments.parameters.empty(), "No arguments expected."); - return toBigEndian(u256(storageEmpty(m_contractAddress) ? 1 : 0)); + return toBigEndian(u256(storageEmpty(m_contractAddress) ? 1 : 0)); } - } + }, + { + "account", + [this](FunctionCall const& _call) -> optional + { + soltestAssert(_call.arguments.parameters.size() == 1, "Account number expected."); + size_t accountNumber = static_cast(stoi(_call.arguments.parameters.at(0).rawString)); + // Need to pad it to 32-bytes to workaround limitations in BytesUtils::formatHex. + return toBigEndian(h256(ExecutionFramework::setAccount(accountNumber).asBytes(), h256::AlignRight)); + } + }, }; } diff --git a/test/libsolidity/SemanticTest.h b/test/libsolidity/SemanticTest.h index 97623cb1c..adbcd4f5d 100644 --- a/test/libsolidity/SemanticTest.h +++ b/test/libsolidity/SemanticTest.h @@ -80,7 +80,7 @@ public: private: TestResult runTest(std::ostream& _stream, std::string const& _linePrefix, bool _formatted, bool _isYulRun, bool _isEwasmRun); bool checkGasCostExpectation(TestFunctionCall& io_test, bool _compileViaYul) const; - std::map makeBuiltins() const; + std::map makeBuiltins(); SourceMap m_sources; std::size_t m_lineOffset; std::vector m_tests; diff --git a/test/libsolidity/semanticTests/isoltestTesting/account.sol b/test/libsolidity/semanticTests/isoltestTesting/account.sol new file mode 100644 index 000000000..0703f4a37 --- /dev/null +++ b/test/libsolidity/semanticTests/isoltestTesting/account.sol @@ -0,0 +1,48 @@ +contract AccountBuiltinTest { + function who_am_i() public returns (address result) { + result = msg.sender; + } +} +// ==== +// compileViaYul: also +// ---- +// constructor() +// account: 0 -> 0x1212121212121212121212121212120000000012 +// who_am_i() -> 0x1212121212121212121212121212120000000012 +// balance: 0x1212121212121212121212121212120000000012 -> 1267650600228229401496703205376 +// account: 1 -> 0x1212121212121212121212121212120000001012 +// who_am_i() -> 0x1212121212121212121212121212120000001012 +// balance: 0x1212121212121212121212121212120000001012 -> 1267650600228229401496703205376 +// account: 2 -> 0x1212121212121212121212121212120000002012 +// who_am_i() -> 0x1212121212121212121212121212120000002012 +// balance: 0x1212121212121212121212121212120000002012 -> 1267650600228229401496703205376 +// account: 3 -> 0x1212121212121212121212121212120000003012 +// who_am_i() -> 0x1212121212121212121212121212120000003012 +// balance: 0x1212121212121212121212121212120000003012 -> 1267650600228229401496703205376 +// account: 4 -> 0x1212121212121212121212121212120000004012 +// who_am_i() -> 0x1212121212121212121212121212120000004012 +// balance: 0x1212121212121212121212121212120000004012 -> 1267650600228229401496703205376 +// account: 5 -> 0x1212121212121212121212121212120000005012 +// who_am_i() -> 0x1212121212121212121212121212120000005012 +// balance: 0x1212121212121212121212121212120000005012 -> 1267650600228229401496703205376 +// account: 6 -> 0x1212121212121212121212121212120000006012 +// who_am_i() -> 0x1212121212121212121212121212120000006012 +// balance: 0x1212121212121212121212121212120000006012 -> 1267650600228229401496703205376 +// account: 7 -> 0x1212121212121212121212121212120000007012 +// who_am_i() -> 0x1212121212121212121212121212120000007012 +// balance: 0x1212121212121212121212121212120000007012 -> 1267650600228229401496703205376 +// account: 8 -> 0x1212121212121212121212121212120000008012 +// who_am_i() -> 0x1212121212121212121212121212120000008012 +// balance: 0x1212121212121212121212121212120000008012 -> 1267650600228229401496703205376 +// account: 9 -> 0x1212121212121212121212121212120000009012 +// who_am_i() -> 0x1212121212121212121212121212120000009012 +// balance: 0x1212121212121212121212121212120000009012 -> 1267650600228229401496703205376 +// account: 10 -> 0x121212121212121212121212121212000000a012 +// who_am_i() -> 0x121212121212121212121212121212000000a012 +// balance: 0x121212121212121212121212121212000000a012 -> 0 +// account: 11 -> 0x121212121212121212121212121212000000b012 +// who_am_i() -> 0x121212121212121212121212121212000000b012 +// balance: 0x121212121212121212121212121212000000b012 -> 0 +// account: 12 -> 0x121212121212121212121212121212000000c012 +// who_am_i() -> 0x121212121212121212121212121212000000c012 +// balance: 0x121212121212121212121212121212000000c012 -> 0