Skip to content

Commit

Permalink
assert
Browse files Browse the repository at this point in the history
  • Loading branch information
mattn committed Jan 27, 2024
1 parent 0861336 commit b4af11e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
31 changes: 21 additions & 10 deletions main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,28 @@ static inline std::vector<uint8_t> hex2bytes(const std::string &hex) {
}

void relay_send(ws28::Client *client, const nlohmann::json &data) {
assert(client);
auto s = data.dump();
spdlog::debug("{} << {}", client->GetIP(), s);
client->Send(s.data(), s.size(), 1);
}

static inline void relay_notice(ws28::Client *client, const std::string &msg) {
assert(client);
nlohmann::json data = {"NOTICE", msg};
relay_send(client, data);
}

static inline void relay_notice(ws28::Client *client, const std::string &id,
const std::string &msg) {
assert(client);
nlohmann::json data = {"NOTICE", id, msg};
relay_send(client, data);
}

static bool signature_verify(const std::vector<uint8_t> &bytes_sig,
const std::vector<uint8_t> &bytes_pub,
const uint8_t *digest) {
const uint8_t digest[32]) {
#define secp256k1_context_flags \
(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY)
secp256k1_context *ctx = secp256k1_context_create(secp256k1_context_flags);
Expand Down Expand Up @@ -105,6 +108,7 @@ static void make_filter(filter_t &filter, nlohmann::json &data) {
}

static void do_relay_req(ws28::Client *client, nlohmann::json &data) {
assert(client);
std::string sub = data[1];
std::vector<filter_t> filters;
for (size_t i = 2; i < data.size(); i++) {
Expand Down Expand Up @@ -133,6 +137,7 @@ static void do_relay_req(ws28::Client *client, nlohmann::json &data) {
}

static void do_relay_count(ws28::Client *client, nlohmann::json &data) {
assert(client);
std::string sub = data[1];
std::vector<filter_t> filters;
for (size_t i = 2; i < data.size(); i++) {
Expand All @@ -158,6 +163,7 @@ static void do_relay_count(ws28::Client *client, nlohmann::json &data) {
}

static void do_relay_close(ws28::Client *client, nlohmann::json &data) {
assert(client);
std::string sub = data[1];
auto it = subscribers.begin();
while (it != subscribers.end()) {
Expand Down Expand Up @@ -229,9 +235,9 @@ static bool matched_filters(const std::vector<filter_t> &filters,
return found;
}

static bool check_event(const event_t& ev) {
static bool check_event(const event_t &ev) {
nlohmann::json check = {0, ev.pubkey, ev.created_at,
ev.kind, ev.tags, ev.content};
ev.kind, ev.tags, ev.content};
auto dump = check.dump();
check.clear();

Expand Down Expand Up @@ -290,7 +296,8 @@ static void do_relay_event(ws28::Client *client, nlohmann::json &data) {
std::string d;
for (const auto &tag : ev.tags) {
if (tag.size() >= 2 && tag[0] == "d") {
if (!delete_record_by_kind_and_pubkey_and_dtag(ev.kind, ev.pubkey, tag)) {
if (!delete_record_by_kind_and_pubkey_and_dtag(ev.kind, ev.pubkey,
tag)) {
return;
}
}
Expand All @@ -316,10 +323,7 @@ static void do_relay_event(ws28::Client *client, nlohmann::json &data) {
}
}

static void http_request_callback(ws28::HTTPRequest &req,
ws28::HTTPResponse &resp) {
const static auto nip11 = R"(
{
const static auto nip11 = R"({
"name": "cagliostr",
"description": "nostr relay written in C++",
"pubkey": "2c7cc62a697ea3a7826521f3fd34f0cb273693cbe5e9310f35449f43622a5cdc",
Expand Down Expand Up @@ -357,9 +361,10 @@ static void http_request_callback(ws28::HTTPRequest &req,
},
"fees": {},
"icon": "https://mattn.github.io/assets/image/mattn-mohawk.webp"
}
)";
})";

static void http_request_callback(ws28::HTTPRequest &req,
ws28::HTTPResponse &resp) {
resp.header("Access-Control-Allow-Origin", "*");
if (req.method == "GET") {
auto accept = req.headers.Get("accept");
Expand Down Expand Up @@ -397,6 +402,7 @@ static bool check_callback(ws28::Client * /*client*/, ws28::HTTPRequest &req) {
}

static void disconnect_callback(ws28::Client *client) {
assert(client);
spdlog::debug("DISCONNECT {}", client->GetIP());
auto it = subscribers.begin();
while (it != subscribers.end()) {
Expand All @@ -415,6 +421,8 @@ static inline bool check_method(std::string &method) {

static void data_callback(ws28::Client *client, char *data, size_t len,
int /*opcode*/) {
assert(client);
assert(data);
std::string s(data, len);
spdlog::debug("{} >> {}", client->GetIP(), s);
try {
Expand Down Expand Up @@ -462,6 +470,7 @@ static void data_callback(ws28::Client *client, char *data, size_t len,
}

static void signal_handler(uv_signal_t *req, int /*signum*/) {
assert(req);
uv_signal_stop(req);
spdlog::warn("!! SIGINT");
for (auto &s : subscribers) {
Expand All @@ -478,6 +487,8 @@ static void signal_handler(uv_signal_t *req, int /*signum*/) {
}

static std::string env(const char *name, const char *defvalue) {
assert(name);
assert(defvalue);
const char *value = getenv(name);
if (value == nullptr) {
value = defvalue;
Expand Down
5 changes: 4 additions & 1 deletion records.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ static std::string join(const std::vector<std::string> &v,
if (!v.empty()) {
s += v[0];
for (decltype(v.size()) i = 1, c = v.size(); i < c; ++i) {
if (delim)
if (delim) {
s += delim;
}
s += v[i];
}
}
Expand Down Expand Up @@ -54,6 +55,7 @@ bool insert_record(const event_t &ev) {

bool send_records(ws28::Client *client, const std::string &sub,
const std::vector<filter_t> &filters, bool do_count) {
assert(client);
auto count = 0;
for (const auto &filter : filters) {
std::string sql;
Expand Down Expand Up @@ -303,6 +305,7 @@ bool delete_record_by_kind_and_pubkey_and_dtag(

static void sqlite3_trace_callback(void * /*user_data*/,
const char *statement) {
assert(statement);
spdlog::debug("{}", statement);
}

Expand Down

0 comments on commit b4af11e

Please sign in to comment.