Skip to content

Commit

Permalink
Merge pull request namecoin#204 from domob1812/name-new
Browse files Browse the repository at this point in the history
Clean up name_new.
  • Loading branch information
domob1812 committed Dec 17, 2014
2 parents 4fb7dc5 + e897d37 commit d0dd75f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CHooks
virtual int LockinHeight() = 0;
virtual std::string IrcPrefix() = 0;
virtual void MessageStart(char* pchMessageStart) = 0;
virtual void AcceptToMemoryPool(DatabaseSet& dbset, const CTransaction& tx) = 0;
virtual bool AcceptToMemoryPool(DatabaseSet& dbset, const CTransaction& tx) = 0;
virtual void RemoveFromMemoryPool(const CTransaction& tx) = 0;

/* These are for display and wallet management purposes. Not for use to decide
Expand Down
5 changes: 3 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -509,14 +509,15 @@ CTransaction::AcceptToMemoryPool (DatabaseSet& dbset, bool fCheckInputs,
}
}

if (!hooks->AcceptToMemoryPool (dbset, *this))
return error ("AcceptToMemoryPool: hook failed");

// Store transaction in memory
CRITICAL_BLOCK(cs_mapTransactions)
{
AddToMemoryPoolUnchecked();
}

hooks->AcceptToMemoryPool (dbset, *this);

printf("AcceptToMemoryPool(): accepted %s\n", hash.ToString().substr(0,10).c_str());
return true;
}
Expand Down
74 changes: 37 additions & 37 deletions src/namecoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@ template<typename T> void ConvertTo(Value& value, bool fAllowNull=false);
static const int BUG_WORKAROUND_BLOCK_START = 139750; // Bug was not exploited before block 139872, so skip checking earlier blocks
static const int BUG_WORKAROUND_BLOCK = 150000; // Point of hard fork

/* Disallow transactions without NAMECOIN_TX_VERSION but with name outputs
after this height. */
static const int FORK_HEIGHT_TXVERSION = 300000;

map<vector<unsigned char>, uint256> mapMyNames;
map<vector<unsigned char>, set<uint256> > mapNamePending;
std::map<vchType, uint256> mapMyNames;
std::map<vchType, set<uint256> > mapNamePending;
std::set<vchType> setNewHashes;

#ifdef GUI
extern std::map<uint160, vchType> mapMyNameHashes;
Expand Down Expand Up @@ -78,7 +75,7 @@ class CNamecoinHooks : public CHooks
virtual bool Lockin(int nHeight, uint256 hash);
virtual int LockinHeight();
virtual string IrcPrefix();
virtual void AcceptToMemoryPool(DatabaseSet& dbset, const CTransaction& tx);
virtual bool AcceptToMemoryPool(DatabaseSet& dbset, const CTransaction& tx);
virtual void RemoveFromMemoryPool(const CTransaction& tx);

virtual void MessageStart(char* pchMessageStart)
Expand Down Expand Up @@ -1189,18 +1186,18 @@ Value name_new(const Array& params, bool fHelp)
"name_new <name>"
+ HelpRequiringPassphrase());

vector<unsigned char> vchName = vchFromValue(params[0]);
const vchType vchName = vchFromValue(params[0]);

CWalletTx wtx;
wtx.nVersion = NAMECOIN_TX_VERSION;

uint64 rand = GetRand((uint64)-1);
vector<unsigned char> vchRand = CBigNum(rand).getvch();
vector<unsigned char> vchToHash(vchRand);
const uint64 rand = GetRand((uint64)-1);
const vchType vchRand = CBigNum(rand).getvch();
vchType vchToHash(vchRand);
vchToHash.insert(vchToHash.end(), vchName.begin(), vchName.end());
uint160 hash = Hash160(vchToHash);
const uint160 hash = Hash160(vchToHash);

vector<unsigned char> vchPubKey = pwalletMain->GetKeyFromKeyPool();
const vchType vchPubKey = pwalletMain->GetKeyFromKeyPool();
CScript scriptPubKeyOrig;
scriptPubKeyOrig.SetBitcoinAddress(vchPubKey);
CScript scriptPubKey;
Expand Down Expand Up @@ -2032,36 +2029,40 @@ bool CNamecoinHooks::IsMine(const CTransaction& tx, const CTxOut& txout, bool ig
return false;
}

void
bool
CNamecoinHooks::AcceptToMemoryPool (DatabaseSet& dbset, const CTransaction& tx)
{
if (tx.nVersion != NAMECOIN_TX_VERSION)
return;
return true;

if (tx.vout.size() < 1)
{
error("AcceptToMemoryPool() : no output in name tx %s\n", tx.ToString().c_str());
return;
}
return error ("AcceptToMemoryPool: no output in name tx %s\n",
tx.GetHash ().ToString ().c_str ());

vector<vector<unsigned char> > vvch;
std::vector<vchType> vvch;

int op;
int nOut;

bool good = DecodeNameTx(tx, op, nOut, vvch, BUG_WORKAROUND_BLOCK);

if (!good)
{
error("AcceptToMemoryPool() : no output out script in name tx %s", tx.ToString().c_str());
return;
}
return error ("AcceptToMemoryPool: no output out script in name tx %s",
tx.GetHash ().ToString ().c_str ());

if (op != OP_NAME_NEW)
{
CRITICAL_BLOCK(cs_main)
mapNamePending[vvch[0]].insert(tx.GetHash());
}
if (op == OP_NAME_NEW)
{
const vchType& hash = vvch[0];
if (setNewHashes.count (hash) > 0)
return error ("AcceptToMemoryPool: duplicate name_new hash in tx %s",
tx.GetHash ().ToString ().c_str ());
setNewHashes.insert (hash);
}
else
CRITICAL_BLOCK (cs_main)
mapNamePending[vvch[0]].insert (tx.GetHash ());

return true;
}

void CNamecoinHooks::RemoveFromMemoryPool(const CTransaction& tx)
Expand Down Expand Up @@ -2161,17 +2162,16 @@ CNamecoinHooks::ConnectInputs (DatabaseSet& dbset,
bool found = false;

int prevOp;
vector<vchType> vvchPrevArgs;
std::vector<vchType> vvchPrevArgs;

// Bug workaround
if (fMiner || !fBlock || pindexBlock->nHeight >= BUG_WORKAROUND_BLOCK)
{
// Strict check - bug disallowed
for (int i = 0; i < tx.vin.size(); i++)
{
CTxOut& out = vTxPrev[i].vout[tx.vin[i].prevout.n];

vector<vector<unsigned char> > vvchPrevArgsRead;
const CTxOut& out = vTxPrev[i].vout[tx.vin[i].prevout.n];
std::vector<vchType> vvchPrevArgsRead;

if (DecodeNameScript(out.scriptPubKey, prevOp, vvchPrevArgsRead))
{
Expand All @@ -2191,7 +2191,7 @@ CNamecoinHooks::ConnectInputs (DatabaseSet& dbset,
bool fBug = false;
for (int i = 0; i < tx.vin.size(); i++)
{
CTxOut& out = vTxPrev[i].vout[tx.vin[i].prevout.n];
const CTxOut& out = vTxPrev[i].vout[tx.vin[i].prevout.n];

int nOldSize = vvchPrevArgs.size();
if (DecodeNameScript(out.scriptPubKey, prevOp, vvchPrevArgs))
Expand Down Expand Up @@ -2228,7 +2228,7 @@ CNamecoinHooks::ConnectInputs (DatabaseSet& dbset,
}

if (foundOuts
&& (!fBlock || pindexBlock->nHeight >= FORK_HEIGHT_TXVERSION))
&& (!fBlock || pindexBlock->nHeight >= FORK_HEIGHT_STRICTCHECKS))
return error("ConnectInputHook: non-Namecoin tx has name outputs");

// Make sure name-op outputs are not spent by a regular transaction, or the name
Expand All @@ -2238,7 +2238,7 @@ CNamecoinHooks::ConnectInputs (DatabaseSet& dbset,
return true;
}

vector<vchType> vvchArgs;
std::vector<vchType> vvchArgs;
int op;
int nOut;

Expand Down Expand Up @@ -2492,7 +2492,7 @@ bool CNamecoinHooks::CheckTransaction(const CTransaction& tx)
if (tx.nVersion != NAMECOIN_TX_VERSION)
return true;

vector<vector<unsigned char> > vvch;
std::vector<vchType> vvch;
int op;
int nOut;

Expand Down
1 change: 1 addition & 0 deletions src/namecoin.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class uint256;

extern std::map<vchType, uint256> mapMyNames;
extern std::map<vchType, std::set<uint256> > mapNamePending;
extern std::set<vchType> setNewHashes;

std::string stringFromVch(const std::vector<unsigned char> &vch);
std::vector<unsigned char> vchFromString(const std::string &str);
Expand Down

0 comments on commit d0dd75f

Please sign in to comment.