Hush Full Node software. We were censored from Github, this is where all development happens now. https://hush.is
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

270 lines
8.9 KiB

6 years ago
#include "cc/eval.h"
#include "importcoin.h"
6 years ago
#include "main.h"
#include "notarisationdb.h"
6 years ago
#include "komodo_structs.h"
6 years ago
/*
* This file is built in the server
*/
6 years ago
/* On KMD */
uint256 CalculateProofRoot(const char* symbol, uint32_t targetCCid, int kmdHeight,
std::vector<uint256> &moms, uint256 &destNotarisationTxid)
6 years ago
{
/*
* Notaries don't wait for confirmation on KMD before performing a backnotarisation,
* but we need a determinable range that will encompass all merkle roots. Include MoMs
* including the block height of the last notarisation until the height before the
* previous notarisation.
*
* kmdHeight notarisations-0 notarisations-1
* | |********************|
* > scan backwards >
6 years ago
*/
6 years ago
6 years ago
if (targetCCid <= 1)
return uint256();
if (kmdHeight < 0 || kmdHeight > chainActive.Height())
return uint256();
6 years ago
int seenOwnNotarisations = 0;
6 years ago
for (int i=0; i<1440; i++) {
if (i > kmdHeight) break;
NotarisationsInBlock notarisations;
uint256 blockHash = *chainActive[kmdHeight-i]->phashBlock;
if (!GetBlockNotarisations(blockHash, notarisations))
6 years ago
continue;
BOOST_FOREACH(Notarisation& nota, notarisations) {
NotarisationData& data = nota.second;
if (data.ccId != targetCCid)
continue;
if (strcmp(data.symbol, symbol) == 0)
{
seenOwnNotarisations++;
if (seenOwnNotarisations == 2)
goto end;
if (seenOwnNotarisations == 1)
destNotarisationTxid = nota.first;
6 years ago
}
if (seenOwnNotarisations == 1)
6 years ago
moms.push_back(data.MoM);
}
}
end:
return GetMerkleRoot(moms);
}
/* On KMD */
TxProof GetCrossChainProof(const uint256 txid, const char* targetSymbol, uint32_t targetCCid,
const TxProof assetChainProof)
6 years ago
{
/*
* Here we are given a proof generated by an assetchain A which goes from given txid to
* an assetchain MoM. We need to go from the notarisationTxid for A to the MoMoM range of the
* backnotarisation for B (given by kmdheight of notarisation), find the MoM within the MoMs for
* that range, and finally extend the proof to lead to the MoMoM (proof root).
*/
EvalRef eval;
uint256 MoM = assetChainProof.second.Exec(txid);
6 years ago
// Get a kmd height for given notarisation Txid
int kmdHeight;
{
CTransaction sourceNotarisation;
uint256 hashBlock;
CBlockIndex blockIdx;
if (eval->GetTxConfirmed(assetChainProof.first, sourceNotarisation, blockIdx))
6 years ago
kmdHeight = blockIdx.nHeight;
else if (eval->GetTxUnconfirmed(assetChainProof.first, sourceNotarisation, hashBlock))
6 years ago
kmdHeight = chainActive.Tip()->nHeight;
else
throw std::runtime_error("Notarisation not found");
}
// Get MoMs for kmd height and symbol
std::vector<uint256> moms;
uint256 targetChainNotarisationTxid;
uint256 MoMoM = CalculateProofRoot(targetSymbol, targetCCid, kmdHeight, moms, targetChainNotarisationTxid);
6 years ago
if (MoMoM.IsNull())
throw std::runtime_error("No MoMs found");
// Find index of source MoM in MoMoM
int nIndex;
6 years ago
for (nIndex=0; nIndex<moms.size(); nIndex++) {
6 years ago
if (moms[nIndex] == MoM)
goto cont;
6 years ago
}
6 years ago
throw std::runtime_error("Couldn't find MoM within MoMoM set");
cont:
// Create a branch
std::vector<uint256> vBranch;
6 years ago
{
CBlock fakeBlock;
for (int i=0; i<moms.size(); i++) {
CTransaction fakeTx;
// first value in CTransaction memory is it's hash
memcpy((void*)&fakeTx, moms[i].begin(), 32);
fakeBlock.vtx.push_back(fakeTx);
}
vBranch = fakeBlock.GetMerkleBranch(nIndex);
6 years ago
}
// Concatenate branches
MerkleBranch newBranch = assetChainProof.second;
newBranch << MerkleBranch(nIndex, vBranch);
6 years ago
// Check proof
if (newBranch.Exec(txid) != MoMoM)
6 years ago
throw std::runtime_error("Proof check failed");
return std::make_pair(targetChainNotarisationTxid,newBranch);
6 years ago
}
/*
* Takes an importTx that has proof leading to assetchain root
* and extends proof to cross chain root
*/
void CompleteImportTransaction(CTransaction &importTx)
{
TxProof proof;
CTransaction burnTx;
std::vector<CTxOut> payouts;
if (!UnmarshalImportTx(importTx, proof, burnTx, payouts))
throw std::runtime_error("Couldn't parse importTx");
std::string targetSymbol;
uint32_t targetCCid;
uint256 payoutsHash;
if (!UnmarshalBurnTx(burnTx, targetSymbol, &targetCCid, payoutsHash))
throw std::runtime_error("Couldn't parse burnTx");
proof = GetCrossChainProof(burnTx.GetHash(), targetSymbol.data(), targetCCid, proof);
importTx = MakeImportCoinTransaction(proof, burnTx, importTx.vout);
}
struct notarized_checkpoint *komodo_npptr_at(int idx);
struct notarized_checkpoint *komodo_npptr_for_height(int32_t height, int *idx);
6 years ago
/* On assetchain */
bool GetNextBacknotarisation(uint256 kmdNotarisationTxid, std::pair<uint256,NotarisationData> &out)
6 years ago
{
/*
* Here we are given a txid, and a proof.
* We go from the KMD notarisation txid to the backnotarisation,
* then jump to the next backnotarisation, which contains the corresponding MoMoM.
6 years ago
*/
Notarisation bn;
if (!GetBackNotarisation(kmdNotarisationTxid, bn))
return false;
6 years ago
int npIdx;
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");
}
6 years ago
struct notarized_checkpoint* komodo_npptr(int32_t height);
6 years ago
6 years ago
int32_t komodo_MoM(int32_t *notarized_htp,uint256 *MoMp,uint256 *kmdtxidp,int32_t nHeight,uint256 *MoMoMp,int32_t *MoMoMoffsetp,int32_t *MoMoMdepthp,int32_t *kmdstartip,int32_t *kmdendip);
/*
* On assetchain
* in: txid
* out: pair<notarisationTxHash,merkleBranch>
*/
TxProof GetAssetchainProof(uint256 hash)
6 years ago
{
6 years ago
int nIndex;
CBlockIndex* blockIndex;
struct notarized_checkpoint* np;
6 years ago
std::vector<uint256> branch;
{
uint256 blockHash;
CTransaction tx;
if (!GetTransaction(hash, tx, blockHash, true))
throw std::runtime_error("cannot find transaction");
blockIndex = mapBlockIndex[blockHash];
if (!(np = komodo_npptr(blockIndex->nHeight)))
6 years ago
throw std::runtime_error("notarisation not found");
// index of block in MoM leaves
6 years ago
nIndex = np->notarized_height - blockIndex->nHeight;
6 years ago
}
// build merkle chain from blocks to MoM
{
// since the merkle branch code is tied up in a block class
// and we want to make a merkle branch for something that isnt transactions
CBlock fakeBlock;
6 years ago
for (int i=0; i<np->MoMdepth; i++) {
uint256 mRoot = chainActive[np->notarized_height - i]->hashMerkleRoot;
6 years ago
CTransaction fakeTx;
// first value in CTransaction memory is it's hash
memcpy((void*)&fakeTx, mRoot.begin(), 32);
fakeBlock.vtx.push_back(fakeTx);
}
branch = fakeBlock.GetMerkleBranch(nIndex);
// Check branch
6 years ago
if (np->MoM != CBlock::CheckMerkleBranch(blockIndex->hashMerkleRoot, branch, nIndex))
6 years ago
throw std::runtime_error("Failed merkle block->MoM");
}
// Now get the tx merkle branch
{
CBlock block;
if (fHavePruned && !(blockIndex->nStatus & BLOCK_HAVE_DATA) && blockIndex->nTx > 0)
throw std::runtime_error("Block not available (pruned data)");
if(!ReadBlockFromDisk(block, blockIndex,1))
throw std::runtime_error("Can't read block from disk");
// Locate the transaction in the block
int nTxIndex;
for (nTxIndex = 0; nTxIndex < (int)block.vtx.size(); nTxIndex++)
if (block.vtx[nTxIndex].GetHash() == hash)
break;
if (nTxIndex == (int)block.vtx.size())
throw std::runtime_error("Error locating tx in block");
std::vector<uint256> txBranch = block.GetMerkleBranch(nTxIndex);
// Check branch
if (block.hashMerkleRoot != CBlock::CheckMerkleBranch(hash, txBranch, nTxIndex))
throw std::runtime_error("Failed merkle tx->block");
// concatenate branches
nIndex = (nIndex << txBranch.size()) + nTxIndex;
branch.insert(branch.begin(), txBranch.begin(), txBranch.end());
}
// Check the proof
6 years ago
if (np->MoM != CBlock::CheckMerkleBranch(hash, branch, nIndex))
6 years ago
throw std::runtime_error("Failed validating MoM");
// All done!
CDataStream ssProof(SER_NETWORK, PROTOCOL_VERSION);
6 years ago
return std::make_pair(np->notarized_desttxid, MerkleBranch(nIndex, branch));
6 years ago
}