Skip to content

Commit

Permalink
Port wallet db
Browse files Browse the repository at this point in the history
  • Loading branch information
timemarkovqtum committed Jun 18, 2024
1 parent e3a3dad commit e13a643
Show file tree
Hide file tree
Showing 5 changed files with 658 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/wallet/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ class CWalletTx
const Txid& GetHash() const LIFETIMEBOUND { return tx->GetHash(); }
const Wtxid& GetWitnessHash() const LIFETIMEBOUND { return tx->GetWitnessHash(); }
bool IsCoinBase() const { return tx->IsCoinBase(); }
bool IsCoinStake() const { return tx->IsCoinStake(); }

private:
// Disable copying of CWalletTx objects to prevent bugs where instances get
Expand Down
120 changes: 115 additions & 5 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,23 @@ void CWallet::AddToSpends(const COutPoint& outpoint, const uint256& wtxid, Walle
SyncMetaData(range);
}

void CWallet::RemoveFromSpends(const COutPoint& outpoint, const uint256& wtxid)
{
std::pair<TxSpends::iterator, TxSpends::iterator> range;
range = mapTxSpends.equal_range(outpoint);
TxSpends::iterator it = range.first;
for(; it != range.second; ++ it)
{
if(it->second == wtxid)
{
mapTxSpends.erase(it);
break;
}
}
range = mapTxSpends.equal_range(outpoint);
if(range.first != range.second)
SyncMetaData(range);
}

void CWallet::AddToSpends(const CWalletTx& wtx, WalletBatch* batch)
{
Expand All @@ -786,6 +803,15 @@ void CWallet::AddToSpends(const CWalletTx& wtx, WalletBatch* batch)
AddToSpends(txin.prevout, wtx.GetHash(), batch);
}

void CWallet::RemoveFromSpends(const CWalletTx& wtx)
{
if (wtx.IsCoinBase()) // Coinbases don't spend anything!
return;

for(const CTxIn& txin : wtx.tx->vin)
RemoveFromSpends(txin.prevout, wtx.GetHash());
}

bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
{
if (IsCrypted())
Expand Down Expand Up @@ -1306,7 +1332,9 @@ bool CWallet::AbandonTransaction(const uint256& hashTx)

auto try_updating_state = [](CWalletTx& wtx) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet) {
// If the orig tx was not in block/mempool, none of its spends can be.
assert(!wtx.isConfirmed());
if (!wtx.IsCoinStake()) {
assert(!wtx.isConfirmed());
}
assert(!wtx.InMempool());
// If already conflicted or abandoned, no need to set abandoned
if (!wtx.isConflicted() && !wtx.isAbandoned()) {
Expand Down Expand Up @@ -3356,6 +3384,22 @@ bool CWallet::BackupWallet(const std::string& strDest) const
return GetDatabase().Backup(strDest);
}

bool CWallet::LoadToken(const CTokenInfo &token)
{
uint256 hash = token.GetHash();
mapToken[hash] = token;

return true;
}

bool CWallet::LoadTokenTx(const CTokenTx &tokenTx)
{
uint256 hash = tokenTx.GetHash();
mapTokenTx[hash] = tokenTx;

return true;
}

CKeyPool::CKeyPool()
{
nTime = GetTime();
Expand Down Expand Up @@ -3389,22 +3433,33 @@ int CWallet::GetTxBlocksToMaturity(const CWalletTx& wtx) const
{
AssertLockHeld(cs_wallet);

if (!wtx.IsCoinBase()) {
if (!(wtx.IsCoinBase() || wtx.IsCoinStake())) {
return 0;
}
int chain_depth = GetTxDepthInMainChain(wtx);
assert(chain_depth >= 0); // coinbase tx should not be conflicted
return std::max(0, (COINBASE_MATURITY+1) - chain_depth);
int nHeight = GetLastBlockHeight() + 1;
int coinbaseMaturity = Params().GetConsensus().CoinbaseMaturity(nHeight);
return std::max(0, (coinbaseMaturity+1) - chain_depth);
}

bool CWallet::IsTxImmatureCoinBase(const CWalletTx& wtx) const
bool CWallet::IsTxImmature(const CWalletTx& wtx) const
{
AssertLockHeld(cs_wallet);

// note GetBlocksToMaturity is 0 for non-coinbase tx
return GetTxBlocksToMaturity(wtx) > 0;
}

bool CWallet::IsTxImmatureCoinBase(const CWalletTx& wtx) const
{
return wtx.IsCoinBase() && IsTxImmature(wtx);
}

bool CWallet::IsTxImmatureCoinStake(const CWalletTx& wtx) const
{
return wtx.IsCoinStake() && IsTxImmature(wtx);
}

bool CWallet::IsCrypted() const
{
return HasEncryptionKeys();
Expand Down Expand Up @@ -4457,6 +4512,26 @@ util::Result<MigrationResult> MigrateLegacyToDescriptor(const std::string& walle
return res;
}

uint256 CTokenInfo::GetHash() const
{
return (HashWriter{} << SER_INFO_GETHASH(*this)).GetHash();
}

uint256 CTokenTx::GetHash() const
{
return (HashWriter{} << SER_INFO_GETHASH(*this)).GetHash();
}

uint256 CDelegationInfo::GetHash() const
{
return (HashWriter{} << SER_INFO_GETHASH(*this)).GetHash();
}

uint256 CSuperStakerInfo::GetHash() const
{
return (HashWriter{} << SER_INFO_GETHASH(*this)).GetHash();
}

void CWallet::CacheNewScriptPubKeys(const std::set<CScript>& spks, ScriptPubKeyMan* spkm)
{
for (const auto& script : spks) {
Expand All @@ -4469,4 +4544,39 @@ void CWallet::TopUpCallback(const std::set<CScript>& spks, ScriptPubKeyMan* spkm
// Update scriptPubKey cache
CacheNewScriptPubKeys(spks, spkm);
}

bool CWallet::LoadContractData(const std::string &address, const std::string &key, const std::string &value)
{
bool ret = true;
if(key == "name")
{
mapContractBook[address].name = value;
}
else if(key == "abi")
{
mapContractBook[address].abi = value;
}
else
{
ret = false;
}
return ret;
}

bool CWallet::LoadDelegation(const CDelegationInfo &delegation)
{
uint256 hash = delegation.GetHash();
mapDelegation[hash] = delegation;

return true;
}

bool CWallet::LoadSuperStaker(const CSuperStakerInfo &superStaker)
{
uint256 hash = superStaker.GetHash();
mapSuperStaker[hash] = superStaker;

return true;
}

} // namespace wallet
Loading

0 comments on commit e13a643

Please sign in to comment.