solidity/test/contracts/FixedFeeRegistrar.cpp

256 lines
10 KiB
C++
Raw Normal View History

2015-07-29 17:31:25 +00:00
/*
This file is part of solidity.
2015-07-29 17:31:25 +00:00
solidity is free software: you can redistribute it and/or modify
2015-07-29 17:31:25 +00:00
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
2015-07-29 17:31:25 +00:00
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
2015-07-29 17:31:25 +00:00
*/
// SPDX-License-Identifier: GPL-3.0
2015-07-29 17:31:25 +00:00
/**
* @author Christian <c@ethdev.com>
* @date 2015
* Tests for a fixed fee registrar contract.
*/
#include <libsolutil/LazyInit.h>
2015-07-29 17:31:25 +00:00
#include <string>
#include <tuple>
#include <optional>
2016-03-18 08:22:15 +00:00
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4535) // calling _set_se_translator requires /EHa
#endif
2015-07-29 17:31:25 +00:00
#include <boost/test/unit_test.hpp>
2016-03-18 08:22:15 +00:00
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
2016-06-09 16:54:29 +00:00
#include <test/libsolidity/SolidityExecutionFramework.h>
2015-07-29 17:31:25 +00:00
using namespace std;
using namespace solidity;
using namespace solidity::util;
using namespace solidity::test;
2015-07-29 17:31:25 +00:00
namespace solidity::frontend::test
2015-07-29 17:31:25 +00:00
{
2015-08-04 14:31:27 +00:00
namespace
{
2015-07-29 17:31:25 +00:00
static char const* registrarCode = R"DELIMITER(
//sol FixedFeeRegistrar
// Simple global registrar with fixed-fee reservations.
// @authors:
// Gav Wood <g@ethdev.com>
2020-01-10 17:13:57 +00:00
pragma solidity >=0.4.0 <0.8.0;
2016-08-19 17:57:21 +00:00
2019-09-19 19:31:42 +00:00
abstract contract Registrar {
2015-07-29 17:31:25 +00:00
event Changed(string indexed name);
2019-11-05 17:25:34 +00:00
function owner(string memory _name) public virtual view returns (address o_owner);
function addr(string memory _name) public virtual view returns (address o_address);
function subRegistrar(string memory _name) virtual public view returns (address o_subRegistrar);
function content(string memory _name) public virtual view returns (bytes32 o_content);
2015-07-29 17:31:25 +00:00
}
contract FixedFeeRegistrar is Registrar {
struct Record {
address addr;
address subRegistrar;
bytes32 content;
address owner;
}
modifier onlyrecordowner(string memory _name) { if (m_record(_name).owner == msg.sender) _; }
2015-07-29 17:31:25 +00:00
2018-07-11 13:57:07 +00:00
function reserve(string memory _name) public payable {
Record storage rec = m_record(_name);
if (rec.owner == 0x0000000000000000000000000000000000000000 && msg.value >= c_fee) {
2015-07-29 17:31:25 +00:00
rec.owner = msg.sender;
2018-06-26 18:09:54 +00:00
emit Changed(_name);
2015-07-29 17:31:25 +00:00
}
}
function disown(string memory _name, address payable _refund) onlyrecordowner(_name) public {
2018-05-23 13:39:26 +00:00
delete m_recordData[uint(keccak256(bytes(_name))) / 8];
2016-06-28 22:14:22 +00:00
if (!_refund.send(c_fee))
revert();
2018-06-26 18:09:54 +00:00
emit Changed(_name);
2015-07-29 17:31:25 +00:00
}
2018-07-11 13:57:07 +00:00
function transfer(string memory _name, address _newOwner) onlyrecordowner(_name) public {
2015-07-29 17:31:25 +00:00
m_record(_name).owner = _newOwner;
2018-06-26 18:09:54 +00:00
emit Changed(_name);
2015-07-29 17:31:25 +00:00
}
2018-07-11 13:57:07 +00:00
function setAddr(string memory _name, address _a) onlyrecordowner(_name) public {
2015-07-29 17:31:25 +00:00
m_record(_name).addr = _a;
2018-06-26 18:09:54 +00:00
emit Changed(_name);
2015-07-29 17:31:25 +00:00
}
2018-07-11 13:57:07 +00:00
function setSubRegistrar(string memory _name, address _registrar) onlyrecordowner(_name) public {
2015-07-29 17:31:25 +00:00
m_record(_name).subRegistrar = _registrar;
2018-06-26 18:09:54 +00:00
emit Changed(_name);
2015-07-29 17:31:25 +00:00
}
2018-07-11 13:57:07 +00:00
function setContent(string memory _name, bytes32 _content) onlyrecordowner(_name) public {
2015-07-29 17:31:25 +00:00
m_record(_name).content = _content;
2018-06-26 18:09:54 +00:00
emit Changed(_name);
2015-07-29 17:31:25 +00:00
}
2018-07-11 13:57:07 +00:00
function record(string memory _name) public view returns (address o_addr, address o_subRegistrar, bytes32 o_content, address o_owner) {
Record storage rec = m_record(_name);
2015-07-29 17:31:25 +00:00
o_addr = rec.addr;
o_subRegistrar = rec.subRegistrar;
o_content = rec.content;
o_owner = rec.owner;
}
2019-09-16 12:33:43 +00:00
function addr(string memory _name) public override view returns (address) { return m_record(_name).addr; }
function subRegistrar(string memory _name) public override view returns (address) { return m_record(_name).subRegistrar; }
function content(string memory _name) public override view returns (bytes32) { return m_record(_name).content; }
function owner(string memory _name) public override view returns (address) { return m_record(_name).owner; }
2015-07-29 17:31:25 +00:00
Record[2**253] m_recordData;
function m_record(string memory _name) view internal returns (Record storage o_record) {
2018-05-23 13:39:26 +00:00
return m_recordData[uint(keccak256(bytes(_name))) / 8];
2015-07-29 17:31:25 +00:00
}
uint constant c_fee = 69 ether;
}
)DELIMITER";
static LazyInit<bytes> s_compiledRegistrar;
2015-07-29 17:31:25 +00:00
class RegistrarTestFramework: public SolidityExecutionFramework
2015-07-29 17:31:25 +00:00
{
protected:
void deployRegistrar()
{
bytes const& compiled = s_compiledRegistrar.init([&]{
return compileContract(registrarCode, "FixedFeeRegistrar");
});
2018-02-27 18:32:05 +00:00
sendMessage(compiled, true);
BOOST_REQUIRE(m_transactionSuccessful);
2016-06-28 21:57:02 +00:00
BOOST_REQUIRE(!m_output.empty());
2015-07-29 17:31:25 +00:00
}
u256 const m_fee = u256("69000000000000000000");
};
2015-08-04 14:31:27 +00:00
}
2015-07-29 17:31:25 +00:00
/// This is a test suite that tests optimised code!
BOOST_FIXTURE_TEST_SUITE(SolidityFixedFeeRegistrar, RegistrarTestFramework)
BOOST_AUTO_TEST_CASE(creation)
{
2016-06-28 21:57:02 +00:00
deployRegistrar();
2015-07-29 17:31:25 +00:00
}
BOOST_AUTO_TEST_CASE(reserve)
{
// Test that reserving works and fee is taken into account.
deployRegistrar();
string name[] = {"abc", "def", "ghi"};
BOOST_REQUIRE(callContractFunctionWithValue("reserve(string)", m_fee, encodeDyn(name[0])) == encodeArgs());
2016-06-18 00:08:20 +00:00
BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name[0])) == encodeArgs(h256(account(0), h256::AlignRight)));
2015-07-29 17:31:25 +00:00
BOOST_REQUIRE(callContractFunctionWithValue("reserve(string)", m_fee + 1, encodeDyn(name[1])) == encodeArgs());
2016-06-18 00:08:20 +00:00
BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name[1])) == encodeArgs(h256(account(0), h256::AlignRight)));
2015-07-29 17:31:25 +00:00
BOOST_REQUIRE(callContractFunctionWithValue("reserve(string)", m_fee - 1, encodeDyn(name[2])) == encodeArgs());
2019-12-14 13:38:44 +00:00
BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name[2])) == encodeArgs(h256{}));
2015-07-29 17:31:25 +00:00
}
BOOST_AUTO_TEST_CASE(double_reserve)
{
// Test that it is not possible to re-reserve from a different address.
deployRegistrar();
string name = "abc";
BOOST_REQUIRE(callContractFunctionWithValue("reserve(string)", m_fee, encodeDyn(name)) == encodeArgs());
2016-06-18 00:08:20 +00:00
BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name)) == encodeArgs(h256(account(0), h256::AlignRight)));
2015-07-29 17:31:25 +00:00
Make the Solidity repository standalone. This commit is the culmination of several months of work to decouple Solidity from the webthree-umbrella so that it can be developed in parallel with cpp-ethereum (the Ethereum C++ runtime) and so that even for the Solidity unit-tests there is no hard-dependency onto the C++ runtime. The Tests-over-IPC refactoring was a major step in the same process which was already committed. This commit contains the following changes: - A subset of the CMake functionality in webthree-helpers was extracted and tailored for Solidity into ./cmake. Further cleanup is certainly possible. - A subset of the libdevcore functionality in libweb3core was extracted and tailored for Solidity into ./libdevcore. Further cleanup is certainly possible - The gas price constants in EVMSchedule were orphaned into libevmasm. - Some other refactorings and cleanups were made to sever unnecessary EVM dependencies in the Solidity unit-tests. - TravisCI and Appveyor support was added, covering builds and running of the unit-tests (Linux and macOS only for now) - A bug-fix was made to get the Tests-over-IPC running on macOS. - There are still reliability issues in the unit-tests, which need immediate attention. The Travis build has been flipped to run the unit-tests 5 times, to try to flush these out. - The Emscripten automation which was previously in webthree-umbrella was merged into the TravisCI automation here. - The development ZIP deployment step has been commented out, but we will want to read that ONLY for release branch. Further iteration on these changes will definitely be needed, but I feel these have got to sufficient maturity than holding them back further isn't winning us anything. It is go time :-)
2016-08-01 05:25:37 +00:00
sendEther(account(1), 100 * ether);
2016-06-18 00:08:20 +00:00
m_sender = account(1);
2015-07-29 17:31:25 +00:00
BOOST_REQUIRE(callContractFunctionWithValue("reserve(string)", m_fee, encodeDyn(name)) == encodeArgs());
2016-06-18 00:08:20 +00:00
BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name)) == encodeArgs(h256(account(0), h256::AlignRight)));
2015-07-29 17:31:25 +00:00
}
BOOST_AUTO_TEST_CASE(properties)
{
// Test setting and retrieving the various properties works.
deployRegistrar();
string names[] = {"abc", "def", "ghi"};
size_t addr = 0x9872543;
2016-06-18 00:08:20 +00:00
size_t count = 1;
2015-07-29 17:31:25 +00:00
for (string const& name: names)
{
addr++;
2016-06-18 00:08:20 +00:00
m_sender = account(0);
Make the Solidity repository standalone. This commit is the culmination of several months of work to decouple Solidity from the webthree-umbrella so that it can be developed in parallel with cpp-ethereum (the Ethereum C++ runtime) and so that even for the Solidity unit-tests there is no hard-dependency onto the C++ runtime. The Tests-over-IPC refactoring was a major step in the same process which was already committed. This commit contains the following changes: - A subset of the CMake functionality in webthree-helpers was extracted and tailored for Solidity into ./cmake. Further cleanup is certainly possible. - A subset of the libdevcore functionality in libweb3core was extracted and tailored for Solidity into ./libdevcore. Further cleanup is certainly possible - The gas price constants in EVMSchedule were orphaned into libevmasm. - Some other refactorings and cleanups were made to sever unnecessary EVM dependencies in the Solidity unit-tests. - TravisCI and Appveyor support was added, covering builds and running of the unit-tests (Linux and macOS only for now) - A bug-fix was made to get the Tests-over-IPC running on macOS. - There are still reliability issues in the unit-tests, which need immediate attention. The Travis build has been flipped to run the unit-tests 5 times, to try to flush these out. - The Emscripten automation which was previously in webthree-umbrella was merged into the TravisCI automation here. - The development ZIP deployment step has been commented out, but we will want to read that ONLY for release branch. Further iteration on these changes will definitely be needed, but I feel these have got to sufficient maturity than holding them back further isn't winning us anything. It is go time :-)
2016-08-01 05:25:37 +00:00
sendEther(account(count), 100 * ether);
2016-06-18 00:08:20 +00:00
m_sender = account(count);
Address owner = m_sender;
2015-07-29 17:31:25 +00:00
// setting by sender works
BOOST_REQUIRE(callContractFunctionWithValue("reserve(string)", m_fee, encodeDyn(name)) == encodeArgs());
2016-06-18 00:08:20 +00:00
BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name)) == encodeArgs(h256(owner, h256::AlignRight)));
2015-07-29 17:31:25 +00:00
BOOST_CHECK(callContractFunction("setAddr(string,address)", u256(0x40), u256(addr), u256(name.length()), name) == encodeArgs());
BOOST_CHECK(callContractFunction("addr(string)", encodeDyn(name)) == encodeArgs(addr));
BOOST_CHECK(callContractFunction("setSubRegistrar(string,address)", u256(0x40), addr + 20, u256(name.length()), name) == encodeArgs());
BOOST_CHECK(callContractFunction("subRegistrar(string)", encodeDyn(name)) == encodeArgs(addr + 20));
BOOST_CHECK(callContractFunction("setContent(string,bytes32)", u256(0x40), addr + 90, u256(name.length()), name) == encodeArgs());
BOOST_CHECK(callContractFunction("content(string)", encodeDyn(name)) == encodeArgs(addr + 90));
2016-06-18 00:08:20 +00:00
count++;
2015-07-29 17:31:25 +00:00
// but not by someone else
2016-06-20 23:39:06 +00:00
m_sender = account(0);
Make the Solidity repository standalone. This commit is the culmination of several months of work to decouple Solidity from the webthree-umbrella so that it can be developed in parallel with cpp-ethereum (the Ethereum C++ runtime) and so that even for the Solidity unit-tests there is no hard-dependency onto the C++ runtime. The Tests-over-IPC refactoring was a major step in the same process which was already committed. This commit contains the following changes: - A subset of the CMake functionality in webthree-helpers was extracted and tailored for Solidity into ./cmake. Further cleanup is certainly possible. - A subset of the libdevcore functionality in libweb3core was extracted and tailored for Solidity into ./libdevcore. Further cleanup is certainly possible - The gas price constants in EVMSchedule were orphaned into libevmasm. - Some other refactorings and cleanups were made to sever unnecessary EVM dependencies in the Solidity unit-tests. - TravisCI and Appveyor support was added, covering builds and running of the unit-tests (Linux and macOS only for now) - A bug-fix was made to get the Tests-over-IPC running on macOS. - There are still reliability issues in the unit-tests, which need immediate attention. The Travis build has been flipped to run the unit-tests 5 times, to try to flush these out. - The Emscripten automation which was previously in webthree-umbrella was merged into the TravisCI automation here. - The development ZIP deployment step has been commented out, but we will want to read that ONLY for release branch. Further iteration on these changes will definitely be needed, but I feel these have got to sufficient maturity than holding them back further isn't winning us anything. It is go time :-)
2016-08-01 05:25:37 +00:00
sendEther(account(count), 100 * ether);
2016-06-18 00:08:20 +00:00
m_sender = account(count);
BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name)) == encodeArgs(h256(owner, h256::AlignRight)));
2015-07-29 17:31:25 +00:00
BOOST_CHECK(callContractFunction("setAddr(string,address)", u256(0x40), addr + 1, u256(name.length()), name) == encodeArgs());
BOOST_CHECK(callContractFunction("addr(string)", encodeDyn(name)) == encodeArgs(addr));
BOOST_CHECK(callContractFunction("setSubRegistrar(string,address)", u256(0x40), addr + 20 + 1, u256(name.length()), name) == encodeArgs());
BOOST_CHECK(callContractFunction("subRegistrar(string)", encodeDyn(name)) == encodeArgs(addr + 20));
BOOST_CHECK(callContractFunction("setContent(string,bytes32)", u256(0x40), addr + 90 + 1, u256(name.length()), name) == encodeArgs());
BOOST_CHECK(callContractFunction("content(string)", encodeDyn(name)) == encodeArgs(addr + 90));
2016-06-18 00:08:20 +00:00
count++;
2015-07-29 17:31:25 +00:00
}
}
BOOST_AUTO_TEST_CASE(transfer)
{
deployRegistrar();
string name = "abc";
BOOST_REQUIRE(callContractFunctionWithValue("reserve(string)", m_fee, encodeDyn(name)) == encodeArgs());
2016-06-18 00:08:20 +00:00
BOOST_CHECK(callContractFunction("setContent(string,bytes32)", u256(0x40), h256(account(0), h256::AlignRight), u256(name.length()), name) == encodeArgs());
2015-07-29 17:31:25 +00:00
BOOST_CHECK(callContractFunction("transfer(string,address)", u256(0x40), u256(555), u256(name.length()), name) == encodeArgs());
BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name)) == encodeArgs(u256(555)));
2016-06-18 00:08:20 +00:00
BOOST_CHECK(callContractFunction("content(string)", encodeDyn(name)) == encodeArgs(h256(account(0), h256::AlignRight)));
2015-07-29 17:31:25 +00:00
}
BOOST_AUTO_TEST_CASE(disown)
{
deployRegistrar();
string name = "abc";
BOOST_REQUIRE(callContractFunctionWithValue("reserve(string)", m_fee, encodeDyn(name)) == encodeArgs());
2016-06-18 00:08:20 +00:00
BOOST_CHECK(callContractFunction("setContent(string,bytes32)", u256(0x40), h256(account(0), h256::AlignRight), u256(name.length()), name) == encodeArgs());
2015-07-29 17:31:25 +00:00
BOOST_CHECK(callContractFunction("setAddr(string,address)", u256(0x40), u256(124), u256(name.length()), name) == encodeArgs());
BOOST_CHECK(callContractFunction("setSubRegistrar(string,address)", u256(0x40), u256(125), u256(name.length()), name) == encodeArgs());
BOOST_CHECK_EQUAL(balanceAt(Address(0x124)), 0);
2015-07-29 17:31:25 +00:00
BOOST_CHECK(callContractFunction("disown(string,address)", u256(0x40), u256(0x124), name.size(), name) == encodeArgs());
BOOST_CHECK_EQUAL(balanceAt(Address(0x124)), m_fee);
2015-07-29 17:31:25 +00:00
BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name)) == encodeArgs(u256(0)));
BOOST_CHECK(callContractFunction("content(string)", encodeDyn(name)) == encodeArgs(u256(0)));
BOOST_CHECK(callContractFunction("addr(string)", encodeDyn(name)) == encodeArgs(u256(0)));
BOOST_CHECK(callContractFunction("subRegistrar(string)", encodeDyn(name)) == encodeArgs(u256(0)));
}
BOOST_AUTO_TEST_SUITE_END()
} // end namespaces