From a83e54cfd3cb8d31bafe9dd980a6871143bd77c7 Mon Sep 17 00:00:00 2001 From: Mathias Baumann Date: Thu, 4 Apr 2019 12:04:53 +0200 Subject: [PATCH] TestFramework: Use getBlockByNumber and eth_flush to get transaction confirmation --- test/ExecutionFramework.cpp | 25 +++++++++++++++++++++++++ test/ExecutionFramework.h | 1 + 2 files changed, 26 insertions(+) diff --git a/test/ExecutionFramework.cpp b/test/ExecutionFramework.cpp index 7b4397e63..23a715133 100644 --- a/test/ExecutionFramework.cpp +++ b/test/ExecutionFramework.cpp @@ -29,6 +29,9 @@ #include +#include +#include + using namespace std; using namespace dev; using namespace dev::test; @@ -137,6 +140,7 @@ void ExecutionFramework::sendMessage(bytes const& _data, bool _isCreation, u256 } string txHash = m_rpc.eth_sendTransaction(d); + waitForTransaction(txHash); m_rpc.test_mineBlocks(1); RPCSession::TransactionReceipt receipt(m_rpc.eth_getTransactionReceipt(txHash)); @@ -174,6 +178,27 @@ void ExecutionFramework::sendMessage(bytes const& _data, bool _isCreation, u256 m_transactionSuccessful = (m_gas != m_gasUsed); } +void ExecutionFramework::waitForTransaction(std::string const& _txHash) const +{ + for (int polls = 0; polls < 3000; polls++) + { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + auto pendingBlock = m_rpc.eth_getBlockByNumber("pending", false); + + if (!pendingBlock["transactions"].empty()) + { + BOOST_REQUIRE_EQUAL(pendingBlock["transactions"][0].asString(), _txHash); + return; + } + + if (polls == 200) + { + cerr << "Note: Already used 200 iterations while waiting for transaction confirmation. Issuing an eth_flush request." << endl; + m_rpc.rpcCall("eth_flush"); + } + } +} + void ExecutionFramework::sendEther(Address const& _to, u256 const& _value) { RPCSession::TransactionData d; diff --git a/test/ExecutionFramework.h b/test/ExecutionFramework.h index 4a42382d0..11e364c59 100644 --- a/test/ExecutionFramework.h +++ b/test/ExecutionFramework.h @@ -247,6 +247,7 @@ private: protected: void sendMessage(bytes const& _data, bool _isCreation, u256 const& _value = 0); void sendEther(Address const& _to, u256 const& _value); + void waitForTransaction(std::string const& _txHash) const; size_t currentTimestamp(); size_t blockTimestamp(u256 _number);