From f5191af24b2922826541577bd0d4ecffe8b45f54 Mon Sep 17 00:00:00 2001 From: Oran Juice Date: Sat, 11 Apr 2015 23:53:57 +0530 Subject: [PATCH] Wallet seems functional with new IPC --- src/ipc/daemon_ipc_handlers.cpp | 8 +- src/ipc/include/wap_client.h | 4 +- src/ipc/include/wap_client_engine.inc | 22 +++--- src/ipc/include/wap_proto.h | 14 ++-- src/ipc/wap_client/wap_client.c | 5 +- src/ipc/wap_proto.c | 104 +++++++++++++++++++------- src/wallet/wallet2.cpp | 12 +-- src/wallet/wallet2.h | 23 +----- 8 files changed, 113 insertions(+), 79 deletions(-) diff --git a/src/ipc/daemon_ipc_handlers.cpp b/src/ipc/daemon_ipc_handlers.cpp index 9b7acc3c8..d649def04 100644 --- a/src/ipc/daemon_ipc_handlers.cpp +++ b/src/ipc/daemon_ipc_handlers.cpp @@ -216,10 +216,11 @@ namespace IPC wap_proto_set_status(message, STATUS_CORE_BUSY); return; } - const char *tx_id = wap_proto_tx_id(message); + zchunk_t *tx_id = wap_proto_tx_id(message); crypto::hash hash; - memcpy(hash.data, tx_id + 1, crypto::HASH_SIZE); + memcpy(hash.data, zchunk_data(tx_id), crypto::HASH_SIZE); std::vector output_indexes; + bool r = core->get_tx_outputs_gindexs(hash, output_indexes); if (!r) { @@ -249,6 +250,7 @@ namespace IPC for (unsigned int i = 0; i < amounts_count; i++) { req.amounts.push_back(amounts[i]); } + cryptonote::COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::response res; if (!core->get_random_outs_for_amounts(req, res)) { @@ -287,8 +289,6 @@ namespace IPC result_json.Accept(writer); std::string block_string = buffer.GetString(); -std::cout << block_string << std::endl; - zframe_t *frame = zframe_new(block_string.c_str(), block_string.length()); wap_proto_set_random_outputs(message, &frame); diff --git a/src/ipc/include/wap_client.h b/src/ipc/include/wap_client.h index 4d37830bc..33f9ac3e0 100644 --- a/src/ipc/include/wap_client.h +++ b/src/ipc/include/wap_client.h @@ -80,7 +80,7 @@ WAP_EXPORT int // Request a set of blocks from the server. // Returns >= 0 if successful, -1 if interrupted. WAP_EXPORT int - wap_client_get (wap_client_t *self, const char *tx_id); + wap_client_get (wap_client_t *self, zchunk_t **tx_id_p); // Request a set of blocks from the server. // Returns >= 0 if successful, -1 if interrupted. @@ -90,7 +90,7 @@ WAP_EXPORT int // Ask for tx output indexes. // Returns >= 0 if successful, -1 if interrupted. WAP_EXPORT int - wap_client_output_indexes (wap_client_t *self, const char *tx_id); + wap_client_output_indexes (wap_client_t *self, zchunk_t **tx_id_p); // Ask for tx output indexes. // Returns >= 0 if successful, -1 if interrupted. diff --git a/src/ipc/include/wap_client_engine.inc b/src/ipc/include/wap_client_engine.inc index 8c39bfd0e..08d999458 100644 --- a/src/ipc/include/wap_client_engine.inc +++ b/src/ipc/include/wap_client_engine.inc @@ -136,7 +136,7 @@ struct _client_args_t { zlist_t *block_ids; uint64_t start_height; zchunk_t *tx_as_hex; - char *tx_id; + zchunk_t *tx_id; uint64_t outs_count; zframe_t *amounts; char *address; @@ -281,7 +281,7 @@ s_client_destroy (s_client_t **self_p) zstr_free (&self->args.identity); zlist_destroy (&self->args.block_ids); zchunk_destroy (&self->args.tx_as_hex); - zstr_free (&self->args.tx_id); + zchunk_destroy (&self->args.tx_id); zframe_destroy (&self->args.amounts); zstr_free (&self->args.address); client_terminate (&self->client); @@ -1450,8 +1450,8 @@ s_client_handle_cmdpipe (zloop_t *loop, zsock_t *reader, void *argument) } else if (streq (method, "GET")) { - zstr_free (&self->args.tx_id); - zsock_recv (self->cmdpipe, "s", &self->args.tx_id); + zchunk_destroy (&self->args.tx_id); + zsock_recv (self->cmdpipe, "p", &self->args.tx_id); s_client_execute (self, get_event); } else @@ -1460,8 +1460,8 @@ s_client_handle_cmdpipe (zloop_t *loop, zsock_t *reader, void *argument) } else if (streq (method, "OUTPUT INDEXES")) { - zstr_free (&self->args.tx_id); - zsock_recv (self->cmdpipe, "s", &self->args.tx_id); + zchunk_destroy (&self->args.tx_id); + zsock_recv (self->cmdpipe, "p", &self->args.tx_id); s_client_execute (self, output_indexes_event); } else @@ -1855,11 +1855,12 @@ wap_client_put (wap_client_t *self, zchunk_t **tx_as_hex_p) // Returns >= 0 if successful, -1 if interrupted. int -wap_client_get (wap_client_t *self, const char *tx_id) +wap_client_get (wap_client_t *self, zchunk_t **tx_id_p) { assert (self); - zsock_send (self->actor, "ss", "GET", tx_id); + zsock_send (self->actor, "sp", "GET", *tx_id_p); + *tx_id_p = NULL; // Take ownership of tx_id if (s_accept_reply (self, "GET OK", "FAILURE", NULL)) return -1; // Interrupted or timed-out return self->status; @@ -1887,11 +1888,12 @@ wap_client_save (wap_client_t *self) // Returns >= 0 if successful, -1 if interrupted. int -wap_client_output_indexes (wap_client_t *self, const char *tx_id) +wap_client_output_indexes (wap_client_t *self, zchunk_t **tx_id_p) { assert (self); - zsock_send (self->actor, "ss", "OUTPUT INDEXES", tx_id); + zsock_send (self->actor, "sp", "OUTPUT INDEXES", *tx_id_p); + *tx_id_p = NULL; // Take ownership of tx_id if (s_accept_reply (self, "OUTPUT INDEXES OK", "FAILURE", NULL)) return -1; // Interrupted or timed-out return self->status; diff --git a/src/ipc/include/wap_proto.h b/src/ipc/include/wap_proto.h index eb3cadd63..38e34ad8b 100644 --- a/src/ipc/include/wap_proto.h +++ b/src/ipc/include/wap_proto.h @@ -49,7 +49,7 @@ PUT-OK, or ERROR. status number 8 Transaction ID OUTPUT_INDEXES - Ask for tx output indexes. - tx_id string Transaction ID + tx_id chunk Transaction ID OUTPUT_INDEXES_OK - Daemon returns tx output indexes. status number 8 Status @@ -65,7 +65,7 @@ PUT-OK, or ERROR. GET - Wallet requests transaction data from the daemon. Daemon replies with GET-OK, or ERROR. - tx_id string Transaction ID + tx_id chunk Transaction ID GET_OK - Daemon replies with transaction data. tx_data chunk Transaction data @@ -239,11 +239,15 @@ zchunk_t * void wap_proto_set_tx_as_hex (wap_proto_t *self, zchunk_t **chunk_p); -// Get/set the tx_id field -const char * +// Get a copy of the tx_id field +zchunk_t * wap_proto_tx_id (wap_proto_t *self); +// Get the tx_id field and transfer ownership to caller +zchunk_t * + wap_proto_get_tx_id (wap_proto_t *self); +// Set the tx_id field, transferring ownership from caller void - wap_proto_set_tx_id (wap_proto_t *self, const char *value); + wap_proto_set_tx_id (wap_proto_t *self, zchunk_t **chunk_p); // Get a copy of the o_indexes field zframe_t * diff --git a/src/ipc/wap_client/wap_client.c b/src/ipc/wap_client/wap_client.c index 50d737439..d706f5457 100644 --- a/src/ipc/wap_client/wap_client.c +++ b/src/ipc/wap_client/wap_client.c @@ -155,7 +155,7 @@ prepare_blocks_command (client_t *self) static void prepare_get_output_indexes_command (client_t *self) { - wap_proto_set_tx_id (self->message, self->args->tx_id); + wap_proto_set_tx_id (self->message, &self->args->tx_id); } // --------------------------------------------------------------------------- @@ -213,7 +213,7 @@ signal_have_put_ok (client_t *self) static void prepare_get_command (client_t *self) { - wap_proto_set_tx_id (self->message, self->args->tx_id); + wap_proto_set_tx_id (self->message, &self->args->tx_id); } @@ -359,6 +359,7 @@ signal_server_not_present (client_t *self) static void prepare_get_random_outs_command (client_t *self) { + wap_proto_set_outs_count(self->message, self->args->outs_count); wap_proto_set_amounts (self->message, &self->args->amounts); } diff --git a/src/ipc/wap_proto.c b/src/ipc/wap_proto.c index 16dd0a964..7681536b9 100644 --- a/src/ipc/wap_proto.c +++ b/src/ipc/wap_proto.c @@ -49,7 +49,7 @@ struct _wap_proto_t { // Transaction as hex zchunk_t *tx_as_hex; // Transaction ID - char tx_id [256]; + zchunk_t *tx_id; // Output Indexes zframe_t *o_indexes; // Outs count @@ -245,6 +245,7 @@ wap_proto_destroy (wap_proto_t **self_p) zlist_destroy (&self->block_ids); zmsg_destroy (&self->block_data); zchunk_destroy (&self->tx_as_hex); + zchunk_destroy (&self->tx_id); zframe_destroy (&self->o_indexes); zframe_destroy (&self->amounts); zframe_destroy (&self->random_outputs); @@ -367,7 +368,17 @@ wap_proto_recv (wap_proto_t *self, zsock_t *input) break; case WAP_PROTO_OUTPUT_INDEXES: - GET_STRING (self->tx_id); + { + size_t chunk_size; + GET_NUMBER4 (chunk_size); + if (self->needle + chunk_size > (self->ceiling)) { + zsys_warning ("wap_proto: tx_id is missing data"); + goto malformed; + } + zchunk_destroy (&self->tx_id); + self->tx_id = zchunk_new (self->needle, chunk_size); + self->needle += chunk_size; + } break; case WAP_PROTO_OUTPUT_INDEXES_OK: @@ -404,7 +415,17 @@ wap_proto_recv (wap_proto_t *self, zsock_t *input) break; case WAP_PROTO_GET: - GET_STRING (self->tx_id); + { + size_t chunk_size; + GET_NUMBER4 (chunk_size); + if (self->needle + chunk_size > (self->ceiling)) { + zsys_warning ("wap_proto: tx_id is missing data"); + goto malformed; + } + zchunk_destroy (&self->tx_id); + self->tx_id = zchunk_new (self->needle, chunk_size); + self->needle += chunk_size; + } break; case WAP_PROTO_GET_OK: @@ -520,7 +541,9 @@ wap_proto_send (wap_proto_t *self, zsock_t *output) frame_size += 8; // status break; case WAP_PROTO_OUTPUT_INDEXES: - frame_size += 1 + strlen (self->tx_id); + frame_size += 4; // Size is 4 octets + if (self->tx_id) + frame_size += zchunk_size (self->tx_id); break; case WAP_PROTO_OUTPUT_INDEXES_OK: frame_size += 8; // status @@ -532,7 +555,9 @@ wap_proto_send (wap_proto_t *self, zsock_t *output) frame_size += 8; // status break; case WAP_PROTO_GET: - frame_size += 1 + strlen (self->tx_id); + frame_size += 4; // Size is 4 octets + if (self->tx_id) + frame_size += zchunk_size (self->tx_id); break; case WAP_PROTO_GET_OK: frame_size += 4; // Size is 4 octets @@ -606,7 +631,15 @@ wap_proto_send (wap_proto_t *self, zsock_t *output) break; case WAP_PROTO_OUTPUT_INDEXES: - PUT_STRING (self->tx_id); + if (self->tx_id) { + PUT_NUMBER4 (zchunk_size (self->tx_id)); + memcpy (self->needle, + zchunk_data (self->tx_id), + zchunk_size (self->tx_id)); + self->needle += zchunk_size (self->tx_id); + } + else + PUT_NUMBER4 (0); // Empty chunk break; case WAP_PROTO_OUTPUT_INDEXES_OK: @@ -625,7 +658,15 @@ wap_proto_send (wap_proto_t *self, zsock_t *output) break; case WAP_PROTO_GET: - PUT_STRING (self->tx_id); + if (self->tx_id) { + PUT_NUMBER4 (zchunk_size (self->tx_id)); + memcpy (self->needle, + zchunk_data (self->tx_id), + zchunk_size (self->tx_id)); + self->needle += zchunk_size (self->tx_id); + } + else + PUT_NUMBER4 (0); // Empty chunk break; case WAP_PROTO_GET_OK: @@ -757,10 +798,7 @@ wap_proto_print (wap_proto_t *self) case WAP_PROTO_OUTPUT_INDEXES: zsys_debug ("WAP_PROTO_OUTPUT_INDEXES:"); - if (self->tx_id) - zsys_debug (" tx_id='%s'", self->tx_id); - else - zsys_debug (" tx_id="); + zsys_debug (" tx_id=[ ... ]"); break; case WAP_PROTO_OUTPUT_INDEXES_OK: @@ -795,10 +833,7 @@ wap_proto_print (wap_proto_t *self) case WAP_PROTO_GET: zsys_debug ("WAP_PROTO_GET:"); - if (self->tx_id) - zsys_debug (" tx_id='%s'", self->tx_id); - else - zsys_debug (" tx_id="); + zsys_debug (" tx_id=[ ... ]"); break; case WAP_PROTO_GET_OK: @@ -1159,24 +1194,35 @@ wap_proto_set_tx_as_hex (wap_proto_t *self, zchunk_t **chunk_p) // -------------------------------------------------------------------------- -// Get/set the tx_id field +// Get the tx_id field without transferring ownership -const char * +zchunk_t * wap_proto_tx_id (wap_proto_t *self) { assert (self); return self->tx_id; } +// Get the tx_id field and transfer ownership to caller + +zchunk_t * +wap_proto_get_tx_id (wap_proto_t *self) +{ + zchunk_t *tx_id = self->tx_id; + self->tx_id = NULL; + return tx_id; +} + +// Set the tx_id field, transferring ownership from caller + void -wap_proto_set_tx_id (wap_proto_t *self, const char *value) +wap_proto_set_tx_id (wap_proto_t *self, zchunk_t **chunk_p) { assert (self); - assert (value); - if (value == self->tx_id) - return; - strncpy (self->tx_id, value, 255); - self->tx_id [255] = 0; + assert (chunk_p); + zchunk_destroy (&self->tx_id); + self->tx_id = *chunk_p; + *chunk_p = NULL; } @@ -1521,7 +1567,8 @@ wap_proto_test (bool verbose) } wap_proto_set_id (self, WAP_PROTO_OUTPUT_INDEXES); - wap_proto_set_tx_id (self, "Life is short but Now lasts for ever"); + zchunk_t *output_indexes_tx_id = zchunk_new ("Captcha Diem", 12); + wap_proto_set_tx_id (self, &output_indexes_tx_id); // Send twice wap_proto_send (self, output); wap_proto_send (self, output); @@ -1529,7 +1576,8 @@ wap_proto_test (bool verbose) for (instance = 0; instance < 2; instance++) { wap_proto_recv (self, input); assert (wap_proto_routing_id (self)); - assert (streq (wap_proto_tx_id (self), "Life is short but Now lasts for ever")); + assert (memcmp (zchunk_data (wap_proto_tx_id (self)), "Captcha Diem", 12) == 0); + zchunk_destroy (&output_indexes_tx_id); } wap_proto_set_id (self, WAP_PROTO_OUTPUT_INDEXES_OK); @@ -1581,7 +1629,8 @@ wap_proto_test (bool verbose) } wap_proto_set_id (self, WAP_PROTO_GET); - wap_proto_set_tx_id (self, "Life is short but Now lasts for ever"); + zchunk_t *get_tx_id = zchunk_new ("Captcha Diem", 12); + wap_proto_set_tx_id (self, &get_tx_id); // Send twice wap_proto_send (self, output); wap_proto_send (self, output); @@ -1589,7 +1638,8 @@ wap_proto_test (bool verbose) for (instance = 0; instance < 2; instance++) { wap_proto_recv (self, input); assert (wap_proto_routing_id (self)); - assert (streq (wap_proto_tx_id (self), "Life is short but Now lasts for ever")); + assert (memcmp (zchunk_data (wap_proto_tx_id (self)), "Captcha Diem", 12) == 0); + zchunk_destroy (&get_tx_id); } wap_proto_set_id (self, WAP_PROTO_GET_OK); diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index d89860e0c..0d6ad02b9 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -185,11 +185,9 @@ void wallet2::process_new_transaction(const cryptonote::transaction& tx, uint64_ "transactions outputs size=" + std::to_string(tx.vout.size()) + " not match with COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES response size=" + std::to_string(res.o_indexes.size()));*/ - char *size_prepended_tx_id = new char[crypto::HASH_SIZE + 1]; - size_prepended_tx_id[0] = crypto::HASH_SIZE; - memcpy(size_prepended_tx_id + 1, tx_id.data, crypto::HASH_SIZE); - int rc = wap_client_output_indexes(ipc_client, size_prepended_tx_id); - delete size_prepended_tx_id; + zchunk_t *tx_id_chunk = zchunk_new(tx_id.data, crypto::HASH_SIZE); + int rc = wap_client_output_indexes(ipc_client, &tx_id_chunk); + THROW_WALLET_EXCEPTION_IF(rc < 0, error::no_connection_to_daemon, "get_output_indexes"); uint64_t status = wap_client_status(ipc_client); THROW_WALLET_EXCEPTION_IF(status == IPC::STATUS_CORE_BUSY, error::daemon_busy, "get_output_indexes"); @@ -1178,7 +1176,6 @@ std::vector wallet2::create_transactions(std::vector(zframe_data(outputs_frame)); -std::string tmp(frame_data, frame_size); -std::cout << tmp << std::endl; rapidjson::Document json; THROW_WALLET_EXCEPTION_IF(json.Parse(frame_data, frame_size).HasParseError(), error::get_random_outs_error, "Couldn't JSON parse random outputs."); for (rapidjson::SizeType i = 0; i < json["outputs"].Size(); i++) { COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount output; - output.amount = json["outputs"][i]["amount"].GetInt(); + output.amount = json["outputs"][i]["amount"].GetInt64(); for (rapidjson::SizeType j = 0; j < json["outputs"][i]["outs"].Size(); j++) { COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::out_entry entry; - entry.global_amount_index = json["outputs"][i]["outs"][j]["global_amount_index"].GetInt(); + entry.global_amount_index = json["outputs"][i]["outs"][j]["global_amount_index"].GetInt64(); std::string out_key(json["outputs"][i]["outs"][j]["out_key"].GetString(), json["outputs"][i]["outs"][j]["out_key"].GetStringLength()); memcpy(entry.out_key.data, out_key.c_str(), 32); output.outs.push_back(entry);