hello and ack exchange
This commit is contained in:
107
src/main.c
107
src/main.c
@@ -84,53 +84,6 @@ static bool GenerateTestMinerIdentity(uint8_t privateKey[32], uint8_t compressed
|
||||
return false;
|
||||
}
|
||||
|
||||
static 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';
|
||||
}
|
||||
|
||||
static bool MineBlock(block_t* block) {
|
||||
if (!block) {
|
||||
return false;
|
||||
@@ -148,40 +101,6 @@ static bool MineBlock(block_t* block) {
|
||||
}
|
||||
}
|
||||
|
||||
static 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 bool FlushChainAndSheet(blockchain_t* chain,
|
||||
const char* chainDataDir,
|
||||
uint256_t currentSupply,
|
||||
@@ -507,7 +426,7 @@ int main(int argc, char* argv[]) {
|
||||
char supplyStr[80];
|
||||
Uint256ToDecimal(¤tSupply, supplyStr, sizeof(supplyStr));
|
||||
printf("Current chain has %zu blocks, total supply %s\n", Chain_Size(chain), supplyStr);
|
||||
printf("Commands: mine <x>, send <address> <amount>, balance [address], flushchain, fullverify, wipechain, genaddr, exit\n");
|
||||
printf("Commands: mine <x>, send <address> <amount>, balance [address], connect <ipv4>, flushchain, fullverify, wipechain, genaddr, exit\n");
|
||||
|
||||
char line[1024];
|
||||
while (true) {
|
||||
@@ -682,6 +601,28 @@ int main(int argc, char* argv[]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strcmp(cmd, "connect") == 0) {
|
||||
char* ipStr = strtok(NULL, " \t");
|
||||
char* extra = strtok(NULL, " \t");
|
||||
if (!ipStr || extra) {
|
||||
printf("usage: connect <ipv4>\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!IsValidIPv4(ipStr)) {
|
||||
printf("invalid IPv4 address\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (Node_ConnectPeer(node, ipStr, LISTEN_PORT) != 0) {
|
||||
printf("failed to connect to %s:%u\n", ipStr, (unsigned int)LISTEN_PORT);
|
||||
continue;
|
||||
}
|
||||
|
||||
printf("connect requested to %s:%u\n", ipStr, (unsigned int)LISTEN_PORT);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strcmp(cmd, "flushchain") == 0) {
|
||||
if (FlushChainAndSheet(chain, chainDataDir, currentSupply, currentReward)) {
|
||||
printf("chain flushed\n");
|
||||
@@ -758,7 +699,7 @@ int main(int argc, char* argv[]) {
|
||||
break;
|
||||
}
|
||||
|
||||
printf("Unknown command. Available: mine, send, balance, flushchain, fullverify, wipechain, genaddr, exit\n");
|
||||
printf("Unknown command. Available: mine, send, balance, connect, flushchain, fullverify, wipechain, genaddr, exit\n");
|
||||
}
|
||||
|
||||
(void)FlushChainAndSheet(chain, chainDataDir, currentSupply, currentReward);
|
||||
|
||||
@@ -218,15 +218,47 @@ void Node_Server_OnData(tcp_connection_t* client) {
|
||||
printf("Received HELLO from node %u: protoVersion=%u, blockHeight=%" PRIu64 "\n",
|
||||
client ? client->connectionId : 0U, protoVersion, blockHeight);
|
||||
|
||||
// Craft and send ACK_HELLO
|
||||
uint8_t ackBuf[100];
|
||||
uint8_t* ackData = ackBuf;
|
||||
size_t ackOffset = 0;
|
||||
memcpy(ackData + ackOffset, &protoVersion, sizeof(protoVersion));
|
||||
ackOffset += sizeof(protoVersion);
|
||||
memcpy(ackData + ackOffset, ¤tBlockHeight, sizeof(currentBlockHeight));
|
||||
ackOffset += sizeof(currentBlockHeight);
|
||||
|
||||
break;
|
||||
}
|
||||
case PACKET_TYPE_ACK_HELLO: {
|
||||
// This is illegal
|
||||
printf("Received unexpected ACK_HELLO packet from node %u\n", client ? client->connectionId : 0U);
|
||||
|
||||
// Send the error and kill the connection
|
||||
const char* msg = "You can't ACK_HELLO me! I'm a server!";
|
||||
Node_SendPacket(Node_FromConnection(client), client, PACKET_TYPE_ERROR, msg, strlen(msg));
|
||||
TcpConnection_RequestClose(client);
|
||||
return;
|
||||
}
|
||||
case PACKET_TYPE_FETCH_BLOCK:
|
||||
case PACKET_TYPE_BLOCK_DATA:
|
||||
case PACKET_TYPE_BROADCAST_BLOCK:
|
||||
case PACKET_TYPE_ACK_BLOCK:
|
||||
case PACKET_TYPE_BROADCAST_TX:
|
||||
case PACKET_TYPE_ACK_TX:
|
||||
case PACKET_TYPE_ERROR: {
|
||||
// Decode the message inside as text
|
||||
char* text = (char*)malloc(payloadLen + 1);
|
||||
if (!text) {
|
||||
return;
|
||||
}
|
||||
memcpy(text, payload, payloadLen);
|
||||
text[payloadLen] = '\0';
|
||||
printf("Received packet type %u from node %u with message: %s\n",
|
||||
(unsigned int)packetType, client ? client->connectionId : 0U, text);
|
||||
free(text);
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return;
|
||||
}
|
||||
@@ -273,14 +305,50 @@ void Node_Client_OnData(tcp_connection_t* client) {
|
||||
}
|
||||
|
||||
switch (packetType) {
|
||||
case PACKET_TYPE_HELLO:
|
||||
case PACKET_TYPE_HELLO: {
|
||||
// This is illegal
|
||||
printf("Received unexpected HELLO packet from node %u\n", client ? client->connectionId : 0U);
|
||||
|
||||
// Send the error and kill the connection
|
||||
const char* msg = "You can't HELLO me! I'm a client!";
|
||||
Node_SendPacket(Node_FromConnection(client), client, PACKET_TYPE_ERROR, msg, strlen(msg));
|
||||
TcpConnection_RequestClose(client);
|
||||
return;
|
||||
}
|
||||
case PACKET_TYPE_ACK_HELLO: {
|
||||
// Decode ACK_HELLO
|
||||
if (payloadLen < sizeof(uint32_t) + sizeof(uint64_t)) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t protoVersion;
|
||||
uint64_t blockHeight;
|
||||
memcpy(&protoVersion, payload, sizeof(protoVersion));
|
||||
memcpy(&blockHeight, payload + sizeof(protoVersion), sizeof(blockHeight));
|
||||
|
||||
printf("Received ACK_HELLO from node %u with protoVersion %u and blockHeight %lu\n", client ? client->connectionId : 0U, protoVersion, (unsigned long)blockHeight);
|
||||
break;
|
||||
}
|
||||
case PACKET_TYPE_FETCH_BLOCK:
|
||||
case PACKET_TYPE_BLOCK_DATA:
|
||||
case PACKET_TYPE_BROADCAST_BLOCK:
|
||||
case PACKET_TYPE_ACK_BLOCK:
|
||||
case PACKET_TYPE_BROADCAST_TX:
|
||||
case PACKET_TYPE_ACK_TX:
|
||||
case PACKET_TYPE_ERROR: {
|
||||
// Decode the message inside as text
|
||||
char* text = (char*)malloc(payloadLen + 1);
|
||||
if (!text) {
|
||||
return;
|
||||
}
|
||||
memcpy(text, payload, payloadLen);
|
||||
text[payloadLen] = '\0';
|
||||
printf("Received packet type %u from node %u with message: %s\n",
|
||||
(unsigned int)packetType, client ? client->connectionId : 0U, text);
|
||||
free(text);
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ int TxMempool_Insert(signed_transaction_t tx) {
|
||||
if (!txMempool) { return -1; }
|
||||
|
||||
uint8_t txHash[32];
|
||||
Transaction_CalculateHash(&tx.transaction, txHash);
|
||||
Transaction_CalculateHash(&tx, txHash);
|
||||
|
||||
key32_t key;
|
||||
memcpy(key.bytes, txHash, 32);
|
||||
|
||||
Reference in New Issue
Block a user