aboutsummaryrefslogtreecommitdiffstats
path: root/test/contracts/AuctionRegistrar.cpp
blob: e30f4572751be75eddb754a2185f39e98f24eb93 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
    This file is part of cpp-ethereum.

    cpp-ethereum is free software: you can redistribute it and/or modify
    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.

    cpp-ethereum is distributed in the hope that it will be useful,
    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 cpp-ethereum.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * @author Christian <c@ethdev.com>
 * @date 2015
 * Tests for a fixed fee registrar contract.
 */

#include <string>
#include <tuple>
#include <boost/test/unit_test.hpp>
#include <libdevcore/Hash.h>
#include <libethcore/ABI.h>
#include <test/libsolidity/SolidityExecutionFramework.h>

using namespace std;

namespace dev
{
namespace solidity
{
namespace test
{

namespace
{

static char const* registrarCode = R"DELIMITER(
//sol

contract NameRegister {
    function addr(string _name) constant returns (address o_owner);
    function name(address _owner) constant returns (string o_name);
}

contract Registrar is NameRegister {
    event Changed(string indexed name);
    event PrimaryChanged(string indexed name, address indexed addr);

    function owner(string _name) constant returns (address o_owner);
    function addr(string _name) constant returns (address o_address);
    function subRegistrar(string _name) constant returns (address o_subRegistrar);
    function content(string _name) constant returns (bytes32 o_content);

    function name(address _owner) constant returns (string o_name);
}

contract AuctionSystem {
    event AuctionEnded(string indexed _name, address _winner);
    event NewBid(string indexed _name, address _bidder, uint _value);

    /// Function that is called once an auction ends.
    function onAuctionEnd(string _name) internal;

    function bid(string _name, address _bidder, uint _value) internal {
        var auction = m_auctions[_name];
        if (auction.endDate > 0 && now > auction.endDate)
        {
            AuctionEnded(_name, auction.highestBidder);
            onAuctionEnd(_name);
            delete m_auctions[_name];
            return;
        }
        if (msg.value > auction.highestBid)
        {
            // new bid on auction
            auction.secondHighestBid = auction.highestBid;
            auction.sumOfBids += _value;
            auction.highestBid = _value;
            auction.highestBidder = _bidder;
            auction.endDate = now + c_biddingTime;

            NewBid(_name, _bidder, _value);
        }
    }

    uint constant c_biddingTime = 7 days;

    struct Auction {
        address highestBidder;
        uint highestBid;
        uint secondHighestBid;
        uint sumOfBids;
        uint endDate;
    }
    mapping(string => Auction) m_auctions;
}

contract GlobalRegistrar is Registrar, AuctionSystem {
    struct Record {
        address owner;
        address primary;
        address subRegistrar;
        bytes32 content;
        uint renewalDate;
    }

    uint constant c_renewalInterval = 1 years;
    uint constant c_freeBytes = 12;

    function Registrar() {
        // TODO: Populate with hall-of-fame.
    }

    function() {
        // prevent people from just sending funds to the registrar
        throw;
    }

    function onAuctionEnd(string _name) internal {
        var auction = m_auctions[_name];
        var record = m_toRecord[_name];
        if (record.owner != 0)
            record.owner.send(auction.sumOfBids - auction.highestBid / 100);
        else
            auction.highestBidder.send(auction.highestBid - auction.secondHighestBid);
        record.renewalDate = now + c_renewalInterval;
        record.owner = auction.highestBidder;
        Changed(_name);
    }

    function reserve(string _name) external {
        if (bytes(_name).length == 0)
            throw;
        bool needAuction = requiresAuction(_name);
        if (needAuction)
        {
            if (now < m_toRecord[_name].renewalDate)
                throw;
            bid(_name, msg.sender, msg.value);
        }
        else
        {
            Record record = m_toRecord[_name];
            if (record.owner != 0)
                throw;
            m_toRecord[_name].owner = msg.sender;
            Changed(_name);
        }
    }

    function requiresAuction(string _name) internal returns (bool) {
        return bytes(_name).length < c_freeBytes;
    }

    modifier onlyrecordowner(string _name) { if (m_toRecord[_name].owner == msg.sender) _ }

    function transfer(string _name, address _newOwner) onlyrecordowner(_name) {
        m_toRecord[_name].owner = _newOwner;
        Changed(_name);
    }

    function disown(string _name) onlyrecordowner(_name) {
        if (stringsEqual(m_toName[m_toRecord[_name].primary], _name))
        {
            PrimaryChanged(_name, m_toRecord[_name].primary);
            m_toName[m_toRecord[_name].primary] = "";
        }
        delete m_toRecord[_name];
        Changed(_name);
    }

    function setAddress(string _name, address _a, bool _primary) onlyrecordowner(_name) {
        m_toRecord[_name].primary = _a;
        if (_primary)
        {
            PrimaryChanged(_name, _a);
            m_toName[_a] = _name;
        }
        Changed(_name);
    }
    function setSubRegistrar(string _name, address _registrar) onlyrecordowner(_name) {
        m_toRecord[_name].subRegistrar = _registrar;
        Changed(_name);
    }
    function setContent(string _name, bytes32 _content) onlyrecordowner(_name) {
        m_toRecord[_name].content = _content;
        Changed(_name);
    }

    function stringsEqual(string storage _a, string memory _b) internal returns (bool) {
        bytes storage a = bytes(_a);
        bytes memory b = bytes(_b);
        if (a.length != b.length)
            return false;
        // @todo unroll this loop
        for (uint i = 0; i < a.length; i ++)
            if (a[i] != b[i])
                return false;
        return true;
    }

    function owner(string _name) constant returns (address) { return m_toRecord[_name].owner; }
    function addr(string _name) constant returns (address) { return m_toRecord[_name].primary; }
    function subRegistrar(string _name) constant returns (address) { return m_toRecord[_name].subRegistrar; }
    function content(string _name) constant returns (bytes32) { return m_toRecord[_name].content; }
    function name(address _addr) constant returns (string o_name) { return m_toName[_addr]; }

    mapping (address => string) m_toName;
    mapping (string => Record) m_toRecord;
}
)DELIMITER";

static unique_ptr<bytes> s_compiledRegistrar;

class AuctionRegistrarTestFramework: public ExecutionFramework
{
protected:
    void deployRegistrar()
    {
        if (!s_compiledRegistrar)
        {
            m_optimize = true;
            m_compiler.reset(false, m_addStandardSources);
            m_compiler.addSource("", registrarCode);
            ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(m_optimize, m_optimizeRuns), "Compiling contract failed");
            s_compiledRegistrar.reset(new bytes(m_compiler.object("GlobalRegistrar").bytecode));
        }
        sendMessage(*s_compiledRegistrar, true);
        BOOST_REQUIRE(!m_output.empty());
    }

    using ContractInterface = ExecutionFramework::ContractInterface;
    class RegistrarInterface: public ContractInterface
    {
    public:
        RegistrarInterface(ExecutionFramework& _framework): ContractInterface(_framework) {}
        void reserve(string const& _name)
        {
            callString("reserve", _name);
        }
        u160 owner(string const& _name)
        {
            return callStringReturnsAddress("owner", _name);
        }
        void setAddress(string const& _name, u160 const& _address, bool _primary)
        {
            callStringAddressBool("setAddress", _name, _address, _primary);
        }
        u160 addr(string const& _name)
        {
            return callStringReturnsAddress("addr", _name);
        }
        string name(u160 const& _addr)
        {
            return callAddressReturnsString("name", _addr);
        }
        void setSubRegistrar(string const& _name, u160 const& _address)
        {
            callStringAddress("setSubRegistrar", _name, _address);
        }
        u160 subRegistrar(string const& _name)
        {
            return callStringReturnsAddress("subRegistrar", _name);
        }
        void setContent(string const& _name, h256 const& _content)
        {
            callStringBytes32("setContent", _name, _content);
        }
        h256 content(string const& _name)
        {
            return callStringReturnsBytes32("content", _name);
        }
        void transfer(string const& _name, u160 const& _target)
        {
            return callStringAddress("transfer", _name, _target);
        }
        void disown(string const& _name)
        {
            return callString("disown", _name);
        }
    };

    u256 const m_biddingTime = u256(7 * 24 * 3600);
    u256 const m_renewalInterval = u256(365 * 24 * 3600);
};

}

/// This is a test suite that tests optimised code!
BOOST_FIXTURE_TEST_SUITE(SolidityAuctionRegistrar, AuctionRegistrarTestFramework)

BOOST_AUTO_TEST_CASE(creation)
{
    deployRegistrar();
}

BOOST_AUTO_TEST_CASE(reserve)
{
    // Test that reserving works for long strings
    deployRegistrar();
    vector<string> names{"abcabcabcabcabc", "defdefdefdefdef", "ghighighighighighighighighighighighighighighi"};
    m_sender = Address(0x123);

    RegistrarInterface registrar(*this);

    // should not work
    registrar.reserve("");
    BOOST_CHECK_EQUAL(registrar.owner(""), u160(0));

    for (auto const& name: names)
    {
        registrar.reserve(name);
        BOOST_CHECK_EQUAL(registrar.owner(name), u160(0x123));
    }
}

BOOST_AUTO_TEST_CASE(double_reserve_long)
{
    // Test that it is not possible to re-reserve from a different address.
    deployRegistrar();
    string name = "abcabcabcabcabcabcabcabcabcabca";
    m_sender = Address(0x123);
    RegistrarInterface registrar(*this);
    registrar.reserve(name);
    BOOST_CHECK_EQUAL(registrar.owner(name), u160(0x123));

    m_sender = Address(0x124);
    registrar.reserve(name);
    BOOST_CHECK_EQUAL(registrar.owner(name), u160(0x123));
}

BOOST_AUTO_TEST_CASE(properties)
{
    // Test setting and retrieving  the various properties works.
    deployRegistrar();
    RegistrarInterface registrar(*this);
    string names[] = {"abcaeouoeuaoeuaoeu", "defncboagufra,fui", "ghagpyajfbcuajouhaeoi"};
    size_t addr = 0x9872543;
    for (string const& name: names)
    {
        addr++;
        size_t sender = addr + 10007;
        m_sender = Address(sender);
        // setting by sender works
        registrar.reserve(name);
        BOOST_CHECK_EQUAL(registrar.owner(name), u160(sender));
        registrar.setAddress(name, addr, true);
        BOOST_CHECK_EQUAL(registrar.addr(name), u160(addr));
        registrar.setSubRegistrar(name, addr + 20);
        BOOST_CHECK_EQUAL(registrar.subRegistrar(name), u160(addr + 20));
        registrar.setContent(name, h256(u256(addr + 90)));
        BOOST_CHECK_EQUAL(registrar.content(name), h256(u256(addr + 90)));

        // but not by someone else
        m_sender = Address(h256(addr + 10007 - 1));
        BOOST_CHECK_EQUAL(registrar.owner(name), u160(sender));
        registrar.setAddress(name, addr + 1, true);
        BOOST_CHECK_EQUAL(registrar.addr(name), u160(addr));
        registrar.setSubRegistrar(name, addr + 20 + 1);
        BOOST_CHECK_EQUAL(registrar.subRegistrar(name), u160(addr + 20));
        registrar.setContent(name, h256(u256(addr + 90 + 1)));
        BOOST_CHECK_EQUAL(registrar.content(name), h256(u256(addr + 90)));
    }
}

BOOST_AUTO_TEST_CASE(transfer)
{
    deployRegistrar();
    string name = "abcaoeguaoucaeoduceo";
    m_sender = Address(0x123);
    RegistrarInterface registrar(*this);
    registrar.reserve(name);
    registrar.setContent(name, h256(u256(123)));
    registrar.transfer(name, u160(555));
    BOOST_CHECK_EQUAL(registrar.owner(name), u160(555));
    BOOST_CHECK_EQUAL(registrar.content(name), h256(u256(123)));
}

BOOST_AUTO_TEST_CASE(disown)
{
    deployRegistrar();
    string name = "abcaoeguaoucaeoduceo";
    m_sender = Address(0x123);
    RegistrarInterface registrar(*this);
    registrar.reserve(name);
    registrar.setContent(name, h256(u256(123)));
    registrar.setAddress(name, u160(124), true);
    registrar.setSubRegistrar(name, u160(125));
    BOOST_CHECK_EQUAL(registrar.name(u160(124)), name);

    // someone else tries disowning
    m_sender = Address(0x128);
    registrar.disown(name);
    BOOST_CHECK_EQUAL(registrar.owner(name), 0x123);

    m_sender = Address(0x123);
    registrar.disown(name);
    BOOST_CHECK_EQUAL(registrar.owner(name), 0);
    BOOST_CHECK_EQUAL(registrar.addr(name), 0);
    BOOST_CHECK_EQUAL(registrar.subRegistrar(name), 0);
    BOOST_CHECK_EQUAL(registrar.content(name), h256());
    BOOST_CHECK_EQUAL(registrar.name(u160(124)), "");
}

BOOST_AUTO_TEST_CASE(auction_simple)
{
    deployRegistrar();
    string name = "x";
    m_sender = Address(0x123);
    RegistrarInterface registrar(*this);
    // initiate auction
    registrar.setNextValue(8);
    registrar.reserve(name);
    BOOST_CHECK_EQUAL(registrar.owner(name), 0);
    // "wait" until auction end
    m_envInfo.setTimestamp(m_envInfo.timestamp() + m_biddingTime + 10);
    // trigger auction again
    registrar.reserve(name);
    BOOST_CHECK_EQUAL(registrar.owner(name), 0x123);
}

BOOST_AUTO_TEST_CASE(auction_bidding)
{
    deployRegistrar();
    string name = "x";
    m_sender = Address(0x123);
    RegistrarInterface registrar(*this);
    // initiate auction
    registrar.setNextValue(8);
    registrar.reserve(name);
    BOOST_CHECK_EQUAL(registrar.owner(name), 0);
    // overbid self
    m_envInfo.setTimestamp(m_biddingTime - 10);
    registrar.setNextValue(12);
    registrar.reserve(name);
    // another bid by someone else
    m_sender = Address(0x124);
    m_envInfo.setTimestamp(2 * m_biddingTime - 50);
    registrar.setNextValue(13);
    registrar.reserve(name);
    BOOST_CHECK_EQUAL(registrar.owner(name), 0);
    // end auction by first bidder (which is not highest) trying to overbid again (too late)
    m_sender = Address(0x123);
    m_envInfo.setTimestamp(4 * m_biddingTime);
    registrar.setNextValue(20);
    registrar.reserve(name);
    BOOST_CHECK_EQUAL(registrar.owner(name), 0x124);
}

BOOST_AUTO_TEST_CASE(auction_renewal)
{
    deployRegistrar();
    string name = "x";
    RegistrarInterface registrar(*this);
    // register name by auction
    m_sender = Address(0x123);
    registrar.setNextValue(8);
    registrar.reserve(name);
    m_envInfo.setTimestamp(4 * m_biddingTime);
    registrar.reserve(name);
    BOOST_CHECK_EQUAL(registrar.owner(name), 0x123);

    // try to re-register before interval end
    m_sender = Address(0x222);
    registrar.setNextValue(80);
    m_envInfo.setTimestamp(m_envInfo.timestamp() + m_renewalInterval - 1);
    registrar.reserve(name);
    m_envInfo.setTimestamp(m_envInfo.timestamp() + m_biddingTime);
    // if there is a bug in the renewal logic, this would transfer the ownership to 0x222,
    // but if there is no bug, this will initiate the auction, albeit with a zero bid
    registrar.reserve(name);
    BOOST_CHECK_EQUAL(registrar.owner(name), 0x123);

    m_envInfo.setTimestamp(m_envInfo.timestamp() + 2);
    registrar.setNextValue(80);
    registrar.reserve(name);
    BOOST_CHECK_EQUAL(registrar.owner(name), 0x123);
    m_envInfo.setTimestamp(m_envInfo.timestamp() + m_biddingTime + 2);
    registrar.reserve(name);
    BOOST_CHECK_EQUAL(registrar.owner(name), 0x222);
}

BOOST_AUTO_TEST_SUITE_END()

}
}
} // end namespaces