Skip to content

Commit

Permalink
Cache scrypt hashes on disk
Browse files Browse the repository at this point in the history
instead of recalculating every time. This policy is able to give us 10-20x startng speed up. User can disable caching with -fastindex=0 option.

Note that this commit makes blockindex code incompatible with old blockchain database files. You need to remove old blockchain files and syncronize with the network again.

Another changes:

1. Stake pooled keys are removed.
  • Loading branch information
alex authored and alex committed Dec 31, 2013
1 parent 4c8a536 commit c0e8991
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 42 deletions.
8 changes: 3 additions & 5 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ CClientUIInterface uiInterface;
std::string strWalletFileName;
unsigned int nNodeLifespan;
unsigned int nDerivationMethodIndex;
bool fUseFastIndex;
enum Checkpoints::CPMode CheckpointsMode;

//////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -250,8 +251,6 @@ std::string HelpMessage()
" -bind=<addr> " + _("Bind to given address. Use [host]:port notation for IPv6") + "\n" +
" -dnsseed " + _("Find peers using DNS lookup (default: 1)") + "\n" +
" -cppolicy " + _("Sync checkpoints policy (default: strict)") + "\n" +
" -stakepooledkeys " + _("Use pooled pubkeys for the last coinstake output (default: 0)") + "\n" +
" -derivationmethod " + _("Which key derivation method to use by default (default: sha512)") + "\n" +
" -banscore=<n> " + _("Threshold for disconnecting misbehaving peers (default: 100)") + "\n" +
" -bantime=<n> " + _("Number of seconds to keep misbehaving peers from reconnecting (default: 86400)") + "\n" +
" -maxreceivebuffer=<n> " + _("Maximum per-connection receive buffer, <n>*1000 bytes (default: 5000)") + "\n" +
Expand Down Expand Up @@ -360,7 +359,7 @@ bool AppInit2()
// ********************************************************* Step 2: parameter interactions

nNodeLifespan = GetArg("-addrlifespan", 7);
fStakeUsePooledKeys = GetBoolArg("-stakepooledkeys", false);
fUseFastIndex = GetBoolArg("-fastindex", true);

CheckpointsMode = Checkpoints::STRICT;
std::string strCpMode = GetArg("-cppolicy", "strict");
Expand All @@ -374,8 +373,7 @@ bool AppInit2()
if(strCpMode == "permissive")
CheckpointsMode = Checkpoints::PERMISSIVE;

if(GetArg("-derivationmethod", "sha512") == "scrypt+sha512")
nDerivationMethodIndex = 1;
nDerivationMethodIndex = 0;

fTestNet = GetBoolArg("-testnet");
if (fTestNet) {
Expand Down
1 change: 0 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ const string strMessageMagic = "NovaCoin Signed Message:\n";
int64 nTransactionFee = MIN_TX_FEE;
int64 nMinimumInputValue = MIN_TX_FEE;

bool fStakeUsePooledKeys = false;
extern enum Checkpoints::CPMode CheckpointsMode;

//////////////////////////////////////////////////////////////////////////////
Expand Down
16 changes: 13 additions & 3 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ extern std::map<uint256, CBlock*> mapOrphanBlocks;
// Settings
extern int64 nTransactionFee;
extern int64 nMinimumInputValue;
extern bool fStakeUsePooledKeys;
extern bool fUseFastIndex;
extern unsigned int nDerivationMethodIndex;

// Minimum disk space required - used in CheckDiskSpace()
Expand Down Expand Up @@ -1349,6 +1349,9 @@ class CBlockIndex
/** Used to marshal pointers into hashes for db storage. */
class CDiskBlockIndex : public CBlockIndex
{
private:
uint256 blockHash;

public:
uint256 hashPrev;
uint256 hashNext;
Expand All @@ -1357,6 +1360,7 @@ class CDiskBlockIndex : public CBlockIndex
{
hashPrev = 0;
hashNext = 0;
blockHash = 0;
}

explicit CDiskBlockIndex(CBlockIndex* pindex) : CBlockIndex(*pindex)
Expand Down Expand Up @@ -1398,20 +1402,26 @@ class CDiskBlockIndex : public CBlockIndex
READWRITE(nTime);
READWRITE(nBits);
READWRITE(nNonce);
READWRITE(blockHash);
)

uint256 GetBlockHash() const
{
if (fUseFastIndex && (nTime < GetAdjustedTime() - 12 * nMaxClockDrift) && blockHash != 0)
return blockHash;

CBlock block;
block.nVersion = nVersion;
block.hashPrevBlock = hashPrev;
block.hashMerkleRoot = hashMerkleRoot;
block.nTime = nTime;
block.nBits = nBits;
block.nNonce = nNonce;
return block.GetHash();
}

const_cast<CDiskBlockIndex*>(this)->blockHash = block.GetHash();

return blockHash;
}

std::string ToString() const
{
Expand Down
8 changes: 0 additions & 8 deletions src/qt/locale/bitcoin_en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2786,14 +2786,6 @@ This label turns red, if the priority is smaller than &quot;medium&quot;.
<source>Sync checkpoints policy (default: strict)</source>
<translation type="obsolete">Sync checkpoints policy (default: strict)</translation>
</message>
<message>
<source>Use pooled pubkeys for the last coinstake output (default: 0)</source>
<translation type="obsolete">Use pooled pubkeys for the last coinstake output (default: 0)</translation>
</message>
<message>
<source>Which key derivation method to use by default (default: sha512)</source>
<translation type="obsolete">Which key derivation method to use by default (default: sha512)</translation>
</message>
<message>
<location line="+12"/>
<source>Invalid -tor address: &apos;%s&apos;</source>
Expand Down
8 changes: 0 additions & 8 deletions src/qt/locale/bitcoin_ru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2798,14 +2798,6 @@ This label turns red, if the priority is smaller than &quot;medium&quot;.
<source>Sync checkpoints policy (default: strict)</source>
<translation type="obsolete">Политика синхронизированных меток (по умолчанию: strict)</translation>
</message>
<message>
<source>Use pooled pubkeys for the last coinstake output (default: 0)</source>
<translation type="obsolete">Использовать для coinstake транзакций ключи из пула (по умолчанию: 0)</translation>
</message>
<message>
<source>Which key derivation method to use by default (default: sha512)</source>
<translation type="obsolete">Выбор функции для создания ключа шифрования (по умолчанию: sha512)</translation>
</message>
<message>
<location line="+12"/>
<source>Invalid -tor address: &apos;%s&apos;</source>
Expand Down
6 changes: 4 additions & 2 deletions src/txdb-bdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,10 @@ bool CTxDB::LoadBlockIndexGuts()
CDiskBlockIndex diskindex;
ssValue >> diskindex;

uint256 blockHash = diskindex.GetBlockHash();

// Construct block index object
CBlockIndex* pindexNew = InsertBlockIndex(diskindex.GetBlockHash());
CBlockIndex* pindexNew = InsertBlockIndex(blockHash);
pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev);
pindexNew->pnext = InsertBlockIndex(diskindex.hashNext);
pindexNew->nFile = diskindex.nFile;
Expand All @@ -397,7 +399,7 @@ bool CTxDB::LoadBlockIndexGuts()
pindexNew->nNonce = diskindex.nNonce;

// Watch for genesis block
if (pindexGenesisBlock == NULL && diskindex.GetBlockHash() == (!fTestNet ? hashGenesisBlock : hashGenesisBlockTestNet))
if (pindexGenesisBlock == NULL && blockHash == (!fTestNet ? hashGenesisBlock : hashGenesisBlockTestNet))
pindexGenesisBlock = pindexNew;

if (!pindexNew->CheckIndex())
Expand Down
6 changes: 4 additions & 2 deletions src/txdb-leveldb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,10 @@ bool CTxDB::LoadBlockIndex()
CDiskBlockIndex diskindex;
ssValue >> diskindex;

uint256 blockHash = diskindex.GetBlockHash();

// Construct block index object
CBlockIndex* pindexNew = InsertBlockIndex(diskindex.GetBlockHash());
CBlockIndex* pindexNew = InsertBlockIndex(blockHash);
pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev);
pindexNew->pnext = InsertBlockIndex(diskindex.hashNext);
pindexNew->nFile = diskindex.nFile;
Expand All @@ -340,7 +342,7 @@ bool CTxDB::LoadBlockIndex()
pindexNew->nNonce = diskindex.nNonce;

// Watch for genesis block
if (pindexGenesisBlock == NULL && diskindex.GetBlockHash() == hashGenesisBlock)
if (pindexGenesisBlock == NULL && blockHash == (!fTestNet ? hashGenesisBlock : hashGenesisBlockTestNet))
pindexGenesisBlock = pindexNew;

if (!pindexNew->CheckIndex()) {
Expand Down
13 changes: 0 additions & 13 deletions src/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1627,19 +1627,6 @@ bool CWallet::CreateCoinStake(const CKeyStore& keystore, unsigned int nBits, int
// Set output amount
if (txNew.vout.size() == 3)
{
// Should we use keys from pool for the last coinstake output?
if (fStakeUsePooledKeys)
{
CReserveKey reservekey((CWallet*) &keystore);

// Replace current key with the new one
txNew.vout[2].SetNull();
txNew.vout[2].scriptPubKey << reservekey.GetReservedKey() << OP_CHECKSIG;

// Remove key from pool
reservekey.KeepKey();
}

txNew.vout[1].nValue = ((nCredit - nMinFee) / 2 / CENT) * CENT;
txNew.vout[2].nValue = nCredit - nMinFee - txNew.vout[1].nValue;
}
Expand Down

0 comments on commit c0e8991

Please sign in to comment.