Browse Source

crosschain updates

minor
Duke Leto 4 years ago
parent
commit
18ce9a6070
  1. 2
      src/chain.h
  2. 58
      src/crosschain.cpp
  3. 11
      src/crosschain.h
  4. 26
      src/main.cpp
  5. 3
      src/pow.cpp
  6. 3
      src/utilmoneystr.cpp
  7. 5
      src/wallet-utility.cpp

2
src/chain.h

@ -3,7 +3,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. *
* *
@ -18,7 +17,6 @@
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/
#ifndef HUSH_CHAIN_H
#define HUSH_CHAIN_H

58
src/crosschain.cpp

@ -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. *
* *
@ -19,13 +22,12 @@
#include "main.h"
#include "notarizationdb.h"
#include "merkleblock.h"
#include "cc/CCinclude.h"
/*
* 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.
* The notarisation nA0 must fall between 2 notarisations of B,
* ie, nB0 and nB1. An MoMoM including this range is propagated to
@ -33,7 +35,7 @@
*
* A: TX bnA0
* \ /
* KMD: nB0 nA0 nB1 nB2
* HUSH: nB0 nA0 nB1 nB2
* \ \ \
* B: bnB0 bnB1 bnB2
*/
@ -42,21 +44,21 @@
// 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;
CBlockIndex *komodo_getblockindex(uint256 hash);
/* On KMD */
uint256 CalculateProofRoot(const char* symbol, uint32_t targetCCid, int kmdHeight,
/* On HUSH */
uint256 CalculateProofRoot(const char* symbol, uint32_t targetCCid, int hushHeight,
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
* including the block height of the last notarisation until the height before the
* previous notarisation.
*
* kmdHeight notarisations-0 notarisations-1
* hushHeight notarisations-0 notarisations-1
* *********************|
* > scan backwards >
*/
@ -64,7 +66,7 @@ uint256 CalculateProofRoot(const char* symbol, uint32_t targetCCid, int kmdHeigh
if (targetCCid < 2)
return uint256();
if (kmdHeight < 0 || kmdHeight > chainActive.Height())
if (hushHeight < 0 || hushHeight > chainActive.Height())
return uint256();
int seenOwnNotarizations = 0, i = 0;
@ -73,9 +75,9 @@ uint256 CalculateProofRoot(const char* symbol, uint32_t targetCCid, int kmdHeigh
std::set<uint256> tmp_moms;
for (i=0; i<NOTARISATION_SCAN_LIMIT_BLOCKS; i++) {
if (i > kmdHeight) break;
if (i > hushHeight) break;
NotarizationsInBlock notarisations;
uint256 blockHash = *chainActive[kmdHeight-i]->phashBlock;
uint256 blockHash = *chainActive[hushHeight-i]->phashBlock;
if (!GetBlockNotarizations(blockHash, notarisations))
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,
const TxProof assetChainProof, int32_t offset)
{
/*
* 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
* 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).
*/
EvalRef eval;
uint256 MoM = assetChainProof.second.Exec(txid);
// Get a kmd height for given notarisation Txid
int kmdHeight;
// Get a Hush height for given notarisation Txid
int hushHeight;
{
CTransaction sourceNotarization;
uint256 hashBlock;
CBlockIndex blockIdx;
if (!eval->GetTxConfirmed(assetChainProof.first, sourceNotarization, blockIdx))
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.
// 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
@ -178,17 +180,17 @@ TxProof GetCrossChainProof(const uint256 txid, const char* targetSymbol, uint32_
auto isTarget = [&](Notarization &nota) {
return strcmp(nota.second.symbol, targetSymbol) == 0;
};
kmdHeight = ScanNotarizationsFromHeight(kmdHeight, isTarget, nota);
if (!kmdHeight)
hushHeight = ScanNotarizationsFromHeight(hushHeight, isTarget, nota);
if (!hushHeight)
throw std::runtime_error("Cannot find notarisation for target inclusive of source");
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;
uint256 targetChainNotarizationTxid;
uint256 MoMoM = CalculateProofRoot(targetSymbol, targetCCid, kmdHeight, moms, targetChainNotarizationTxid);
uint256 MoMoM = CalculateProofRoot(targetSymbol, targetCCid, hushHeight, moms, targetChainNotarizationTxid);
if (MoMoM.IsNull())
throw std::runtime_error("No MoMs found");
@ -257,15 +259,15 @@ bool IsSameAssetChain(const Notarization &nota) {
/* On assetchain */
bool GetNextBacknotarisation(uint256 kmdNotarizationTxid, Notarization &out)
bool GetNextBacknotarisation(uint256 hushNotarizationTxid, Notarization &out)
{
/*
* 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.
*/
Notarization bn;
if (!GetBackNotarization(kmdNotarizationTxid, bn))
if (!GetBackNotarization(hushNotarizationTxid, bn))
return false;
// 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
* 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...
*/
Notarization bn;
if (!GetBackNotarization(kmdNotarizationHash, bn))
if (!GetBackNotarization(hushNotarizationHash, bn))
return false;
// Need to get block height of that backnotarisation

11
src/crosschain.h

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

26
src/main.cpp

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

3
src/pow.cpp

@ -3,7 +3,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. *
* *
@ -18,7 +17,6 @@
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/
#include "pow.h"
#include "consensus/upgrades.h"
#include "arith_uint256.h"
@ -501,6 +499,7 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
nbits = bnTarget.GetCompact();
nbits = (nbits & 0xfffffffc) | zawyflag;
}
fprintf(stderr,"%s: nbits=%d\n", __func__, nbits);
return(nbits);
}

3
src/utilmoneystr.cpp

@ -1,10 +1,9 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 The Bitcoin Core developers
// Copyright (c) 2016-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 "utilmoneystr.h"
#include "primitives/transaction.h"
#include "tinyformat.h"
#include "utilstrencodings.h"

5
src/wallet-utility.cpp

@ -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 <string>
// Include local headers
#include "wallet/walletdb.h"
#include "util.h"
#include "base58.h"

Loading…
Cancel
Save