From 39f4940c6196dc15f998c32b50ad0d27c4b964bd Mon Sep 17 00:00:00 2001 From: Marko Simovic Date: Thu, 3 Apr 2014 11:01:03 -0400 Subject: [PATCH] Added new transaction tests for checking miner fees --- test/TestHelper.cpp | 9 +++++ test/TestHelper.h | 1 + test/txTest.cpp | 81 ++++++++++++++++++++++++++++++++++++--------- 3 files changed, 76 insertions(+), 15 deletions(-) diff --git a/test/TestHelper.cpp b/test/TestHelper.cpp index e33c105b7..54cb84ccb 100644 --- a/test/TestHelper.cpp +++ b/test/TestHelper.cpp @@ -37,4 +37,13 @@ void mine(Client& c, int numBlocks) c.stopMining(); } +void connectClients(Client& c1, Client& c2) +{ + short c1Port = 20000; + short c2Port = 21000; + c1.startNetwork(c1Port); + c2.startNetwork(c2Port); + c2.connect("127.0.0.1", c1Port); +} + } diff --git a/test/TestHelper.h b/test/TestHelper.h index 5860f7fa6..748373baa 100644 --- a/test/TestHelper.h +++ b/test/TestHelper.h @@ -25,5 +25,6 @@ namespace eth { void mine(Client& c, int numBlocks); +void connectClients(Client& c1, Client& c2); } diff --git a/test/txTest.cpp b/test/txTest.cpp index 757c3df3a..d1a5566d1 100644 --- a/test/txTest.cpp +++ b/test/txTest.cpp @@ -29,38 +29,89 @@ using namespace std; using namespace eth; -BOOST_AUTO_TEST_CASE(mine_and_send_to_peer) +BOOST_AUTO_TEST_CASE(mine_local_simple_tx) { KeyPair kp1 = KeyPair::create(); KeyPair kp2 = KeyPair::create(); - string c1DataDir = (boost::filesystem::temp_directory_path() / boost::filesystem::unique_path()).string(); - string c2DataDir = (boost::filesystem::temp_directory_path() / boost::filesystem::unique_path()).string(); + Client c1("TestClient1", kp1.address(), (boost::filesystem::temp_directory_path() / boost::filesystem::unique_path()).string()); + + //mine some blocks so that client 1 has a balance + mine(c1, 1); + auto c1bal = c1.state().balance(kp1.address()); + BOOST_REQUIRE(c1bal > 0); + + //send c2 some eth from c1 + auto txAmount = c1bal / 2u; + auto gasPrice = 10 * szabo; + auto gas = eth::c_callGas; + c1.transact(kp1.secret(), txAmount, gasPrice, gas, kp2.address(), bytes()); + + //mine some more to include the transaction on chain + mine(c1, 1); + auto c2bal = c1.state().balance(kp2.address()); + BOOST_REQUIRE(c2bal > 0); + BOOST_REQUIRE(c2bal == txAmount); +} - Client c1("TestClient1", kp1.address(), c1DataDir); - Client c2("TestClient2", kp2.address(), c2DataDir); +BOOST_AUTO_TEST_CASE(mine_and_send_to_peer) +{ + KeyPair kp1 = KeyPair::create(); + KeyPair kp2 = KeyPair::create(); - short c1Port = 20000; - short c2Port = 21000; + Client c1("TestClient1", kp1.address(), (boost::filesystem::temp_directory_path() / boost::filesystem::unique_path()).string()); + Client c2("TestClient2", kp2.address(), (boost::filesystem::temp_directory_path() / boost::filesystem::unique_path()).string()); - //connect the two clients up - c1.startNetwork(c1Port); - c2.startNetwork(c2Port); - c2.connect("127.0.0.1", c1Port); + connectClients(c1, c2); //mine some blocks so that client 1 has a balance mine(c1, 1); auto c1bal = c1.state().balance(kp1.address()); BOOST_REQUIRE(c1bal > 0); -// BOOST_REQUIRE(c1bal > c1.state().fee()); //send c2 some eth from c1 -// auto txAmount = c1bal - c1.state().fee(); -// c1.transact(kp1.secret(), c2.address(), txAmount); + auto txAmount = c1bal / 2u; + auto gasPrice = 10 * szabo; + auto gas = eth::c_callGas; + c1.transact(kp1.secret(), txAmount, gasPrice, gas, kp2.address(), bytes()); //mine some more to include the transaction on chain mine(c1, 1); auto c2bal = c2.state().balance(kp2.address()); BOOST_REQUIRE(c2bal > 0); -// BOOST_REQUIRE(c2bal == txAmount); + BOOST_REQUIRE(c2bal == txAmount); +} + +BOOST_AUTO_TEST_CASE(mine_and_send_to_peer_fee_check) +{ + KeyPair kp1 = KeyPair::create(); + KeyPair kp2 = KeyPair::create(); + + Client c1("TestClient1", kp1.address(), (boost::filesystem::temp_directory_path() / boost::filesystem::unique_path()).string()); + Client c2("TestClient2", kp2.address(), (boost::filesystem::temp_directory_path() / boost::filesystem::unique_path()).string()); + + connectClients(c1, c2); + + //mine some blocks so that client 1 has a balance + mine(c1, 1); + + auto c1StartBalance = c1.state().balance(kp1.address()); + auto c2StartBalance = c2.state().balance(kp2.address()); + BOOST_REQUIRE(c1StartBalance > 0); + BOOST_REQUIRE(c2StartBalance == 0); + + //send c2 some eth from c1 + auto txAmount = c1StartBalance / 2u; + auto gasPrice = 10 * szabo; + auto gas = eth::c_callGas; + c1.transact(kp1.secret(), txAmount, gasPrice, gas, c2.address(), bytes()); + + //mine some more, this time with second client (so he can get fees from first client's tx) + mine(c2, 1); + + auto c1EndBalance = c1.state().balance(kp1.address()); + auto c2EndBalance = c2.state().balance(kp2.address()); + BOOST_REQUIRE(c1EndBalance > 0); + BOOST_REQUIRE(c1EndBalance == c1StartBalance - txAmount - gasPrice * gas); + BOOST_REQUIRE(c2EndBalance > 0); }