Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rest: added getLastBlockInfo endpoint #235

Open
wants to merge 3 commits into
base: 0.1.4-dogebox-pre
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ jobs:
goal: install
- name: x86_64-macos
host: x86_64-apple-darwin15
os: macos-12
os: macos-13
run-tests: true
dep-opts: "SPEED=slow V=1"
config-opts: "--enable-static --disable-shared --enable-test-passwd"
Expand Down
3 changes: 3 additions & 0 deletions include/dogecoin/spv.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ typedef struct dogecoin_spv_client_
dogecoin_bool called_sync_completed;
void *headers_db_ctx;
const dogecoin_headers_db_interface *headers_db;
uint64_t last_block_size;
uint64_t last_block_tx_count;
uint64_t last_block_total_tx_size;

/* callbacks */
/* ========= */
Expand Down
16 changes: 16 additions & 0 deletions src/rest.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <dogecoin/spv.h>
#include <dogecoin/wallet.h>

#define TIMESTAMP_MAX_LEN 32

/**
* This function is called when an http request is received
Expand Down Expand Up @@ -207,6 +208,21 @@ void dogecoin_http_request_cb(struct evhttp_request *req, void *arg) {
} else if (strcmp(path, "/getChaintip") == 0) {
dogecoin_blockindex* tip = client->headers_db->getchaintip(client->headers_db_ctx);
evbuffer_add_printf(evb, "Chain tip: %d\n", tip->height);
} else if (strcmp(path, "/getTimestamp") == 0) {
dogecoin_blockindex* tip = client->headers_db->getchaintip(client->headers_db_ctx);
char s[TIMESTAMP_MAX_LEN];
time_t t = tip->header.timestamp;
struct tm *p = localtime(&t);
strftime(s, sizeof(s), "%F %T", p);
evbuffer_add_printf(evb, "%s\n", s);
} else if (strcmp(path, "/getLastBlockInfo") == 0) {
uint64_t size = client->last_block_size;
uint64_t tx_count = client->last_block_tx_count;
uint64_t total_tx_size = client->last_block_total_tx_size;

evbuffer_add_printf(evb, "Block size: %lu\n", size);
evbuffer_add_printf(evb, "Tx count: %lu\n", tx_count);
evbuffer_add_printf(evb, "Total tx size: %lu\n", total_tx_size);
} else {
evhttp_send_error(req, HTTP_NOTFOUND, "Not Found");
evbuffer_free(evb);
Expand Down
9 changes: 8 additions & 1 deletion src/spv.c
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,12 @@ void dogecoin_net_spv_post_cmd(dogecoin_node *node, dogecoin_p2p_msg_hdr *hdr, s

client->nodegroup->log_write_cb("Start parsing %d transactions...\n", (int)amount_of_txs);

// update the last block info for the client
client->last_block_tx_count = amount_of_txs;
client->last_block_size = hdr->data_len;

uint64_t total_tx_size = 0;

size_t consumedlength = 0;
unsigned int i;
for (i = 0; i < amount_of_txs; i++)
Expand All @@ -599,9 +605,10 @@ void dogecoin_net_spv_post_cmd(dogecoin_node *node, dogecoin_p2p_msg_hdr *hdr, s
}
deser_skip(buf, consumedlength);
if (client->sync_transaction) { client->sync_transaction(client->sync_transaction_ctx, tx, i, pindex); }
total_tx_size += consumedlength;
dogecoin_tx_free(tx);
}

client->last_block_total_tx_size = total_tx_size;
client->nodegroup->log_write_cb("done (took %lld secs)\n", (unsigned long long)(time(NULL) - start));
}
else
Expand Down
Loading