crosschain updates

This commit is contained in:
Duke Leto
2020-12-09 18:30:57 -05:00
parent 46327f9def
commit 18ce9a6070
7 changed files with 56 additions and 52 deletions

View File

@@ -3,7 +3,6 @@
// Copyright (c) 2019-2020 The Hush developers // Copyright (c) 2019-2020 The Hush developers
// Distributed under the GPLv3 software license, see the accompanying // Distributed under the GPLv3 software license, see the accompanying
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html // file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
/****************************************************************************** /******************************************************************************
* Copyright © 2014-2019 The SuperNET Developers. * * Copyright © 2014-2019 The SuperNET Developers. *
* * * *
@@ -18,7 +17,6 @@
* Removal or modification of this copyright notice is prohibited. * * Removal or modification of this copyright notice is prohibited. *
* * * *
******************************************************************************/ ******************************************************************************/
#ifndef HUSH_CHAIN_H #ifndef HUSH_CHAIN_H
#define HUSH_CHAIN_H #define HUSH_CHAIN_H

View File

@@ -1,3 +1,6 @@
// Copyright (c) 2019-2020 The Hush developers
// Distributed under the GPLv3 software license, see the accompanying
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
/****************************************************************************** /******************************************************************************
* Copyright © 2014-2019 The SuperNET Developers. * * Copyright © 2014-2019 The SuperNET Developers. *
* * * *
@@ -19,13 +22,12 @@
#include "main.h" #include "main.h"
#include "notarizationdb.h" #include "notarizationdb.h"
#include "merkleblock.h" #include "merkleblock.h"
#include "cc/CCinclude.h" #include "cc/CCinclude.h"
/* /*
* The crosschain workflow. * The crosschain workflow.
* *
* 3 chains, A, B, and KMD. We would like to prove TX on B. * 3 chains, A, B, and HUSH. We would like to prove TX on B.
* There is a notarisation, nA0, which will include TX via an MoM. * There is a notarisation, nA0, which will include TX via an MoM.
* The notarisation nA0 must fall between 2 notarisations of B, * The notarisation nA0 must fall between 2 notarisations of B,
* ie, nB0 and nB1. An MoMoM including this range is propagated to * ie, nB0 and nB1. An MoMoM including this range is propagated to
@@ -33,7 +35,7 @@
* *
* A: TX bnA0 * A: TX bnA0
* \ / * \ /
* KMD: nB0 nA0 nB1 nB2 * HUSH: nB0 nA0 nB1 nB2
* \ \ \ * \ \ \
* B: bnB0 bnB1 bnB2 * B: bnB0 bnB1 bnB2
*/ */
@@ -42,21 +44,21 @@
// because it might be disconnecting blocks at the same time. // because it might be disconnecting blocks at the same time.
// TODO: this assumes a blocktime of 60 seconds and limiting of 1 day of blocks
int NOTARISATION_SCAN_LIMIT_BLOCKS = 1440; int NOTARISATION_SCAN_LIMIT_BLOCKS = 1440;
CBlockIndex *komodo_getblockindex(uint256 hash); CBlockIndex *komodo_getblockindex(uint256 hash);
/* On HUSH */
/* On KMD */ uint256 CalculateProofRoot(const char* symbol, uint32_t targetCCid, int hushHeight,
uint256 CalculateProofRoot(const char* symbol, uint32_t targetCCid, int kmdHeight,
std::vector<uint256> &moms, uint256 &destNotarizationTxid) std::vector<uint256> &moms, uint256 &destNotarizationTxid)
{ {
/* /*
* Notaries don't wait for confirmation on KMD before performing a backnotarisation, * Notaries don't wait for confirmation on HUSH before performing a backnotarisation,
* but we need a determinable range that will encompass all merkle roots. Include MoMs * 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 * including the block height of the last notarisation until the height before the
* previous notarisation. * previous notarisation.
* *
* kmdHeight notarisations-0 notarisations-1 * hushHeight notarisations-0 notarisations-1
* *********************| * *********************|
* > scan backwards > * > scan backwards >
*/ */
@@ -64,7 +66,7 @@ uint256 CalculateProofRoot(const char* symbol, uint32_t targetCCid, int kmdHeigh
if (targetCCid < 2) if (targetCCid < 2)
return uint256(); return uint256();
if (kmdHeight < 0 || kmdHeight > chainActive.Height()) if (hushHeight < 0 || hushHeight > chainActive.Height())
return uint256(); return uint256();
int seenOwnNotarizations = 0, i = 0; int seenOwnNotarizations = 0, i = 0;
@@ -73,9 +75,9 @@ uint256 CalculateProofRoot(const char* symbol, uint32_t targetCCid, int kmdHeigh
std::set<uint256> tmp_moms; std::set<uint256> tmp_moms;
for (i=0; i<NOTARISATION_SCAN_LIMIT_BLOCKS; i++) { for (i=0; i<NOTARISATION_SCAN_LIMIT_BLOCKS; i++) {
if (i > kmdHeight) break; if (i > hushHeight) break;
NotarizationsInBlock notarisations; NotarizationsInBlock notarisations;
uint256 blockHash = *chainActive[kmdHeight-i]->phashBlock; uint256 blockHash = *chainActive[hushHeight-i]->phashBlock;
if (!GetBlockNotarizations(blockHash, notarisations)) if (!GetBlockNotarizations(blockHash, notarisations))
continue; continue;
@@ -144,31 +146,31 @@ int ScanNotarizationsFromHeight(int nHeight, const IsTarget f, Notarization &fou
} }
/* On KMD */ /* On HUSH */
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, int32_t offset) const TxProof assetChainProof, int32_t offset)
{ {
/* /*
* Here we are given a proof generated by an assetchain A which goes from given txid to * 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 * 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 * backnotarisation for B (given by hushHeight of notarisation), find the MoM within the MoMs for
* that range, and finally extend the proof to lead to the MoMoM (proof root). * that range, and finally extend the proof to lead to the MoMoM (proof root).
*/ */
EvalRef eval; EvalRef eval;
uint256 MoM = assetChainProof.second.Exec(txid); uint256 MoM = assetChainProof.second.Exec(txid);
// Get a kmd height for given notarisation Txid // Get a Hush height for given notarisation Txid
int kmdHeight; int hushHeight;
{ {
CTransaction sourceNotarization; CTransaction sourceNotarization;
uint256 hashBlock; uint256 hashBlock;
CBlockIndex blockIdx; CBlockIndex blockIdx;
if (!eval->GetTxConfirmed(assetChainProof.first, sourceNotarization, blockIdx)) if (!eval->GetTxConfirmed(assetChainProof.first, sourceNotarization, blockIdx))
throw std::runtime_error("Notarization not found"); throw std::runtime_error("Notarization not found");
kmdHeight = blockIdx.GetHeight(); hushHeight = blockIdx.GetHeight();
} }
// We now have a kmdHeight of the notarisation from chain A. So we know that a MoM exists // We now have a hushHeight of the notarisation from chain A. So we know that a MoM exists
// at that height. // at that height.
// If we call CalculateProofRoot with that height, it'll scan backwards, until it finds // If we call CalculateProofRoot with that height, it'll scan backwards, until it finds
// a notarisation from B, and it might not include our notarisation from A // a notarisation from B, and it might not include our notarisation from A
@@ -178,17 +180,17 @@ TxProof GetCrossChainProof(const uint256 txid, const char* targetSymbol, uint32_
auto isTarget = [&](Notarization &nota) { auto isTarget = [&](Notarization &nota) {
return strcmp(nota.second.symbol, targetSymbol) == 0; return strcmp(nota.second.symbol, targetSymbol) == 0;
}; };
kmdHeight = ScanNotarizationsFromHeight(kmdHeight, isTarget, nota); hushHeight = ScanNotarizationsFromHeight(hushHeight, isTarget, nota);
if (!kmdHeight) if (!hushHeight)
throw std::runtime_error("Cannot find notarisation for target inclusive of source"); throw std::runtime_error("Cannot find notarisation for target inclusive of source");
if ( offset != 0 ) if ( offset != 0 )
kmdHeight += offset; hushHeight += offset;
// Get MoMs for kmd height and symbol // Get MoMs for Hush height and symbol
std::vector<uint256> moms; std::vector<uint256> moms;
uint256 targetChainNotarizationTxid; uint256 targetChainNotarizationTxid;
uint256 MoMoM = CalculateProofRoot(targetSymbol, targetCCid, kmdHeight, moms, targetChainNotarizationTxid); uint256 MoMoM = CalculateProofRoot(targetSymbol, targetCCid, hushHeight, moms, targetChainNotarizationTxid);
if (MoMoM.IsNull()) if (MoMoM.IsNull())
throw std::runtime_error("No MoMs found"); throw std::runtime_error("No MoMs found");
@@ -257,15 +259,15 @@ bool IsSameAssetChain(const Notarization &nota) {
/* On assetchain */ /* On assetchain */
bool GetNextBacknotarisation(uint256 kmdNotarizationTxid, Notarization &out) bool GetNextBacknotarisation(uint256 hushNotarizationTxid, Notarization &out)
{ {
/* /*
* Here we are given a txid, and a proof. * Here we are given a txid, and a proof.
* We go from the KMD notarisation txid to the backnotarisation, * We go from the HUSH notarisation txid to the backnotarisation,
* then jump to the next backnotarisation, which contains the corresponding MoMoM. * then jump to the next backnotarisation, which contains the corresponding MoMoM.
*/ */
Notarization bn; Notarization bn;
if (!GetBackNotarization(kmdNotarizationTxid, bn)) if (!GetBackNotarization(hushNotarizationTxid, bn))
return false; return false;
// Need to get block height of that backnotarisation // Need to get block height of that backnotarisation
@@ -281,17 +283,17 @@ bool GetNextBacknotarisation(uint256 kmdNotarizationTxid, Notarization &out)
} }
bool CheckMoMoM(uint256 kmdNotarizationHash, uint256 momom) bool CheckMoMoM(uint256 hushNotarizationHash, uint256 momom)
{ {
/* /*
* Given a notarisation hash and an MoMoM. Backnotarisations may arrive out of order * Given a notarisation hash and an MoMoM. Backnotarisations may arrive out of order
* or multiple in the same block. So dereference the notarisation hash to the corresponding * or multiple in the same block. So dereference the notarisation hash to the corresponding
* backnotarisation and scan around the kmdheight to see if the MoMoM is a match. * backnotarisation and scan around the hushheight to see if the MoMoM is a match.
* This is a sledgehammer approach... * This is a sledgehammer approach...
*/ */
Notarization bn; Notarization bn;
if (!GetBackNotarization(kmdNotarizationHash, bn)) if (!GetBackNotarization(hushNotarizationHash, bn))
return false; return false;
// Need to get block height of that backnotarisation // Need to get block height of that backnotarisation

View File

@@ -15,9 +15,8 @@
* Removal or modification of this copyright notice is prohibited. * * Removal or modification of this copyright notice is prohibited. *
* * * *
******************************************************************************/ ******************************************************************************/
#ifndef HUSH_CROSSCHAIN_H
#ifndef CROSSCHAIN_H #define HUSH_CROSSCHAIN_H
#define CROSSCHAIN_H
#include "cc/eval.h" #include "cc/eval.h"
const int CROSSCHAIN_HUSH = 1; const int CROSSCHAIN_HUSH = 1;
@@ -35,14 +34,14 @@ bool CheckTxAuthority(const CTransaction &tx, CrosschainAuthority auth);
TxProof GetAssetchainProof(uint256 hash,CTransaction burnTx); TxProof GetAssetchainProof(uint256 hash,CTransaction burnTx);
/* On HUSH */ /* On HUSH */
uint256 CalculateProofRoot(const char* symbol, uint32_t targetCCid, int kmdHeight, uint256 CalculateProofRoot(const char* symbol, uint32_t targetCCid, int hushHeight,
std::vector<uint256> &moms, uint256 &destNotarizationTxid); std::vector<uint256> &moms, uint256 &destNotarizationTxid);
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,int32_t offset); const TxProof assetChainProof,int32_t offset);
void CompleteImportTransaction(CTransaction &importTx,int32_t offset); void CompleteImportTransaction(CTransaction &importTx,int32_t offset);
/* On assetchain */ /* On assetchain */
bool CheckMoMoM(uint256 kmdNotarizationHash, uint256 momom); bool CheckMoMoM(uint256 hushNotarizationHash, uint256 momom);
bool CheckNotariesApproval(uint256 burntxid, const std::vector<uint256> & notaryTxids); bool CheckNotariesApproval(uint256 burntxid, const std::vector<uint256> & notaryTxids);
#endif /* CROSSCHAIN_H */ #endif /* HUSH_CROSSCHAIN_H */

View File

@@ -264,11 +264,7 @@ namespace {
set<int> setDirtyFileInfo; set<int> setDirtyFileInfo;
} // anon namespace } // anon namespace
//////////////////////////////////////////////////////////////////////////////
//
// Registration of network node signals. // Registration of network node signals.
//
namespace { namespace {
struct CBlockReject { struct CBlockReject {
@@ -5356,9 +5352,11 @@ bool AcceptBlockHeader(int32_t *futureblockp,const CBlockHeader& block, CValidat
// Check for duplicate // Check for duplicate
uint256 hash = block.GetHash(); uint256 hash = block.GetHash();
BlockMap::iterator miSelf = mapBlockIndex.find(hash); BlockMap::iterator miSelf = mapBlockIndex.find(hash);
if(fDebug) {
std::cerr << __func__ << ": blockhash=" << hash.ToString() << endl;
}
CBlockIndex *pindex = NULL; CBlockIndex *pindex = NULL;
if (miSelf != mapBlockIndex.end()) if (miSelf != mapBlockIndex.end()) {
{
// Block header is already known. // Block header is already known.
if ( (pindex = miSelf->second) == 0 ) if ( (pindex = miSelf->second) == 0 )
miSelf->second = pindex = AddToBlockIndex(block); miSelf->second = pindex = AddToBlockIndex(block);
@@ -5366,6 +5364,7 @@ bool AcceptBlockHeader(int32_t *futureblockp,const CBlockHeader& block, CValidat
*ppindex = pindex; *ppindex = pindex;
if ( pindex != 0 && (pindex->nStatus & BLOCK_FAILED_MASK) != 0 ) { if ( pindex != 0 && (pindex->nStatus & BLOCK_FAILED_MASK) != 0 ) {
if ( ASSETCHAINS_CC == 0 ) { if ( ASSETCHAINS_CC == 0 ) {
std::cerr << __func__ << ": block " << hash.ToString() << " marked invalid";
return state.Invalid(error("%s: block is marked invalid", __func__), 0, "duplicate"); return state.Invalid(error("%s: block is marked invalid", __func__), 0, "duplicate");
} else { } else {
fprintf(stderr,"reconsider block %s\n",hash.GetHex().c_str()); fprintf(stderr,"reconsider block %s\n",hash.GetHex().c_str());
@@ -5380,6 +5379,9 @@ bool AcceptBlockHeader(int32_t *futureblockp,const CBlockHeader& block, CValidat
return false; return false;
} }
} }
if(fDebug) {
fprintf(stderr,"%s: CheckBlockHeader passed\n",__func__);
}
// Get prev block index // Get prev block index
CBlockIndex* pindexPrev = NULL; CBlockIndex* pindexPrev = NULL;
if (hash != chainparams.GetConsensus().hashGenesisBlock) if (hash != chainparams.GetConsensus().hashGenesisBlock)
@@ -5401,12 +5403,14 @@ bool AcceptBlockHeader(int32_t *futureblockp,const CBlockHeader& block, CValidat
if ( (pindexPrev->nStatus & BLOCK_FAILED_MASK) ) if ( (pindexPrev->nStatus & BLOCK_FAILED_MASK) )
return state.DoS(100, error("%s: prev block invalid", __func__), REJECT_INVALID, "bad-prevblk"); return state.DoS(100, error("%s: prev block invalid", __func__), REJECT_INVALID, "bad-prevblk");
} }
if (!ContextualCheckBlockHeader(block, state, pindexPrev)) if (!ContextualCheckBlockHeader(block, state, pindexPrev)) {
{
//fprintf(stderr,"AcceptBlockHeader ContextualCheckBlockHeader failed\n"); //fprintf(stderr,"AcceptBlockHeader ContextualCheckBlockHeader failed\n");
LogPrintf("%s: ContextualCheckBlockHeader failed\n",__func__); LogPrintf("%s: ContextualCheckBlockHeader failed\n",__func__);
return false; return false;
} }
if(fDebug) {
fprintf(stderr,"%s: ContextualCheckBlockHeader passed\n", hash.ToString());
}
if (pindex == NULL) if (pindex == NULL)
{ {
if ( (pindex= AddToBlockIndex(block)) != 0 ) if ( (pindex= AddToBlockIndex(block)) != 0 )
@@ -7775,8 +7779,10 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
CBlockIndex *pindexLast = NULL; CBlockIndex *pindexLast = NULL;
BOOST_FOREACH(const CBlockHeader& header, headers) { BOOST_FOREACH(const CBlockHeader& header, headers) {
//printf("size.%i, solution size.%i\n", (int)sizeof(header), (int)header.nSolution.size()); if(fDebug) {
//printf("hash.%s prevhash.%s nonce.%s\n", header.GetHash().ToString().c_str(), header.hashPrevBlock.ToString().c_str(), header.nNonce.ToString().c_str()); printf("%s: size.%i, solution size.%i\n", __func__, (int)sizeof(header), (int)header.nSolution.size());
printf("%s: hash.%s prevhash.%s nonce.%s\n", __func__, header.GetHash().ToString().c_str(), header.hashPrevBlock.ToString().c_str(), header.nNonce.ToString().c_str());
}
CValidationState state; CValidationState state;
if (pindexLast != NULL && header.hashPrevBlock != pindexLast->GetBlockHash()) { if (pindexLast != NULL && header.hashPrevBlock != pindexLast->GetBlockHash()) {

View File

@@ -3,7 +3,6 @@
// Copyright (c) 2019-2020 The Hush developers // Copyright (c) 2019-2020 The Hush developers
// Distributed under the GPLv3 software license, see the accompanying // Distributed under the GPLv3 software license, see the accompanying
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html // file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
/****************************************************************************** /******************************************************************************
* Copyright © 2014-2019 The SuperNET Developers. * * Copyright © 2014-2019 The SuperNET Developers. *
* * * *
@@ -18,7 +17,6 @@
* Removal or modification of this copyright notice is prohibited. * * Removal or modification of this copyright notice is prohibited. *
* * * *
******************************************************************************/ ******************************************************************************/
#include "pow.h" #include "pow.h"
#include "consensus/upgrades.h" #include "consensus/upgrades.h"
#include "arith_uint256.h" #include "arith_uint256.h"
@@ -501,6 +499,7 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
nbits = bnTarget.GetCompact(); nbits = bnTarget.GetCompact();
nbits = (nbits & 0xfffffffc) | zawyflag; nbits = (nbits & 0xfffffffc) | zawyflag;
} }
fprintf(stderr,"%s: nbits=%d\n", __func__, nbits);
return(nbits); return(nbits);
} }

View File

@@ -1,10 +1,9 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 The Bitcoin Core developers // Copyright (c) 2009-2014 The Bitcoin Core developers
// Copyright (c) 2016-2020 The Hush developers
// Distributed under the GPLv3 software license, see the accompanying // Distributed under the GPLv3 software license, see the accompanying
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html // file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
#include "utilmoneystr.h" #include "utilmoneystr.h"
#include "primitives/transaction.h" #include "primitives/transaction.h"
#include "tinyformat.h" #include "tinyformat.h"
#include "utilstrencodings.h" #include "utilstrencodings.h"

View File

@@ -1,7 +1,8 @@
// Copyright (c) 2019-2020 The Hush developers
// Distributed under the GPLv3 software license, see the accompanying
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
#include <iostream> #include <iostream>
#include <string> #include <string>
// Include local headers
#include "wallet/walletdb.h" #include "wallet/walletdb.h"
#include "util.h" #include "util.h"
#include "base58.h" #include "base58.h"