diff --git a/src/crosschain.cpp b/src/crosschain.cpp index dd352b025..10910a48a 100644 --- a/src/crosschain.cpp +++ b/src/crosschain.cpp @@ -1,8 +1,11 @@ #include "cc/eval.h" +#include "crosschain.h" #include "importcoin.h" #include "main.h" #include "notarisationdb.h" -#include "komodo_structs.h" + + +int NOTARISATION_SCAN_LIMIT_BLOCKS = 1440; /* @@ -32,7 +35,7 @@ uint256 CalculateProofRoot(const char* symbol, uint32_t targetCCid, int kmdHeigh int seenOwnNotarisations = 0; - for (int i=0; i<1440; i++) { + for (int i=0; i kmdHeight) break; NotarisationsInBlock notarisations; uint256 blockHash = *chainActive[kmdHeight-i]->phashBlock; @@ -60,6 +63,32 @@ end: } +/* + * Get a notarisation from a given height + * + * Will scan notarisations leveldb up to a limit + */ +template +int ScanNotarisationsFromHeight(int nHeight, const IsTarget f, Notarisation &found) +{ + int limit = std::min(nHeight + NOTARISATION_SCAN_LIMIT_BLOCKS, chainActive.Height()); + + for (int h=nHeight; hphashBlock; + if (!GetBlockNotarisations(blockHash, notarisations)) + continue; + + BOOST_FOREACH(found, notarisations) { + if (f(found)) { + return h; + } + } + } + return 0; +} + + /* On KMD */ TxProof GetCrossChainProof(const uint256 txid, const char* targetSymbol, uint32_t targetCCid, const TxProof assetChainProof) @@ -152,12 +181,13 @@ void CompleteImportTransaction(CTransaction &importTx) } -struct notarized_checkpoint *komodo_npptr_at(int idx); -struct notarized_checkpoint *komodo_npptr_for_height(int32_t height, int *idx); +bool IsSameAssetChain(const Notarisation ¬a) { + return strcmp(nota.second.symbol, ASSETCHAINS_SYMBOL) == 0; +}; /* On assetchain */ -bool GetNextBacknotarisation(uint256 kmdNotarisationTxid, std::pair &out) +bool GetNextBacknotarisation(uint256 kmdNotarisationTxid, Notarisation &out) { /* * Here we are given a txid, and a proof. @@ -168,18 +198,10 @@ bool GetNextBacknotarisation(uint256 kmdNotarisationTxid, std::pairnotarized_desttxid, out); - throw std::runtime_error("Can't get backnotarisation"); + return (bool) ScanNotarisationsFromHeight(bn.second.height+1, &IsSameAssetChain, out); } -struct notarized_checkpoint* komodo_npptr(int32_t height); - /* * On assetchain * in: txid @@ -187,9 +209,9 @@ struct notarized_checkpoint* komodo_npptr(int32_t height); */ TxProof GetAssetchainProof(uint256 hash) { - int nIndex, md; + int nIndex; CBlockIndex* blockIndex; - struct notarized_checkpoint* np; + Notarisation nota; std::vector branch; { @@ -202,20 +224,18 @@ TxProof GetAssetchainProof(uint256 hash) throw std::runtime_error("tx still in mempool"); blockIndex = mapBlockIndex[blockHash]; - if (!(np = komodo_npptr(blockIndex->nHeight))) + if (!ScanNotarisationsFromHeight(blockIndex->nHeight, &IsSameAssetChain, nota)) throw std::runtime_error("notarisation not found"); // index of block in MoM leaves - nIndex = np->notarized_height - blockIndex->nHeight; - // MoMdepth shares space with ccid - md = np->MoMdepth & 0xffff; + nIndex = nota.second.height - blockIndex->nHeight; } // build merkle chain from blocks to MoM { std::vector leaves, tree; - for (int i=0; inotarized_height - i]->hashMerkleRoot; + for (int i=0; ihashMerkleRoot; leaves.push_back(mRoot); } bool fMutated; @@ -224,7 +244,7 @@ TxProof GetAssetchainProof(uint256 hash) // Check branch uint256 ourResult = SafeCheckMerkleBranch(blockIndex->hashMerkleRoot, branch, nIndex); - if (np->MoM != ourResult) + if (nota.second.MoM != ourResult) throw std::runtime_error("Failed merkle block->MoM"); } @@ -259,12 +279,10 @@ TxProof GetAssetchainProof(uint256 hash) } // Check the proof - if (np->MoM != CBlock::CheckMerkleBranch(hash, branch, nIndex)) + if (nota.second.MoM != CBlock::CheckMerkleBranch(hash, branch, nIndex)) throw std::runtime_error("Failed validating MoM"); // All done! CDataStream ssProof(SER_NETWORK, PROTOCOL_VERSION); - return std::make_pair(np->notarized_desttxid, MerkleBranch(nIndex, branch)); + return std::make_pair(nota.second.txHash, MerkleBranch(nIndex, branch)); } - - diff --git a/src/main.cpp b/src/main.cpp index d8dc2ddf1..b67b1c478 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2919,7 +2919,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin } // Record Notarisations - NotarisationsInBlock notarisations = GetNotarisationsInBlock(block, pindex->nHeight); + NotarisationsInBlock notarisations = ScanBlockNotarisations(block, pindex->nHeight); if (notarisations.size() > 0) { CLevelDBBatch batch; batch.Write(block.GetHash(), notarisations); diff --git a/src/notarisationdb.cpp b/src/notarisationdb.cpp index 4b2ade793..6210d88dd 100644 --- a/src/notarisationdb.cpp +++ b/src/notarisationdb.cpp @@ -12,7 +12,7 @@ NotarisationDB *pnotarisations; NotarisationDB::NotarisationDB(size_t nCacheSize, bool fMemory, bool fWipe) : CLevelDBWrapper(GetDataDir() / "notarisations", nCacheSize, fMemory, fWipe, false, 64) { } -NotarisationsInBlock GetNotarisationsInBlock(const CBlock &block, int nHeight) +NotarisationsInBlock ScanBlockNotarisations(const CBlock &block, int nHeight) { EvalRef eval; NotarisationsInBlock vNotarisations; diff --git a/src/notarisationdb.h b/src/notarisationdb.h index 59c500fc4..ce5360e7d 100644 --- a/src/notarisationdb.h +++ b/src/notarisationdb.h @@ -18,7 +18,7 @@ extern NotarisationDB *pnotarisations; typedef std::pair Notarisation; typedef std::vector NotarisationsInBlock; -NotarisationsInBlock GetNotarisationsInBlock(const CBlock &block, int nHeight); +NotarisationsInBlock ScanBlockNotarisations(const CBlock &block, int nHeight); bool GetBlockNotarisations(uint256 blockHash, NotarisationsInBlock &nibs); bool GetBackNotarisation(uint256 notarisationHash, Notarisation &n); void WriteBackNotarisations(const NotarisationsInBlock notarisations, CLevelDBBatch &batch);