hello and ack exchange
This commit is contained in:
@@ -6,13 +6,15 @@
|
||||
typedef enum {
|
||||
PACKET_TYPE_NONE = 0,
|
||||
PACKET_TYPE_HELLO = 1, // Hello, let's connect! Here's who I am, and what I have!
|
||||
PACKET_TYPE_FETCH_BLOCK = 2, // I want a Block
|
||||
PACKET_TYPE_BLOCK_DATA = 3, // Here's a Block (response to fetch) - I don't care what you do with it, but you wanted it, so here it is!
|
||||
PACKET_TYPE_BROADCAST_BLOCK = 4, // Here's a new Block I want to share with the network (unsolicited - e.g. just mined it)
|
||||
PACKET_TYPE_ACK_BLOCK = 5, // I have received your block, here's what I did with it (response to broadcast)
|
||||
PACKET_TYPE_BROADCAST_TX = 6, // Here's a new transaction I want to share with the network
|
||||
PACKET_TYPE_ACK_TX = 7, // I have received your transaction, here's what I did with it (response to broadcast)
|
||||
PACKET_TYPE_MAX = 9
|
||||
PACKET_TYPE_ACK_HELLO = 2, // I received your hello, here's who I am and what I have (response to hello)
|
||||
PACKET_TYPE_FETCH_BLOCK = 3, // I want a Block
|
||||
PACKET_TYPE_BLOCK_DATA = 4, // Here's a Block (response to fetch) - I don't care what you do with it, but you wanted it, so here it is!
|
||||
PACKET_TYPE_BROADCAST_BLOCK = 5, // Here's a new Block I want to share with the network (unsolicited - e.g. just mined it)
|
||||
PACKET_TYPE_ACK_BLOCK = 6, // I have received your block, here's what I did with it (response to broadcast)
|
||||
PACKET_TYPE_BROADCAST_TX = 7, // Here's a new transaction I want to share with the network
|
||||
PACKET_TYPE_ACK_TX = 8, // I have received your transaction, here's what I did with it (response to broadcast)
|
||||
PACKET_TYPE_ERROR = 9, // Something went wrong with the packet you sent me, here's an error message (can be response to any packet)
|
||||
PACKET_TYPE_MAX = 10
|
||||
} packet_type_t;
|
||||
|
||||
static inline int PacketType_IsValid(uint8_t packetType) {
|
||||
|
||||
@@ -10,6 +10,7 @@ KHASH_INIT(tx_mempool_map_m, key32_t, signed_transaction_t, 1, hash_key32, eq_ke
|
||||
extern khash_t(tx_mempool_map_m)* txMempool;
|
||||
|
||||
void TxMempool_Init();
|
||||
// Assumed that the transation was confirmed to be valid
|
||||
int TxMempool_Insert(signed_transaction_t tx);
|
||||
bool TxMempool_Lookup(uint8_t* txHash, signed_transaction_t* out);
|
||||
void TxMempool_Print();
|
||||
|
||||
132
include/utils.h
132
include/utils.h
@@ -5,6 +5,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <uint256.h>
|
||||
|
||||
typedef struct {
|
||||
uint8_t bytes[32];
|
||||
@@ -30,4 +31,135 @@ static inline void AddressToHexString(const uint8_t address[32], char out[65]) {
|
||||
to_hex(address, out);
|
||||
}
|
||||
|
||||
static inline bool ParseHexAddress32(const char* in, uint8_t outAddress[32]) {
|
||||
if (!in || !outAddress) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* p = in;
|
||||
if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
|
||||
p += 2;
|
||||
}
|
||||
|
||||
if (strlen(p) != 64) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < 32; ++i) {
|
||||
char hi = p[i * 2];
|
||||
char lo = p[i * 2 + 1];
|
||||
int hiVal = (hi >= '0' && hi <= '9') ? (hi - '0') :
|
||||
(hi >= 'a' && hi <= 'f') ? (10 + hi - 'a') :
|
||||
(hi >= 'A' && hi <= 'F') ? (10 + hi - 'A') : -1;
|
||||
int loVal = (lo >= '0' && lo <= '9') ? (lo - '0') :
|
||||
(lo >= 'a' && lo <= 'f') ? (10 + lo - 'a') :
|
||||
(lo >= 'A' && lo <= 'F') ? (10 + lo - 'A') : -1;
|
||||
|
||||
if (hiVal < 0 || loVal < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
outAddress[i] = (uint8_t)((hiVal << 4) | loVal);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline bool IsValidIPv4(const char* ip) {
|
||||
if (!ip || *ip == '\0') {
|
||||
return false;
|
||||
}
|
||||
|
||||
int octetCount = 0;
|
||||
const char* p = ip;
|
||||
|
||||
while (*p != '\0') {
|
||||
if (octetCount >= 4) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (*p < '0' || *p > '9') {
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned int value = 0;
|
||||
int digits = 0;
|
||||
while (*p >= '0' && *p <= '9') {
|
||||
value = (value * 10u) + (unsigned int)(*p - '0');
|
||||
if (value > 255u) {
|
||||
return false;
|
||||
}
|
||||
++digits;
|
||||
if (digits > 3) {
|
||||
return false;
|
||||
}
|
||||
++p;
|
||||
}
|
||||
|
||||
if (digits == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
++octetCount;
|
||||
if (octetCount < 4) {
|
||||
if (*p != '.') {
|
||||
return false;
|
||||
}
|
||||
++p;
|
||||
if (*p == '\0') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return octetCount == 4;
|
||||
}
|
||||
|
||||
static inline void Uint256ToDecimal(const uint256_t* value, char* out, size_t outSize) {
|
||||
if (!value || !out || outSize == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint64_t tmp[4] = {
|
||||
value->limbs[0],
|
||||
value->limbs[1],
|
||||
value->limbs[2],
|
||||
value->limbs[3]
|
||||
};
|
||||
|
||||
if (tmp[0] == 0 && tmp[1] == 0 && tmp[2] == 0 && tmp[3] == 0) {
|
||||
if (outSize >= 2) {
|
||||
out[0] = '0';
|
||||
out[1] = '\0';
|
||||
} else {
|
||||
out[0] = '\0';
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
char digits[80];
|
||||
size_t digitCount = 0;
|
||||
|
||||
while (tmp[0] != 0 || tmp[1] != 0 || tmp[2] != 0 || tmp[3] != 0) {
|
||||
uint64_t remainder = 0;
|
||||
for (int i = 3; i >= 0; --i) {
|
||||
__uint128_t cur = ((__uint128_t)remainder << 64) | tmp[i];
|
||||
tmp[i] = (uint64_t)(cur / 10u);
|
||||
remainder = (uint64_t)(cur % 10u);
|
||||
}
|
||||
|
||||
if (digitCount < sizeof(digits) - 1) {
|
||||
digits[digitCount++] = (char)('0' + remainder);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
size_t writeLen = (digitCount < (outSize - 1)) ? digitCount : (outSize - 1);
|
||||
for (size_t i = 0; i < writeLen; ++i) {
|
||||
out[i] = digits[digitCount - 1 - i];
|
||||
}
|
||||
out[writeLen] = '\0';
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user