Skip to content

Commit 0ca9524

Browse files
committed
Fix linting errors
Signed-off-by: Michael Maurer <[email protected]>
1 parent 10a017c commit 0ca9524

File tree

14 files changed

+194
-182
lines changed

14 files changed

+194
-182
lines changed

scripts/parsec-run-local.sh

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
IP="localhost"
44
PORT="8888"
55
RUNNER_TYPE="evm"
6-
LOGLEVEL="TRACE"
7-
DBG=
6+
LOGLEVEL="WARN"
87

98
function print_help() {
109
echo "Usage: parsec-run-local.sh [OPTIONS]"

scripts/py_bench.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ PORT="8889"
77

88
#cp ./scripts/pythonContractConverter.py ./build/tools/bench/parsec/py/
99

10-
./build/tools/bench/parsec/py/py_bench --component_id=0 --ticket_machine0_endpoint=$IP:7777 --ticket_machine_count=1 --shard_count=1 --shard0_count=1 --shard00_endpoint=$IP:5556 --agent_count=1 --agent0_endpoint=$IP:$PORT
10+
rr record ./build/tools/bench/parsec/py/py_bench --component_id=0 --ticket_machine0_endpoint=$IP:7777 --ticket_machine_count=1 --shard_count=1 --shard0_count=1 --shard00_endpoint=$IP:5556 --agent_count=1 --agent0_endpoint=$IP:$PORT
1111
echo done

src/parsec/agent/agentd.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,10 @@ auto main(int argc, char** argv) -> int {
172172
log->info("Agent running");
173173

174174
std::thread ticket_state_logger([&broker] {
175+
static constexpr auto log_timestep = 10;
175176
while(running) {
176177
broker->log_tickets();
177-
std::this_thread::sleep_for(std::chrono::seconds(10));
178+
std::this_thread::sleep_for(std::chrono::seconds(log_timestep));
178179
}
179180
});
180181

src/parsec/agent/impl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ namespace cbdc::parsec::agent {
352352
}
353353
}
354354

355-
void impl::do_commit() {
355+
void impl::do_commit() { // should force this to wait on locks
356356
std::unique_lock l(m_mut);
357357
assert(m_state == state::function_started
358358
|| m_state == state::commit_failed

src/parsec/agent/runners/py/impl.cpp

+74-59
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ namespace cbdc::parsec::agent::runner {
4444
PyObject* localDictionary = PyDict_New();
4545

4646
if(m_function.size() == 0) {
47-
m_log->error("Contract has length 0, key may be invalid");
47+
m_log->warn(
48+
"Contract has length 0, key may be invalid. Bailing out.");
4849
m_result_callback(runtime_locking_shard::state_update_type());
4950
return true;
5051
}
@@ -54,7 +55,7 @@ namespace cbdc::parsec::agent::runner {
5455
// Parse the parameters buffer into its component strings.
5556
auto params = parse_params();
5657

57-
for(runtime_locking_shard::key_type key : m_shard_inputs) {
58+
for(runtime_locking_shard::key_type& key : m_shard_inputs) {
5859
auto dest = cbdc::buffer();
5960
auto valid_resp = std::promise<bool>();
6061

@@ -70,6 +71,7 @@ namespace cbdc::parsec::agent::runner {
7071
auto data = handle_try_lock_input_arg(std::move(res), dest);
7172
valid_resp.set_value(data);
7273
});
74+
7375
if(!success) {
7476
m_log->error("Failed to issue try lock command");
7577
m_result_callback(error_code::internal_error);
@@ -82,7 +84,7 @@ namespace cbdc::parsec::agent::runner {
8284
"to be \"0\"");
8385
dest.append("0", 2);
8486
}
85-
params.push_back(dest.c_str());
87+
params.emplace_back(dest.c_str());
8688
}
8789

8890
if(m_input_args.size() != params.size()) {
@@ -97,11 +99,11 @@ namespace cbdc::parsec::agent::runner {
9799
value);
98100
}
99101

100-
auto r = PyRun_String(m_function.c_str(),
101-
Py_file_input,
102-
globalDictionary,
103-
localDictionary);
104-
if(!r) {
102+
auto* r = PyRun_String(m_function.c_str(),
103+
Py_file_input,
104+
globalDictionary,
105+
localDictionary);
106+
if(r == nullptr) {
105107
m_log->error("Python VM generated error:", r);
106108
}
107109
update_state(localDictionary);
@@ -116,7 +118,8 @@ namespace cbdc::parsec::agent::runner {
116118
void py_runner::parse_header() {
117119
/* Assumes that header is <n returns> <n inputs> | return types |
118120
return args | input types | input args | function code */
119-
auto functionString = std::string((char*)m_function.data());
121+
auto functionString
122+
= std::string(static_cast<char*>(m_function.data()));
120123

121124
m_input_args.clear();
122125
m_return_args.clear();
@@ -130,17 +133,15 @@ namespace cbdc::parsec::agent::runner {
130133

131134
arg_delim = functionString.find('|');
132135
arg_string = functionString.substr(0, arg_delim);
133-
pos = 0;
134-
while((pos = arg_string.find(",", 0)) != std::string::npos) {
136+
while((pos = arg_string.find(',', 0)) != std::string::npos) {
135137
m_return_args.push_back(arg_string.substr(0, pos));
136138
arg_string.erase(0, pos + 1);
137139
}
138140
functionString.erase(0, arg_delim + 1);
139141

140142
arg_delim = functionString.find('|');
141143
arg_string = functionString.substr(0, arg_delim);
142-
pos = 0;
143-
while((pos = arg_string.find(",", 0)) != std::string::npos) {
144+
while((pos = arg_string.find(',', 0)) != std::string::npos) {
144145
m_input_args.push_back(arg_string.substr(0, pos));
145146
arg_string.erase(0, pos + 1);
146147
}
@@ -159,57 +160,76 @@ namespace cbdc::parsec::agent::runner {
159160
m_log->error("m_param contains no data");
160161
return params;
161162
}
163+
m_param.append("\0", 1);
162164
size_t pipeCount = 0;
163165
size_t stringCount = 0;
166+
auto paramString
167+
= std::string(static_cast<char*>(m_param.data()), m_param.size());
164168
for(size_t i = 0; i < m_param.size(); i++) {
165-
if(((char*)m_param.data())[i] == '|') {
169+
if(paramString[i] == '|') {
166170
pipeCount++;
167-
} else if(i > 0 && (int)((char*)m_param.data())[i] == 0
168-
&& (int)((char*)m_param.data())[i - 1] != 0) {
171+
} else if(i > 0 && paramString[i] == '\0'
172+
&& paramString[i - 1] != '\0') {
169173
stringCount++;
170174
}
171175
}
176+
auto bail = false;
172177
if(pipeCount != 3) {
173178
m_log->error("m_param sections are improperly formatted");
174-
return params;
179+
bail = true;
175180
} else if(stringCount
176181
!= m_input_args.size() + m_return_args.size() + pipeCount) {
177182
m_log->error("m_param contains too few arguments or arguments are "
178183
"improperly formatted");
184+
bail = true;
185+
}
186+
if(bail) {
179187
return params;
180188
}
181-
m_param.append("\0", 1);
182189
// ---
183190

184-
char* paramString = (char*)m_param.data();
185-
186191
// get parameters that are user inputs
187-
while(*paramString != '|') {
188-
params.emplace_back(paramString);
189-
paramString += params.back().length() + 1;
190-
}
191-
paramString += 2;
192-
193-
// get parameters which are to be pulled from shards
194-
while(*paramString != '|') {
195-
auto tmp = cbdc::buffer();
196-
/// \todo Prefer a solution which does not use strlen
197-
tmp.append(paramString, std::strlen(paramString) + 1);
198-
199-
m_shard_inputs.emplace_back(tmp);
200-
/// \todo Prefer a solution which does not use strlen
201-
paramString += std::strlen(paramString) + 1;
202-
}
203-
paramString += 2;
204-
205-
// get the state update keys
206-
while(*paramString != '|') { // was previously '\0'
207-
auto tmp = cbdc::buffer();
208-
tmp.append(paramString, std::strlen(paramString) + 1);
209-
210-
m_update_keys.emplace_back(tmp);
211-
/// \todo Prefer a solution which does not use strlen
212-
paramString += std::strlen(paramString) + 1;
192+
size_t bufferOffset = 0;
193+
auto it = 0;
194+
// Known that there are 3 parts of the header
195+
while(it < 3) {
196+
auto parsedParam = std::string(
197+
static_cast<char*>(m_param.data_at(bufferOffset)));
198+
if(parsedParam[0] == '|') {
199+
it++;
200+
bufferOffset += 2;
201+
continue;
202+
}
203+
switch(it) {
204+
/*
205+
In first section, take data and place in params
206+
In second section, take data and place in m_shard_inputs
207+
In third section, take data and place in m_update_keys
208+
*/
209+
case 0:
210+
params.emplace_back(std::string(parsedParam));
211+
bufferOffset += params.back().length() + 1;
212+
break;
213+
case 1: {
214+
auto tmp = cbdc::buffer();
215+
tmp.append(parsedParam.c_str(), parsedParam.length() + 1);
216+
m_shard_inputs.emplace_back(tmp);
217+
bufferOffset += tmp.size();
218+
break;
219+
}
220+
case 2: {
221+
auto tmp = cbdc::buffer();
222+
tmp.append(parsedParam.c_str(), parsedParam.length() + 1);
223+
m_update_keys.emplace_back(tmp);
224+
bufferOffset += tmp.size();
225+
break;
226+
}
227+
default:
228+
m_log->fatal(
229+
"Reading past the number of input group "
230+
"sections. Something has gone seriously wrong.");
231+
continue;
232+
}
213233
}
214234

215235
return params;
@@ -251,8 +271,6 @@ namespace cbdc::parsec::agent::runner {
251271

252272
// Communicate updates to agent scope. Will write back to shards.
253273
m_result_callback(std::move(updates));
254-
255-
return;
256274
}
257275

258276
void py_runner::handle_try_lock(
@@ -285,9 +303,7 @@ namespace cbdc::parsec::agent::runner {
285303
res);
286304
if(maybe_error.has_value()) {
287305
m_result_callback(maybe_error.value());
288-
return;
289306
}
290-
return;
291307
}
292308

293309
auto py_runner::handle_try_lock_input_arg(
@@ -331,8 +347,8 @@ namespace cbdc::parsec::agent::runner {
331347
for(size_t i = 0; i < m_return_types.size(); i++) {
332348
auto ch = m_return_types[i];
333349
m_log->trace("Parsing:", m_return_args[i].c_str());
334-
auto word = PyDict_GetItemString(localDictionary,
335-
m_return_args[i].c_str());
350+
auto* word = PyDict_GetItemString(localDictionary,
351+
m_return_args[i].c_str());
336352
auto buf = cbdc::buffer();
337353
switch(ch) {
338354
case 'l': {
@@ -349,16 +365,16 @@ namespace cbdc::parsec::agent::runner {
349365
}
350366
case 's': {
351367
m_log->trace("Parsing string");
352-
char* res;
368+
char* res = nullptr;
353369
if(PyUnicode_Check(word)) {
354-
res = PyBytes_AS_STRING(
370+
res = PyBytes_AsString(
355371
PyUnicode_AsEncodedString(word,
356372
"UTF-8",
357373
"strict"));
358374
} else {
359375
res = PyBytes_AsString(word);
360376
}
361-
if(res) {
377+
if(res != nullptr) {
362378
// PyBytes_AsString returns a null terminated string
363379
buf.append(res, strlen(res) + 1);
364380
}
@@ -373,16 +389,16 @@ namespace cbdc::parsec::agent::runner {
373389
}
374390
case 'c': {
375391
m_log->trace("Parsing char");
376-
char* res;
392+
char* res = nullptr;
377393
if(PyUnicode_Check(word)) {
378-
res = PyBytes_AS_STRING(
394+
res = PyBytes_AsString(
379395
PyUnicode_AsEncodedString(word,
380396
"UTF-8",
381397
"strict"));
382398
} else {
383399
res = PyBytes_AsString(word);
384400
}
385-
if(res) {
401+
if(res != nullptr) {
386402
buf.append(res, strnlen(res, 1));
387403
}
388404
break;
@@ -393,6 +409,5 @@ namespace cbdc::parsec::agent::runner {
393409
}
394410
m_return_values.push_back(buf);
395411
}
396-
return;
397412
}
398413
}

src/parsec/agent/runners/py/pybuffer.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <string>
44

55
namespace cbdc::parsec::pybuffer {
6-
void pyBuffer::appendString(const std::string data) {
6+
void pyBuffer::appendString(const std::string& data) {
77
this->append(data.c_str(), data.length() + 1);
88
}
99

@@ -12,9 +12,9 @@ namespace cbdc::parsec::pybuffer {
1212
this->append("\0", 1);
1313
}
1414

15-
void pyBuffer::appendByteVector(const std::vector<std::byte> data) {
15+
void pyBuffer::appendByteVector(const std::vector<std::byte>& data) {
1616
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
17-
auto dataStr = reinterpret_cast<const char*>(data.data());
17+
const auto* dataStr = reinterpret_cast<const char*>(data.data());
1818
this->append(dataStr, data.size());
1919
this->append("\0", 1);
2020
}

src/parsec/agent/runners/py/pybuffer.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ namespace cbdc::parsec::pybuffer {
1515
public:
1616
/// Append a string to the buffer (with null-terminator)
1717
/// \param data String to append to buffer
18-
void appendString(const std::string data);
18+
void appendString(const std::string& data);
1919
/// Append a C-style string to the buffer of given length
2020
/// \param data Data to append to buffer
2121
/// \param len Length of string
2222
void appendCStr(const char* data, size_t len);
2323
/// Append a byte vector to the buffer
2424
/// \param data Data to append to buffer
25-
void appendByteVector(const std::vector<std::byte> data);
25+
void appendByteVector(const std::vector<std::byte>& data);
2626

2727
/// End a "section" in the buffer. This is used to format the data
2828
/// such that the Python runner can read it.

0 commit comments

Comments
 (0)