/*
This file is part of GNUnet.
(C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
-
+
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 3, or (at your
option) any later version.
-
+
GNUnet is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
-
+
You should have received a copy of the GNU General Public License
along with GNUnet; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* this can calculate the expected number of peers in the network.
*/
#include "platform.h"
+#include <math.h>
#include "gnunet_util_lib.h"
#include "gnunet_constants.h"
#include "gnunet_protocols.h"
/**
* Over how many values do we calculate the weighted average?
*/
-#define HISTORY_SIZE 8
-
-/**
- * Size of the queue to core.
- */
-#define CORE_QUEUE_SIZE 2
+#define HISTORY_SIZE 64
/**
* Message priority to use.
*/
#define NSE_PRIORITY 5
+#if FREEBSD
+#define log2(a) (log(a)/log(2))
+#endif
+
/**
* Amount of work required (W-bit collisions) for NSE proofs, in collision-bits.
*/
/**
* Number of hops this message has taken so far.
*/
- uint32_t hop_count;
+ uint32_t hop_count GNUNET_PACKED;
/**
* Purpose.
* of timestamp and the initiator's public
* key.
*/
- uint32_t matching_bits;
+ uint32_t matching_bits GNUNET_PACKED;
/**
* Public key of the originator.
/**
* Proof of work, causing leading zeros when hashed with pkey.
*/
- uint64_t proof_of_work;
+ uint64_t proof_of_work GNUNET_PACKED;
/**
* Signature (over range specified in purpose).
/**
* The current network size estimate. Number of bits matching on
- * average thus far.
+ * average thus far.
*/
static double current_size_estimate;
setup_estimate_message (struct GNUNET_NSE_ClientMessage *em)
{
unsigned int i;
+ unsigned int j;
double mean;
double sum;
double std_dev;
double variance;
double val;
- double weight;
+ double nsize;
+
+#define WEST 1
+ /* Weighted incremental algorithm for stddev according to West (1979) */
+#if WEST
double sumweight;
+ double weight;
double q;
double r;
double temp;
- /* Weighted incremental algorithm for stddev according to West (1979) */
mean = 0.0;
sum = 0.0;
sumweight = 0.0;
+ variance = 0.0;
for (i = 0; i < estimate_count; i++)
{
- val =
- htonl (size_estimate_messages
- [(estimate_index - i +
- HISTORY_SIZE) % HISTORY_SIZE].matching_bits);
- weight = 1; /* was: estimate_count + 1 - i; */
+ j = (estimate_index - i + HISTORY_SIZE) % HISTORY_SIZE;
+ val = htonl (size_estimate_messages[j].matching_bits);
+ weight = estimate_count + 1 - i;
temp = weight + sumweight;
q = val - mean;
r = q * weight / temp;
- sum += sumweight * q * r;
mean += r;
+ sum += sumweight * q * r;
sumweight = temp;
}
- variance = sum / (sumweight - 1.0);
- GNUNET_assert (variance >= 0);
- std_dev = sqrt (variance);
+ if (estimate_count > 0)
+ variance = (sum / sumweight) * estimate_count / (estimate_count - 1.0);
+#else
+ /* trivial version for debugging */
+ double vsq;
+
+ /* non-weighted trivial version */
+ sum = 0.0;
+ vsq = 0.0;
+ variance = 0.0;
+ mean = 0.0;
+
+ for (i = 0; i < estimate_count; i++)
+ {
+ j = (estimate_index - i + HISTORY_SIZE) % HISTORY_SIZE;
+ val = htonl (size_estimate_messages[j].matching_bits);
+ sum += val;
+ vsq += val * val;
+ }
+ if (0 != estimate_count)
+ {
+ mean = sum / estimate_count;
+ variance = (vsq - mean * sum) / (estimate_count - 1.0); // terrible for numerical stability...
+ }
+#endif
+ if (variance >= 0)
+ std_dev = sqrt (variance);
+ else
+ std_dev = variance; /* must be infinity due to estimate_count == 0 */
current_std_dev = std_dev;
current_size_estimate = mean;
em->header.type = htons (GNUNET_MESSAGE_TYPE_NSE_ESTIMATE);
em->reserved = htonl (0);
em->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
- em->size_estimate = mean - 1.0 / 3.0;
+ em->size_estimate = mean - 0.332747;
+ nsize = log2 (GNUNET_CONTAINER_multihashmap_size (peers) + 1);
+ if (em->size_estimate < nsize)
+ em->size_estimate = nsize;
em->std_deviation = std_dev;
GNUNET_STATISTICS_set (stats, "# nodes in the network (estimate)",
(uint64_t) pow (2, mean - 1.0 / 3.0), GNUNET_NO);
* What delay randomization should we apply for a given number of matching bits?
*
* @param matching_bits number of matching bits
- * @return random delay to apply
+ * @return random delay to apply
*/
static struct GNUNET_TIME_Relative
get_delay_randomization (uint32_t matching_bits)
{
#if USE_RANDOM_DELAYS
struct GNUNET_TIME_Relative ret;
+ uint32_t i;
+ double d;
if (matching_bits == 0)
return GNUNET_TIME_UNIT_ZERO;
- ret.rel_value =
- GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
- (uint32_t) (get_matching_bits_delay
- (matching_bits -
- 1) / (double) (hop_count_max +
- 1)));
+ d = get_matching_bits_delay (matching_bits - 1);
+ i = (uint32_t) (d / (double) (hop_count_max + 1));
+ ret.rel_value = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, i + 1);
return ret;
#else
return GNUNET_TIME_UNIT_ZERO;
/**
- * Get the transmission delay that should be applied for a
+ * Get the transmission delay that should be applied for a
* particular round.
*
* @param round_offset -1 for the previous round (random delay between 0 and 50ms)
* @param cls the 'struct NSEPeerEntry'
* @param tc scheduler context
*/
-static void transmit_task (void *cls,
- const struct GNUNET_SCHEDULER_TaskContext *tc);
+static void
+transmit_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
/**
1, GNUNET_NO);
return 0;
}
+ if (ntohs (size_estimate_messages[idx].header.size) == 0)
+ {
+ GNUNET_STATISTICS_update (stats,
+ "# flood messages not generated (lack of history)",
+ 1, GNUNET_NO);
+ return 0;
+ }
#if DEBUG_NSE
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"In round %llu, sending to `%s' estimate with %u bits\n",
(unsigned long long)
- GNUNET_TIME_absolute_ntoh (size_estimate_messages[idx].timestamp).
- abs_value, GNUNET_i2s (&peer_entry->id),
+ GNUNET_TIME_absolute_ntoh (size_estimate_messages[idx].
+ timestamp).abs_value,
+ GNUNET_i2s (&peer_entry->id),
(unsigned int) ntohl (size_estimate_messages[idx].matching_bits));
#endif
if (ntohl (size_estimate_messages[idx].hop_count) == 0)
fm->timestamp = GNUNET_TIME_absolute_hton (ts);
fm->pkey = my_public_key;
fm->proof_of_work = my_proof;
- GNUNET_CRYPTO_rsa_sign (my_private_key, &fm->purpose, &fm->signature);
+ if (nse_work_required > 0)
+ GNUNET_assert (GNUNET_OK ==
+ GNUNET_CRYPTO_rsa_sign (my_private_key, &fm->purpose,
+ &fm->signature));
+ else
+ memset (&fm->signature, 0, sizeof (fm->signature));
}
unsigned int i;
flood_task = GNUNET_SCHEDULER_NO_TASK;
+ if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+ return;
offset = GNUNET_TIME_absolute_get_remaining (next_timestamp);
if (0 != offset.rel_value)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Proof of work found: %llu!\n",
(unsigned long long) GNUNET_ntohll (counter));
#endif
- for (i = 0; i < HISTORY_SIZE; i++)
- if (ntohl (size_estimate_messages[i].hop_count) == 0)
- {
- size_estimate_messages[i].proof_of_work = my_proof;
- GNUNET_CRYPTO_rsa_sign (my_private_key,
- &size_estimate_messages[i].purpose,
- &size_estimate_messages[i].signature);
- }
write_proof ();
+ setup_flood_message (estimate_index, current_timestamp);
return;
}
counter++;
incoming_flood->proof_of_work))
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Proof of work invalid: %llu!\n"),
- (unsigned long long) GNUNET_ntohll (incoming_flood->
- proof_of_work));
+ (unsigned long long)
+ GNUNET_ntohll (incoming_flood->proof_of_work));
GNUNET_break_op (0);
return GNUNET_NO;
}
- if (GNUNET_OK !=
- GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_NSE_SEND,
- &incoming_flood->purpose,
- &incoming_flood->signature,
- &incoming_flood->pkey))
+ if ((nse_work_required > 0) &&
+ (GNUNET_OK !=
+ GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_NSE_SEND,
+ &incoming_flood->purpose,
+ &incoming_flood->signature,
+ &incoming_flood->pkey)))
{
GNUNET_break_op (0);
return GNUNET_NO;
return GNUNET_OK; /* trigger of the update */
if (peer_entry->previous_round == GNUNET_NO)
{
- /* still stuck in previous round, no point to update, check that
+ /* still stuck in previous round, no point to update, check that
* we are active here though... */
GNUNET_break ((peer_entry->transmit_task != GNUNET_SCHEDULER_NO_TASK) ||
(peer_entry->th != NULL));
* @param message message
* @param peer peer identity this message is from (ignored)
* @param atsi performance data (ignored)
- *
+ * @param atsi_count number of records in 'atsi'
*/
static int
handle_p2p_size_estimate (void *cls, const struct GNUNET_PeerIdentity *peer,
const struct GNUNET_MessageHeader *message,
- const struct GNUNET_TRANSPORT_ATS_Information *atsi)
+ const struct GNUNET_ATS_Information *atsi,
+ unsigned int atsi_count)
{
const struct GNUNET_NSE_FloodMessage *incoming_flood;
struct GNUNET_TIME_Absolute ts;
GNUNET_snprintf (pred, sizeof (pred), "%s", GNUNET_i2s (peer));
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Flood at %llu from `%s' via `%s' at `%s' with bits %u\n",
- (unsigned long long) GNUNET_TIME_absolute_ntoh (incoming_flood->
- timestamp).
- abs_value, origin, pred, GNUNET_i2s (&my_identity),
+ (unsigned long long)
+ GNUNET_TIME_absolute_ntoh (incoming_flood->timestamp).abs_value,
+ origin, pred, GNUNET_i2s (&my_identity),
(unsigned int) matching_bits);
}
#endif
* @param cls closure
* @param peer peer identity this notification is about
* @param atsi performance data
+ * @param atsi_count number of records in 'atsi'
*/
static void
handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_TRANSPORT_ATS_Information *atsi)
+ const struct GNUNET_ATS_Information *atsi,
+ unsigned int atsi_count)
{
struct NSEPeerEntry *peer_entry;
#endif
peer_entry = GNUNET_malloc (sizeof (struct NSEPeerEntry));
peer_entry->id = *peer;
- GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_entry,
- GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
+ GNUNET_assert (GNUNET_OK ==
+ GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey,
+ peer_entry,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
peer_entry->transmit_task =
GNUNET_SCHEDULER_add_delayed (get_transmit_delay (-1), &transmit_task,
peer_entry);
+ GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
}
pos->th = NULL;
}
GNUNET_free (pos);
+ GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
}
* @param cls service closure
* @param server handle to the server for this service
* @param identity the public identity of this peer
- * @param publicKey the public key of this peer
*/
static void
core_init (void *cls, struct GNUNET_CORE_Handle *server,
- const struct GNUNET_PeerIdentity *identity,
- const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
+ const struct GNUNET_PeerIdentity *identity)
{
struct GNUNET_TIME_Absolute now;
struct GNUNET_TIME_Absolute prev_time;
- unsigned int i;
if (server == NULL)
{
gnunet_nse_interval.rel_value;
next_timestamp.abs_value =
current_timestamp.abs_value + gnunet_nse_interval.rel_value;
-
- for (i = 0; i < HISTORY_SIZE; i++)
+ estimate_index = HISTORY_SIZE - 1;
+ estimate_count = 0;
+ if (GNUNET_YES == check_proof_of_work (&my_public_key, my_proof))
{
prev_time.abs_value =
- current_timestamp.abs_value - (HISTORY_SIZE - i -
+ current_timestamp.abs_value - (estimate_index -
1) * gnunet_nse_interval.rel_value;
- setup_flood_message (i, prev_time);
+ setup_flood_message (estimate_index, prev_time);
+ estimate_count++;
}
- estimate_index = HISTORY_SIZE - 1;
- estimate_count = 2;
flood_task =
GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
(next_timestamp), &update_flood_message,
nc = GNUNET_SERVER_notification_context_create (server, 1);
/* Connect to core service and register core handlers */
coreAPI = GNUNET_CORE_connect (cfg, /* Main configuration */
- CORE_QUEUE_SIZE, /* queue size */
- NULL, /* Closure passed to functions */
+ 1, NULL, /* Closure passed to functions */
&core_init, /* Call core_init once connected */
&handle_core_connect, /* Handle connects */
&handle_core_disconnect, /* Handle disconnects */
- NULL, /* Do we care about "status" updates? */
NULL, /* Don't want notified about all incoming messages */
GNUNET_NO, /* For header only inbound notification */
NULL, /* Don't want notified about all outbound messages */