private firstprice auction working in test_brandt

This commit is contained in:
Markus Teich 2016-09-08 20:09:22 +02:00
parent 14546eccb2
commit de5ea20b55
2 changed files with 74 additions and 62 deletions

125
brandt.c
View File

@ -78,8 +78,8 @@ BRANDT_bidder_start (struct BRANDT_Auction *auction,
} }
weprintf("broadcasting msg_init %p from bidder %d", buf, i); weprintf("broadcasting msg_init %p from bidder %d", buf, i);
if (0 == auction->bcast (auction->closure, buf, buflen)) auction->bcast (auction->closure, buf, buflen);
gcry_mpi_set_bit (auction->round_progress, auction->i); gcry_mpi_set_bit (auction->round_progress, auction->i);
free (buf); free (buf);
} }
@ -293,88 +293,89 @@ BRANDT_destroy (struct BRANDT_Auction *auction)
} }
static void
report_outcome (struct BRANDT_Auction *ad,
enum auction_type atype,
enum outcome_type outcome)
{
struct BRANDT_Result *res;
uint16_t reslen = 0;
if (!handler_res[atype][outcome] ||
!(res = handler_res[atype][outcome] (ad, &reslen)))
ad->result (ad->closure, NULL, 0);
else
ad->result (ad->closure, res, reslen);
}
static void static void
advance_round (struct BRANDT_Auction *ad, advance_round (struct BRANDT_Auction *ad,
enum auction_type atype, enum auction_type atype,
enum outcome_type outcome) enum outcome_type outcome)
{ {
struct BRANDT_Result *res;
uint16_t reslen = 0;
unsigned char *buf; unsigned char *buf;
size_t buflen; size_t buflen;
/* if we did not got the current round message from all participants, don't if (!ad->seller_mode && msg_decrypt == ad->cur_round)
* advance to the next round yet. Early return, since we have to wait for
* the other participants messages. In the msg_decrypt round as a bidder we
* only need the one message from the seller. */
for (uint16_t i = 0; i < ad->n; i++)
if (!gcry_mpi_test_bit (ad->round_progress, i) &&
(ad->seller_mode || msg_decrypt != ad->cur_round))
return;
/* Do not advance to the next round if we are the seller and got all
* msg_decrypt messages from the bidders. We need to broadcast our combined
* msg_decrypt message back to the sellers first */
if (!ad->seller_mode || msg_decrypt != ad->cur_round)
{ {
/* clear old round progress and advance to next one */ /* we are a bidder and successfully parsed the msg_decrypt from the
gcry_mpi_clear_highbit (ad->round_progress, 0); * seller => we can determine the auction result */
ad->cur_round++; report_outcome (ad, atype, outcome);
return;
/* prepare next round. */
if (handler_prep[atype][outcome][ad->cur_round])
handler_prep[atype][outcome][ad->cur_round] (ad);
/** \todo: setup round timeout trigger */
if (ad->seller_mode)
{
}
} }
/* send next message if we are not done yet */ /* only continue if the round is complete */
if ((!ad->seller_mode && msg_last > ad->cur_round) || for (uint16_t i = 0; i < ad->n; i++)
(ad->seller_mode && msg_decrypt == ad->cur_round && if (!gcry_mpi_test_bit (ad->round_progress, i))
gcry_mpi_test_bit (ad->round_progress, 0))) return;
if (ad->seller_mode && msg_decrypt == ad->cur_round)
{ {
/* all bidders msg_decrypt received, broadcast combined msg_decrypt */
if (!handler_out[atype][outcome][ad->cur_round] || if (!handler_out[atype][outcome][ad->cur_round] ||
!(buf = handler_out[atype][outcome][ad->cur_round](ad, &buflen))) !(buf = handler_out[atype][outcome][ad->cur_round](ad, &buflen)))
{ {
weprintf ("failed to create message %d buffer", ad->cur_round); weprintf ("failed to create msg %d buffer as seller",
ad->cur_round);
return; return;
} }
ad->bcast (ad->closure, buf, buflen);
/* msg_decrypt unicast to seller or broadcast from seller. /* now we are done as seller and can determine the outcome */
* All other messages are broadcasted */ report_outcome (ad, atype, outcome);
if (msg_decrypt == ad->cur_round && !ad->seller_mode) return;
{
if (0 == ad->ucast (ad->closure, buf, buflen))
gcry_mpi_set_bit (ad->round_progress, ad->i);
}
else
{
if (!ad->seller_mode && 0 == ad->bcast (ad->closure, buf, buflen))
gcry_mpi_set_bit (ad->round_progress, ad->i);
}
} }
if (msg_last == ad->cur_round || /* round complete, advance to next one */
(msg_decrypt == ad->cur_round && ad->seller_mode && gcry_mpi_clear_highbit (ad->round_progress, 0);
gcry_mpi_test_bit (ad->round_progress, 0))) ad->cur_round++;
/* prepare next round. */
if (handler_prep[atype][outcome][ad->cur_round])
handler_prep[atype][outcome][ad->cur_round] (ad);
if (ad->seller_mode)
{ {
/* done with all rounds, determine outcome here */ /** \todo: setup round timeout trigger */
if (!handler_res[atype][outcome] || /* seller does not send regular messages */
!(res = handler_res[atype][outcome] (ad, &reslen))) return;
{
weprintf ("failed to determine result");
/** \todo: call result with null pointer here? */
return;
}
ad->result (ad->closure, res, reslen);
/* seller still needs to broadcast the decryption to all bidders */
if (!ad->seller_mode)
return;
} }
/* create next message buffer */
if (!handler_out[atype][outcome][ad->cur_round] ||
!(buf = handler_out[atype][outcome][ad->cur_round](ad, &buflen)))
{
weprintf ("failed to create msg %d buffer as bidder", ad->cur_round);
return;
}
/* msg_decrypt unicast to seller. All other messages are broadcasted */
if (msg_decrypt == ad->cur_round)
ad->ucast (ad->closure, buf, buflen);
else
ad->bcast (ad->closure, buf, buflen);
gcry_mpi_set_bit (ad->round_progress, ad->i);
} }

View File

@ -133,6 +133,17 @@ cb_result (void *auction_closure,
struct BRANDT_Result results[], struct BRANDT_Result results[],
uint16_t results_len) uint16_t results_len)
{ {
uint16_t *s = (uint16_t *)auction_closure;
if (0 == results_len)
weprintf ("result determined by agent %d: none", *s);
for (uint16_t i = 0; i < results_len; i++)
weprintf ("result determined by agent %d: bidder %d got status %d with price %d",
*s,
results[i].bidder,
results[i].status,
results[i].price);
} }