add changelog
[oweals/gnunet.git] / src / auction / gnunet-auction-create.c
1 /*
2    This file is part of GNUnet.
3    Copyright (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 GNUnet e.V.
4
5    GNUnet is free software: you can redistribute it and/or modify it
6    under the terms of the GNU Affero General Public License as published
7    by the Free Software Foundation, either version 3 of the License,
8    or (at your option) any later version.
9
10    GNUnet is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Affero General Public License for more details.
14
15    You should have received a copy of the GNU Affero General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @file auction/gnunet-auction-create.c
23  * @brief tool to create a new auction
24  * @author Markus Teich
25  */
26 #include "platform.h"
27
28 #include <float.h>
29
30 #include "gnunet_util_lib.h"
31 #include <jansson.h>
32 /* #include "gnunet_auction_service.h" */
33
34 #define FIRST_PRICE 0
35 #define OUTCOME_PRIVATE 0
36 #define OUTCOME_PUBLIC 1
37
38 static int ret; /** Final status code. */
39 static char *fndesc; /** filename of the item description */
40 static char *fnprices; /** filename of the price map */
41 static struct GNUNET_TIME_Relative dround; /** max round duration */
42 static struct GNUNET_TIME_Relative dstart; /** time until auction starts */
43 static unsigned int m = FIRST_PRICE; /** auction parameter m */
44 static int outcome = OUTCOME_PRIVATE; /** outcome */
45 static int interactive; /** keep running in foreground */
46
47
48 /**
49  * Main function that will be run by the scheduler.
50  *
51  * @param cls closure
52  * @param args remaining command-line arguments
53  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
54  * @param cfg configuration
55  */
56 static void
57 run (void *cls,
58      char *const *args,
59      const char *cfgfile,
60      const struct GNUNET_CONFIGURATION_Handle *cfg)
61 {
62   unsigned int i;
63   double cur, prev = DBL_MAX;
64   json_t *pmap;
65   json_t *parray;
66   json_t *pnode;
67   json_error_t jerr;
68
69   /* cmdline parsing */
70   if (GNUNET_TIME_UNIT_ZERO.rel_value_us == dstart.rel_value_us)
71   {
72     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
73                 "required argument --regtime missing or invalid (zero)\n");
74     goto fail;
75   }
76   if (GNUNET_TIME_UNIT_ZERO.rel_value_us == dround.rel_value_us)
77   {
78     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
79                 "required argument --roundtime missing or invalid (zero)\n");
80     goto fail;
81   }
82   if (! fndesc)
83   {
84     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
85                 "required argument --description missing\n");
86     goto fail;
87   }
88   if (! fnprices)
89   {
90     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
91                 "required argument --pricemap missing\n");
92     goto fail;
93   }
94
95   /* parse and check pricemap validity */
96   if (! (pmap = json_load_file (fnprices, JSON_DECODE_INT_AS_REAL, &jerr)))
97   {
98     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
99                 "parsing pricemap json at %d:%d: %s\n",
100                 jerr.line, jerr.column, jerr.text);
101     goto fail;
102   }
103   if (-1 == json_unpack_ex (pmap, &jerr, JSON_VALIDATE_ONLY,
104                             "{s:s, s:[]}", "currency", "prices"))
105   {
106     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
107                 "validating pricemap: %s\n", jerr.text);
108     goto fail;
109   }
110   if (! (parray = json_object_get (pmap, "prices")) || ! json_is_array (parray))
111   {
112     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
113                 "could not get `prices` array node from pricemap\n");
114     goto fail;
115   }
116   if (0 == json_array_size (parray))
117   {
118     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "empty pricemap array\n");
119     goto fail;
120   }
121   json_array_foreach (parray, i, pnode)
122   {
123     if (-1 == json_unpack_ex (pnode, &jerr, 0, "F", &cur))
124     {
125       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
126                   "validating pricearray index %d: %s\n", i, jerr.text);
127       goto fail;
128     }
129     if (prev <= cur)
130     {
131       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
132                   "validating pricearray index %d: "
133                   "prices must be strictly monotonically decreasing\n",
134                   i);
135       goto fail;
136     }
137     prev = cur;
138   }
139
140   return;
141
142 fail:
143   ret = 1;
144   return;
145 }
146
147
148 /**
149  * The main function.
150  *
151  * @param argc number of arguments from the command line
152  * @param argv command line arguments
153  * @return 0 ok, 1 on error
154  */
155 int
156 main (int argc, char *const *argv)
157 {
158   struct GNUNET_GETOPT_CommandLineOption options[] = {
159     GNUNET_GETOPT_option_filename ('d',
160                                    "description",
161                                    "FILE",
162                                    gettext_noop (
163                                      "description of the item to be sold"),
164                                    &fndesc),
165
166     GNUNET_GETOPT_option_filename ('p',
167                                    "pricemap",
168                                    "FILE",
169                                    gettext_noop ("mapping of possible prices"),
170                                    &fnprices),
171
172     GNUNET_GETOPT_option_relative_time ('r',
173                                         "roundtime",
174                                         "DURATION",
175                                         gettext_noop ("max duration per round"),
176                                         &dround),
177
178     GNUNET_GETOPT_option_relative_time ('s',
179                                         "regtime",
180                                         "DURATION",
181                                         gettext_noop (
182                                           "duration until auction starts"),
183                                         &dstart),
184     GNUNET_GETOPT_option_uint ('m',
185                                "m",
186                                "NUMBER",
187                                gettext_noop ("number of items to sell\n"
188                                              "0 for first price auction\n"
189                                              ">0 for vickrey/M+1st price auction"),
190                                &m),
191
192     GNUNET_GETOPT_option_flag ('u',
193                                "public",
194                                gettext_noop ("public auction outcome"),
195                                &outcome),
196
197     GNUNET_GETOPT_option_flag ('i',
198                                "interactive",
199                                gettext_noop (
200                                  "keep running in foreground until auction completes"),
201                                &interactive),
202
203     GNUNET_GETOPT_OPTION_END
204   };
205
206   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
207     return 2;
208
209   ret = (GNUNET_OK ==
210          GNUNET_PROGRAM_run (argc, argv,
211                              "gnunet-auction-create",
212                              gettext_noop ("create a new auction and "
213                                            "start listening for bidders"),
214                              options,
215                              &run,
216                              NULL)) ? ret : 1;
217   GNUNET_free ((void *) argv);
218   return ret;
219 }