Skip to content

Commit

Permalink
Port wallet interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
timemarkovqtum committed Feb 22, 2024
1 parent 2d030b6 commit f328d2d
Show file tree
Hide file tree
Showing 8 changed files with 636 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/node/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,8 @@ class NodeImpl : public Node
}
int64_t getBlockSubsidy(int nHeight) override
{
return {};
const CChainParams& chainparams = Params();
return GetBlockSubsidy(nHeight, chainparams.GetConsensus());
}
uint64_t getNetworkStakeWeight() override
{
Expand Down Expand Up @@ -611,7 +612,8 @@ class ChainImpl : public Chain
}
std::map<COutPoint, uint32_t> getImmatureStakes() override
{
return {};
LOCK(cs_main);
return GetImmatureStakes(chainman());
}
std::optional<int> findLocatorFork(const CBlockLocator& locator) override
{
Expand Down
21 changes: 21 additions & 0 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6033,3 +6033,24 @@ CAmount GetTxGasFee(const CMutableTransaction& _tx, const CTxMemPool& mempool, C
{
return {};
}

std::map<COutPoint, uint32_t> GetImmatureStakes(ChainstateManager& chainman)
{
std::map<COutPoint, uint32_t> immatureStakes;
int height = chainman.ActiveChain().Height();
int coinbaseMaturity = chainman.GetConsensus().CoinbaseMaturity(height + 1);
for(int i = 0; i < coinbaseMaturity -1; i++) {
CBlockIndex* block = chainman.ActiveChain()[height - i];
if(block)
{
immatureStakes[block->prevoutStake] = block->nTime;
}
else
{
break;
}
}
return immatureStakes;
}
//////////////////////////////////////////////////////////////////////////////////

3 changes: 3 additions & 0 deletions src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@ static_assert(std::is_nothrow_destructible_v<CScriptCheck>);
/** Initializes the script-execution cache */
[[nodiscard]] bool InitScriptExecutionCache(size_t max_size_bytes);

std::map<COutPoint, uint32_t> GetImmatureStakes(ChainstateManager& chainman);
/////////////////////////////////////////////////////////////////

/** Functions for validating blocks and updating the block tree */

/** Context-independent validity checks */
Expand Down
102 changes: 98 additions & 4 deletions src/wallet/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ WalletTx MakeWalletTx(CWallet& wallet, const CWalletTx& wtx)
result.time = wtx.GetTxTime();
result.value_map = wtx.mapValue;
result.is_coinbase = wtx.IsCoinBase();
result.is_coinstake = wtx.IsCoinStake();
result.is_in_main_chain = wallet.IsTxInMainChain(wtx);
result.has_create_or_call = wtx.tx->HasCreateOrCall();
if(result.has_create_or_call)
{
CTxDestination tx_sender_address;
if(wtx.tx && wtx.tx->vin.size() > 0 && wallet.mapWallet.find(wtx.tx->vin[0].prevout.hash) != wallet.mapWallet.end() &&
ExtractDestination(wallet.mapWallet.at(wtx.tx->vin[0].prevout.hash).tx->vout[wtx.tx->vin[0].prevout.n].scriptPubKey, tx_sender_address)) {
result.tx_sender_key = wallet.GetKeyForDestination(tx_sender_address);
}

for(CTxDestination address : result.txout_address) {
result.txout_keys.emplace_back(wallet.GetKeyForDestination(address));
}
}
return result;
}

Expand Down Expand Up @@ -482,19 +497,98 @@ class WalletImpl : public Wallet
}
bool isUnspentAddress(const std::string &qtumAddress) override
{
return {};
LOCK(m_wallet->cs_wallet);

std::vector<COutput> vecOutputs = AvailableCoinsListUnspent(*m_wallet).All();
for (const COutput& out : vecOutputs)
{
CTxDestination address;
const CScript& scriptPubKey = out.txout.scriptPubKey;
bool fValidAddress = ExtractDestination(scriptPubKey, address);

if(fValidAddress && EncodeDestination(address) == qtumAddress && out.txout.nValue)
{
return true;
}
}
return false;
}
bool isMineAddress(const std::string &strAddress) override
{
return {};
LOCK(m_wallet->cs_wallet);

CTxDestination address = DecodeDestination(strAddress);
if(!IsValidDestination(address) || !m_wallet->IsMine(address))
{
return false;
}
return true;
}
std::vector<std::string> availableAddresses(bool fIncludeZeroValue) EXCLUSIVE_LOCKS_REQUIRED(m_wallet->cs_wallet)
{
return {};
std::vector<std::string> result;
std::vector<COutput> vecOutputs;
std::map<std::string, bool> mapAddress;

if(fIncludeZeroValue)
{
// Get the user created addresses in from the address book and add them if they are mine
for (const auto& item : m_wallet->m_address_book) {
if(!m_wallet->IsMine(item.first)) continue;
if(item.second.purpose != AddressPurpose::RECEIVE) continue;
if(item.second.receive_requests.size() == 0) continue;

std::string strAddress = EncodeDestination(item.first);
if (mapAddress.find(strAddress) == mapAddress.end())
{
mapAddress[strAddress] = true;
result.push_back(strAddress);
}
}

// Get all coins including the 0 values
CoinFilterParams params;
params.min_amount = 0;
vecOutputs = AvailableCoinsListUnspent(*m_wallet, nullptr, params).All();
}
else
{
// Get all spendable coins
vecOutputs = AvailableCoinsListUnspent(*m_wallet).All();
}

// Extract all coins addresses and add them in the list
for (const COutput& out : vecOutputs)
{
CTxDestination address;
const CScript& scriptPubKey = out.txout.scriptPubKey;
bool fValidAddress = ExtractDestination(scriptPubKey, address);

if (!fValidAddress || !m_wallet->IsMine(address)) continue;

std::string strAddress = EncodeDestination(address);
if (mapAddress.find(strAddress) == mapAddress.end())
{
mapAddress[strAddress] = true;
result.push_back(strAddress);
}
}

return result;
}
bool tryGetAvailableAddresses(std::vector<std::string> &spendableAddresses, std::vector<std::string> &allAddresses, bool &includeZeroValue) override
{
return {};
TRY_LOCK(m_wallet->cs_wallet, locked_wallet);
if (!locked_wallet) {
return false;
}

spendableAddresses = availableAddresses(false);
allAddresses = availableAddresses(true);
int num_blocks = m_wallet->GetLastBlockHeight();
includeZeroValue = num_blocks >= Params().GetConsensus().QIP5Height;

return true;
}
CoinsList listCoins() override
{
Expand Down
Loading

0 comments on commit f328d2d

Please sign in to comment.