Skip to content

Commit

Permalink
Playback via ledger entries (#721)
Browse files Browse the repository at this point in the history
  • Loading branch information
olgavrou authored Feb 3, 2020
1 parent 3836632 commit 15d42ac
Show file tree
Hide file tree
Showing 37 changed files with 1,321 additions and 484 deletions.
6 changes: 6 additions & 0 deletions cmake/pbft.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
add_definitions(-DSIGN_BATCH)
set(SIGN_BATCH ON)

if(SAN)
add_definitions(-DUSE_STD_MALLOC)
endif()

set(PBFT_SRC
${CMAKE_SOURCE_DIR}/src/consensus/pbft/libbyz/globalstate.cpp
${CMAKE_SOURCE_DIR}/src/consensus/pbft/libbyz/Client.cpp
Expand Down Expand Up @@ -47,6 +51,7 @@ set(PBFT_SRC
${CMAKE_SOURCE_DIR}/src/consensus/pbft/libbyz/request_id_gen.cpp
${CMAKE_SOURCE_DIR}/src/consensus/pbft/libbyz/New_principal.cpp
${CMAKE_SOURCE_DIR}/src/consensus/pbft/libbyz/Network_open.cpp
${CMAKE_SOURCE_DIR}/src/consensus/pbft/libbyz/Append_entries.cpp
)

if("sgx" IN_LIST TARGET)
Expand Down Expand Up @@ -114,6 +119,7 @@ if("virtual" IN_LIST TARGET)
${CMAKE_SOURCE_DIR}/src/consensus/pbft/libbyz/test ${EVERCRYPT_INC}
)

add_dependencies(libcommontest.mock flatbuffers)
target_compile_options(libcommontest.mock PRIVATE -stdlib=libc++)

function(use_libbyz name)
Expand Down
20 changes: 20 additions & 0 deletions src/consensus/consensustypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,24 @@ namespace ccf
{
using ObjectId = uint64_t;
using NodeId = ObjectId;
using Index = int64_t;
using Node2NodeMsg = uint64_t;
}

namespace consensus
{
#pragma pack(push, 1)
template <typename T = ccf::Node2NodeMsg>
struct ConsensusHeader
{
T msg;
ccf::NodeId from_node;
};

struct AppendEntriesIndex
{
ccf::Index idx;
ccf::Index prev_idx;
};
#pragma pack(pop)
}
11 changes: 11 additions & 0 deletions src/consensus/ledgerenclave.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ namespace consensus
serialized::skip(data, size, entry_len);
}

std::pair<std::vector<uint8_t>, bool> get_entry(
const uint8_t*& data, size_t& size)
{
auto entry_len = serialized::read<uint32_t>(data, size);
std::vector<uint8_t> entry(data, data + entry_len);

serialized::skip(data, size, entry_len);

return std::make_pair(std::move(entry), true);
}

/**
* Truncate the ledger at a given index.
*
Expand Down
36 changes: 36 additions & 0 deletions src/consensus/pbft/libbyz/Append_entries.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) Microsoft Corporation.
// Copyright (c) 1999 Miguel Castro, Barbara Liskov.
// Copyright (c) 2000, 2001 Miguel Castro, Rodrigo Rodrigues, Barbara Liskov.
// Licensed under the MIT license.
#include "Append_entries.h"

#include "Message_tags.h"
#include "ds/logger.h"
#include "pbft_assert.h"

Append_entries::Append_entries() :
Message(Append_entries_tag, sizeof(Append_entries_rep))
{}

bool Append_entries::verify()
{
return true;
}

bool Append_entries::convert(Message* m1, Append_entries*& m2)
{
if (!m1->has_tag(Append_entries_tag, sizeof(Append_entries_rep)))
{
return false;
}

m1->trim();
m2 = (Append_entries*)m1;
return true;
}

Append_entries_rep& Append_entries::rep() const
{
PBFT_ASSERT(ALIGNED(msg), "Improperly aligned pointer");
return *((Append_entries_rep*)msg);
}
30 changes: 30 additions & 0 deletions src/consensus/pbft/libbyz/Append_entries.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) Microsoft Corporation.
// Copyright (c) 1999 Miguel Castro, Barbara Liskov.
// Copyright (c) 2000, 2001 Miguel Castro, Rodrigo Rodrigues, Barbara Liskov.
// Licensed under the MIT license.
#pragma once

#include "Message.h"
#include "nodeinfo.h"

//
// Append entries messages have the following format:
//
#pragma pack(push)
#pragma pack(1)
struct Append_entries_rep : public Message_rep
{};
#pragma pack(pop)

class Append_entries : public Message
{
public:
Append_entries();

bool verify();

static bool convert(Message* m1, Append_entries*& m2);

private:
Append_entries_rep& rep() const;
};
10 changes: 10 additions & 0 deletions src/consensus/pbft/libbyz/Certificate.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,16 @@ void Certificate<T>::reset_f()
template <class T>
bool Certificate<T>::add(T* m)
{
auto principal = pbft::GlobalState::get_node().get_principal(m->id());
if (!principal)
{
LOG_INFO_FMT(
"Principal with id {} has not been configured yet, rejecting the message",
m->id());
delete m;
return false;
}

if (bmap.none() && f != pbft::GlobalState::get_node().f())
{
reset_f();
Expand Down
7 changes: 6 additions & 1 deletion src/consensus/pbft/libbyz/LedgerWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "Request.h"

LedgerWriter::LedgerWriter(
pbft::Store& store_, pbft::PrePreparesMap& pbft_pre_prepares_map_) :
pbft::PbftStore& store_, pbft::PrePreparesMap& pbft_pre_prepares_map_) :
store(store_),
pbft_pre_prepares_map(pbft_pre_prepares_map_)
{}
Expand Down Expand Up @@ -34,6 +34,11 @@ void LedgerWriter::write_prepare(
}
}

void LedgerWriter::write_pre_prepare(ccf::Store::Tx& tx)
{
store.commit_tx(tx);
}

void LedgerWriter::write_pre_prepare(Pre_prepare* pp)
{
store.commit_pre_prepare(
Expand Down
5 changes: 3 additions & 2 deletions src/consensus/pbft/libbyz/LedgerWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
class LedgerWriter
{
private:
pbft::Store& store;
pbft::PbftStore& store;
pbft::PrePreparesMap& pbft_pre_prepares_map;

public:
LedgerWriter(
pbft::Store& store_, pbft::PrePreparesMap& pbft_pre_prepares_map_);
pbft::PbftStore& store_, pbft::PrePreparesMap& pbft_pre_prepares_map_);
virtual ~LedgerWriter() = default;
void write_prepare(const Prepared_cert& prepared_cert, Seqno seqno);
void write_pre_prepare(Pre_prepare* pp);
void write_pre_prepare(ccf::Store::Tx& tx);
void write_view_change(View_change* vc);
};
3 changes: 2 additions & 1 deletion src/consensus/pbft/libbyz/Message_tags.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const short Fetch_tag = 15;
const short Query_stable_tag = 16;
const short Reply_stable_tag = 17;
const short Network_open_tag = 18;
const short Max_message_tag = 19;
const short Append_entries_tag = 19;
const short Max_message_tag = 20;

// Message used for testing are in the 100+ range
const short New_principal_tag = 100;
5 changes: 5 additions & 0 deletions src/consensus/pbft/libbyz/Pre_prepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@ bool Pre_prepare::pre_verify()
{
auto sender_principal =
pbft::GlobalState::get_node().get_principal(sender);
if (!sender_principal)
{
LOG_INFO_FMT("Sender principal has not been configured yet {}", sender);
return false;
}

if (
!sender_principal->has_certificate_set() &&
Expand Down
14 changes: 12 additions & 2 deletions src/consensus/pbft/libbyz/Prepared_cert.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,18 @@ class Prepared_cert

inline bool Prepared_cert::add(Prepare* m)
{
#ifdef SIGN_BATCH
int id = m->id();
auto principal = pbft::GlobalState::get_node().get_principal(id);
if (!principal)
{
LOG_INFO_FMT(
"Principal with id {} has not been configured yet, rejecting prepare",
id);
delete m;
return false;
}

#ifdef SIGN_BATCH
PbftSignature& digest_sig = m->digest_sig();
PrePrepareProof proof;
std::copy(
Expand All @@ -153,7 +163,7 @@ inline bool Prepared_cert::add(Prepare* m)
#ifdef SIGN_BATCH
if (result)
{
proof.cert = pbft::GlobalState::get_node().get_principal(id)->get_cert();
proof.cert = principal->get_cert();
pre_prepare_proof.insert({id, proof});
}
#endif
Expand Down
Loading

0 comments on commit 15d42ac

Please sign in to comment.