create function to query notarisations leveldb by height
This commit is contained in:
@@ -1,8 +1,11 @@
|
|||||||
#include "cc/eval.h"
|
#include "cc/eval.h"
|
||||||
|
#include "crosschain.h"
|
||||||
#include "importcoin.h"
|
#include "importcoin.h"
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "notarisationdb.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;
|
int seenOwnNotarisations = 0;
|
||||||
|
|
||||||
for (int i=0; i<1440; i++) {
|
for (int i=0; i<NOTARISATION_SCAN_LIMIT_BLOCKS; i++) {
|
||||||
if (i > kmdHeight) break;
|
if (i > kmdHeight) break;
|
||||||
NotarisationsInBlock notarisations;
|
NotarisationsInBlock notarisations;
|
||||||
uint256 blockHash = *chainActive[kmdHeight-i]->phashBlock;
|
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 <typename IsTarget>
|
||||||
|
int ScanNotarisationsFromHeight(int nHeight, const IsTarget f, Notarisation &found)
|
||||||
|
{
|
||||||
|
int limit = std::min(nHeight + NOTARISATION_SCAN_LIMIT_BLOCKS, chainActive.Height());
|
||||||
|
|
||||||
|
for (int h=nHeight; h<limit; h++) {
|
||||||
|
NotarisationsInBlock notarisations;
|
||||||
|
uint256 blockHash = *chainActive[h]->phashBlock;
|
||||||
|
if (!GetBlockNotarisations(blockHash, notarisations))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
BOOST_FOREACH(found, notarisations) {
|
||||||
|
if (f(found)) {
|
||||||
|
return h;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* On KMD */
|
/* On KMD */
|
||||||
TxProof GetCrossChainProof(const uint256 txid, const char* targetSymbol, uint32_t targetCCid,
|
TxProof GetCrossChainProof(const uint256 txid, const char* targetSymbol, uint32_t targetCCid,
|
||||||
const TxProof assetChainProof)
|
const TxProof assetChainProof)
|
||||||
@@ -152,12 +181,13 @@ void CompleteImportTransaction(CTransaction &importTx)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
struct notarized_checkpoint *komodo_npptr_at(int idx);
|
bool IsSameAssetChain(const Notarisation ¬a) {
|
||||||
struct notarized_checkpoint *komodo_npptr_for_height(int32_t height, int *idx);
|
return strcmp(nota.second.symbol, ASSETCHAINS_SYMBOL) == 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/* On assetchain */
|
/* On assetchain */
|
||||||
bool GetNextBacknotarisation(uint256 kmdNotarisationTxid, std::pair<uint256,NotarisationData> &out)
|
bool GetNextBacknotarisation(uint256 kmdNotarisationTxid, Notarisation &out)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* Here we are given a txid, and a proof.
|
* Here we are given a txid, and a proof.
|
||||||
@@ -168,18 +198,10 @@ bool GetNextBacknotarisation(uint256 kmdNotarisationTxid, std::pair<uint256,Nota
|
|||||||
if (!GetBackNotarisation(kmdNotarisationTxid, bn))
|
if (!GetBackNotarisation(kmdNotarisationTxid, bn))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
int npIdx;
|
return (bool) ScanNotarisationsFromHeight(bn.second.height+1, &IsSameAssetChain, out);
|
||||||
struct notarized_checkpoint* np = komodo_npptr_for_height(bn.second.height, &npIdx);
|
|
||||||
if (!(np = komodo_npptr_at(npIdx+1)))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return GetBackNotarisation(np->notarized_desttxid, out);
|
|
||||||
throw std::runtime_error("Can't get backnotarisation");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
struct notarized_checkpoint* komodo_npptr(int32_t height);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* On assetchain
|
* On assetchain
|
||||||
* in: txid
|
* in: txid
|
||||||
@@ -187,9 +209,9 @@ struct notarized_checkpoint* komodo_npptr(int32_t height);
|
|||||||
*/
|
*/
|
||||||
TxProof GetAssetchainProof(uint256 hash)
|
TxProof GetAssetchainProof(uint256 hash)
|
||||||
{
|
{
|
||||||
int nIndex, md;
|
int nIndex;
|
||||||
CBlockIndex* blockIndex;
|
CBlockIndex* blockIndex;
|
||||||
struct notarized_checkpoint* np;
|
Notarisation nota;
|
||||||
std::vector<uint256> branch;
|
std::vector<uint256> branch;
|
||||||
|
|
||||||
{
|
{
|
||||||
@@ -202,20 +224,18 @@ TxProof GetAssetchainProof(uint256 hash)
|
|||||||
throw std::runtime_error("tx still in mempool");
|
throw std::runtime_error("tx still in mempool");
|
||||||
|
|
||||||
blockIndex = mapBlockIndex[blockHash];
|
blockIndex = mapBlockIndex[blockHash];
|
||||||
if (!(np = komodo_npptr(blockIndex->nHeight)))
|
if (!ScanNotarisationsFromHeight(blockIndex->nHeight, &IsSameAssetChain, nota))
|
||||||
throw std::runtime_error("notarisation not found");
|
throw std::runtime_error("notarisation not found");
|
||||||
|
|
||||||
// index of block in MoM leaves
|
// index of block in MoM leaves
|
||||||
nIndex = np->notarized_height - blockIndex->nHeight;
|
nIndex = nota.second.height - blockIndex->nHeight;
|
||||||
// MoMdepth shares space with ccid
|
|
||||||
md = np->MoMdepth & 0xffff;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// build merkle chain from blocks to MoM
|
// build merkle chain from blocks to MoM
|
||||||
{
|
{
|
||||||
std::vector<uint256> leaves, tree;
|
std::vector<uint256> leaves, tree;
|
||||||
for (int i=0; i<md; i++) {
|
for (int i=0; i<nota.second.MoMDepth; i++) {
|
||||||
uint256 mRoot = chainActive[np->notarized_height - i]->hashMerkleRoot;
|
uint256 mRoot = chainActive[nota.second.height - i]->hashMerkleRoot;
|
||||||
leaves.push_back(mRoot);
|
leaves.push_back(mRoot);
|
||||||
}
|
}
|
||||||
bool fMutated;
|
bool fMutated;
|
||||||
@@ -224,7 +244,7 @@ TxProof GetAssetchainProof(uint256 hash)
|
|||||||
|
|
||||||
// Check branch
|
// Check branch
|
||||||
uint256 ourResult = SafeCheckMerkleBranch(blockIndex->hashMerkleRoot, branch, nIndex);
|
uint256 ourResult = SafeCheckMerkleBranch(blockIndex->hashMerkleRoot, branch, nIndex);
|
||||||
if (np->MoM != ourResult)
|
if (nota.second.MoM != ourResult)
|
||||||
throw std::runtime_error("Failed merkle block->MoM");
|
throw std::runtime_error("Failed merkle block->MoM");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -259,12 +279,10 @@ TxProof GetAssetchainProof(uint256 hash)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check the proof
|
// 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");
|
throw std::runtime_error("Failed validating MoM");
|
||||||
|
|
||||||
// All done!
|
// All done!
|
||||||
CDataStream ssProof(SER_NETWORK, PROTOCOL_VERSION);
|
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2919,7 +2919,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Record Notarisations
|
// Record Notarisations
|
||||||
NotarisationsInBlock notarisations = GetNotarisationsInBlock(block, pindex->nHeight);
|
NotarisationsInBlock notarisations = ScanBlockNotarisations(block, pindex->nHeight);
|
||||||
if (notarisations.size() > 0) {
|
if (notarisations.size() > 0) {
|
||||||
CLevelDBBatch batch;
|
CLevelDBBatch batch;
|
||||||
batch.Write(block.GetHash(), notarisations);
|
batch.Write(block.GetHash(), notarisations);
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ NotarisationDB *pnotarisations;
|
|||||||
NotarisationDB::NotarisationDB(size_t nCacheSize, bool fMemory, bool fWipe) : CLevelDBWrapper(GetDataDir() / "notarisations", nCacheSize, fMemory, fWipe, false, 64) { }
|
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;
|
EvalRef eval;
|
||||||
NotarisationsInBlock vNotarisations;
|
NotarisationsInBlock vNotarisations;
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ extern NotarisationDB *pnotarisations;
|
|||||||
typedef std::pair<uint256,NotarisationData> Notarisation;
|
typedef std::pair<uint256,NotarisationData> Notarisation;
|
||||||
typedef std::vector<Notarisation> NotarisationsInBlock;
|
typedef std::vector<Notarisation> NotarisationsInBlock;
|
||||||
|
|
||||||
NotarisationsInBlock GetNotarisationsInBlock(const CBlock &block, int nHeight);
|
NotarisationsInBlock ScanBlockNotarisations(const CBlock &block, int nHeight);
|
||||||
bool GetBlockNotarisations(uint256 blockHash, NotarisationsInBlock &nibs);
|
bool GetBlockNotarisations(uint256 blockHash, NotarisationsInBlock &nibs);
|
||||||
bool GetBackNotarisation(uint256 notarisationHash, Notarisation &n);
|
bool GetBackNotarisation(uint256 notarisationHash, Notarisation &n);
|
||||||
void WriteBackNotarisations(const NotarisationsInBlock notarisations, CLevelDBBatch &batch);
|
void WriteBackNotarisations(const NotarisationsInBlock notarisations, CLevelDBBatch &batch);
|
||||||
|
|||||||
Reference in New Issue
Block a user