Skip to content

Commit 1708a1f

Browse files
committedJul 28, 2014
First cut of a gateway.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 67cc7b9 commit 1708a1f

15 files changed

+727
-74
lines changed
 

‎Makefile

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
PETTYCOIN_OBJS := block.o check_block.o check_tx.o difficulty.o shadouble.o timestamp.o gateways.o hash_tx.o pettycoin.o merkle_txs.o merkle_recurse.o tx_cmp.o genesis.o marshal.o hash_block.o prev_txhashes.o state.o tal_packet.o dns.o netaddr.o peer.o peer_cache.o pseudorand.o welcome.o log.o generating.o blockfile.o pending.o log_helper.o txhash.o signature.o proof.o chain.o features.o todo.o base58.o sync.o create_refs.o shard.o packet_io.o tx.o complain.o block_shard.o recv_block.o input_refs.o peer_wants.o inputhash.o tx_in_hashes.o merkle_hashes.o recv_tx.o reward.o recv_complain.o json.o jsonrpc.o getinfo.o ecode_names.o sendrawtransaction.c pettycoin_dir.o pkt_names.o
2-
PETTYCOIN_GENERATE_OBJS := pettycoin-generate.o merkle_hashes.o merkle_recurse.o hash_tx.o tx_cmp.o shadouble.o marshal.o minimal_log.o timestamp.o tal_packet.o
1+
PETTYCOIN_OBJS := block.o check_block.o check_tx.o difficulty.o shadouble.o timestamp.o gateways.o hash_tx.o pettycoin.o merkle_txs.o merkle_recurse.o tx_cmp.o genesis.o marshal.o hash_block.o prev_txhashes.o state.o tal_packet.o dns.o netaddr.o peer.o peer_cache.o pseudorand.o welcome.o log.o generating.o blockfile.o pending.o log_helper.o txhash.o signature.o proof.o chain.o features.o todo.o base58.o sync.o create_refs.o shard.o packet_io.o tx.o complain.o block_shard.o recv_block.o input_refs.o peer_wants.o inputhash.o tx_in_hashes.o merkle_hashes.o recv_tx.o reward.o recv_complain.o json.o jsonrpc.o getinfo.o ecode_names.o sendrawtransaction.c pettycoin_dir.o pkt_names.o hex.o
2+
PETTYCOIN_GENERATE_OBJS := pettycoin-generate.o merkle_hashes.o merkle_recurse.o hash_tx.o tx_cmp.o shadouble.o marshal.o minimal_log.o timestamp.o tal_packet.o hex.o
33
MKGENESIS_OBJS := mkgenesis.o shadouble.o hash_block.o merkle_hashes.o merkle_recurse.o minimal_log.o
44
SIZES_OBJS := sizes.o
55
MKPRIV_OBJS := mkpriv.o
6-
PETTYCOIN_TX_OBJS := pettycoin-tx.o base58.o create_tx.o marshal.o hash_tx.o minimal_log.o shadouble.o signature.o hash_block.o merkle_recurse.o json.o pettycoin_dir.o
7-
PETTYCOIN_QUERY_OBJS := pettycoin-query.o json.o base58.o pettycoin_dir.o
6+
PETTYCOIN_TX_OBJS := pettycoin-tx.o base58.o create_tx.o marshal.o hash_tx.o minimal_log.o shadouble.o signature.o hash_block.o merkle_recurse.o json.o pettycoin_dir.o hex.o
7+
PETTYCOIN_QUERY_OBJS := pettycoin-query.o json.o base58.o pettycoin_dir.o hex.o
88
PETTY_ADDR_OBJS := petty-addr.o base58.o
9+
PETTYCOIN_GATEWAY_OBJS := pettycoin-gateway.o hex.o json.o pettycoin_dir.o base58.o
910

10-
BINS := pettycoin-generate mkgenesis pettycoin sizes mkpriv pettycoin-tx pettycoin-query petty-addr
11+
BINS := pettycoin-generate mkgenesis pettycoin sizes mkpriv pettycoin-tx pettycoin-query petty-addr pettycoin-gateway
1112
CCAN_OBJS := ccan-asort.o ccan-breakpoint.o ccan-tal.o ccan-tal-path.o ccan-tal-str.o ccan-take.o ccan-list.o ccan-str.o ccan-opt-helpers.o ccan-opt.o ccan-opt-parse.o ccan-opt-usage.o ccan-read_write_all.o ccan-htable.o ccan-io-io.o ccan-io-poll.o ccan-timer.o ccan-time.o ccan-noerr.o ccan-hash.o ccan-isaac64.o ccan-net.o ccan-err.o ccan-tal-grab_file.o
1213
CCANDIR=ccan/
1314
VERSION:=$(shell git describe --dirty --always 2>/dev/null || echo Unknown)
@@ -47,6 +48,8 @@ sizes: $(SIZES_OBJS) $(CCAN_OBJS)
4748

4849
petty-addr: $(PETTY_ADDR_OBJS) $(CCAN_OBJS)
4950
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(PETTY_ADDR_OBJS) $(CCAN_OBJS) $(LDLIBS)
51+
pettycoin-gateway: $(PETTYCOIN_GATEWAY_OBJS) $(CCAN_OBJS)
52+
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(PETTYCOIN_GATEWAY_OBJS) $(CCAN_OBJS) $(LDLIBS)
5053

5154
genesis.c: mkgenesis
5255
./mkgenesis $(TEST_GENESIS_DIFFICULTY) $(TEST_GENESIS_TIMESTAMP) $(TEST_GENESIS_NONCE) > $@.tmp; STATUS=$$?; if [ $$STATUS = 0 ]; then mv $@.tmp $@; else rm -f $@.tmp; exit $$STATUS; fi

‎hex.c

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "hex.h"
2+
#include <ccan/short_types/short_types.h>
3+
#include <stdio.h>
4+
5+
static bool char_to_hex(u8 *val, char c)
6+
{
7+
if (c >= '0' && c <= '9') {
8+
*val = c - '0';
9+
return true;
10+
}
11+
if (c >= 'a' && c <= 'f') {
12+
*val = c - 'a' + 10;
13+
return true;
14+
}
15+
if (c >= 'A' && c <= 'F') {
16+
*val = c - 'A' + 10;
17+
return true;
18+
}
19+
return false;
20+
}
21+
22+
bool from_hex(const char *str, size_t slen, void *buf, size_t bufsize)
23+
{
24+
u8 v1, v2;
25+
u8 *p = buf;
26+
27+
while (slen > 1) {
28+
if (!char_to_hex(&v1, str[0]) || !char_to_hex(&v2, str[1]))
29+
return false;
30+
if (!bufsize)
31+
return false;
32+
*(p++) = (v1 << 4) | v2;
33+
str += 2;
34+
slen -= 2;
35+
bufsize--;
36+
}
37+
return slen == 0 && bufsize == 0;
38+
}
39+
40+
char *to_hex(const tal_t *ctx, const void *buf, size_t bufsize)
41+
{
42+
const u8 *p = buf;
43+
size_t i;
44+
char *hex = tal_arr(ctx, char, bufsize * 2 + 1);
45+
46+
for (i = 0; i < bufsize; i++)
47+
sprintf(hex + i*2, "%02x", p[i]);
48+
49+
return hex;
50+
}

‎hex.h

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef PETTYCOIN_HEX_H
2+
#define PETTYCOIN_HEX_H
3+
#include "config.h"
4+
#include <ccan/tal/tal.h>
5+
#include <stdbool.h>
6+
7+
/* Unpack slen hex digits into buf; fail on bad char or not exact length */
8+
bool from_hex(const char *str, size_t slen, void *buf, size_t bufsize);
9+
10+
/* Allocate hex string off ctx */
11+
char *to_hex(const tal_t *ctx, const void *buf, size_t bufsize);
12+
13+
#endif /* PETTYCOIN_HEX_H */

‎json.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* JSON core and helpers */
22
#include "base58.h"
3+
#include "hex.h"
34
#include "json.h"
45
#include "protocol.h"
56
#include <assert.h>
@@ -276,12 +277,7 @@ void json_add_null(char **result, const char *fieldname)
276277
void json_add_hex(char **result, const char *fieldname, const void *data,
277278
size_t len)
278279
{
279-
char *hex = tal_arr(*result, char, len * 2 + 1);
280-
const u8 *p = data;
281-
size_t i;
282-
283-
for (i = 0; i < len; i++)
284-
sprintf(hex + i*2, "%02x", p[i]);
280+
char *hex = to_hex(*result, data, len);
285281

286282
json_add_string(result, fieldname, hex);
287283
tal_free(hex);

‎pettycoin-gateway.c

+618
Large diffs are not rendered by default.

‎pettycoin-generate.c

+5-35
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "difficulty.h"
88
#include "features.h"
99
#include "generate.h"
10+
#include "hex.h"
1011
#include "marshal.h"
1112
#include "merkle_hashes.h"
1213
#include "protocol.h"
@@ -294,39 +295,6 @@ static void read_txs(struct working_block *w)
294295
tal_free(update);
295296
}
296297

297-
static bool char_to_hex(u8 *val, char c)
298-
{
299-
if (c >= '0' && c <= '9') {
300-
*val = c - '0';
301-
return true;
302-
}
303-
if (c >= 'a' && c <= 'f') {
304-
*val = c - 'a' + 10;
305-
return true;
306-
}
307-
if (c >= 'A' && c <= 'F') {
308-
*val = c - 'A' + 10;
309-
return true;
310-
}
311-
return false;
312-
}
313-
314-
static bool from_hex(const char *str, u8 *buf, size_t bufsize)
315-
{
316-
u8 v1, v2;
317-
318-
while (*str) {
319-
if (!char_to_hex(&v1, str[0]) || !char_to_hex(&v2, str[1]))
320-
return false;
321-
if (!bufsize)
322-
return false;
323-
*(buf++) = (v1 << 4) | v2;
324-
str += 2;
325-
bufsize--;
326-
}
327-
return bufsize == 0;
328-
}
329-
330298
/* ''Talkin' 'bout my generation...'' */
331299
static void write_block(int fd, const struct working_block *w)
332300
{
@@ -373,14 +341,16 @@ int main(int argc, char *argv[])
373341
" <num-prev-txhashes> <depth> <shardorder> [<nonce>]",
374342
argv[0]);
375343

376-
if (!from_hex(argv[1], reward_address.addr, sizeof(reward_address)))
344+
if (!from_hex(argv[1], strlen(argv[1]),
345+
reward_address.addr, sizeof(reward_address)))
377346
errx(1, "Invalid reward address");
378347

379348
difficulty = strtoul(argv[2], NULL, 0);
380349
if (!valid_difficulty(difficulty))
381350
errx(1, "Invalid difficulty");
382351

383-
if (!from_hex(argv[3], prev_hash.sha, sizeof(prev_hash)))
352+
if (!from_hex(argv[3], strlen(argv[3]),
353+
prev_hash.sha, sizeof(prev_hash)))
384354
errx(1, "Invalid previous hash");
385355

386356
depth = strtoul(argv[5], NULL, 0);

‎pettycoin-tx.c

+11-28
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "base58.h"
1919
#include "create_tx.h"
2020
#include "hash_tx.h"
21-
#include "json.h"
21+
#include "hex.h"
2222
#include "log.h"
2323
#include "marshal.h"
2424
#include <assert.h>
@@ -117,32 +117,19 @@ static void usage(void)
117117
);
118118
}
119119

120-
static bool from_hex(const char *hexstr, u8 *dest)
121-
{
122-
unsigned int i;
123-
124-
for (i = 0; i < strlen(hexstr) / 2; i++) {
125-
unsigned int v;
126-
127-
if (sscanf(hexstr + i*2, "%02x", &v) != 1)
128-
return false;
129-
dest[i] = v;
130-
}
131-
return true;
132-
}
133-
134120
static struct protocol_double_sha parse_txhash(const char *txraw)
135121
{
136-
union protocol_tx *tx = malloc(strlen(txraw) / 2);
122+
size_t len = strlen(txraw) / 2;
123+
union protocol_tx *tx = malloc(len);
137124
struct protocol_double_sha sha;
138125

139-
if (strlen(txraw) / 2 < sizeof(struct protocol_tx_hdr))
126+
if (len < sizeof(struct protocol_tx_hdr))
140127
errx(1, "Short raw tx '%s'", txraw);
141128

142-
if (strlen(txraw) % 2 || !from_hex(txraw, (u8 *)tx))
129+
if (!from_hex(txraw, strlen(txraw), (u8 *)tx, len))
143130
errx(1, "Bad raw tx '%s'", txraw);
144131

145-
if (marshal_tx_len(tx) != strlen(txraw) / 2)
132+
if (marshal_tx_len(tx) != len)
146133
errx(1, "Bad length raw tx '%s'", txraw);
147134

148135
/* You can make this crash, of course */
@@ -157,7 +144,7 @@ static struct protocol_double_sha parse_tx(const char *txstr)
157144
if (strstarts(txstr, "raw:"))
158145
return parse_txhash(txstr + 4);
159146

160-
if (strlen(txstr) != 32 * 2 || !from_hex(txstr, sha.sha))
147+
if (!from_hex(txstr, strlen(txstr), &sha.sha, sizeof(sha.sha)))
161148
errx(1, "Bad sha '%s'", txstr);
162149

163150
return sha;
@@ -173,7 +160,7 @@ int main(int argc, char *argv[])
173160
bool normal = false;
174161
bool to_gateway = false;
175162
bool pay_fee = true;
176-
char *request;
163+
char *txhash;
177164

178165
if (argc < 3)
179166
usage();
@@ -245,12 +232,8 @@ int main(int argc, char *argv[])
245232
pay_fee, input, key);
246233
}
247234

248-
request = tal_arr(NULL, char, 0);
249-
/* Reuse this as an easy hexstring maker */
250-
json_add_hex(&request, NULL, tx, marshal_tx_len(tx));
251-
/* Strip "" */
252-
request[strlen(request) - 1] = '\0';
253-
printf("%s", request + 1);
254-
tal_free(request);
235+
txhash = to_hex(NULL, tx, marshal_tx_len(tx));
236+
printf("%s", txhash);
237+
tal_free(txhash);
255238
return 0;
256239
}

‎test/run-01-json.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "../json.c"
22
#include "../base58.c"
3+
#include "../hex.c"
34
#include "helper_key.h"
45

56
int main(void)

‎test/run-01-jsonrpc.c

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ char *pettycoin_to_base58(const tal_t *ctx, bool test_net,
2525
{ fprintf(stderr, "pettycoin_to_base58 called!\n"); abort(); }
2626
/* Generated stub for sendrawtransaction_command */
2727
struct json_command sendrawtransaction_command;
28+
/* Generated stub for to_hex */
29+
char *to_hex(const tal_t *ctx, const void *buf, size_t bufsize)
30+
{ fprintf(stderr, "to_hex called!\n"); abort(); }
2831
/* AUTOGENERATED MOCKS END */
2932

3033
void test(const char *input, const char *expect, bool needs_more, int extra)

‎test/run-02-generate.c

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ static time_t my_time(time_t *p)
3434
#include "../shard.c"
3535
#include "../tal_packet.c"
3636
#include "../minimal_log.c"
37+
#include "../hex.c"
3738

3839
int main(int argc, char *argv[])
3940
{

‎test/run-03-check_block.c

+3
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ void complain_doublespend(struct state *state,
128128
const union protocol_tx *tx2,
129129
const struct protocol_input_ref *refs2)
130130
{ fprintf(stderr, "complain_doublespend called!\n"); abort(); }
131+
/* Generated stub for from_hex */
132+
bool from_hex(const char *str, size_t slen, void *buf, size_t bufsize)
133+
{ fprintf(stderr, "from_hex called!\n"); abort(); }
131134
/* Generated stub for log_to_file */
132135
void log_to_file(int fd, const struct log *log)
133136
{ fprintf(stderr, "log_to_file called!\n"); abort(); }

‎test/run-04-create_normal_transaction.c

+3
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ void complain_doublespend(struct state *state,
117117
const union protocol_tx *tx2,
118118
const struct protocol_input_ref *refs2)
119119
{ fprintf(stderr, "complain_doublespend called!\n"); abort(); }
120+
/* Generated stub for from_hex */
121+
bool from_hex(const char *str, size_t slen, void *buf, size_t bufsize)
122+
{ fprintf(stderr, "from_hex called!\n"); abort(); }
120123
/* Generated stub for log_to_file */
121124
void log_to_file(int fd, const struct log *log)
122125
{ fprintf(stderr, "log_to_file called!\n"); abort(); }

‎test/run-05-create_to_gateway_transaction.c

+3
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ void complain_doublespend(struct state *state,
117117
const union protocol_tx *tx2,
118118
const struct protocol_input_ref *refs2)
119119
{ fprintf(stderr, "complain_doublespend called!\n"); abort(); }
120+
/* Generated stub for from_hex */
121+
bool from_hex(const char *str, size_t slen, void *buf, size_t bufsize)
122+
{ fprintf(stderr, "from_hex called!\n"); abort(); }
120123
/* Generated stub for log_to_file */
121124
void log_to_file(int fd, const struct log *log)
122125
{ fprintf(stderr, "log_to_file called!\n"); abort(); }

‎test/run-05-proof.c

+3
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ void complain_doublespend(struct state *state,
117117
const union protocol_tx *tx2,
118118
const struct protocol_input_ref *refs2)
119119
{ fprintf(stderr, "complain_doublespend called!\n"); abort(); }
120+
/* Generated stub for from_hex */
121+
bool from_hex(const char *str, size_t slen, void *buf, size_t bufsize)
122+
{ fprintf(stderr, "from_hex called!\n"); abort(); }
120123
/* Generated stub for log_to_file */
121124
void log_to_file(int fd, const struct log *log)
122125
{ fprintf(stderr, "log_to_file called!\n"); abort(); }

‎test/run-07-block_swizzle.c

+3
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ void complain_doublespend(struct state *state,
129129
const union protocol_tx *tx2,
130130
const struct protocol_input_ref *refs2)
131131
{ fprintf(stderr, "complain_doublespend called!\n"); abort(); }
132+
/* Generated stub for from_hex */
133+
bool from_hex(const char *str, size_t slen, void *buf, size_t bufsize)
134+
{ fprintf(stderr, "from_hex called!\n"); abort(); }
132135
/* Generated stub for reward_amount */
133136
u32 reward_amount(const struct block *reward_block,
134137
const union protocol_tx *tx)

0 commit comments

Comments
 (0)
Please sign in to comment.