hello and ack exchange

This commit is contained in:
2026-04-25 20:28:38 +02:00
parent 32b9a57366
commit bd972bfab6
6 changed files with 236 additions and 92 deletions
+24 -83
View File
@@ -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(&currentSupply, 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);