Skip to content

Commit

Permalink
Fix duplicate send ID insufficient balance (nanocurrency#1015)
Browse files Browse the repository at this point in the history
  • Loading branch information
PlasmaPower authored Aug 24, 2018
1 parent 207a1af commit edcdb15
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
15 changes: 5 additions & 10 deletions rai/core_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,8 @@ TEST (rpc, send_idempotent)
request.put ("wallet", wallet);
request.put ("action", "send");
request.put ("source", rai::test_genesis_key.pub.to_account ());
request.put ("destination", rai::test_genesis_key.pub.to_account ());
request.put ("amount", "100");
request.put ("destination", rai::account (0).to_account ());
request.put ("amount", (rai::genesis_amount - (rai::genesis_amount / 4)).convert_to<std::string> ());
request.put ("id", "123abc");
test_response response (request, rpc, system.service);
while (response.status == 0)
Expand All @@ -368,15 +368,15 @@ TEST (rpc, send_idempotent)
rai::block_hash block;
ASSERT_FALSE (block.decode_hex (block_text));
ASSERT_TRUE (system.nodes[0]->ledger.block_exists (block));
ASSERT_EQ (system.nodes[0]->balance (rai::test_genesis_key.pub), rai::genesis_amount - 100);
ASSERT_EQ (system.nodes[0]->balance (rai::test_genesis_key.pub), rai::genesis_amount / 4);
test_response response2 (request, rpc, system.service);
while (response2.status == 0)
{
system.poll ();
}
ASSERT_EQ (200, response2.status);
ASSERT_EQ (response2.json.get<std::string> ("block"), block_text);
ASSERT_EQ (system.nodes[0]->balance (rai::test_genesis_key.pub), rai::genesis_amount - 100);
ASSERT_EQ (system.nodes[0]->balance (rai::test_genesis_key.pub), rai::genesis_amount / 4);
request.erase ("id");
request.put ("id", "456def");
test_response response3 (request, rpc, system.service);
Expand All @@ -385,12 +385,7 @@ TEST (rpc, send_idempotent)
system.poll ();
}
ASSERT_EQ (200, response3.status);
std::string block2_text (response3.json.get<std::string> ("block"));
rai::block_hash block2;
ASSERT_NE (block2, block);
ASSERT_FALSE (block2.decode_hex (block2_text));
ASSERT_TRUE (system.nodes[0]->ledger.block_exists (block2));
ASSERT_EQ (system.nodes[0]->balance (rai::test_genesis_key.pub), rai::genesis_amount - 200);
ASSERT_EQ (response3.json.get<std::string> ("error"), "Insufficient balance");
}

TEST (rpc, stop)
Expand Down
37 changes: 19 additions & 18 deletions rai/node/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2606,29 +2606,30 @@ void rai::rpc_handler::send ()
if (!ec)
{
boost::optional<std::string> send_id (request.get_optional<std::string> ("id"));
if (balance >= amount.number ())
{
auto rpc_l (shared_from_this ());
auto response_a (response);
wallet->send_async (source, destination, amount.number (), [response_a](std::shared_ptr<rai::block> block_a) {
if (block_a != nullptr)
auto rpc_l (shared_from_this ());
auto response_a (response);
wallet->send_async (source, destination, amount.number (), [balance, amount, response_a](std::shared_ptr<rai::block> block_a) {
if (block_a != nullptr)
{
rai::uint256_union hash (block_a->hash ());
boost::property_tree::ptree response_l;
response_l.put ("block", hash.to_string ());
response_a (response_l);
}
else
{
if (balance >= amount.number ())
{
rai::uint256_union hash (block_a->hash ());
boost::property_tree::ptree response_l;
response_l.put ("block", hash.to_string ());
response_a (response_l);
error_response (response_a, "Error generating block");
}
else
{
error_response (response_a, "Error generating block");
std::error_code ec (nano::error_common::insufficient_balance);
error_response (response_a, ec.message ());
}
},
work == 0, send_id);
}
else
{
ec = nano::error_common::insufficient_balance;
}
}
},
work == 0, send_id);
}
}
else
Expand Down

0 comments on commit edcdb15

Please sign in to comment.