Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: btc4ever on July 26, 2013, 12:12:12 AM



Title: Adding -walletlog option. Need some advice.
Post by: btc4ever on July 26, 2013, 12:12:12 AM
My application needs to be notified of incoming transactions and confirmations.

I am aware of the new -walletnotify flag, but I view this as a farily heavyweight solution because using it requires forking a new process and then that process must make a jsonrpc call back to bitcoind in order to call gettransaction and finally get the needed information. And I have noticed that at times bitcoind is laggy responding to jsonrpc requests.

So my initial reasoning was that it would be much more efficient to just log the transaction in json from the same place that walletnotify processing occurs in bitcoind. inside CWallet::AddToWallet().

I have implemented a patch that does this. The first problem is that confirmations is always reported as 0. So my working theory is that perhaps the code is executing too early in the codepath, before the confirmation count field gets updated.

Also, it seems that for receives, the AddToWallet() function gets called twice, but for sends it gets called 3 times. Ideally, this logging feature would log up to N confirmations, so really it needs to be somewhere that gets called for each confirmation. any suggestions where?     ( Apparently this is a limitation for the -walletnotify feature as well. )

Here is example output from the log file for a received transaction:

[{"account":"","address":"AHBL4ezNrqdipSXknANkbGGKXfJtzvv7A6","category":"receive","amount":0.10000000,"confirmations":0,"txid":"3b69b0bcc907c22803fd32299324b38b6d5082ba55fd1a8eaebf5759f5330754","time":1374792299,"timereceived":1374792299}] [{"account":"","address":"AHBL4ezNrqdipSXknANkbGGKXfJtzvv7A6","category":"receive","amount":0.10000000,"confirmations":0,"txid":"3b69b0bcc907c22803fd32299324b38b6d5082ba55fd1a8eaebf5759f5330754","time":1374792299,"timereceived":1374792299}]

This is my first time diving into bitcoind code, and I am presently intending this patch just for my own private use. I know it is rough -- so please don't rake me over the coals too much. ;-) Here's the patch:


diff --git a/src/init.cpp b/src/init.cpp
index 3dce3cf..4bfdc65 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -354,0 +355 @@ std::string HelpMessage()
+        "  -walletlog=<file>     "  + _("Log to file when a wallet transaction changes. non-standard.") + "\n" +
diff --git a/src/wallet.cpp b/src/wallet.cpp
index 872afae..364c992 100644
--- a/src/wallet.cpp
+++ b/src/wallet.cpp
@@ -12,0 +13,4 @@
+#include <boost/filesystem.hpp>
+#include "json/json_spirit_value.h"
+#include "json/json_spirit_writer_template.h"
+
@@ -13,0 +18 @@ using namespace std;
+using namespace json_spirit;
@@ -14,0 +20 @@ using namespace std;
+void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDepth, bool fLong, Array& ret);
@@ -496,0 +503,17 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn)
+        std::string strLog = GetArg("-walletlog", "");
+        if ( !strLog.empty() )
+        {
+            FILE* fh = fopen( strLog.c_str(), "a");
+            if( !fh ) {
+                printf("Unable to open wallet log file: %s", strLog.c_str() );
+            }
+            else {
+                Array ret;
+                ListTransactions(wtx, "*", 0, true, ret);
+               
+                string output = write_string(Value(ret), false) + "\n";
+                fputs( output.c_str(), fh );
+                fclose( fh );
+            }
+        }
+       


Title: Re: Adding -walletlog option. Need some advice.
Post by: kjj on July 26, 2013, 08:14:31 PM
I'm not sure that you'll actually gain much.

There is nothing internal to bitcoind that cares enough about confirmation count to give you an easy place to hook for that.

I strongly suggest that you split your problem into two parts.  The first is a lightweight program that accepts the input from -walletnotify and -blocknotify and dumps it into an asynchronous processing queue.  The second is either a cron job or a long running process that inspects that queue and can do slower things like making RPC calls back to bitcoind for more information.

As an aside, I've never seen a transaction that gave more than 2 hits it -walletnotify.  Can you tell me more about the send that you saw 3 notices for?