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 @@
- Host: A device that is possibly connected to the Coincer network
- Identifier: The public key of a cryptographic keypair
- 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
neighbour from which we've received the message and the originator's
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,
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 encrypted_payload Encrypted data.
......@@ -49,7 +49,8 @@ int create_encrypted(message_t *message, const char *encrypted_payload)
log_error("Creating encrypted");
return 1;
}
payload = (char *) malloc(strlen(encrypted_payload + 1) * sizeof(char));
payload = (char *) malloc((strlen(encrypted_payload) + 1) *
sizeof(char));
if (!payload) {
log_error("Creating encrypted payload");
free(encrypted);
......@@ -264,7 +265,7 @@ int create_trade_execution(trade_execution_t **execution, const trade_t *trade)
}
if (ret) {
free(execution);
free(*execution);
}
return ret;
......@@ -296,8 +297,6 @@ static int create_trade_execution_basic(trade_execution_basic_t **basic,
return 1;
}
*basic = exec_data;
switch (trade->step) {
case TS_COMMITMENT:
memcpy(exec_data->commitment,
......@@ -320,6 +319,7 @@ static int create_trade_execution_basic(trade_execution_basic_t **basic,
sizeof(char));
if (!script) {
log_error("Script for trade.execution message");
free(exec_data);
return 1;
}
strcpy(script, trade_data->my_script);
......@@ -333,6 +333,7 @@ static int create_trade_execution_basic(trade_execution_basic_t **basic,
sizeof(char));
if (!script) {
log_error("Script for trade.execution message");
free(exec_data);
return 1;
}
strcpy(script, trade_data->my_script);
......@@ -341,9 +342,12 @@ static int create_trade_execution_basic(trade_execution_basic_t **basic,
default:
log_error("Non-existent step for trade.execution "
"creation");
free(exec_data);
return 1;
}
*basic = exec_data;
return 0;
}
......
......@@ -485,6 +485,7 @@ static int process_encrypted(const encrypted_t *encrypted_payload,
}
free(json_payload);
json_payload = NULL;
/* trade.execution has its own parsing function */
if (payload_type != TRADE_EXECUTION) {
......@@ -510,7 +511,7 @@ static int process_encrypted(const encrypted_t *encrypted_payload,
}
trade_type = trade->type;
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 */
if (decode_trade_execution(json_payload_data,
trade_type,
......@@ -573,6 +574,7 @@ static int process_encrypted(const encrypted_t *encrypted_payload,
* @param global_state The global state.
*
* @return 0 Successfully processed.
* @return 1 Failure.
*/
static int process_p2p_bye(const message_t *message,
const char *json_message,
......@@ -656,6 +658,7 @@ static int process_p2p_hello(const message_t *message,
* @param hosts Our known hosts.
*
* @return 0 Successfully processed.
* @return 1 Failure.
*/
static int process_p2p_peers_adv(const message_t *message,
neighbour_t *sender,
......@@ -1010,8 +1013,6 @@ static int process_trade_proposal(const trade_proposal_t *trade_proposal,
return 1;
}
send_market_cancel(&global_state->neighbours, order);
order_flags_set(order, ORDER_TRADING);
return 0;
......@@ -1026,6 +1027,7 @@ static int process_trade_proposal(const trade_proposal_t *trade_proposal,
* @param trade trade.reject related to this trade.
*
* @return 0 Successfully processed.
* @return 1 Failure.
*/
static int process_trade_reject(const trade_reject_t *trade_reject,
const identity_t *my_identity,
......
......@@ -42,7 +42,7 @@ enum trade_step {
TS_KEY_AND_COMMITTED_EXCHANGE, /**< Pubkey and 'committed' exchange. */
TS_SCRIPT_ORIGIN, /**< Received or sent the first 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. */
TS_COINS_CP_COMMITMENT,
TS_COINS_CLAIM, /**< Coins claim. */
......@@ -53,7 +53,7 @@ enum trade_step {
* Trade data holder.
*/
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_step step; /**< Current step of the trade. */
......
......@@ -69,9 +69,11 @@ void trade_basic_clear(trade_basic_t *data)
{
if (data->my_script) {
free(data->my_script);
data->my_script = NULL;
}
if (data->cp_script) {
free(data->cp_script);
data->cp_script = NULL;
}
}
......@@ -238,7 +240,9 @@ int trade_basic_update(trade_t *trade,
if (trade_script_validate(trade,
execution->script)) {
log_error("Counterparty's script "
"is invalid");
"is invalid [step "
"TS_KEY_AND_"
"COMMITTED_EXCHANGE");
return 1;
}
trade_data->cp_script = execution->script;
......@@ -251,7 +255,8 @@ int trade_basic_update(trade_t *trade,
break;
case TS_SCRIPT_ORIGIN:
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;
}
trade_data->cp_script = execution->script;
......@@ -261,7 +266,8 @@ int trade_basic_update(trade_t *trade,
break;
case TS_SCRIPT_RESPONSE:
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;
}
trade_data->cp_script = execution->script;
......@@ -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,
trade)) {
log_error("Sending trade.execution");
log_error("Sending trade.execution "
"[step TS_COMMITMENT]");
return 1;
}
break;
......@@ -473,14 +480,18 @@ int trade_step_basic_perform(trade_t *trade, global_state_t *global_state)
!trade_script_originator(trade)) {
if (trade_script_generate(trade,
global_state)) {
log_error("Creating a trade script");
log_error("Creating a trade script "
"[step TS_KEY_AND_"
"COMMITTED_EXCHANGE");
return 1;
}
}
if (send_trade_execution(&global_state->routing_table,
trade)) {
log_error("Sending trade.execution");
log_error("Sending trade.execution "
"[step TS_KEY_AND_"
"COMMITTED_EXCHANGE]");
return 1;
}
break;
......@@ -495,25 +506,29 @@ int trade_step_basic_perform(trade_t *trade, global_state_t *global_state)
SHA3_256_SIZE,
trade_data->hx);
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;
}
if (send_trade_execution(&global_state->routing_table,
trade)) {
log_error("Sending trade.execution");
log_error("Sending trade.execution "
"[step TS_SCRIPT_ORIGIN]");
return 1;
}
break;
case TS_SCRIPT_RESPONSE:
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;
}
if (send_trade_execution(&global_state->routing_table,
trade)) {
log_error("Sending trade.execution");
log_error("Sending trade.execution "
"[step TS_SCRIPT_RESPONSE]");
return 1;
}
break;
......@@ -522,7 +537,13 @@ int trade_step_basic_perform(trade_t *trade, global_state_t *global_state)
log_error("Saving a trade");
return 1;
}
/* TODO: Commit coins */
if (!(trade->order->flags & ORDER_FOREIGN)) {
send_market_cancel(&global_state->neighbours,
trade->order);
}
break;
}
......
Markdown is supported
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