Skip to content

Commit

Permalink
-walletnotify for new wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
mike31 committed Feb 9, 2017
1 parent 60f205a commit 342cf0f
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/core/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ std::string HelpMessage(HelpMessageMode mode)
strUsage += " -maxtxfee=<amt> " + strprintf(_("Maximum total fees to use in a single wallet transaction, setting too low may abort large transactions (default: %s)"), FormatMoney(maxTxFee)) + "\n";
strUsage += " -upgradewallet " + _("Upgrade wallet to latest format") + " " + _("on startup") + "\n";
strUsage += " -wallet=<file> " + _("Specify wallet file (within data directory)") + " " + strprintf(_("(default: %s)"), "wallet.dat") + "\n";
strUsage += " -walletnotify=<cmd> " + _("Execute command when a wallet transaction changes (%s in cmd is replaced by TxID)") + "\n";
strUsage += " -walletnotify=<cmd> " + _("Execute command when a wallet transaction changes (%s in cmd is replaced by TxID, see only documentation for more options)") + "\n";
/* MCHN START */
strUsage += " -walletdbversion=1|2 " + _("Specify wallet version, 1 - not scalable, 2 (default) - scalable") + "\n";
strUsage += " -autosubscribe=streams|assets|\"streams,assets\"|\"assets,streams\" " + _("Automatically subscribe to new streams and/or assets") + "\n";
Expand Down
4 changes: 2 additions & 2 deletions src/core/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1391,7 +1391,7 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa
/* MCHN START */
if(fAddToWallet)
{
pwalletTxsMain->AddTx(NULL,tx,-1,NULL,-1);
pwalletTxsMain->AddTx(NULL,tx,-1,NULL,-1,0);
}
/* MCHN END */
if(fAddToWallet)
Expand Down Expand Up @@ -2647,7 +2647,7 @@ bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew, CBlock *
for (unsigned int i = 0; i < pblock->vtx.size(); i++)
{
const CTransaction &tx = pblock->vtx[i];
err=pwalletTxsMain->AddTx(NULL,tx,pindexNew->nHeight,&pos,i);
err=pwalletTxsMain->AddTx(NULL,tx,pindexNew->nHeight,&pos,i,pindexNew->GetBlockHash());
if(err)
{
return error("ConnectTip() : ConnectBlock %s failed, Wtxs AddTx %s, error: %d", pindexNew->GetBlockHash().ToString(),tx.GetHash().ToString(),err);
Expand Down
2 changes: 1 addition & 1 deletion src/protocol/multichainblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ bool ReplayMemPool(CTxMemPool& pool, int from,bool accept)
}
else
{
pwalletTxsMain->AddTx(NULL,tx,-1,NULL,-1);
pwalletTxsMain->AddTx(NULL,tx,-1,NULL,-1,0);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1493,7 +1493,7 @@ int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate,bo
{
if(pindex->nHeight) // Skip 0-block coinbase
{
err=pwalletTxsMain->AddTx(imp,tx,pindex->nHeight,&pos,block_tx_index);
err=pwalletTxsMain->AddTx(imp,tx,pindex->nHeight,&pos,block_tx_index,pindex->GetBlockHash());
}
}
}
Expand Down Expand Up @@ -1544,7 +1544,7 @@ int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate,bo
{
const CTransaction& tx = mempool.mapTx[hash].GetTx();
LogPrint("wallet","wtxs: Mempool tx: %s\n",hash.ToString().c_str());
pwalletTxsMain->AddTx(imp,tx,-1,NULL,-1);
pwalletTxsMain->AddTx(imp,tx,-1,NULL,-1,0);
}
}

Expand Down Expand Up @@ -3049,7 +3049,7 @@ bool CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey, stri
}
else
{
pwalletTxsMain->AddTx(NULL,wtxNew,-1,NULL,-1);
pwalletTxsMain->AddTx(NULL,wtxNew,-1,NULL,-1,0);
SyncWithWallets(wtxNew, NULL);
}

Expand Down
105 changes: 102 additions & 3 deletions src/wallet/wallettxs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,96 @@
// MultiChain code distributed under the GPLv3 license, see COPYING file.

#include "wallet/wallettxs.h"
#include "utils/core_io.h"

#include "json/json_spirit_utils.h"
#include "json/json_spirit_value.h"
using namespace json_spirit;

#include <boost/thread.hpp>
#include <boost/algorithm/string/replace.hpp>
#include "json/json_spirit_writer_template.h"

int64_t GetAdjustedTime();
void TxToJSON(const CTransaction& tx, const uint256 hashBlock, Object& entry);
bool CBitcoinAddressFromTxEntity(CBitcoinAddress &address,mc_TxEntity *lpEntity);

using namespace std;


void WalletTxNotify(mc_TxImport *imp,const CWalletTx& tx,int block,bool fFound,uint256 block_hash)
{
std::string strNotifyCmd = GetArg("-walletnotify", "");
if ( strNotifyCmd.empty() )
{
return;
}
if(imp->m_ImportID)
{
return;
}

boost::replace_all(strNotifyCmd, "%s", tx.GetHash().ToString());

boost::replace_all(strNotifyCmd, "%c", strprintf("%d",fFound ? 0 : 1));
boost::replace_all(strNotifyCmd, "%n", strprintf("%d",block));
boost::replace_all(strNotifyCmd, "%b", block_hash.ToString());
boost::replace_all(strNotifyCmd, "%h", EncodeHexTx(*static_cast<const CTransaction*>(&tx)));

string strAddresses="";
string strEntities="";
CBitcoinAddress address;
mc_EntityDetails entity;
uint256 txid;

for(int i=0;i<imp->m_TmpEntities->GetCount();i++)
{
mc_TxEntity *lpent;
lpent=(mc_TxEntity *)imp->m_TmpEntities->GetRow(i);
if(lpent->m_EntityType & MC_TET_CHAINPOS)
{
switch(lpent->m_EntityType & MC_TET_TYPE_MASK)
{
case MC_TET_PUBKEY_ADDRESS:
case MC_TET_SCRIPT_ADDRESS:
if(CBitcoinAddressFromTxEntity(address,lpent))
{
if(strAddresses.size())
{
strAddresses += ",";
}
strAddresses += address.ToString();
}
break;
case MC_TET_STREAM:
case MC_TET_ASSET:
if(mc_gState->m_Assets->FindEntityByShortTxID(&entity,lpent->m_EntityID))
{
if(strEntities.size())
{
strEntities += ",";
}
txid=*(uint256*)entity.GetTxID();
strEntities += txid.ToString();
}
break;
}
}
}

boost::replace_all(strNotifyCmd, "%a", strAddresses);
boost::replace_all(strNotifyCmd, "%e", strEntities);

Object result;
TxToJSON(tx, block_hash, result);
string str=write_string(Value(result),false);
boost::replace_all(str, "\"", "\\\"");
boost::replace_all(strNotifyCmd, "%j", "\"" + str + "\"");


boost::thread t(runCommand, strNotifyCmd); // thread runs free
}

void mc_Coin::Zero()
{
m_EntityID=0;
Expand Down Expand Up @@ -1581,18 +1666,18 @@ CWalletTx mc_WalletTxChoppedCopy(CWallet *lpWallet,const CWalletTx& tx)
}


int mc_WalletTxs::AddTx(mc_TxImport *import,const CTransaction& tx,int block,CDiskTxPos* block_pos,uint32_t block_tx_index)
int mc_WalletTxs::AddTx(mc_TxImport *import,const CTransaction& tx,int block,CDiskTxPos* block_pos,uint32_t block_tx_index,uint256 block_hash)
{
if((m_Mode & MC_WMD_TXS) == 0)
{
return MC_ERR_NOERROR;
}
CWalletTx wtx(m_lpWallet,tx);

return AddTx(import,wtx,block,block_pos,block_tx_index);
return AddTx(import,wtx,block,block_pos,block_tx_index,block_hash);
}

int mc_WalletTxs::AddTx(mc_TxImport *import,const CWalletTx& tx,int block,CDiskTxPos* block_pos,uint32_t block_tx_index)
int mc_WalletTxs::AddTx(mc_TxImport *import,const CWalletTx& tx,int block,CDiskTxPos* block_pos,uint32_t block_tx_index,uint256 block_hash)
{
int err,i,j,entcount,lockres,entpos;
mc_TxImport *imp;
Expand Down Expand Up @@ -1628,6 +1713,7 @@ int mc_WalletTxs::AddTx(mc_TxImport *import,const CWalletTx& tx,int block,CDiskT
std::vector<mc_Coin> txoutsOut;
uint256 hash;
unsigned char *ptrOut;
bool fAlreadyInTheWalletForNotify;

uint32_t txsize;
uint32_t txfullsize;
Expand Down Expand Up @@ -2293,6 +2379,17 @@ int mc_WalletTxs::AddTx(mc_TxImport *import,const CWalletTx& tx,int block,CDiskT
block_tx_offset=0;
}

fAlreadyInTheWalletForNotify=false;
if(!GetArg("-walletnotify", "").empty())
{
mc_TxDefRow StoredTxDef;
if(m_Database->GetTx(&StoredTxDef,(unsigned char*)&hash) == 0)
{
fAlreadyInTheWalletForNotify=true;
}
}


LogPrint("wallet","wtxs: Found %d entities in tx %s, flags: %08X, import %d\n",imp->m_TmpEntities->GetCount(),tx.GetHash().ToString().c_str(),flags,imp->m_ImportID);
err=m_Database->AddTx(imp,(unsigned char*)&hash,(unsigned char*)&ss[0],txsize,txfullsize,block,block_file,block_offset,block_tx_offset,block_tx_index,flags,timestamp,imp->m_TmpEntities);
if(err == MC_ERR_NOERROR) // Adding tx to unconfirmed send
Expand All @@ -2307,6 +2404,8 @@ int mc_WalletTxs::AddTx(mc_TxImport *import,const CWalletTx& tx,int block,CDiskT
}
}

WalletTxNotify(imp,tx,block,fAlreadyInTheWalletForNotify,block_hash);

if(err == MC_ERR_NOERROR) // Updating UTXO map
{
for(i=0;i<(int)txoutsIn.size();i++)
Expand Down
6 changes: 4 additions & 2 deletions src/wallet/wallettxs.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,17 @@ typedef struct mc_WalletTxs
const CTransaction& tx, // Tx to add
int block, // block height, -1 for mempool
CDiskTxPos* block_pos, // Position in the block
uint32_t block_tx_index); // Tx index in block
uint32_t block_tx_index, // Tx index in block
uint256 block_hash); // Block hash


int AddTx( // Adds tx to the wallet
mc_TxImport *import, // Import object, NULL if chain update
const CWalletTx& tx, // Tx to add
int block, // block height, -1 for mempool
CDiskTxPos* block_pos, // Position in the block
uint32_t block_tx_index); // Tx index in block
uint32_t block_tx_index, // Tx index in block
uint256 block_hash); // Block hash

int BeforeCommit(mc_TxImport *import); // Should be called before re-adding tx while processing block
int Commit(mc_TxImport *import); // Commit when block was processed
Expand Down

0 comments on commit 342cf0f

Please sign in to comment.