From 7b5e5fb1681301c52c3562777cf903c365528f9e Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 6 Aug 2015 11:01:59 +0200 Subject: [PATCH] Actual auction tests. --- libethcore/ABI.h | 5 ++ test/contracts/AuctionRegistrar.cpp | 85 ++++++++++++++++++- test/libsolidity/solidityExecutionFramework.h | 10 ++- 3 files changed, 94 insertions(+), 6 deletions(-) diff --git a/libethcore/ABI.h b/libethcore/ABI.h index 09aca6754..7d7be90dd 100644 --- a/libethcore/ABI.h +++ b/libethcore/ABI.h @@ -68,5 +68,10 @@ template T abiOut(bytes const& _data) return ABIDeserialiser::deserialise(o); } +template T abiOut(bytesConstRef& _data) +{ + return ABIDeserialiser::deserialise(_data); +} + } } diff --git a/test/contracts/AuctionRegistrar.cpp b/test/contracts/AuctionRegistrar.cpp index 3832e8e09..7c5d9fa35 100644 --- a/test/contracts/AuctionRegistrar.cpp +++ b/test/contracts/AuctionRegistrar.cpp @@ -289,6 +289,9 @@ protected: return callString("disown", _name); } }; + + u256 const m_biddingTime = u256(7 * 24 * 3600); + u256 const m_renewalInterval = u256(365 * 24 * 3600); }; } @@ -393,6 +396,7 @@ BOOST_AUTO_TEST_CASE(disown) 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); @@ -405,11 +409,86 @@ BOOST_AUTO_TEST_CASE(disown) 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); } -//@todo: -// - reverse lookup -// - actual auction +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() diff --git a/test/libsolidity/solidityExecutionFramework.h b/test/libsolidity/solidityExecutionFramework.h index 3f4437200..98241b2ff 100644 --- a/test/libsolidity/solidityExecutionFramework.h +++ b/test/libsolidity/solidityExecutionFramework.h @@ -171,6 +171,8 @@ public: public: ContractInterface(ExecutionFramework& _framework): m_framework(_framework) {} + void setNextValue(u256 const& _value) { m_nextValue = _value; } + protected: template bytes const& call(std::string const& _sig, Args const&... _arguments) @@ -210,11 +212,13 @@ public: std::string callAddressReturnsString(std::string const& _name, u160 const& _arg) { - bytes const& ret = call(_name + "(address)", _arg); + bytesConstRef ret = ref(call(_name + "(address)", _arg)); BOOST_REQUIRE(ret.size() >= 0x20); + u256 offset = eth::abiOut(ret); + BOOST_REQUIRE_EQUAL(offset, 0x20); u256 len = eth::abiOut(ret); - BOOST_REQUIRE(ret.size() == (0x20 + len) / 32 * 32); - return std::string(ret.begin() + 0x20, ret.begin() + 0x20 + size_t(len)); + BOOST_REQUIRE_EQUAL(ret.size(), ((len + 0x1f) / 0x20) * 0x20); + return ret.cropped(0, size_t(len)).toString(); } h256 callStringReturnsBytes32(std::string const& _name, std::string const& _arg)