Skip to content

Commit d0e07d8

Browse files
authored
Performance (#946)
1 parent 5d94163 commit d0e07d8

27 files changed

+443
-227
lines changed

src/consensus/pbft/libbyz/Node.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ void Node::send(Message* m, int i)
190190

191191
void Node::send(Message* m, Principal* p)
192192
{
193-
PBFT_ASSERT(m->size() <= Max_message_size, "Message is too big");
194193
PBFT_ASSERT(m->tag() < Max_message_tag, "Invalid message tag");
195194
PBFT_ASSERT(p != nullptr, "Must send to a principal");
196195

src/consensus/pbft/libbyz/Prepare.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Prepare::Prepare(View v, Seqno s, Digest& d, Principal* dst, bool is_signed) :
3434
{
3535
struct signature
3636
{
37-
uint32_t magic = 0xba55ba11;
37+
uint32_t magic = 0xba5eba11;
3838
NodeId id;
3939
Digest d;
4040

src/consensus/pbft/libbyz/Rep_info.cpp

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,14 @@ char* Rep_info::new_reply(
4242
r->set_size(message_size);
4343
r->trim();
4444
char* ret = r->contents() + sizeof(Reply_rep);
45-
std::lock_guard<SpinLock> mguard(lock);
46-
auto ret_insert = reps.insert({Key{(size_t)pid, rid, n}, std::move(r)});
47-
if (ret_insert.second)
4845
{
49-
return ret;
46+
std::lock_guard<SpinLock> mguard(lock);
47+
auto ret_insert =
48+
reps.insert({Key{static_cast<size_t>(pid), rid, n}, std::move(r)});
49+
if (ret_insert.second)
50+
{
51+
return ret;
52+
}
5053
}
5154

5255
return nullptr;
@@ -59,22 +62,23 @@ int Rep_info::new_reply_size() const
5962

6063
void Rep_info::end_reply(int pid, Request_id rid, Seqno n, int size)
6164
{
62-
std::lock_guard<SpinLock> mguard(lock);
63-
auto it = reps.find({(size_t)pid, rid, n});
64-
if (it != reps.end())
65+
Reply* r;
6566
{
67+
std::lock_guard<SpinLock> mguard(lock);
68+
auto it = reps.find({static_cast<size_t>(pid), rid, n});
69+
if (it == reps.end())
70+
{
71+
LOG_INFO_FMT(
72+
" Attempt to end reply not in this < {}, {}, {} >", pid, rid, n);
73+
return;
74+
}
6675
Reply* r = it->second.get();
6776
Reply_rep& rr = r->rep();
6877
rr.rid = rid;
6978
rr.reply_size = size;
70-
rr.digest = Digest(r->contents() + sizeof(Reply_rep), size);
7179
int old_size = sizeof(Reply_rep) + rr.reply_size;
7280
r->set_size(old_size + MAC_size);
73-
return;
7481
}
75-
76-
LOG_INFO << " Attempt to end reply not in this < " << pid << "," << rid << ","
77-
<< n << ">" << std::endl;
7882
}
7983

8084
Reply* Rep_info::reply(int pid, Request_id rid, Seqno n)
@@ -91,34 +95,40 @@ Reply* Rep_info::reply(int pid, Request_id rid, Seqno n)
9195

9296
void Rep_info::send_reply(int pid, Request_id rid, Seqno n, View v, int id)
9397
{
94-
std::lock_guard<SpinLock> mguard(lock);
95-
auto it = reps.find({(size_t)pid, rid, n});
96-
if (it != reps.end())
98+
std::unique_ptr<Reply> r;
9799
{
98-
Reply* r = it->second.get();
99-
Reply_rep& rr = r->rep();
100+
std::lock_guard<SpinLock> mguard(lock);
101+
auto it = reps.find({(size_t)pid, rid, n});
100102

101-
PBFT_ASSERT(rr.reply_size != -1, "Invalid state");
102-
PBFT_ASSERT(rr.extra == 0 && rr.v == 0 && rr.replica == 0, "Invalid state");
103+
if (it == reps.end())
104+
{
105+
LOG_INFO << " Attempt to send reply not in this < " << pid << "," << rid
106+
<< "," << n << ">" << std::endl;
107+
return;
108+
}
103109

104-
int old_size = sizeof(Reply_rep) + rr.reply_size;
110+
r = std::move(it->second);
111+
reps.erase(it);
112+
}
105113

106-
rr.extra = 1;
107-
rr.v = v;
108-
rr.replica = id;
114+
Reply_rep& rr = r->rep();
109115

110-
r->auth_type = Auth_type::out;
111-
r->auth_len = sizeof(Reply_rep);
112-
r->auth_src_offset = 0;
113-
r->auth_dst_offset = old_size;
116+
PBFT_ASSERT(rr.reply_size != -1, "Invalid state");
117+
PBFT_ASSERT(rr.extra == 0 && rr.v == 0 && rr.replica == 0, "Invalid state");
114118

115-
pbft::GlobalState::get_node().send(r, pid);
116-
reps.erase(it);
117-
return;
118-
}
119+
int old_size = sizeof(Reply_rep) + rr.reply_size;
120+
121+
rr.extra = 1;
122+
rr.v = v;
123+
rr.replica = id;
124+
125+
r->auth_type = Auth_type::out;
126+
r->auth_len = sizeof(Reply_rep);
127+
r->auth_src_offset = 0;
128+
r->auth_dst_offset = old_size;
119129

120-
LOG_INFO << " Attempt to send reply not in this < " << pid << "," << rid
121-
<< "," << n << ">" << std::endl;
130+
pbft::GlobalState::get_node().send(r.get(), pid);
131+
return;
122132
}
123133

124134
void Rep_info::clear()
@@ -133,7 +143,6 @@ void Rep_info::dump_state(std::ostream& os)
133143
for (auto& pair : reps)
134144
{
135145
os << " cid: " << pair.first.cid << " rid: " << pair.first.rid
136-
<< " seqno: " << pair.first.n
137-
<< " digest hash:" << pair.second->digest().hash() << std::endl;
146+
<< " seqno: " << pair.first.n << std::endl;
138147
}
139148
}

src/consensus/pbft/libbyz/Rep_info_exactly_once.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ void Rep_info_exactly_once::end_reply(int pid, Request_id rid, int sz)
103103
Reply_rep& rr = r->rep();
104104
rr.rid = rid;
105105
rr.reply_size = sz;
106-
rr.digest = Digest(r->contents() + sizeof(Reply_rep), sz);
107106

108107
int old_size = sizeof(Reply_rep) + rr.reply_size;
109108
r->set_size(old_size + MAC_size);
@@ -185,7 +184,6 @@ void Rep_info_exactly_once::dump_state(std::ostream& os)
185184
for (int i = 0; i < reps.size(); i++)
186185
{
187186
os << "i: " << i << " rid: " << reps[i]->request_id()
188-
<< " digest hash:" << reps[i]->digest().hash()
189187
<< " tentative:" << ireps[i].tentative << std::endl;
190188
}
191189
}

src/consensus/pbft/libbyz/Rep_info_exactly_once.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@ class Rep_info_exactly_once
4242
// Effects: Returns the timestamp in the last message sent to
4343
// principal "pid".
4444

45-
Digest& digest(int pid);
46-
// Requires: "pid" is a valid principal identifier.
47-
// Effects: Returns a reference to the digest of the last reply
48-
// value sent to pid.
49-
5045
Reply* reply(int pid);
5146
// Requires: "pid" is a valid principal identifier.
5247
// Effects: Returns a pointer to the last reply value sent to "pid"
@@ -138,11 +133,6 @@ inline Request_id Rep_info_exactly_once::req_id(int pid)
138133
return reps[pid]->request_id();
139134
}
140135

141-
inline Digest& Rep_info_exactly_once::digest(int pid)
142-
{
143-
return reps[pid]->digest();
144-
}
145-
146136
inline Reply* Rep_info_exactly_once::reply(int pid)
147137
{
148138
return reps[pid];

src/consensus/pbft/libbyz/Replica.cpp

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,13 @@ void Replica::receive_message(const uint8_t* data, uint32_t size)
367367
bool Replica::compare_execution_results(
368368
const ByzInfo& info, Pre_prepare* pre_prepare)
369369
{
370+
// We are currently not ordering the execution on the backups correctly.
371+
// This will be resolved in the immediate future.
372+
if (enclave::ThreadMessaging::thread_count > 2)
373+
{
374+
return true;
375+
}
376+
370377
auto& r_pp_root = pre_prepare->get_replicated_state_merkle_root();
371378

372379
auto execution_match = true;
@@ -441,11 +448,10 @@ void Replica::playback_request(ccf::Store::Tx& tx)
441448

442449
waiting_for_playback_pp = true;
443450

444-
std::vector<std::unique_ptr<ExecCommandMsg>> cmds;
445-
cmds.emplace_back(execute_tentative_request(
451+
vec_exec_cmds[0] = std::move(execute_tentative_request(
446452
*req, playback_max_local_commit_value, true, &tx, true));
447453

448-
exec_command(cmds, playback_byz_info);
454+
exec_command(vec_exec_cmds, playback_byz_info, 1);
449455
}
450456

451457
void Replica::playback_pre_prepare(ccf::Store::Tx& tx)
@@ -2174,6 +2180,7 @@ std::unique_ptr<ExecCommandMsg> Replica::execute_tentative_request(
21742180
last_tentative_execute,
21752181
max_local_commit_value,
21762182
stash_replier,
2183+
request.user_id(),
21772184
&Replica::execute_tentative_request_end,
21782185
tx);
21792186

@@ -2211,7 +2218,8 @@ void Replica::execute_tentative_request_end(ExecCommandMsg& msg, ByzInfo& info)
22112218
bool Replica::create_execute_commands(
22122219
Pre_prepare* pp,
22132220
int64_t& max_local_commit_value,
2214-
std::vector<std::unique_ptr<ExecCommandMsg>>& cmds)
2221+
std::array<std::unique_ptr<ExecCommandMsg>, Max_requests_in_batch>& cmds,
2222+
uint32_t& num_requests)
22152223
{
22162224
if (
22172225
pp->seqno() == last_tentative_execute + 1 && !state.in_fetch_state() &&
@@ -2224,6 +2232,7 @@ bool Replica::create_execute_commands(
22242232
Pre_prepare::Requests_iter iter(pp);
22252233
Request request;
22262234

2235+
num_requests = 0;
22272236
while (iter.get(request))
22282237
{
22292238
auto cmd = execute_tentative_request(
@@ -2232,7 +2241,8 @@ bool Replica::create_execute_commands(
22322241
!iter.has_more_requests(),
22332242
nullptr,
22342243
pp->seqno());
2235-
cmds.push_back(std::move(cmd));
2244+
cmds[num_requests] = std::move(cmd);
2245+
++num_requests;
22362246
}
22372247
return true;
22382248
}
@@ -2246,10 +2256,11 @@ bool Replica::execute_tentative(Pre_prepare* pp, ByzInfo& info)
22462256
pp->seqno(),
22472257
last_tentative_execute);
22482258

2249-
std::vector<std::unique_ptr<ExecCommandMsg>> cmds;
2250-
if (create_execute_commands(pp, info.max_local_commit_value, cmds))
2259+
uint32_t num_requests;
2260+
if (create_execute_commands(
2261+
pp, info.max_local_commit_value, vec_exec_cmds, num_requests))
22512262
{
2252-
exec_command(cmds, info);
2263+
exec_command(vec_exec_cmds, info, num_requests);
22532264
return true;
22542265
}
22552266
return false;
@@ -2267,8 +2278,9 @@ bool Replica::execute_tentative(
22672278
void(cb)(Pre_prepare*, Replica*, std::unique_ptr<ExecTentativeCbCtx>),
22682279
std::unique_ptr<ExecTentativeCbCtx> ctx)
22692280
{
2270-
std::vector<std::unique_ptr<ExecCommandMsg>> cmds;
2271-
if (create_execute_commands(pp, ctx->info.max_local_commit_value, cmds))
2281+
uint32_t num_requests;
2282+
if (create_execute_commands(
2283+
pp, ctx->info.max_local_commit_value, vec_exec_cmds, num_requests))
22722284
{
22732285
ByzInfo& info = ctx->info;
22742286
if (cb != nullptr)
@@ -2290,7 +2302,7 @@ bool Replica::execute_tentative(
22902302
}
22912303
}
22922304

2293-
exec_command(cmds, info);
2305+
exec_command(vec_exec_cmds, info, num_requests);
22942306
if (!node_info.general_info.support_threading)
22952307
{
22962308
cb(pp, this, std::move(ctx));

src/consensus/pbft/libbyz/Replica.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,14 @@ class Replica : public Node, public IMessageReceiveBase
318318

319319
bool is_exec_pending = false;
320320
std::list<Message*> pending_recv_msgs;
321+
std::array<std::unique_ptr<ExecCommandMsg>, Max_requests_in_batch>
322+
vec_exec_cmds;
321323

322324
bool create_execute_commands(
323325
Pre_prepare* pp,
324326
int64_t& max_local_commit_value,
325-
std::vector<std::unique_ptr<ExecCommandMsg>>& cmds);
327+
std::array<std::unique_ptr<ExecCommandMsg>, Max_requests_in_batch>& cmds,
328+
uint32_t& num_requests);
326329

327330
bool execute_tentative(Pre_prepare* pp, ByzInfo& info);
328331

src/consensus/pbft/libbyz/Reply.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ Reply::Reply(
3030
Request_id req,
3131
Seqno n,
3232
int replica,
33-
Digest& d,
3433
Principal* p,
3534
bool tentative) :
3635
Message(Reply_tag, sizeof(Reply_rep) + MAC_size)
@@ -49,7 +48,6 @@ Reply::Reply(
4948
rep().n = n;
5049
rep().replica = replica;
5150
rep().reply_size = -1;
52-
rep().digest = d;
5351

5452
INCR_OP(reply_auth);
5553
START_CC(reply_auth_cycles);
@@ -90,7 +88,6 @@ void Reply::authenticate(Principal* p, int act_len, bool tentative)
9088
}
9189

9290
rep().reply_size = act_len;
93-
rep().digest = Digest(contents() + sizeof(Reply_rep), act_len);
9491
int old_size = sizeof(Reply_rep) + act_len;
9592
set_size(old_size + MAC_size);
9693

@@ -155,16 +152,6 @@ bool Reply::pre_verify()
155152
return false;
156153
}
157154

158-
// Check reply
159-
if (full())
160-
{
161-
Digest d(contents() + sizeof(Reply_rep), rep_size);
162-
if (d != rep().digest)
163-
{
164-
return false;
165-
}
166-
}
167-
168155
// Check signature.
169156
INCR_OP(reply_auth_ver);
170157
START_CC(reply_auth_ver_cycles);

src/consensus/pbft/libbyz/Reply.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ struct Reply_rep : public Message_rep
2222
View v; // current view
2323
Request_id rid; // unique request identifier
2424
Seqno n; // sequence number when request was executed
25-
Digest digest; // digest of reply.
2625
int replica; // id of replica sending the reply
2726
int reply_size; // if negative, reply is not full.
2827
// Followed by a reply that is "reply_size" bytes long and
@@ -77,7 +76,6 @@ class Reply : public Message
7776
Request_id req,
7877
Seqno n,
7978
int replica,
80-
Digest& d,
8179
Principal* p,
8280
bool tentative);
8381
// Effects: Creates a new empty Reply message and appends a MAC for principal
@@ -100,9 +98,6 @@ class Reply : public Message
10098
int id() const;
10199
// Effects: Fetches the replier's identifier from the message.
102100

103-
Digest& digest() const;
104-
// Effects: Fetches the digest from the message.
105-
106101
char* reply(int& len);
107102
// Effects: Returns a pointer to the reply and sets len to the
108103
// reply size.
@@ -159,11 +154,6 @@ inline int Reply::id() const
159154
return rep().replica;
160155
}
161156

162-
inline Digest& Reply::digest() const
163-
{
164-
return rep().digest;
165-
}
166-
167157
inline char* Reply::reply(int& len)
168158
{
169159
len = rep().reply_size;
@@ -187,6 +177,6 @@ inline bool Reply::match(Reply* r)
187177
return false;
188178
}
189179

190-
return (rep().digest == r->rep().digest) & (rep().n == r->rep().n) &
180+
return (rep().n == r->rep().n) &
191181
((!is_tentative() & !r->is_tentative()) | (view() == r->view()));
192182
}

src/consensus/pbft/libbyz/test/replica_test.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,13 @@ static char* service_mem = 0;
189189
static IMessageReceiveBase* message_receive_base;
190190

191191
ExecCommand exec_command =
192-
[](std::vector<std::unique_ptr<ExecCommandMsg>>& msgs, ByzInfo& info) {
193-
for (auto& msg : msgs)
192+
[](
193+
std::array<std::unique_ptr<ExecCommandMsg>, Max_requests_in_batch>& msgs,
194+
ByzInfo& info,
195+
uint32_t num_requests) {
196+
for (uint32_t i = 0; i < num_requests; ++i)
194197
{
198+
std::unique_ptr<ExecCommandMsg>& msg = msgs[i];
195199
Byz_req* inb = &msg->inb;
196200
Byz_rep& outb = msg->outb;
197201
int client = msg->client;

0 commit comments

Comments
 (0)