Skip to content

Commit

Permalink
Fix linting errors
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Maurer <[email protected]>
  • Loading branch information
maurermi committed Dec 14, 2023
1 parent 995709b commit 230f402
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 182 deletions.
3 changes: 1 addition & 2 deletions scripts/parsec-run-local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
IP="localhost"
PORT="8888"
RUNNER_TYPE="evm"
LOGLEVEL="TRACE"
DBG=
LOGLEVEL="WARN"

function print_help() {
echo "Usage: parsec-run-local.sh [OPTIONS]"
Expand Down
2 changes: 1 addition & 1 deletion scripts/py_bench.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ PORT="8889"

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

./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
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
echo done
3 changes: 2 additions & 1 deletion src/parsec/agent/agentd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,10 @@ auto main(int argc, char** argv) -> int {
log->info("Agent running");

std::thread ticket_state_logger([&broker] {
static constexpr auto log_timestep = 10;
while(running) {
broker->log_tickets();
std::this_thread::sleep_for(std::chrono::seconds(10));
std::this_thread::sleep_for(std::chrono::seconds(log_timestep));
}
});

Expand Down
2 changes: 1 addition & 1 deletion src/parsec/agent/impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ namespace cbdc::parsec::agent {
}
}

void impl::do_commit() {
void impl::do_commit() { // should force this to wait on locks
std::unique_lock l(m_mut);
assert(m_state == state::function_started
|| m_state == state::commit_failed
Expand Down
133 changes: 74 additions & 59 deletions src/parsec/agent/runners/py/impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ namespace cbdc::parsec::agent::runner {
PyObject* localDictionary = PyDict_New();

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

for(runtime_locking_shard::key_type key : m_shard_inputs) {
for(runtime_locking_shard::key_type& key : m_shard_inputs) {
auto dest = cbdc::buffer();
auto valid_resp = std::promise<bool>();

Expand All @@ -70,6 +71,7 @@ namespace cbdc::parsec::agent::runner {
auto data = handle_try_lock_input_arg(std::move(res), dest);
valid_resp.set_value(data);
});

if(!success) {
m_log->error("Failed to issue try lock command");
m_result_callback(error_code::internal_error);
Expand All @@ -82,7 +84,7 @@ namespace cbdc::parsec::agent::runner {
"to be \"0\"");
dest.append("0", 2);
}
params.push_back(dest.c_str());
params.emplace_back(dest.c_str());
}

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

auto r = PyRun_String(m_function.c_str(),
Py_file_input,
globalDictionary,
localDictionary);
if(!r) {
auto* r = PyRun_String(m_function.c_str(),
Py_file_input,
globalDictionary,
localDictionary);
if(r == nullptr) {
m_log->error("Python VM generated error:", r);
}
update_state(localDictionary);
Expand All @@ -116,7 +118,8 @@ namespace cbdc::parsec::agent::runner {
void py_runner::parse_header() {
/* Assumes that header is <n returns> <n inputs> | return types |
return args | input types | input args | function code */
auto functionString = std::string((char*)m_function.data());
auto functionString
= std::string(static_cast<char*>(m_function.data()));

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

arg_delim = functionString.find('|');
arg_string = functionString.substr(0, arg_delim);
pos = 0;
while((pos = arg_string.find(",", 0)) != std::string::npos) {
while((pos = arg_string.find(',', 0)) != std::string::npos) {
m_return_args.push_back(arg_string.substr(0, pos));
arg_string.erase(0, pos + 1);
}
functionString.erase(0, arg_delim + 1);

arg_delim = functionString.find('|');
arg_string = functionString.substr(0, arg_delim);
pos = 0;
while((pos = arg_string.find(",", 0)) != std::string::npos) {
while((pos = arg_string.find(',', 0)) != std::string::npos) {
m_input_args.push_back(arg_string.substr(0, pos));
arg_string.erase(0, pos + 1);
}
Expand All @@ -159,57 +160,76 @@ namespace cbdc::parsec::agent::runner {
m_log->error("m_param contains no data");
return params;
}
m_param.append("\0", 1);
size_t pipeCount = 0;
size_t stringCount = 0;
auto paramString
= std::string(static_cast<char*>(m_param.data()), m_param.size());
for(size_t i = 0; i < m_param.size(); i++) {
if(((char*)m_param.data())[i] == '|') {
if(paramString[i] == '|') {
pipeCount++;
} else if(i > 0 && (int)((char*)m_param.data())[i] == 0
&& (int)((char*)m_param.data())[i - 1] != 0) {
} else if(i > 0 && paramString[i] == '\0'
&& paramString[i - 1] != '\0') {
stringCount++;
}
}
auto bail = false;
if(pipeCount != 3) {
m_log->error("m_param sections are improperly formatted");
return params;
bail = true;
} else if(stringCount
!= m_input_args.size() + m_return_args.size() + pipeCount) {
m_log->error("m_param contains too few arguments or arguments are "
"improperly formatted");
bail = true;
}
if(bail) {
return params;
}
m_param.append("\0", 1);
// ---

char* paramString = (char*)m_param.data();

// get parameters that are user inputs
while(*paramString != '|') {
params.emplace_back(paramString);
paramString += params.back().length() + 1;
}
paramString += 2;

// get parameters which are to be pulled from shards
while(*paramString != '|') {
auto tmp = cbdc::buffer();
/// \todo Prefer a solution which does not use strlen
tmp.append(paramString, std::strlen(paramString) + 1);

m_shard_inputs.emplace_back(tmp);
/// \todo Prefer a solution which does not use strlen
paramString += std::strlen(paramString) + 1;
}
paramString += 2;

// get the state update keys
while(*paramString != '|') { // was previously '\0'
auto tmp = cbdc::buffer();
tmp.append(paramString, std::strlen(paramString) + 1);

m_update_keys.emplace_back(tmp);
/// \todo Prefer a solution which does not use strlen
paramString += std::strlen(paramString) + 1;
size_t bufferOffset = 0;
auto it = 0;
// Known that there are 3 parts of the header
while(it < 3) {
auto parsedParam = std::string(
static_cast<char*>(m_param.data_at(bufferOffset)));
if(parsedParam[0] == '|') {
it++;
bufferOffset += 2;
continue;
}
switch(it) {
/*
In first section, take data and place in params
In second section, take data and place in m_shard_inputs
In third section, take data and place in m_update_keys
*/
case 0:
params.emplace_back(std::string(parsedParam));
bufferOffset += params.back().length() + 1;
break;
case 1: {
auto tmp = cbdc::buffer();
tmp.append(parsedParam.c_str(), parsedParam.length() + 1);
m_shard_inputs.emplace_back(tmp);
bufferOffset += tmp.size();
break;
}
case 2: {
auto tmp = cbdc::buffer();
tmp.append(parsedParam.c_str(), parsedParam.length() + 1);
m_update_keys.emplace_back(tmp);
bufferOffset += tmp.size();
break;
}
default:
m_log->fatal(
"Reading past the number of input group "
"sections. Something has gone seriously wrong.");
continue;
}
}

return params;
Expand Down Expand Up @@ -251,8 +271,6 @@ namespace cbdc::parsec::agent::runner {

// Communicate updates to agent scope. Will write back to shards.
m_result_callback(std::move(updates));

return;
}

void py_runner::handle_try_lock(
Expand Down Expand Up @@ -285,9 +303,7 @@ namespace cbdc::parsec::agent::runner {
res);
if(maybe_error.has_value()) {
m_result_callback(maybe_error.value());
return;
}
return;
}

auto py_runner::handle_try_lock_input_arg(
Expand Down Expand Up @@ -331,8 +347,8 @@ namespace cbdc::parsec::agent::runner {
for(size_t i = 0; i < m_return_types.size(); i++) {
auto ch = m_return_types[i];
m_log->trace("Parsing:", m_return_args[i].c_str());
auto word = PyDict_GetItemString(localDictionary,
m_return_args[i].c_str());
auto* word = PyDict_GetItemString(localDictionary,
m_return_args[i].c_str());
auto buf = cbdc::buffer();
switch(ch) {
case 'l': {
Expand All @@ -349,16 +365,16 @@ namespace cbdc::parsec::agent::runner {
}
case 's': {
m_log->trace("Parsing string");
char* res;
char* res = nullptr;
if(PyUnicode_Check(word)) {
res = PyBytes_AS_STRING(
res = PyBytes_AsString(
PyUnicode_AsEncodedString(word,
"UTF-8",
"strict"));
} else {
res = PyBytes_AsString(word);
}
if(res) {
if(res != nullptr) {
// PyBytes_AsString returns a null terminated string
buf.append(res, strlen(res) + 1);
}
Expand All @@ -373,16 +389,16 @@ namespace cbdc::parsec::agent::runner {
}
case 'c': {
m_log->trace("Parsing char");
char* res;
char* res = nullptr;
if(PyUnicode_Check(word)) {
res = PyBytes_AS_STRING(
res = PyBytes_AsString(
PyUnicode_AsEncodedString(word,
"UTF-8",
"strict"));
} else {
res = PyBytes_AsString(word);
}
if(res) {
if(res != nullptr) {
buf.append(res, strnlen(res, 1));
}
break;
Expand All @@ -393,6 +409,5 @@ namespace cbdc::parsec::agent::runner {
}
m_return_values.push_back(buf);
}
return;
}
}
6 changes: 3 additions & 3 deletions src/parsec/agent/runners/py/pybuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <string>

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

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

void pyBuffer::appendByteVector(const std::vector<std::byte> data) {
void pyBuffer::appendByteVector(const std::vector<std::byte>& data) {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto dataStr = reinterpret_cast<const char*>(data.data());
const auto* dataStr = reinterpret_cast<const char*>(data.data());
this->append(dataStr, data.size());
this->append("\0", 1);
}
Expand Down
4 changes: 2 additions & 2 deletions src/parsec/agent/runners/py/pybuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ namespace cbdc::parsec::pybuffer {
public:
/// Append a string to the buffer (with null-terminator)
/// \param data String to append to buffer
void appendString(const std::string data);
void appendString(const std::string& data);
/// Append a C-style string to the buffer of given length
/// \param data Data to append to buffer
/// \param len Length of string
void appendCStr(const char* data, size_t len);
/// Append a byte vector to the buffer
/// \param data Data to append to buffer
void appendByteVector(const std::vector<std::byte> data);
void appendByteVector(const std::vector<std::byte>& data);

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

0 comments on commit 230f402

Please sign in to comment.