Skip to content

Commit

Permalink
Merge pull request #2531 from jamescowens/fix_poll_serialization
Browse files Browse the repository at this point in the history
voting: Change m_additional_fields serialization
  • Loading branch information
jamescowens committed Jul 4, 2022
2 parents 6ba7d33 + e104213 commit 12a328b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
3 changes: 3 additions & 0 deletions src/gridcoin/project.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "gridcoin/contract/handler.h"
#include "gridcoin/contract/payload.h"
#include "serialize.h"
#include "pubkey.h"

#include <memory>
#include <vector>
Expand Down Expand Up @@ -58,6 +59,7 @@ class Project : public IContractPayload
std::string m_url; //!< As it exists in the contract value field.
int64_t m_timestamp; //!< Timestamp of the contract.
bool m_gdpr_controls; //!< Boolean to indicate whether project has GDPR stats export controls.
CPubKey m_public_key; //!< Project public key.

//!
//! \brief Initialize an empty, invalid project object.
Expand Down Expand Up @@ -187,6 +189,7 @@ class Project : public IContractPayload

if (m_version >= 2) {
READWRITE(m_gdpr_controls);
READWRITE(m_public_key);
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/gridcoin/voting/payloads.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ class PollPayload : public IContractPayload
{
READWRITE(m_version);
READWRITE(m_poll);

// The poll m_additional_fields is serialized here rather than in the poll class, because it depends on the
// payload version.
if (m_version >= 3) {
READWRITE(m_poll.m_additional_fields);
}

READWRITE(m_claim);
}
}; // PollPayload
Expand Down
10 changes: 1 addition & 9 deletions src/gridcoin/voting/poll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,7 @@ const std::vector<Poll::PollTypeRules> Poll::POLL_TYPE_RULES = {
// These must be kept in the order that corresponds to the PollType enum.
// { min duration, min vote percent AVW, { vector of required additional fieldnames } }
{ 0, 0, {} }, // PollType::UNKNOWN
// Note there is NO payload version protection on the vector of required additional fieldnames
// and all payloads less than v3 only allowed PollType::SURVEY. Furthermore, the serialization
// of the poll class additional fields depends only on whether the poll type is SURVEY. The net
// of this is that the required additional fieldnames need to remain an empty vector for SURVEY.
//
// If a new SURVEY type is needed in the future with additional fields, a new enum entry should
// be created for it.
//
// In addition note that any poll type that has a min vote percent AVW requirement must
// Note that any poll type that has a min vote percent AVW requirement must
// also require the weight type of BALANCE_AND_MAGNITUDE, so therefore the
// only poll type that can actually use BALANCE is SURVEY. All other WeightTypes are deprecated.
{ 7, 0, {} }, // PollType::SURVEY
Expand Down
15 changes: 2 additions & 13 deletions src/gridcoin/voting/poll.h
Original file line number Diff line number Diff line change
Expand Up @@ -558,19 +558,8 @@ class Poll
READWRITE(m_choices);
}

// Note: this is a little dirty but works, because all polls prior to v3 are SURVEY, and the
// additional fields for survey is an empty vector. Therefore this serialization will only
// be operative if a poll type other than survey is used, and this cannot occur until v3+.
// Refer to the comments in POLL_TYPE_RULES. This is necessary because the only other solution would be
// to pass the poll payload version into the poll object, which would be problematic.
//
// TODO: Remove COMMUNITY after finishing isolated fork testing. (Community was used to test v3 polls
// before the introduction of additional fields, and therefore the community polls on the isolated
// testing fork do not have the m_additional_fields serialization and removal of the COMMUNITY below
// will result in an serialization I/O error.
if (m_type != PollType::SURVEY && m_type != PollType::COMMUNITY) {
READWRITE(m_additional_fields);
}
// Note that m_additional_fields is not serialized here, but rather in the PollPayload class. This
// is because it depends on the poll payload version, which is not available here.
}
}; // Poll
} // namespace GRC
Expand Down
15 changes: 12 additions & 3 deletions src/test/gridcoin/project_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ BOOST_AUTO_TEST_CASE(it_serializes_to_a_stream_for_add)
<< GRC::Project::CURRENT_VERSION
<< std::string("Enigma")
<< std::string("http://enigma.test/@")
<< true;
<< true
<< CPubKey{};

CDataStream streamv2(SER_NETWORK, PROTOCOL_VERSION);
projectv2.Serialize(streamv2, GRC::ContractAction::ADD);
Expand All @@ -204,14 +205,19 @@ BOOST_AUTO_TEST_CASE(it_deserializes_from_a_stream_for_add)
BOOST_CHECK_EQUAL(projectv1.m_url, "http://enigma.test/@");
BOOST_CHECK_EQUAL(projectv1.m_timestamp, 0);
BOOST_CHECK_EQUAL(projectv1.m_gdpr_controls, false);
BOOST_CHECK(projectv1.m_public_key == CPubKey{});

BOOST_CHECK(projectv1.WellFormed(GRC::ContractAction::ADD) == true);

CPubKey public_key = CPubKey(ParseHex(
"111111111111111111111111111111111111111111111111111111111111111111"));

CDataStream streamv2 = CDataStream(SER_NETWORK, PROTOCOL_VERSION)
<< GRC::Project::CURRENT_VERSION
<< std::string("Enigma")
<< std::string("http://enigma.test/@")
<< true;
<< true
<< public_key;

GRC::Project projectv2;
projectv2.Unserialize(streamv2, GRC::ContractAction::ADD);
Expand All @@ -221,6 +227,7 @@ BOOST_AUTO_TEST_CASE(it_deserializes_from_a_stream_for_add)
BOOST_CHECK_EQUAL(projectv2.m_url, "http://enigma.test/@");
BOOST_CHECK_EQUAL(projectv2.m_timestamp, 0);
BOOST_CHECK_EQUAL(projectv2.m_gdpr_controls, true);
BOOST_CHECK(projectv2.m_public_key == public_key);

BOOST_CHECK(projectv2.WellFormed(GRC::ContractAction::ADD) == true);

Expand Down Expand Up @@ -274,6 +281,7 @@ BOOST_AUTO_TEST_CASE(it_deserializes_from_a_stream_for_delete)
BOOST_CHECK_EQUAL(projectv1.m_url, "");
BOOST_CHECK_EQUAL(projectv1.m_timestamp, 0);
BOOST_CHECK_EQUAL(projectv1.m_gdpr_controls, false);
BOOST_CHECK(projectv1.m_public_key == CPubKey{});

BOOST_CHECK(projectv1.WellFormed(GRC::ContractAction::REMOVE) == true);

Expand All @@ -288,7 +296,8 @@ BOOST_AUTO_TEST_CASE(it_deserializes_from_a_stream_for_delete)
BOOST_CHECK_EQUAL(projectv2.m_name, "Enigma");
BOOST_CHECK_EQUAL(projectv2.m_url, "");
BOOST_CHECK_EQUAL(projectv2.m_timestamp, 0);
BOOST_CHECK_EQUAL(projectv1.m_gdpr_controls, false);
BOOST_CHECK_EQUAL(projectv2.m_gdpr_controls, false);
BOOST_CHECK(projectv2.m_public_key == CPubKey{});

BOOST_CHECK(projectv2.WellFormed(GRC::ContractAction::REMOVE) == true);
}
Expand Down

0 comments on commit 12a328b

Please sign in to comment.