| 111 | } |
| 112 | |
| 113 | void CWallet::SyncTransaction(const uint256 &hash, CBaseTx *pTx, const CBlock *pBlock) { |
| 114 | assert(pTx != nullptr || pBlock != nullptr); |
| 115 | |
| 116 | if (hash.IsNull() && pTx == nullptr) { // this is block Sync |
| 117 | uint256 blockhash = pBlock->GetHash(); |
| 118 | auto GenesisBlockProgress = [&]() {}; |
| 119 | |
| 120 | auto ConnectBlockProgress = [&]() { |
| 121 | CWalletAccountTxDb acctTxDb(this, blockhash, pBlock->GetHeight()); |
| 122 | for (const auto &sptx : pBlock->vptx) { |
| 123 | uint256 txid = sptx->GetHash(); |
| 124 | // confirm the tx is mine |
| 125 | if (IsMine(sptx.get())) { |
| 126 | acctTxDb.AddTx(txid, sptx.get()); |
| 127 | } |
| 128 | if (unconfirmedTx.count(txid) > 0) { |
| 129 | CWalletDB(strWalletFile).EraseUnconfirmedTx(txid); |
| 130 | unconfirmedTx.erase(txid); |
| 131 | } |
| 132 | } |
| 133 | if (acctTxDb.GetTxSize() > 0) { // write to disk |
| 134 | mapInBlockTx[blockhash] = acctTxDb; // add to map |
| 135 | acctTxDb.WriteToDisk(); |
| 136 | } |
| 137 | }; |
| 138 | |
| 139 | auto DisConnectBlockProgress = [&]() { |
| 140 | for (const auto &sptx : pBlock->vptx) { |
| 141 | if (sptx->IsBlockRewardTx()) { |
| 142 | continue; |
| 143 | } |
| 144 | if (IsMine(sptx.get())) { |
| 145 | unconfirmedTx[sptx.get()->GetHash()] = sptx.get()->GetNewInstance(); |
| 146 | CWalletDB(strWalletFile).WriteUnconfirmedTx(sptx.get()->GetHash(), unconfirmedTx[sptx.get()->GetHash()]); |
| 147 | } |
| 148 | } |
| 149 | if (mapInBlockTx.count(blockhash)) { |
| 150 | CWalletDB(strWalletFile).EraseBlockTx(blockhash); |
| 151 | mapInBlockTx.erase(blockhash); |
| 152 | } |
| 153 | }; |
| 154 | |
| 155 | auto IsConnect = [&]() { // Connect or disconnect |
| 156 | return mapBlockIndex.count(blockhash) && chainActive.Contains(mapBlockIndex[blockhash]); |
| 157 | }; |
| 158 | |
| 159 | { |
| 160 | LOCK2(cs_main, cs_wallet); |
| 161 | // GenesisBlock progress |
| 162 | if (SysCfg().GetGenesisBlockHash() == blockhash) { |
| 163 | GenesisBlockProgress(); |
| 164 | } else if (IsConnect()) { |
| 165 | // connect block |
| 166 | ConnectBlockProgress(); |
| 167 | } else { |
| 168 | // disconnect block |
| 169 | DisConnectBlockProgress(); |
| 170 | } |
no test coverage detected