Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth sample disqualifications (type 2) #277

Closed
wants to merge 17 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Binary serialization support
AlexanderSuprunenko committed May 28, 2019
commit f1040e0e10b6e1f60f377519587a3482badf9d10
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -430,6 +430,7 @@ if (OPT_BUILD_TESTS)
${PROJECT_SOURCE_DIR}/test/rta_classes_test.cpp
${PROJECT_SOURCE_DIR}/test/sys_info.cpp
${PROJECT_SOURCE_DIR}/test/strand_test.cpp
${PROJECT_SOURCE_DIR}/test/serialize_test.cpp
${PROJECT_SOURCE_DIR}/test/main.cpp
)

261 changes: 261 additions & 0 deletions include/lib/graft/binary_serialize.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
// Copyright (c) 2018, The Graft Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#pragma once

#include <boost/hana.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>

#include "lib/graft/inout.h"

namespace graft { namespace serializer {

namespace binary_details
{

template<typename ...>
using to_void = void; // maps everything to void, used in non-evaluated contexts

template<typename C, typename = void>
struct is_container : std::false_type
{};

template<typename C>
struct is_container<C,
to_void<decltype(std::declval<C>().begin()),
decltype(std::declval<C>().end()),
typename C::value_type
>> : std::true_type // will be enabled for iterable objects
{};

static_assert( is_container<std::string>::value );
static_assert( is_container<std::vector<int>>::value );
///////////////////////
/*! \brief write_varint adopted from cryptonode.
*/
template<typename Arch, typename V>
// Requires T to be both an integral type and unsigned, should be a compile error if it is not
static void write_varint(Arch& ar, V i) {
// Make sure that there is one after this
while (i >= 0x80) {
char ch = (static_cast<char>(i) & 0x7f) | 0x80;
// ++dest;
ar << ch;
i >>= 7; // I should be in multiples of 7, this should just get the next part
}
// writes the last one to dest
/*
*dest = static_cast<char>(i);
dest++; // Seems kinda pointless...
*/
ar << static_cast<char>(i);
}


/*! \brief read_varint adopted from cryptonode.
*/
template<typename Arch, typename V>
static bool read_varint(Arch& ar, V& write)
{
constexpr int bits = std::numeric_limits<V>::digits;
int read = 0;
write = 0;
for (int shift = 0;; shift += 7) {
/*
if (first == last)
{
return read;
}
*/
/*
unsigned char byte = *first;
++first;
*/
unsigned char byte;
ar >> byte;

++read;

if (shift + 7 >= bits && byte >= 1 << (bits - shift)) {
return -1; //EVARINT_OVERFLOW;
}
if (byte == 0 && shift != 0) {
return -2; //EVARINT_REPRESENT;
}

write |= static_cast<V>(byte & 0x7f) << shift; /* Does the actualy placing into write, stripping the first bit */

/* If there is no next */
if ((byte & 0x80) == 0) {
break;
}
}
return read;
}

//////////////
// forward declarations
template<typename Arch, typename V>
static void bserialize(Arch& ar, V& v);

template<typename Arch, typename V>
static void bdeserialize(Arch& ar, V& v);
//////////////
template<typename Arch, typename V>
static void ser(Arch& ar, V& v)
{
static_assert(!(std::is_trivially_copyable<int>::value && std::is_class<int>::value));
if constexpr(is_container<V>::value)
{
size_t size = v.size();
write_varint(ar, size);
std::for_each(v.begin(), v.end(), [&](auto& item)
{
using naked_member_t = std::remove_cv_t<std::remove_reference_t<decltype(item)>>;
if constexpr(!std::is_base_of<ReflectiveRapidJSON::JsonSerializable<naked_member_t>, naked_member_t>::value)
{
ser(ar, item);
}
else
{
bserialize(ar, item);
}
});
}
else if constexpr(std::is_trivially_copyable<V>::value && std::is_class<V>::value)
{
const uint8_t* p = reinterpret_cast<const uint8_t*>(&v);
for(int i=0; i<sizeof(V); ++i, ++p)
{
ar << *p;
}
}
else
{
ar << v;
}
}

template<typename Arch, typename V>
static void deser(Arch& ar, V& v)
{
if constexpr(is_container<V>::value)
{
size_t size;
read_varint(ar, size);
v.resize(size);
std::for_each(v.begin(), v.end(), [&](auto& item)
{
using naked_member_t = std::remove_cv_t<std::remove_reference_t<decltype(item)>>;
if constexpr(!std::is_base_of<ReflectiveRapidJSON::JsonSerializable<naked_member_t>, naked_member_t>::value)
{
deser(ar, item);
}
else
{
bdeserialize(ar, item);
}
});
}
else if constexpr(std::is_trivially_copyable<V>::value && std::is_class<V>::value)
{
uint8_t* p = reinterpret_cast<uint8_t*>(&v);
for(int i=0; i<sizeof(V); ++i, ++p)
{
ar >> *p;
}
}
else
{
ar >> v;
}
}

template<typename Arch, typename V>
static void bserialize(Arch& ar, V& v)
{
boost::hana::for_each(boost::hana::keys(v), [&](auto key)
{
const auto& member = boost::hana::at_key(v, key);
using naked_member_t = std::remove_cv_t<std::remove_reference_t<decltype(member)>>;
if constexpr(!std::is_base_of<ReflectiveRapidJSON::JsonSerializable<naked_member_t>, naked_member_t>::value)
{
ser(ar, member);
}
else
{
bserialize(ar, member);
}
});
}

template<typename Arch, typename V>
static void bdeserialize(Arch& ar, V& v)
{
boost::hana::for_each(boost::hana::keys(v), [&](auto key)
{
auto& member = boost::hana::at_key(v, key);
using naked_member_t = std::remove_cv_t<std::remove_reference_t<decltype(member)>>;
if constexpr(!std::is_base_of<ReflectiveRapidJSON::JsonSerializable<naked_member_t>, naked_member_t>::value)
{
deser(ar, member);
}
else
{
bdeserialize(ar, member);
}
});
}

} //namespace binary_details

//It is expected that serialize and deserialize throw exception on error
template<typename T>
class Binary
{
public:
static std::string serialize(const T& t)
{
std::ostringstream oss;
boost::archive::binary_oarchive ar(oss, boost::archive::no_header);
binary_details::bserialize(ar, t);
return oss.str();
}

static void deserialize(const std::string& s, T& t)
{
std::istringstream iss(s);
boost::archive::binary_iarchive ar(iss, boost::archive::no_header);
binary_details::bdeserialize(ar,t);
}
};

} } //namespace graft { namespace serializer


70 changes: 0 additions & 70 deletions test/graft_server_test.cpp
Original file line number Diff line number Diff line change
@@ -81,76 +81,6 @@ TEST(InOut, common)
EXPECT_EQ(s_out, s);
}

namespace graft { namespace serializer {

template<typename T>
struct Nothing
{
static std::string serialize(const T& t)
{
return "";
}
static void deserialize(const std::string& s, T& t)
{
}
};

} }

TEST(InOut, serialization)
{
using namespace graft;

GRAFT_DEFINE_IO_STRUCT(J,
(int,x),
(int,y)
);

J j;

Input input;
input.body = "{\"x\":1,\"y\":2}";
j.x = 5; j.y = 6;
j = input.get<J, serializer::JSON<J>>();
EXPECT_EQ(j.x, 1); EXPECT_EQ(j.y, 2);
j = input.get<J>();
EXPECT_EQ(j.x, 1); EXPECT_EQ(j.y, 2);
j.x = 5; j.y = 6;
j = input.getT<serializer::JSON, J>();
EXPECT_EQ(j.x, 1); EXPECT_EQ(j.y, 2);

Output output;
output.load<J, serializer::JSON<J>>(j);
output.load<J>(j);
output.load<>(j);
EXPECT_EQ(input.body, output.body);
output.body.clear();
output.load(j);
EXPECT_EQ(input.body, output.body);
output.body.clear();
output.loadT<serializer::JSON, J>(j);
output.loadT<serializer::JSON>(j);
output.loadT<>(j);
EXPECT_EQ(input.body, output.body);
output.body.clear();
output.loadT(j);
EXPECT_EQ(input.body, output.body);

struct A
{
int x;
int y;
};

A a;

a = input.get<A, serializer::Nothing<A>>();
a = input.getT<serializer::Nothing, A>();
output.load<A, serializer::Nothing<A>>(a);
output.loadT<serializer::Nothing, A>(a);
output.loadT<serializer::Nothing>(a);
}

TEST(InOut, makeUri)
{
{
405 changes: 405 additions & 0 deletions test/serialize_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,405 @@
// Copyright (c) 2018, The Graft Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <gtest/gtest.h>

#include "lib/graft/binary_serialize.h"

#include "include_base_utils.h"
using namespace epee;

#include "cryptonote_basic/cryptonote_format_utils.h"
#include "serialization/binary_utils.h"

#include "lib/graft/jsonrpc.h"
#include <cstring>

namespace
{
GRAFT_DEFINE_IO_STRUCT(Payment,
(uint64, amount),
(uint32, block_height),
(std::string, payment_id),
(std::string, tx_hash),
(uint32, unlock_time)
);

GRAFT_DEFINE_IO_STRUCT(NestedPayment,
(std::string, s),
(Payment, p),
(uint32, i)
);

GRAFT_DEFINE_IO_STRUCT(WithVector,
(std::vector<std::string>, vs),
(std::vector<Payment>, vp)
);

//TODO: does not work
GRAFT_DEFINE_IO_STRUCT(WithVectorInt,
(std::vector<uint32>, v)
);

struct PaymentX
{
uint64 amount;
uint32 block_height;
std::string payment_id;
std::string tx_hash;
uint32 unlock_time;
BEGIN_SERIALIZE()
FIELD(amount)
FIELD(block_height)
FIELD(payment_id)
FIELD(tx_hash)
FIELD(unlock_time)
END_SERIALIZE()
};

struct NestedPaymentX
{
std::string s;
PaymentX p;
uint32 i;
BEGIN_SERIALIZE()
FIELD(s)
FIELD(p)
FIELD(i)
END_SERIALIZE()
};

struct WithVectorX
{
std::vector<std::string> vs;
std::vector<PaymentX> vp;
BEGIN_SERIALIZE()
FIELD(vs)
FIELD(vp)
END_SERIALIZE()
};

//TODO: does not work
struct WithVectorIntX
{
std::vector<uint32> v;
BEGIN_SERIALIZE()
FIELD(v)
END_SERIALIZE()
};

} //namespace

namespace graft { namespace serializer {

template<typename T>
struct Nothing
{
static std::string serialize(const T& t)
{
return "";
}
static void deserialize(const std::string& s, T& t)
{
}
};

} } //namespace graft { namespace serializer

TEST(InOut, serialization)
{
using namespace graft;
namespace serial = graft::serializer;

GRAFT_DEFINE_IO_STRUCT(J,
(int,x),
(int,y)
);

J j;

Input input;
input.body = "{\"x\":1,\"y\":2}";
j.x = 5; j.y = 6;
j = input.get<J, serial::JSON<J>>();
j = input.get<J>();
EXPECT_EQ(j.x, 1); EXPECT_EQ(j.y, 2);
j = input.get<J>();
EXPECT_EQ(j.x, 1); EXPECT_EQ(j.y, 2);
j.x = 5; j.y = 6;
j = input.getT<serial::JSON, J>();
// j = input.getT<J>();
EXPECT_EQ(j.x, 1); EXPECT_EQ(j.y, 2);

Output output;
output.load<J, serial::JSON<J>>(j);
output.load<J>(j);
output.load<>(j);
EXPECT_EQ(input.body, output.body);
output.body.clear();
output.load(j);
EXPECT_EQ(input.body, output.body);

output.body.clear();
output.loadT<serial::JSON, J>(j);
output.loadT<serial::JSON>(j);
output.loadT<>(j);
EXPECT_EQ(input.body, output.body);
output.body.clear();
output.loadT(j);
EXPECT_EQ(input.body, output.body);

struct A
{
int x;
int y;
};

A a;

a = input.get<A, serial::Nothing<A>>();
a = input.getT<serial::Nothing, A>();
output.load<A, serial::Nothing<A>>(a);
output.loadT<serial::Nothing, A>(a);
output.loadT<serial::Nothing>(a);

{
output.load<J, serial::Binary<J>>(j);
std::string s = output.body;
input.body = s;
J j1;
j1 = input.getT<serial::Binary, J>();
EXPECT_EQ(j.x, j1.x);
EXPECT_EQ(j.y, j1.y);
}

Payment p{ {}, 1, 2, "abc", "defg", 5 };
output.load<Payment, serial::Binary<Payment>>(p);
PaymentX px{ 1, 2, "abc", "defg", 5 };
std::string blob;
::serialization::dump_binary(px, blob);
EXPECT_EQ(output.body, blob);

Payment p1;
input.body = output.body;
p1 = input.get<Payment, serial::Binary<Payment>>();
EXPECT_EQ(p.amount, p1.amount);
EXPECT_EQ(p.block_height, p1.block_height);
EXPECT_EQ(p.payment_id, p1.payment_id);
EXPECT_EQ(p.tx_hash, p1.tx_hash);
EXPECT_EQ(p.unlock_time, p1.unlock_time);

{
NestedPayment np{ {}, "something", p, 123 };
output.load<NestedPayment, serial::Binary<NestedPayment>>(np);
NestedPaymentX npx{ "something", px, 123 };
::serialization::dump_binary(npx, blob);
EXPECT_EQ(output.body, blob);

NestedPayment np1;
input.body = output.body;
np1 = input.get<NestedPayment, graft::serializer::Binary<NestedPayment>>();
EXPECT_EQ(np.s, np1.s);
EXPECT_EQ(np.p.amount, np1.p.amount);
EXPECT_EQ(np.p.block_height, np1.p.block_height);
EXPECT_EQ(np.p.payment_id, np1.p.payment_id);
EXPECT_EQ(np.p.tx_hash, np1.p.tx_hash);
EXPECT_EQ(np.p.unlock_time, np1.p.unlock_time);
EXPECT_EQ(np.i, np1.i);
}
{
WithVector wv{ {}, {"1","2","3","4","5"}, {p, p, p} };
output.load<WithVector, graft::serializer::Binary<WithVector>>(wv);
WithVectorX wvx{ {"1","2","3","4","5"}, {px, px, px} };
::serialization::dump_binary(wvx, blob);
EXPECT_EQ(output.body, blob);

input.body = output.body;
WithVector wv1 = input.get<WithVector, graft::serializer::Binary<WithVector>>();
EXPECT_EQ(wv.vs, wv1.vs);
EXPECT_EQ(wv.vp.size(), wv1.vp.size());
for(size_t i = 0; i<wv.vp.size(); ++i)
{
auto& p = wv.vp[i];
auto& p1 = wv1.vp[i];
EXPECT_EQ(p.amount, p1.amount);
EXPECT_EQ(p.block_height, p1.block_height);
EXPECT_EQ(p.payment_id, p1.payment_id);
EXPECT_EQ(p.tx_hash, p1.tx_hash);
EXPECT_EQ(p.unlock_time, p1.unlock_time);
}
}
//does not work yet
#if 0
{
WithVectorInt wv{ {}, {1,2,3,4,5} };
output.load<WithVectorInt, graft::serializer::Binary<WithVectorInt>>(wv);
WithVectorIntX wvx{ {1,2,3,4,5} };
::serialization::dump_binary(wvx, blob);
EXPECT_EQ(output.body, blob);

input.body = output.body;
WithVectorInt wv1 = input.get<WithVectorInt, graft::serializer::Binary<WithVectorInt>>();
EXPECT_EQ(wv.v, wv1.v);
}
#endif
}

namespace
{

GRAFT_DEFINE_IO_STRUCT(DisqualificationItem,
(uint64_t, block_height),
(crypto::hash, block_hash),
(crypto::public_key, id)
);

GRAFT_DEFINE_IO_STRUCT(SignerItem,
(crypto::public_key, signer_id),
(crypto::signature, sign)
);

GRAFT_DEFINE_IO_STRUCT(Disqualification,
(DisqualificationItem, item),
(std::vector<SignerItem>, signers)
);

struct tx_extra_graft_disqualification
{
struct disqualification_item
{
uint64_t block_height;
crypto::hash block_hash;
crypto::public_key id;
BEGIN_SERIALIZE()
FIELD(block_height)
FIELD(block_hash)
FIELD(id)
END_SERIALIZE()
};

struct signer_item
{
crypto::public_key signer_id;
crypto::signature sign;
BEGIN_SERIALIZE()
FIELD(signer_id)
FIELD(sign)
END_SERIALIZE()
};

disqualification_item item;
std::vector<signer_item> signers;
BEGIN_SERIALIZE()
FIELD(item)
FIELD(signers)
END_SERIALIZE()
};


template<typename T>
static void bin_serialize(const T& t, std::string& str)
{
graft::Output out;
out.loadT<graft::serializer::Binary>(t);
str = out.body;
}

template<typename T>
static std::string bin_deserialize(const std::string& str, T& t)
{
graft::Input in;
in.body = str;
try
{
in.getT<graft::serializer::Binary>(t);
}
catch(std::exception& ex)
{
return ex.what();
}
return "";
}

} //namespace

TEST(InOut, serialization1)
{
using namespace graft;
namespace serial = graft::serializer;
#define ZPOD(X) std::memset(&X, 0, sizeof(X));
crypto::public_key pub;
crypto::secret_key sec;
crypto::generate_keys(pub, sec);

Disqualification d;
d.item.block_height = 1;
ZPOD(d.item.block_hash)
d.item.block_hash.data[0] = 2;
ZPOD(d.item.id);
d.item.id.data[0] = 3;
{
SignerItem si;
ZPOD(si);
si.signer_id.data[0] = 4;
*(char*)&si.sign = 5;
d.signers.push_back(std::move(si));
}
std::string d_str;
bin_serialize(d, d_str);

Disqualification d1;
bin_deserialize(d_str, d1);
EXPECT_EQ(d.item.block_height, d1.item.block_height);
EXPECT_EQ(d.item.block_hash, d1.item.block_hash);
EXPECT_EQ(d.item.id, d1.item.id);
EXPECT_EQ(d.signers.size(), d1.signers.size());
EXPECT_EQ(d.signers.size(), 1);
EXPECT_EQ(d.signers[0].signer_id, d1.signers[0].signer_id);
EXPECT_EQ(d.signers[0].sign, d1.signers[0].sign);

tx_extra_graft_disqualification dx;
dx.item.block_height = 1;
ZPOD(dx.item.block_hash)
dx.item.block_hash.data[0] = 2;
ZPOD(dx.item.id);
dx.item.id.data[0] = 3;
{
tx_extra_graft_disqualification::signer_item si;
ZPOD(si);
si.signer_id.data[0] = 4;
*(char*)&si.sign = 5;

std::string si_str;
::serialization::dump_binary(si, si_str);
dx.signers.push_back(std::move(si));
}
std::string dx_str;
::serialization::dump_binary(dx, dx_str);

EXPECT_EQ(d_str, dx_str);
}