2015-07-29 17:31:25 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2015-07-29 17:31:25 +00:00
|
|
|
|
2016-11-18 23:13:20 +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.
|
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
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
|
2016-11-18 23:13:20 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-29 17:31:25 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2015
|
|
|
|
* Tests for a fixed fee registrar contract.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <tuple>
|
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;
|
2016-11-28 00:58:07 +00:00
|
|
|
using namespace dev::test;
|
2015-07-29 17:31:25 +00:00
|
|
|
|
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace solidity
|
|
|
|
{
|
|
|
|
namespace test
|
|
|
|
{
|
|
|
|
|
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>
|
|
|
|
|
2018-07-28 11:57:11 +00:00
|
|
|
pragma solidity >=0.4.0 <0.6.0;
|
2016-08-19 17:57:21 +00:00
|
|
|
|
2015-07-29 17:31:25 +00:00
|
|
|
contract Registrar {
|
|
|
|
event Changed(string indexed name);
|
|
|
|
|
2018-07-11 13:57:07 +00:00
|
|
|
function owner(string memory _name) public view returns (address o_owner);
|
|
|
|
function addr(string memory _name) public view returns (address o_address);
|
|
|
|
function subRegistrar(string memory _name) public view returns (address o_subRegistrar);
|
|
|
|
function content(string memory _name) public 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;
|
|
|
|
}
|
|
|
|
|
2018-07-12 04:18:50 +00:00
|
|
|
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 {
|
2018-07-11 11:23:33 +00:00
|
|
|
Record storage rec = m_record(_name);
|
2018-05-30 21:02:47 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2018-09-12 14:21:43 +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))
|
2018-07-11 23:43:19 +00:00
|
|
|
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) {
|
2018-07-11 11:23:33 +00:00
|
|
|
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;
|
|
|
|
}
|
2018-07-11 13:57:07 +00:00
|
|
|
function addr(string memory _name) public view returns (address) { return m_record(_name).addr; }
|
|
|
|
function subRegistrar(string memory _name) public view returns (address) { return m_record(_name).subRegistrar; }
|
|
|
|
function content(string memory _name) public view returns (bytes32) { return m_record(_name).content; }
|
|
|
|
function owner(string memory _name) public view returns (address) { return m_record(_name).owner; }
|
2015-07-29 17:31:25 +00:00
|
|
|
|
|
|
|
Record[2**253] m_recordData;
|
2018-07-12 04:18:50 +00:00
|
|
|
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 unique_ptr<bytes> s_compiledRegistrar;
|
|
|
|
|
2016-11-28 00:21:01 +00:00
|
|
|
class RegistrarTestFramework: public SolidityExecutionFramework
|
2015-07-29 17:31:25 +00:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
void deployRegistrar()
|
|
|
|
{
|
|
|
|
if (!s_compiledRegistrar)
|
2018-02-27 18:32:05 +00:00
|
|
|
s_compiledRegistrar.reset(new bytes(compileContract(registrarCode, "FixedFeeRegistrar")));
|
|
|
|
|
2015-07-29 17:31:25 +00:00
|
|
|
sendMessage(*s_compiledRegistrar, true);
|
2018-06-15 10:18:00 +00:00
|
|
|
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());
|
|
|
|
BOOST_CHECK(callContractFunction("owner(string)", encodeDyn(name[2])) == encodeArgs(h256(0)));
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
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);
|
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);
|
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());
|
|
|
|
|
2016-06-14 15:01:57 +00:00
|
|
|
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());
|
2016-06-14 15:01:57 +00:00
|
|
|
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
|