diff options
Diffstat (limited to 'src/extensions')
-rw-r--r-- | src/extensions/age_restriction/age_restriction.c | 2 | ||||
-rw-r--r-- | src/extensions/extensions.c | 83 | ||||
-rw-r--r-- | src/extensions/policy_brandt_vickrey_auction/policy_brandt_vickrey_auction.c | 225 |
3 files changed, 162 insertions, 148 deletions
diff --git a/src/extensions/age_restriction/age_restriction.c b/src/extensions/age_restriction/age_restriction.c index 3c38d0f8..58124250 100644 --- a/src/extensions/age_restriction/age_restriction.c +++ b/src/extensions/age_restriction/age_restriction.c @@ -153,7 +153,7 @@ struct TALER_Extension TE_age_restriction = { .manifest = &age_restriction_manifest, /* This extension is not a policy extension */ - .parse_policy_details = NULL, + .create_policy_details = NULL, .policy_get_handler = NULL, .policy_post_handler = NULL, }; diff --git a/src/extensions/extensions.c b/src/extensions/extensions.c index cfc10940..2ed973d9 100644 --- a/src/extensions/extensions.c +++ b/src/extensions/extensions.c @@ -357,31 +357,27 @@ TALER_extensions_load_manifests ( * Policy related */ -static char *fulfilment2str[] = { - [TALER_PolicyFulfilmentPending] = "Pending", - [TALER_PolicyFulfilmentSuccessTransfer] = "SuccessTransfer", - [TALER_PolicyFulfilmentSuccessRefreshable] = "SuccessRefreshable", - [TALER_PolicyFulfilmentFailureTransfer] = "FailureTransfer", - [TALER_PolicyFulfilmentFailureRefreshable] = "FailureRefreshable", - [TALER_PolicyFulfilmentTimeoutTransfer] = "TimeoutTransfer", - [TALER_PolicyFulfilmentTimeoutRefreshable] = "TimeoutRefreshable", +static char *fulfillment2str[] = { + [TALER_PolicyFulfillmentReady] = "Ready", + [TALER_PolicyFulfillmentSuccess] = "Success", + [TALER_PolicyFulfillmentFailure] = "Failure", + [TALER_PolicyFulfillmentTimeout] = "Timeout", + [TALER_PolicyFulfillmentInsufficient] = "Insufficient", }; const char * -TALER_policy_fulfilment_state_str ( - enum TALER_PolicyFulfilmentState state) +TALER_policy_fulfillment_state_str ( + enum TALER_PolicyFulfillmentState state) { - GNUNET_assert (TALER_PolicyFulfilmentStateMax >= state); - return fulfilment2str[state]; + GNUNET_assert (TALER_PolicyFulfillmentStateCount > state); + return fulfillment2str[state]; } enum GNUNET_GenericReturnValue -TALER_extensions_extract_meta_data_from_policy_details ( - const json_t *policy_details, - struct GNUNET_HashCode *serial, - struct GNUNET_TIME_Timestamp *deadline, - enum TALER_PolicyFulfilmentState *state_on_deadline, +TALER_extensions_create_policy_details ( + const json_t *policy_options, + struct TALER_PolicyDetails *details, const char **error_hint) { enum GNUNET_GenericReturnValue ret; @@ -391,14 +387,14 @@ TALER_extensions_extract_meta_data_from_policy_details ( *error_hint = NULL; - if ((NULL == policy_details) || - (! json_is_object (policy_details))) + if ((NULL == policy_options) || + (! json_is_object (policy_options))) { *error_hint = "invalid policy object"; return GNUNET_SYSERR; } - jtype = json_object_get (policy_details, "type"); + jtype = json_object_get (policy_options, "type"); if (NULL == jtype) { *error_hint = "no type in policy object"; @@ -414,7 +410,7 @@ TALER_extensions_extract_meta_data_from_policy_details ( extension = TALER_extensions_get_by_name (type); if ((NULL == extension) || - (NULL == extension->parse_policy_details)) + (NULL == extension->create_policy_details)) { GNUNET_break (0); GNUNET_log (GNUNET_ERROR_TYPE_WARNING, @@ -423,50 +419,13 @@ TALER_extensions_extract_meta_data_from_policy_details ( return GNUNET_NO; } - *deadline = GNUNET_TIME_UNIT_FOREVER_TS; - ret = extension->parse_policy_details (policy_details, - serial, - deadline, - state_on_deadline, - error_hint); - - GNUNET_assert ((TALER_PolicyFulfilmentTimeoutRefreshable == - *state_on_deadline) || - (TALER_PolicyFulfilmentTimeoutTransfer == - *state_on_deadline)); - + details->deadline = GNUNET_TIME_UNIT_FOREVER_TS; + ret = extension->create_policy_details (policy_options, + details, + error_hint); return ret; } -struct TALER_PolicyFulfilmentOutcome * -TALER_policy_fulfilment_outcome_new (size_t len) -{ - struct TALER_PolicyFulfilmentOutcome *out; - - out = GNUNET_new (struct TALER_PolicyFulfilmentOutcome); - out->len = len; - out->positions = GNUNET_new_array (len, - struct - TALER_PolicyFulfilmentOutcomePosition); - - return out; -} - - -void -TALER_policy_fulfilment_outcome_free ( - struct TALER_PolicyFulfilmentOutcome *outcome) -{ - if (NULL == outcome) - return; - - if (NULL != outcome->positions) - GNUNET_free (outcome->positions); - - GNUNET_free (outcome); -} - - /* end of extensions.c */ diff --git a/src/extensions/policy_brandt_vickrey_auction/policy_brandt_vickrey_auction.c b/src/extensions/policy_brandt_vickrey_auction/policy_brandt_vickrey_auction.c index bfd7b813..34c84bb4 100644 --- a/src/extensions/policy_brandt_vickrey_auction/policy_brandt_vickrey_auction.c +++ b/src/extensions/policy_brandt_vickrey_auction/policy_brandt_vickrey_auction.c @@ -31,7 +31,7 @@ #define MAX_RESULT_SIZE 10 * 1024 /* (public) configuration of this extension */ -/* TODO: these fields need to be set in the init handler */ +/* FIXME: these fields need to be set in the init handler */ static struct TALER_ExtensionPolicyBrandtVickreyAuctionConfig BV_config = { .max_bidders = 10, .max_prices = 10, @@ -82,8 +82,8 @@ struct transcript /* Hash of the auction */ struct GNUNET_HashCode h_auction; - /* (n-1) calculated serial_ids */ - struct GNUNET_HashCode *serial_ids; + /* (n-1) calculated hash_codes */ + struct GNUNET_HashCode *hash_codes; /* Type of auction, see libbrandt */ @@ -146,7 +146,7 @@ json_error (json_t **output, /* Create serial as H(bidder_pub, h_auction) */ static void -calculate_serial ( +calculate_hashcode ( struct GNUNET_CRYPTO_EddsaPublicKey *pub, struct GNUNET_HashCode *hc, struct GNUNET_HashCode *serial) @@ -171,7 +171,7 @@ calculate_serial ( * @param[out] jerror JSON output for errors * @return GNUNET_OK on succes * - * TODO: + * FIXME: * - parse and verify signatures */ static enum GNUNET_GenericReturnValue @@ -181,7 +181,7 @@ parse_transcript (const json_t *jtr, { json_t *auc; - // TODO: struct GNUNET_CRYPTO_EddsaSignature sig; + // FIXME: struct GNUNET_CRYPTO_EddsaSignature sig; GNUNET_assert (jtr); GNUNET_assert (tr); @@ -272,7 +272,7 @@ parse_transcript (const json_t *jtr, tr->n, struct GNUNET_CRYPTO_EddsaPublicKey); - tr->serial_ids = GNUNET_new_array ( + tr->hash_codes = GNUNET_new_array ( tr->n, struct GNUNET_HashCode); @@ -284,7 +284,7 @@ parse_transcript (const json_t *jtr, GNUNET_JSON_spec_end (), }; - /* TODO: cleanup */ + /* FIXME: cleanup */ if (GNUNET_OK != GNUNET_JSON_parse (val, spec, @@ -294,9 +294,9 @@ parse_transcript (const json_t *jtr, "bidder no %ld public key couldn't be parsed", idx + 1); - calculate_serial (&tr->bidder_pub[idx], - &tr->h_auction, - &tr->serial_ids[idx]); + calculate_hashcode (&tr->bidder_pub[idx], + &tr->h_auction, + &tr->hash_codes[idx]); } } @@ -316,7 +316,7 @@ parse_transcript (const json_t *jtr, return json_error (jerror, "not the right no. of messages found"); - /* TODO: parse and evaluate signatures */ + /* FIXME: parse and evaluate signatures */ } // Winners @@ -358,9 +358,9 @@ parse_transcript (const json_t *jtr, } } - // TODO: parse and evalue sig of seller + // FIXME: parse and evalue sig of seller -// TODO: check for max values +// FIXME: check for max values DONE: @@ -373,16 +373,18 @@ DONE: * * @param[in] root The original JSON transcript * @param[in,out] transcript The transcript object parsed so far - * @param[out] outcome Outcome object to fill + * @param[in/out] details Array of policy details, provided by the exchange + * @param[in] details_count number of elements in @e details * @param[out] result The JSON result from the program * @return GNUNET_OK on success * - * TODO: Make this resumable + * FIXME: Make this resumable */ static enum GNUNET_GenericReturnValue replay_transcript (const json_t*root, struct transcript *tr, - struct TALER_PolicyFulfilmentOutcome **outcome, + struct TALER_PolicyDetails *details, + size_t details_count, json_t **result) { enum GNUNET_GenericReturnValue ret = GNUNET_SYSERR; @@ -488,30 +490,33 @@ replay_transcript (const json_t*root, return json_error (result, "internal error"); } - // TODO: check each winner with tr->expected, if applicable + // FIXME: check each winner with tr->expected, if applicable - // Create the outcome object + /* First, set all details to default state and values */ + for (size_t i = 0; i< tr->n; i++) { - json_t *w; - size_t idx; - struct TALER_PolicyFulfilmentOutcomePosition *pos; + /* no fees for non-winners */ + TALER_amount_set_zero (details[i].policy_fee.currency, + &details[i].policy_fee); - *outcome = - TALER_policy_fulfilment_outcome_new (tr->n); + /* no transferable amounts (=> accumulated_total becomes refreshable) */ + TALER_amount_set_zero (details[i].transferable_amount.currency, + &details[i].transferable_amount); + + details[i].fulfillment_state = TALER_PolicyFulfillmentSuccess; + } - /* Set outcome for all bidders to a default value first */ - for (uint16_t i = 0; i<tr->n; i++) - { - pos = &((*outcome)->positions[i]); - pos->serial_id = tr->serial_ids[i]; - pos->has_new_amount = false; - pos->state = TALER_PolicyFulfilmentFailureRefreshable; - } + + /* Now, fill the winner details */ + { + json_t *w; + size_t idx; /* Set the outcome of the winners */ json_array_foreach (winners, idx, w) { + // TODO enum GNUNET_GenericReturnValue ret; const char *jerror; uint16_t bidder; @@ -540,7 +545,7 @@ replay_transcript (const json_t*root, LOG_PREFIX "couldn't parse output of replay program: %s\n", jerror); - ret = json_error (result, "internal error"); + ret = json_error (result, "internal error (replay)"); goto DONE; } @@ -549,19 +554,41 @@ replay_transcript (const json_t*root, GNUNET_log (GNUNET_ERROR_TYPE_ERROR, LOG_PREFIX "replay program sent a bidder out of range\n"); - ret = json_error (result, "internal error"); + ret = json_error (result, "internal error (bidder)"); goto DONE; } - // Fill the outcome position for this winning bidder. - pos = &((*outcome)->positions[idx]); - pos->has_new_amount = true; - pos->new_amount = price; - pos->state = TALER_PolicyFulfilmentSuccessTransfer; + // Fill the details for this winning bidder. + { + struct TALER_PolicyDetails *det = NULL; + struct GNUNET_HashCode *hc = &tr->hash_codes[idx]; + + /* Find the corresponding details */ + for (size_t i = 0; i < tr->n; i++) + { + if (GNUNET_CRYPTO_hash_cmp (hc, + &details[idx].hash_code)) + { + det = &details[idx]; + break; + } + } + + if (NULL == det) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + LOG_PREFIX + "Couldeln't find calculate hash code %s in details\n", + GNUNET_h2s (hc)); + ret = json_error (result, "internal error (details)"); + goto DONE; + } + + } } - // TODO: return own result object. - // TODO: sign the result by the exchange + // FIXME: return own result object. + // FIXME: sign the result by the exchange *result = json_copy (res); json_decref (res); } @@ -582,11 +609,6 @@ replay_transcript (const json_t*root, } DONE: - if (GNUNET_OK != ret) - { - TALER_policy_fulfilment_outcome_free (*outcome); - *outcome = NULL; - } return ret; } @@ -642,7 +664,7 @@ auction_load_config ( struct TALER_Extension *ext, json_t *jconfig) { - /* TODO: parse configuration */ + /* FIXME: parse configuration */ ext->enabled = true; return GNUNET_OK; } @@ -656,7 +678,7 @@ auction_policy_get_handler ( struct MHD_Connection *connection, const char *const args[]) { - /* TODO: return some meta-data about supported version, limits, etc.*/ + /* FIXME: return some meta-data about supported version, limits, etc.*/ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, LOG_PREFIX "auction_policy_get_handler not implemented yet\n"); @@ -672,55 +694,89 @@ auction_policy_get_handler ( /** * @brief implements the TALER_Extension.policy_post_handler * - * TODO: make this non-blocking + * FIXME: make this non-blocking */ enum GNUNET_GenericReturnValue auction_policy_post_handler ( const json_t *root, const char *const args[], - enum GNUNET_GenericReturnValue (*serial_id_check)(struct GNUNET_HashCode - serial_ids[], size_t size), - struct TALER_PolicyFulfilmentOutcome **outcome, + struct TALER_PolicyDetails *details, + size_t details_len, json_t **output) { - struct transcript tr = {}; + struct transcript tr = {0}; enum GNUNET_GenericReturnValue ret; - *outcome = NULL; - ret = parse_transcript (root, - &tr, - output); + do { + ret = parse_transcript (root, + &tr, + output); - /* TODO: cleanups! */ - if (GNUNET_OK != ret) - return ret; + if (GNUNET_OK != ret) + break; - serial_id_check (tr.serial_ids, tr.n); + /* Compare the calculated hash_codes of policies with the provided onces */ + { + if (details_len != tr.n) + { + ret = json_error (output, "wrong number of bidders"); + break; + } - return replay_transcript (root, - &tr, - outcome, - output); + for (size_t i = 0; i<details_len; i++) + { + bool found = false; + for (size_t j = 0; j<tr.n; j++) + { + if (GNUNET_CRYPTO_hash_cmp (&details[i].hash_code, + &tr.hash_codes[j])) + { + found = true; + break; + } + } + if (! found) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "details[%ld].hash_code %s not found in transcript\n", + i, + GNUNET_h2s (&details[i].hash_code)); + ret = json_error (output, "internal error (hash_code)"); + goto END; + } + } + } + + ret = replay_transcript (root, + &tr, + details, + details_len, + output); + } while(0); + +END: + GNUNET_free (tr.prices); + GNUNET_free (tr.bidder_pub); + GNUNET_free (tr.hash_codes); + GNUNET_free (tr.expected); + + return ret; } /** - * @brief implements the TALER_Extensions.parse_policy_details interface. + * @brief implements the TALER_Extensions.create_policy_details interface. * * @param[in] input The policy_details for this handler during deposit - * @param[out] serial On success will contain the serial-ID under which the - * @param[out] deadline On success will contain a deadline, might be "forever" - * @param[out] state_on_timeout On success, will be set to the default state that the policy shall be put in case of a timeout. - * @param[out] error_hint On error, will contain a hint - * exchange should store the policy_details in the policy_details table. + * @param[out] details On success will contain the details to the policy + * @param[out] error_hint On error, will contain a hint exchange should store + * the policy_details in the policy_details table. * @return GNUNET_OK if the request was OK */ enum GNUNET_GenericReturnValue -auction_parse_policy_details ( +auction_create_policy_details ( const json_t *input, - struct GNUNET_HashCode *serial, - struct GNUNET_TIME_Timestamp *deadline, - enum TALER_PolicyFulfilmentState *state_on_timeout, + struct TALER_PolicyDetails *details, const char **error_hint) { enum GNUNET_GenericReturnValue ret = GNUNET_NO; @@ -730,13 +786,15 @@ auction_parse_policy_details ( /* We ignore the "type" field as it must have been parsed already upstream * - or this handler wouldn't have been called in first place. */ GNUNET_JSON_spec_fixed_auto ("bidder_pub", &pub), + TALER_JSON_spec_amount ("commitment", + BV_config.auction_fee.currency, + &details->commitment), GNUNET_JSON_spec_fixed_auto ("h_auction", &hc), - GNUNET_JSON_spec_timestamp ("deadline", deadline), + GNUNET_JSON_spec_timestamp ("deadline", &details->deadline), GNUNET_JSON_spec_end (), }; - GNUNET_assert (serial); - GNUNET_assert (deadline); + GNUNET_assert (details); error_hint = NULL; @@ -752,16 +810,13 @@ auction_parse_policy_details ( /* FIXME: check the deadline */ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, LOG_PREFIX "check of deadline %ld not implemented!\n", - deadline->abs_time.abs_value_us); + details->deadline.abs_time.abs_value_us); - calculate_serial (&pub, &hc, serial); + calculate_hashcode (&pub, &hc, &details->hash_code); ret = GNUNET_OK; } while(0); - /* In case of timeout, the coin shall be refreshable by the owner */ - *state_on_timeout = TALER_PolicyFulfilmentTimeoutRefreshable; - return ret; } @@ -777,7 +832,7 @@ struct TALER_Extension TE_auction_brandt = { .disable = &auction_disable, .load_config = &auction_load_config, .manifest = &auction_manifest, - .parse_policy_details = &auction_parse_policy_details, + .create_policy_details = &auction_create_policy_details, .policy_get_handler = &auction_policy_get_handler, .policy_post_handler = &auction_policy_post_handler, }; @@ -847,7 +902,7 @@ libtaler_extension_policy_brandt_vickrey_auction_init (void *arg) LOG_PREFIX "loading... using replay_program '%s'\n", replay_program); - /* TODO: read other config parameters and generate configuration */ + /* FIXME: read other config parameters and generate configuration */ return &TE_auction_brandt; } |