diff --git a/src/gridcoin/contract/contract.cpp b/src/gridcoin/contract/contract.cpp index 262f656c67..fd5a3a07c7 100644 --- a/src/gridcoin/contract/contract.cpp +++ b/src/gridcoin/contract/contract.cpp @@ -677,7 +677,7 @@ Contract Contract::Parse(const std::string& message) return Contract( 1, // Legacy XML-like string contracts always parse to a v1 contract. - Contract::Type::Parse(ExtractXML(message, "", "")), + Contract::Type::ParseLegacy(ExtractXML(message, "", "")), Contract::Action::Parse(ExtractXML(message, "", "")), Contract::Body(ContractPayload::Make( ExtractXML(message, "", ""), @@ -744,6 +744,22 @@ Contract::Type::Type(ContractType type) : EnumByte(type) { } +Contract::Type Contract::Type::ParseLegacy(std::string input) +{ + // For parsing historical contracts. Do not add new contract types + // to this function. Add to Contract::Type::Parse instead. + + // Ordered by frequency: + if (input == "beacon") return ContractType::BEACON; + if (input == "vote") return ContractType::VOTE; + if (input == "poll") return ContractType::POLL; + if (input == "project") return ContractType::PROJECT; + if (input == "scraper") return ContractType::SCRAPER; + if (input == "protocol") return ContractType::PROTOCOL; + + return ContractType::UNKNOWN; +} + Contract::Type Contract::Type::Parse(std::string input) { // Ordered by frequency: diff --git a/src/gridcoin/contract/contract.h b/src/gridcoin/contract/contract.h index d75c704ec9..bc89e4efd6 100644 --- a/src/gridcoin/contract/contract.h +++ b/src/gridcoin/contract/contract.h @@ -67,6 +67,17 @@ class Contract //! //! \brief Parse a \c ContractType value from its legacy string + //! representation. Use the Parse method instead for new code. + //! + //! \param input String representation of a contract type. + //! + //! \return A value enumerated on \c ContractType. Returns the value of + //! \c ContractType::UNKNOWN for an unrecognized contract type. + //! + static Type ParseLegacy(std::string input); + + //! + //! \brief Parse a \c ContractType value from its string //! representation. //! //! \param input String representation of a contract type. diff --git a/src/test/gridcoin/contract_tests.cpp b/src/test/gridcoin/contract_tests.cpp index 358c6955f1..8a712fcb1d 100644 --- a/src/test/gridcoin/contract_tests.cpp +++ b/src/test/gridcoin/contract_tests.cpp @@ -219,15 +219,32 @@ BOOST_AUTO_TEST_CASE(it_initializes_to_a_provided_type) BOOST_AUTO_TEST_CASE(it_parses_a_contract_type_from_a_string) { GRC::Contract::Type type = GRC::Contract::Type::Parse("beacon"); + GRC::Contract::Type type2 = GRC::Contract::Type::ParseLegacy("beacon"); BOOST_CHECK(type == GRC::ContractType::BEACON); + BOOST_CHECK(type2 == GRC::ContractType::BEACON); } BOOST_AUTO_TEST_CASE(it_parses_unknown_contract_types_to_unknown) { GRC::Contract::Type type = GRC::Contract::Type::Parse("something"); + GRC::Contract::Type type2 = GRC::Contract::Type::ParseLegacy("something"); BOOST_CHECK(type == GRC::ContractType::UNKNOWN); + BOOST_CHECK(type2 == GRC::ContractType::UNKNOWN); +} + +BOOST_AUTO_TEST_CASE(it_doesnt_parse_unintended_contract_types) +{ + GRC::Contract::Type unk = GRC::Contract::Type::ParseLegacy("claim"); + GRC::Contract::Type unk2 = GRC::Contract::Type::ParseLegacy("message"); + GRC::Contract::Type type = GRC::Contract::Type::Parse("claim"); + GRC::Contract::Type type2 = GRC::Contract::Type::Parse("message"); + + BOOST_CHECK(unk == GRC::ContractType::UNKNOWN); + BOOST_CHECK(unk2 == GRC::ContractType::UNKNOWN); + BOOST_CHECK(type == GRC::ContractType::CLAIM); + BOOST_CHECK(type2 == GRC::ContractType::MESSAGE); } BOOST_AUTO_TEST_CASE(it_provides_the_wrapped_contract_type_enum_value)