Commit 9f93adfd authored by xpetrak2's avatar xpetrak2
Browse files

Various fixes and changes for the trading impl.

Typos and comments/docs/logs/README adjustments
Sending market.cancel moved into trade impl.
Fix of memleaks
parent 0ed6afe2
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
- Host: A device that is possibly connected to the Coincer network - Host: A device that is possibly connected to the Coincer network
- Identifier: The public key of a cryptographic keypair - Identifier: The public key of a cryptographic keypair
- Identity: A peer representation that we own (identifier + its corresponding - Identity: A peer representation that we own (identifier + its corresponding
secret key) secret key)
- Message trace: Contains a peer-to-peer message's nonce, a pointer to a - Message trace: Contains a peer-to-peer message's nonce, a pointer to a
neighbour from which we've received the message and the originator's neighbour from which we've received the message and the originator's
identifier. The message traces are being used for routing loop detection identifier. The message traces are being used for routing loop detection
......
...@@ -31,7 +31,7 @@ static int create_trade_execution_basic(trade_execution_basic_t **basic, ...@@ -31,7 +31,7 @@ static int create_trade_execution_basic(trade_execution_basic_t **basic,
const trade_t *trade); const trade_t *trade);
/** /**
* Create encrypted and store it in a message. * Create encrypted container and store it in a message.
* *
* @param message Store encrypted in this message. * @param message Store encrypted in this message.
* @param encrypted_payload Encrypted data. * @param encrypted_payload Encrypted data.
...@@ -49,7 +49,8 @@ int create_encrypted(message_t *message, const char *encrypted_payload) ...@@ -49,7 +49,8 @@ int create_encrypted(message_t *message, const char *encrypted_payload)
log_error("Creating encrypted"); log_error("Creating encrypted");
return 1; return 1;
} }
payload = (char *) malloc(strlen(encrypted_payload + 1) * sizeof(char)); payload = (char *) malloc((strlen(encrypted_payload) + 1) *
sizeof(char));
if (!payload) { if (!payload) {
log_error("Creating encrypted payload"); log_error("Creating encrypted payload");
free(encrypted); free(encrypted);
...@@ -264,7 +265,7 @@ int create_trade_execution(trade_execution_t **execution, const trade_t *trade) ...@@ -264,7 +265,7 @@ int create_trade_execution(trade_execution_t **execution, const trade_t *trade)
} }
if (ret) { if (ret) {
free(execution); free(*execution);
} }
return ret; return ret;
...@@ -296,8 +297,6 @@ static int create_trade_execution_basic(trade_execution_basic_t **basic, ...@@ -296,8 +297,6 @@ static int create_trade_execution_basic(trade_execution_basic_t **basic,
return 1; return 1;
} }
*basic = exec_data;
switch (trade->step) { switch (trade->step) {
case TS_COMMITMENT: case TS_COMMITMENT:
memcpy(exec_data->commitment, memcpy(exec_data->commitment,
...@@ -320,6 +319,7 @@ static int create_trade_execution_basic(trade_execution_basic_t **basic, ...@@ -320,6 +319,7 @@ static int create_trade_execution_basic(trade_execution_basic_t **basic,
sizeof(char)); sizeof(char));
if (!script) { if (!script) {
log_error("Script for trade.execution message"); log_error("Script for trade.execution message");
free(exec_data);
return 1; return 1;
} }
strcpy(script, trade_data->my_script); strcpy(script, trade_data->my_script);
...@@ -333,6 +333,7 @@ static int create_trade_execution_basic(trade_execution_basic_t **basic, ...@@ -333,6 +333,7 @@ static int create_trade_execution_basic(trade_execution_basic_t **basic,
sizeof(char)); sizeof(char));
if (!script) { if (!script) {
log_error("Script for trade.execution message"); log_error("Script for trade.execution message");
free(exec_data);
return 1; return 1;
} }
strcpy(script, trade_data->my_script); strcpy(script, trade_data->my_script);
...@@ -341,9 +342,12 @@ static int create_trade_execution_basic(trade_execution_basic_t **basic, ...@@ -341,9 +342,12 @@ static int create_trade_execution_basic(trade_execution_basic_t **basic,
default: default:
log_error("Non-existent step for trade.execution " log_error("Non-existent step for trade.execution "
"creation"); "creation");
free(exec_data);
return 1; return 1;
} }
*basic = exec_data;
return 0; return 0;
} }
......
...@@ -485,6 +485,7 @@ static int process_encrypted(const encrypted_t *encrypted_payload, ...@@ -485,6 +485,7 @@ static int process_encrypted(const encrypted_t *encrypted_payload,
} }
free(json_payload); free(json_payload);
json_payload = NULL;
/* trade.execution has its own parsing function */ /* trade.execution has its own parsing function */
if (payload_type != TRADE_EXECUTION) { if (payload_type != TRADE_EXECUTION) {
...@@ -510,7 +511,7 @@ static int process_encrypted(const encrypted_t *encrypted_payload, ...@@ -510,7 +511,7 @@ static int process_encrypted(const encrypted_t *encrypted_payload,
} }
trade_type = trade->type; trade_type = trade->type;
next_step = trade_step_get_next(trade); next_step = trade_step_get_next(trade);
/* attept to decode the next step of the agreed /* attempt to decode the next step of the agreed
* trading protocol */ * trading protocol */
if (decode_trade_execution(json_payload_data, if (decode_trade_execution(json_payload_data,
trade_type, trade_type,
...@@ -573,6 +574,7 @@ static int process_encrypted(const encrypted_t *encrypted_payload, ...@@ -573,6 +574,7 @@ static int process_encrypted(const encrypted_t *encrypted_payload,
* @param global_state The global state. * @param global_state The global state.
* *
* @return 0 Successfully processed. * @return 0 Successfully processed.
* @return 1 Failure.
*/ */
static int process_p2p_bye(const message_t *message, static int process_p2p_bye(const message_t *message,
const char *json_message, const char *json_message,
...@@ -656,6 +658,7 @@ static int process_p2p_hello(const message_t *message, ...@@ -656,6 +658,7 @@ static int process_p2p_hello(const message_t *message,
* @param hosts Our known hosts. * @param hosts Our known hosts.
* *
* @return 0 Successfully processed. * @return 0 Successfully processed.
* @return 1 Failure.
*/ */
static int process_p2p_peers_adv(const message_t *message, static int process_p2p_peers_adv(const message_t *message,
neighbour_t *sender, neighbour_t *sender,
...@@ -1010,8 +1013,6 @@ static int process_trade_proposal(const trade_proposal_t *trade_proposal, ...@@ -1010,8 +1013,6 @@ static int process_trade_proposal(const trade_proposal_t *trade_proposal,
return 1; return 1;
} }
send_market_cancel(&global_state->neighbours, order);
order_flags_set(order, ORDER_TRADING); order_flags_set(order, ORDER_TRADING);
return 0; return 0;
...@@ -1026,6 +1027,7 @@ static int process_trade_proposal(const trade_proposal_t *trade_proposal, ...@@ -1026,6 +1027,7 @@ static int process_trade_proposal(const trade_proposal_t *trade_proposal,
* @param trade trade.reject related to this trade. * @param trade trade.reject related to this trade.
* *
* @return 0 Successfully processed. * @return 0 Successfully processed.
* @return 1 Failure.
*/ */
static int process_trade_reject(const trade_reject_t *trade_reject, static int process_trade_reject(const trade_reject_t *trade_reject,
const identity_t *my_identity, const identity_t *my_identity,
......
...@@ -42,7 +42,7 @@ enum trade_step { ...@@ -42,7 +42,7 @@ enum trade_step {
TS_KEY_AND_COMMITTED_EXCHANGE, /**< Pubkey and 'committed' exchange. */ TS_KEY_AND_COMMITTED_EXCHANGE, /**< Pubkey and 'committed' exchange. */
TS_SCRIPT_ORIGIN, /**< Received or sent the first script. */ TS_SCRIPT_ORIGIN, /**< Received or sent the first script. */
TS_SCRIPT_RESPONSE, /**< Received or sent the second script. */ TS_SCRIPT_RESPONSE, /**< Received or sent the second script. */
TS_COINS_COMMITMENT, /**< Committing coins into a blackchain. */ TS_COINS_COMMITMENT, /**< Committing coins into a blockchain. */
/** Waiting for the counterparty to commit their coins. */ /** Waiting for the counterparty to commit their coins. */
TS_COINS_CP_COMMITMENT, TS_COINS_CP_COMMITMENT,
TS_COINS_CLAIM, /**< Coins claim. */ TS_COINS_CLAIM, /**< Coins claim. */
...@@ -53,7 +53,7 @@ enum trade_step { ...@@ -53,7 +53,7 @@ enum trade_step {
* Trade data holder. * Trade data holder.
*/ */
typedef struct s_trade { typedef struct s_trade {
order_t *order; /**< Trade refering to this order. */ order_t *order; /**< Trade referring to this order. */
enum trade_type type; /**< Type of the trade. */ enum trade_type type; /**< Type of the trade. */
enum trade_step step; /**< Current step of the trade. */ enum trade_step step; /**< Current step of the trade. */
......
...@@ -69,9 +69,11 @@ void trade_basic_clear(trade_basic_t *data) ...@@ -69,9 +69,11 @@ void trade_basic_clear(trade_basic_t *data)
{ {
if (data->my_script) { if (data->my_script) {
free(data->my_script); free(data->my_script);
data->my_script = NULL;
} }
if (data->cp_script) { if (data->cp_script) {
free(data->cp_script); free(data->cp_script);
data->cp_script = NULL;
} }
} }
...@@ -238,7 +240,9 @@ int trade_basic_update(trade_t *trade, ...@@ -238,7 +240,9 @@ int trade_basic_update(trade_t *trade,
if (trade_script_validate(trade, if (trade_script_validate(trade,
execution->script)) { execution->script)) {
log_error("Counterparty's script " log_error("Counterparty's script "
"is invalid"); "is invalid [step "
"TS_KEY_AND_"
"COMMITTED_EXCHANGE");
return 1; return 1;
} }
trade_data->cp_script = execution->script; trade_data->cp_script = execution->script;
...@@ -251,7 +255,8 @@ int trade_basic_update(trade_t *trade, ...@@ -251,7 +255,8 @@ int trade_basic_update(trade_t *trade,
break; break;
case TS_SCRIPT_ORIGIN: case TS_SCRIPT_ORIGIN:
if (trade_script_validate(trade, execution->script)) { if (trade_script_validate(trade, execution->script)) {
log_error("Counterparty's script is invalid"); log_error("Counterparty's script is invalid "
"[step TS_SCRIPT_ORIGIN]");
return 1; return 1;
} }
trade_data->cp_script = execution->script; trade_data->cp_script = execution->script;
...@@ -261,7 +266,8 @@ int trade_basic_update(trade_t *trade, ...@@ -261,7 +266,8 @@ int trade_basic_update(trade_t *trade,
break; break;
case TS_SCRIPT_RESPONSE: case TS_SCRIPT_RESPONSE:
if (trade_script_validate(trade, execution->script)) { if (trade_script_validate(trade, execution->script)) {
log_error("Counterparty's script is invalid"); log_error("Counterparty's script is invalid "
"[step TS_SCRIPT_RESPONSE]");
return 1; return 1;
} }
trade_data->cp_script = execution->script; trade_data->cp_script = execution->script;
...@@ -463,7 +469,8 @@ int trade_step_basic_perform(trade_t *trade, global_state_t *global_state) ...@@ -463,7 +469,8 @@ int trade_step_basic_perform(trade_t *trade, global_state_t *global_state)
if (send_trade_execution(&global_state->routing_table, if (send_trade_execution(&global_state->routing_table,
trade)) { trade)) {
log_error("Sending trade.execution"); log_error("Sending trade.execution "
"[step TS_COMMITMENT]");
return 1; return 1;
} }
break; break;
...@@ -473,14 +480,18 @@ int trade_step_basic_perform(trade_t *trade, global_state_t *global_state) ...@@ -473,14 +480,18 @@ int trade_step_basic_perform(trade_t *trade, global_state_t *global_state)
!trade_script_originator(trade)) { !trade_script_originator(trade)) {
if (trade_script_generate(trade, if (trade_script_generate(trade,
global_state)) { global_state)) {
log_error("Creating a trade script"); log_error("Creating a trade script "
"[step TS_KEY_AND_"
"COMMITTED_EXCHANGE");
return 1; return 1;
} }
} }
if (send_trade_execution(&global_state->routing_table, if (send_trade_execution(&global_state->routing_table,
trade)) { trade)) {
log_error("Sending trade.execution"); log_error("Sending trade.execution "
"[step TS_KEY_AND_"
"COMMITTED_EXCHANGE]");
return 1; return 1;
} }
break; break;
...@@ -495,25 +506,29 @@ int trade_step_basic_perform(trade_t *trade, global_state_t *global_state) ...@@ -495,25 +506,29 @@ int trade_step_basic_perform(trade_t *trade, global_state_t *global_state)
SHA3_256_SIZE, SHA3_256_SIZE,
trade_data->hx); trade_data->hx);
if (trade_script_generate(trade, global_state)) { if (trade_script_generate(trade, global_state)) {
log_error("Creating a trade script"); log_error("Creating a trade script "
"[step TS_SCRIPT_ORIGIN]");
return 1; return 1;
} }
if (send_trade_execution(&global_state->routing_table, if (send_trade_execution(&global_state->routing_table,
trade)) { trade)) {
log_error("Sending trade.execution"); log_error("Sending trade.execution "
"[step TS_SCRIPT_ORIGIN]");
return 1; return 1;
} }
break; break;
case TS_SCRIPT_RESPONSE: case TS_SCRIPT_RESPONSE:
if (trade_script_generate(trade, global_state)) { if (trade_script_generate(trade, global_state)) {
log_error("Creating a trade script"); log_error("Creating a trade script "
"[step TS_SCRIPT_RESPONSE]");
return 1; return 1;
} }
if (send_trade_execution(&global_state->routing_table, if (send_trade_execution(&global_state->routing_table,
trade)) { trade)) {
log_error("Sending trade.execution"); log_error("Sending trade.execution "
"[step TS_SCRIPT_RESPONSE]");
return 1; return 1;
} }
break; break;
...@@ -522,7 +537,13 @@ int trade_step_basic_perform(trade_t *trade, global_state_t *global_state) ...@@ -522,7 +537,13 @@ int trade_step_basic_perform(trade_t *trade, global_state_t *global_state)
log_error("Saving a trade"); log_error("Saving a trade");
return 1; return 1;
} }
/* TODO: Commit coins */ /* TODO: Commit coins */
if (!(trade->order->flags & ORDER_FOREIGN)) {
send_market_cancel(&global_state->neighbours,
trade->order);
}
break; break;
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment