Catch json parse exceptions in sign/work.

This commit is contained in:
clemahieu 2017-12-22 13:41:33 -06:00
commit b328640745
2 changed files with 50 additions and 26 deletions

View file

@ -100,6 +100,12 @@ TEST (interface, sign_transaction)
free (transaction);
}
TEST (interface, fail_sign_transaction)
{
rai::uint256_union data (0);
xrb_sign_transaction ("", data.bytes.data ());
}
TEST (interface, work_transaction)
{
rai::raw_key key;
@ -117,3 +123,9 @@ TEST (interface, work_transaction)
ASSERT_FALSE (rai::work_validate (*block));
free (transaction);
}
TEST (interface, fail_work_transaction)
{
rai::uint256_union data (0);
xrb_work_transaction ("");
}

View file

@ -78,21 +78,27 @@ void xrb_key_account (const xrb_uint256 key, xrb_uint256 pub)
char * xrb_sign_transaction (const char * transaction, const xrb_uint256 private_key)
{
char * result (nullptr);
boost::property_tree::ptree block_l;
std::string transaction_l (transaction);
std::stringstream block_stream (transaction_l);
boost::property_tree::read_json (block_stream, block_l);
auto block (rai::deserialize_block_json (block_l));
if (block != nullptr)
try
{
boost::property_tree::ptree block_l;
std::string transaction_l (transaction);
std::stringstream block_stream (transaction_l);
boost::property_tree::read_json (block_stream, block_l);
auto block (rai::deserialize_block_json (block_l));
if (block != nullptr)
{
rai::uint256_union pub;
ed25519_publickey (private_key, pub.bytes.data ());
rai::raw_key prv;
prv.data = *reinterpret_cast <rai::uint256_union *> (private_key);
block->signature_set (rai::sign_message (prv, pub, block->hash ()));
auto json (block->to_json ());
result = reinterpret_cast <char *> (malloc (json.size () + 1));
strncpy (result, json.c_str (), json.size () + 1);
}
}
catch (std::runtime_error const & err)
{
rai::uint256_union pub;
ed25519_publickey (private_key, pub.bytes.data ());
rai::raw_key prv;
prv.data = *reinterpret_cast <rai::uint256_union *> (private_key);
block->signature_set (rai::sign_message (prv, pub, block->hash ()));
auto json (block->to_json ());
result = reinterpret_cast <char *> (malloc (json.size () + 1));
strncpy (result, json.c_str (), json.size () + 1);
}
return result;
}
@ -100,19 +106,25 @@ char * xrb_sign_transaction (const char * transaction, const xrb_uint256 private
char * xrb_work_transaction (const char * transaction)
{
char * result (nullptr);
boost::property_tree::ptree block_l;
std::string transaction_l (transaction);
std::stringstream block_stream (transaction_l);
boost::property_tree::read_json (block_stream, block_l);
auto block (rai::deserialize_block_json (block_l));
if (block != nullptr)
try
{
boost::property_tree::ptree block_l;
std::string transaction_l (transaction);
std::stringstream block_stream (transaction_l);
boost::property_tree::read_json (block_stream, block_l);
auto block (rai::deserialize_block_json (block_l));
if (block != nullptr)
{
rai::work_pool pool (std::thread::hardware_concurrency ());
auto work (pool.generate (block->root ()));
block->block_work_set (work);
auto json (block->to_json ());
result = reinterpret_cast <char *> (malloc (json.size () + 1));
strncpy (result, json.c_str (), json.size () + 1);
}
}
catch (std::runtime_error const & err)
{
rai::work_pool pool (std::thread::hardware_concurrency ());
auto work (pool.generate (block->root ()));
block->block_work_set (work);
auto json (block->to_json ());
result = reinterpret_cast <char *> (malloc (json.size () + 1));
strncpy (result, json.c_str (), json.size () + 1);
}
return result;
}