Refactored user I/O with signed inputs and NUPs. (#53)

This commit is contained in:
Ravin Perera
2019-11-03 22:45:23 +05:30
committed by GitHub
parent cb364cc420
commit 83189556de
27 changed files with 958 additions and 596 deletions

View File

@@ -139,6 +139,21 @@ int validate_and_extract_content(const Content **content_ref, const uint8_t *con
return 0;
}
/**
* Creates a non-unl proposal stuct from the given non-unl proposal message.
* @param The Flatbuffer non-unl poporal received from the peer.
* @return A non-unl proposal struct representing the message.
*/
const p2p::nonunl_proposal create_nonunl_proposal_from_msg(const NonUnl_Proposal_Message &msg, uint64_t timestamp)
{
p2p::nonunl_proposal nup;
if (msg.usermessages())
nup.user_messages = flatbuf_usermsgsmap_to_usermsgsmap(msg.usermessages());
return nup;
}
/**
* Creates a proposal stuct from the given proposal message.
* @param The Flatbuffer poporal received from the peer.
@@ -159,15 +174,9 @@ const p2p::proposal create_proposal_from_msg(const Proposal_Message &msg, const
if (msg.users())
p.users = flatbuf_bytearrayvector_to_stringlist(msg.users());
if (msg.raw_inputs())
p.raw_inputs = flatbuf_rawinputs_to_hashbuffermap(msg.raw_inputs());
if (msg.hash_inputs())
p.hash_inputs = flatbuf_bytearrayvector_to_stringlist(msg.hash_inputs());
if (msg.raw_outputs())
p.raw_outputs = flatbuf_rawoutputs_to_hashbuffermap(msg.raw_outputs());
if (msg.hash_outputs())
p.hash_outputs = flatbuf_bytearrayvector_to_stringlist(msg.hash_outputs());
@@ -176,6 +185,23 @@ const p2p::proposal create_proposal_from_msg(const Proposal_Message &msg, const
//---Message creation helpers---//
void create_msg_from_nonunl_proposal(flatbuffers::FlatBufferBuilder &container_builder, const p2p::nonunl_proposal &nup)
{
flatbuffers::FlatBufferBuilder builder(1024);
flatbuffers::Offset<NonUnl_Proposal_Message> nupmsg =
CreateNonUnl_Proposal_Message(
builder,
usermsgsmap_to_flatbuf_usermsgsmap(builder, nup.user_messages));
flatbuffers::Offset<Content> message = CreateContent(builder, Message_NonUnl_Proposal_Message, nupmsg.Union());
builder.Finish(message); // Finished building message content to get serialised content.
// Now that we have built the content message,
// we need to sign it and place it inside a container message.
create_containermsg_from_content(container_builder, builder, false);
}
/**
* Ctreat proposal peer message from the given proposal struct.
* @param container_builder Flatbuffer builder for the container message.
@@ -186,7 +212,6 @@ void create_msg_from_proposal(flatbuffers::FlatBufferBuilder &container_builder,
// todo:get a average propsal message size and allocate content builder based on that.
flatbuffers::FlatBufferBuilder builder(1024);
// Create dummy propsal message
flatbuffers::Offset<Proposal_Message> proposal =
CreateProposal_Message(
builder,
@@ -194,9 +219,7 @@ void create_msg_from_proposal(flatbuffers::FlatBufferBuilder &container_builder,
p.time,
sv_to_flatbuff_bytes(builder, p.lcl),
stringlist_to_flatbuf_bytearrayvector(builder, p.users),
hashbuffermap_to_flatbuf_rawinputs(builder, p.raw_inputs),
stringlist_to_flatbuf_bytearrayvector(builder, p.hash_inputs),
hashbuffermap_to_flatbuf_rawoutputs(builder, p.raw_outputs),
stringlist_to_flatbuf_bytearrayvector(builder, p.hash_outputs));
flatbuffers::Offset<Content> message = CreateContent(builder, Message_Proposal_Message, proposal.Union());
@@ -249,48 +272,24 @@ void create_containermsg_from_content(
//---Conversion helpers from flatbuffers data types to std data types---//
/**
* Returns a hash buffer map from Flatbuffer proposal raw inputs.
*/
const std::unordered_map<std::string, const std::vector<util::hash_buffer>>
flatbuf_rawinputs_to_hashbuffermap(const flatbuffers::Vector<flatbuffers::Offset<RawInputList>> *fbvec)
const std::unordered_map<std::string, const std::list<usr::user_submitted_message>>
flatbuf_usermsgsmap_to_usermsgsmap(const flatbuffers::Vector<flatbuffers::Offset<UserSubmittedMessageGroup>> *fbvec)
{
std::unordered_map<std::string, const std::vector<util::hash_buffer>> map;
std::unordered_map<std::string, const std::list<usr::user_submitted_message>> map;
map.reserve(fbvec->size());
for (const RawInputList *user : *fbvec)
for (const UserSubmittedMessageGroup *group : *fbvec)
{
std::vector<util::hash_buffer> bufvec;
bufvec.reserve(user->inputs()->size());
std::list<usr::user_submitted_message> msglist;
for (auto input : *user->inputs())
for (auto msg : *group->messages())
{
// Create hash_buffer object and manually assign the hash from the input.
util::hash_buffer buf(flatbuff_bytes_to_sv(input->value())); //input->value() is the raw input.
buf.hash = flatbuff_bytes_to_sv(input->key()); //input->key() is the hash of the input.
bufvec.push_back(buf);
msglist.push_back(usr::user_submitted_message(
flatbuff_bytes_to_sv(msg->content()),
flatbuff_bytes_to_sv(msg->signature())
));
}
map.emplace(flatbuff_bytes_to_sv(user->pubkey()), std::move(bufvec));
}
return map;
}
/**
* Returns a hash buffer map from Flatbuffer proposal raw outputs.
*/
const std::unordered_map<std::string, util::hash_buffer>
flatbuf_rawoutputs_to_hashbuffermap(const flatbuffers::Vector<flatbuffers::Offset<RawOutput>> *fbvec)
{
std::unordered_map<std::string, util::hash_buffer> map;
map.reserve(fbvec->size());
for (const RawOutput *user : *fbvec)
{
// Create hash_buffer object and manually assign the hash from the output.
util::hash_buffer buf(flatbuff_bytes_to_sv(user->output()->value())); //output->value() is the raw output.
buf.hash = flatbuff_bytes_to_sv(user->output()->key()); //output->key() is the hash of the output.
map.emplace(flatbuff_bytes_to_sv(user->pubkey()), std::move(buf));
map.emplace(flatbuff_bytes_to_sv(group->pubkey()), std::move(msglist));
}
return map;
}
@@ -298,50 +297,26 @@ flatbuf_rawoutputs_to_hashbuffermap(const flatbuffers::Vector<flatbuffers::Offse
//---Conversion helpers from std data types to flatbuffers data types---//
//---These are used in constructing Flatbuffer messages using builders---//
/**
* Returns Flatbuffer vector of RawInputs from a given map of hash buffer lists.
*/
const flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<RawInputList>>>
hashbuffermap_to_flatbuf_rawinputs(flatbuffers::FlatBufferBuilder &builder, const std::unordered_map<std::string, const std::vector<util::hash_buffer>> &map)
const flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<UserSubmittedMessageGroup>>>
usermsgsmap_to_flatbuf_usermsgsmap(flatbuffers::FlatBufferBuilder &builder, const std::unordered_map<std::string, const std::list<usr::user_submitted_message>> &map)
{
std::vector<flatbuffers::Offset<RawInputList>> fbvec;
std::vector<flatbuffers::Offset<UserSubmittedMessageGroup>> fbvec;
fbvec.reserve(map.size());
for (auto const &[pubkey, bufvec] : map)
for (auto const &[pubkey, msglist] : map)
{
std::vector<flatbuffers::Offset<BytesKeyValuePair>> fbinputsvec;
for (const util::hash_buffer &buf : bufvec)
std::vector<flatbuffers::Offset<UserSubmittedMessage>> fbmsgsvec;
for (const usr::user_submitted_message &msg : msglist)
{
fbinputsvec.push_back(CreateBytesKeyValuePair(
fbmsgsvec.push_back(CreateUserSubmittedMessage(
builder,
sv_to_flatbuff_bytes(builder, buf.hash),
sv_to_flatbuff_bytes(builder, buf.buffer)));
sv_to_flatbuff_bytes(builder, msg.content),
sv_to_flatbuff_bytes(builder, msg.sig)));
}
fbvec.push_back(CreateRawInputList(
fbvec.push_back(CreateUserSubmittedMessageGroup(
builder,
sv_to_flatbuff_bytes(builder, pubkey),
builder.CreateVector(fbinputsvec)));
}
return builder.CreateVector(fbvec);
}
/**
* Returns Flatbuffer vector of RawOutputs from a given map of hash buffers.
*/
const flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<RawOutput>>>
hashbuffermap_to_flatbuf_rawoutputs(flatbuffers::FlatBufferBuilder &builder, const std::unordered_map<std::string, util::hash_buffer> &map)
{
std::vector<flatbuffers::Offset<RawOutput>> fbvec;
fbvec.reserve(map.size());
for (auto const &[pubkey, buf] : map)
{
fbvec.push_back(CreateRawOutput(
builder,
sv_to_flatbuff_bytes(builder, pubkey),
CreateBytesKeyValuePair(
builder,
sv_to_flatbuff_bytes(builder, buf.hash),
sv_to_flatbuff_bytes(builder, buf.buffer))));
builder.CreateVector(fbmsgsvec)));
}
return builder.CreateVector(fbvec);
}