Skip to content

Commit

Permalink
Merge pull request #2396 from div72/contract-separate-legacy-type
Browse files Browse the repository at this point in the history
contract: separate legacy type parsing
  • Loading branch information
jamescowens authored Dec 10, 2021
2 parents 191a2dd + c8b4f86 commit b10d11a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/gridcoin/contract/contract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<MT>", "</MT>")),
Contract::Type::ParseLegacy(ExtractXML(message, "<MT>", "</MT>")),
Contract::Action::Parse(ExtractXML(message, "<MA>", "</MA>")),
Contract::Body(ContractPayload::Make<LegacyPayload>(
ExtractXML(message, "<MK>", "</MK>"),
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions src/gridcoin/contract/contract.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions src/test/gridcoin/contract_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b10d11a

Please sign in to comment.