From 8f949be1527626d65625c9b0446a34f8de849839 Mon Sep 17 00:00:00 2001 From: MEUNIER Thibaud Date: Sat, 6 Apr 2024 15:32:38 +0200 Subject: [PATCH] 20240407 --- Makefile | 2 +- src/spy2.c | 80 ++++++++++-------------------------------------------- 2 files changed, 16 insertions(+), 66 deletions(-) diff --git a/Makefile b/Makefile index 7f579eb..a5bc37f 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ $(BINDIR)/read_block : $(OBJDIR)/read_block.o read_block : $(BINDIR)/read_block -$(BINDIR)/btcspy : $(OBJDIR)/spy.o $(OBJDIR)/daemonize.o +$(BINDIR)/btcspy : $(OBJDIR)/spy2.o $(OBJDIR)/daemonize.o $(CC) -o $@ $^ $(LDFLAGS) btcspy : $(BINDIR)/btcspy diff --git a/src/spy2.c b/src/spy2.c index a693c1b..a76669f 100644 --- a/src/spy2.c +++ b/src/spy2.c @@ -15,8 +15,6 @@ #include #include -#include - /* * Les variables globales */ @@ -26,33 +24,10 @@ FILE* blockchain_fd = NULL; /* * Les outils */ -static void signal_handler(int sig){ - char msg[200]; - sprintf(msg,"process %d signal %d trapped\n", getpid(), sig); - log_signal_message(msg); - switch(sig){ - case SIGUSR1: - // signal USR1 - break; - case SIGUSR2: - // Signal USR2 - break; - case SIGHUP: - // Reload conf and reinitialize processus - break; - case SIGINT: - // CTRL+C - case SIGTERM: - // Terminate : kill -15 - exit(0); - break; - } -} - static btc_bool file_exist(const char *filename) { struct stat buffer; - return (stat (filename, &buffer) == 0); + return (stat(filename, &buffer) == 0); } /* @@ -249,14 +224,14 @@ static void cb_handshake_done(struct btc_node_ *node) * De même, on garde une marge de manoeuvre dans les positions * du fichier */ - if (node->bestknownheight > cb_handshake_done_bestknownheight) - { - if (blockchain_fd != NULL) - { + if (blockchain_fd != NULL) + { /* - * Définir et intialiser un tampon de 20 blocks + * Définir et initialiser un tampon de 20 blocks + * Ca veut dire qu'on considère qu'il n'y a pas de fork + * de plus de 20 blocks */ -#define MARGE_TAMPON 25 +#define MARGE_TAMPON 20 char buffer[MARGE_TAMPON][65]; uint32_t read_height[MARGE_TAMPON]; fpos_t fpos[MARGE_TAMPON]; @@ -284,6 +259,9 @@ static void cb_handshake_done(struct btc_node_ *node) } if (read_height[0] > MARGE_TAMPON) { + /* + * On se sert du haut d ela pile comme point de départ + */ int outlen; node->nodegroup->log_write_cb("STATUS : Setting requested hash with %s\n", buffer[19]); utils_reverse_hex(buffer[19], 64); @@ -292,8 +270,10 @@ static void cb_handshake_done(struct btc_node_ *node) request_headers_or_blocks_hash_init = true; fsetpos(blockchain_fd, fpos+(MARGE_TAMPON-1)); } - } + } + if (node->bestknownheight > cb_handshake_done_bestknownheight) + { node->nodegroup->log_write_cb("STATUS : starting header sync from node %d\n", node->nodeid); node->state |= NODE_HEADERSYNC; request_headers_or_blocks(node, false); @@ -654,6 +634,8 @@ static void cb_post_cmd(struct btc_node_ *node, btc_p2p_msg_hdr *hdr, struct con node->state &= ~NODE_HEADERSYNC; if (blockchain_fd != NULL) fflush(blockchain_fd); + // TRES TRES SALE ... + exit(0); } } } @@ -736,38 +718,6 @@ int main(int ac, char** av) */ if (ac > 1) chdir(av[1]); - /* - * Daemonize - */ - daemonize(); - - /* - * Trapper les signaux pour fermer correctement le programme - */ - int les_signaux_ign[] = { SIGCHLD, SIGTSTP, SIGTTOU, SIGTTIN }; - int les_signaux[] = { SIGHUP, SIGTERM, SIGINT }; - size_t n; - - n = array_len(les_signaux_ign); - for(int i = 0; i < n; i++) - { - if (signal(les_signaux_ign[i],SIG_IGN) == SIG_ERR) - { - printf("\ncan't catch signal %d\n", les_signaux_ign[i]); - return 1; - } - } - - n = array_len(les_signaux); - for(int i = 0; i < n; i++) - { - if (signal(les_signaux[i],signal_handler) == SIG_ERR) - { - printf("\ncan't catch signal %d\n", les_signaux[i]); - return 1; - } - } - /* * Gestion du fichier de chaine de blocks */