Skip to content

Commit b8c8f90

Browse files
committed
Refactor PseudoNode into a library.
PseudoNode has been re-factored into a library: - The PseudoNode API is provided in pseudo_node.h - The PseudoNode library implementation is in pseudo_node.c - A basic "reference" PseudoNode implementation is in main.c The API is hopefully reasonably basic: - A PseudoNode can be created with the PN_create() call. - Various events are reported via "callbacks". Events include: * New block * New transaction * New INV message * (low level) any kind of message * log and warning messages. - Messages can be changed/manipulated by the application, e.g. it is possible to block or insert inv messages. - The API is incomplete (PN_destroy is NYI). The aim is to make it easy to insert arbtiray code into cryptocurrecy networks (i.e. no long "sync" times). There are lots of applications for this... In addition, several bugs were fixed & improvements made, e.g. - Now uses BIP34 to find the height. - TXs are cleaned based on block inclusion or a timeout.a - Bug fixes...
1 parent 0607b13 commit b8c8f90

File tree

9 files changed

+1552
-941
lines changed

9 files changed

+1552
-941
lines changed

Makefile

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
CC = clang
22
STRIP = strip
3-
OBJS = pseudo_node.o sha256.o
4-
CFLAGS = -std=gnu99 -DLINUX -O2 -Wall -Wno-unused-value
5-
CLIBS = -lpthread -ldl
3+
OBJS = pseudo_node.o
4+
CFLAGS = -std=gnu99 -DLINUX -O2 -Wall -Wno-unused-value -fpic
5+
CLIBS = -lpthread -ldl -Wl,-R.
66

7-
pseudonode: $(OBJS)
8-
$(CC) $(CFLAGS) -o pseudonode $(OBJS) $(CLIBS)
7+
pseudonode: libpseudonode.so main.o
8+
$(CC) $(CFLAGS) -o pseudonode main.o $(CLIBS) -L. libpseudonode.so
99
$(STRIP) pseudonode
1010
cp pseudonode pseudonode.linux
1111

12+
libpseudonode.so: $(OBJS)
13+
$(CC) -shared -o libpseudonode.so $(OBJS)
14+
1215
clean:
13-
rm -f $(OBJS) pseduonode
16+
rm -f $(OBJS) main.o libpseudonode.so pseduonode
1417

Makefile.windows

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
CC = x86_64-w64-mingw32-gcc
22
STRIP = x86_64-w64-mingw32-strip
3-
OBJS = pseudo_node.o sha256.o
4-
CFLAGS = -std=gnu99 -DWINDOWS -O2 -Wno-unused-value -mthreads
3+
OBJS = pseudo_node.o
4+
CFLAGS = -std=gnu99 -DWINDOWS -O2 -Wno-unused-value -mthreads -mconsole -fpic
55
CLIBS = -lws2_32 -lkernel32
66

7-
PseudoNode.exe: $(OBJS)
8-
$(CC) $(CFLAGS) -o PseudoNode.exe $(OBJS) $(CLIBS)
7+
PseudoNode.exe: PseudoNode.dll main.o
8+
$(CC) $(CFLAGS) -o PseudoNode.exe main.o $(OBJS) $(CLIBS)
99
$(STRIP) PseudoNode.exe
1010

11+
PseudoNode.dll: $(OBJS)
12+
$(CC) $(CFLAGS) --shared -o PseudoNode.dll $(OBJS) $(CLIBS)
13+
1114
pseudo_node.o: pseudo_node.c windows.c
1215

1316
clean:
14-
rm -f $(OBJS) node
17+
rm -f $(OBJS) main.o PseudoNode.dll PseudoNode.exe
1518

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.5.0
1+
0.6.0

linux.c

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,13 @@
4242
#define MAP_ANONYMOUS MAP_ANON
4343
#endif
4444

45-
static bool use_color = false;
46-
47-
#define color_clear(stream) if (use_color) fputs("\33[0m", stream)
48-
#define color_error(stream) if (use_color) fputs("\33[31m", stream)
49-
#define color_warning(stream) if (use_color) fputs("\33[33m", stream)
50-
#define color_log(stream) if (use_color) fputs("\33[32m", stream)
45+
#define get_error() strerror(errno)
5146

5247
static bool system_init(void)
5348
{
54-
use_color = isatty(1); // stdout
5549
return true;
5650
}
5751

58-
#define get_error() strerror(errno)
59-
6052
static bool rand_init(uint64_t *r)
6153
{
6254
FILE *stream = fopen("/dev/urandom", "r");
@@ -134,9 +126,7 @@ static bool event_wait(event *e)
134126
ts.tv_sec = tv.tv_sec;
135127
ts.tv_nsec = 1000 * tv.tv_usec;
136128
#endif
137-
ts.tv_nsec += rand64() % 1000000000;
138-
ts.tv_sec += 1 + ts.tv_nsec / 1000000000;
139-
ts.tv_nsec = ts.tv_nsec % 1000000000;
129+
ts.tv_sec += 2;
140130
mutex_lock(&e->mutex);
141131
while (!e->set)
142132
{
@@ -224,12 +214,12 @@ static sock socket_accept(sock s, struct in6_addr *addr)
224214
return s1;
225215
}
226216

227-
static bool socket_connect(sock s, struct in6_addr addr)
217+
static bool socket_connect(sock s, struct in6_addr addr, uint16_t port)
228218
{
229219
struct sockaddr_in6 sockaddr;
230220
memset(&sockaddr, 0, sizeof(sockaddr));
231221
sockaddr.sin6_family = AF_INET6;
232-
sockaddr.sin6_port = PORT;
222+
sockaddr.sin6_port = port;
233223
sockaddr.sin6_addr = addr;
234224
int flags = fcntl(s, F_GETFL, 0);
235225
if (flags < 0 || fcntl(s, F_SETFL, (flags | O_NONBLOCK)) != 0)
@@ -240,8 +230,8 @@ static bool socket_connect(sock s, struct in6_addr addr)
240230
if (fcntl(s, F_SETFL, flags) != 0)
241231
return false;
242232
struct timeval tv;
243-
tv.tv_sec = 5;
244-
tv.tv_usec = rand64() % 1000000;
233+
tv.tv_sec = 6;
234+
tv.tv_usec = 0;
245235
fd_set fds;
246236
FD_ZERO(&fds);
247237
FD_SET(s, &fds);
@@ -255,8 +245,8 @@ static ssize_t socket_recv(sock s, void *buf, size_t len, bool *timeout)
255245
{
256246
*timeout = false;
257247
struct timeval tv;
258-
tv.tv_sec = 1;
259-
tv.tv_usec = rand64() % 1000000;
248+
tv.tv_sec = 2;
249+
tv.tv_usec = 0;
260250
fd_set fds;
261251
FD_ZERO(&fds);
262252
FD_SET(s, &fds);
@@ -290,33 +280,3 @@ static void socket_close(sock s, bool err)
290280
close(s);
291281
}
292282

293-
static void server(void)
294-
{
295-
daemon(1, 0);
296-
297-
struct passwd *entry = getpwnam("nobody");
298-
if (entry == NULL)
299-
return;
300-
setgid(entry->pw_gid);
301-
setuid(entry->pw_uid);
302-
}
303-
304-
static void *system_alloc(size_t size)
305-
{
306-
size_t pagesize = sysconf(_SC_PAGESIZE);
307-
size = (((size-1) / pagesize) + 1) * pagesize;
308-
void *ptr = mmap(NULL, size, PROT_READ | PROT_WRITE,
309-
MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
310-
if (ptr == MAP_FAILED)
311-
return NULL;
312-
return ptr;
313-
}
314-
315-
static void system_free(size_t size, void *ptr)
316-
{
317-
size_t pagesize = sysconf(_SC_PAGESIZE);
318-
size = (((size-1) / pagesize) + 1) * pagesize;
319-
int res = munmap(ptr, size);
320-
assert(res == 0);
321-
}
322-

0 commit comments

Comments
 (0)