betprotocol functions

This commit is contained in:
Scott Sadler
2018-04-03 04:48:24 -03:00
parent 660b32c300
commit 375927407b
18 changed files with 371 additions and 55 deletions

View File

@@ -8,6 +8,7 @@
#include "chainparams.h"
#include "checkpoints.h"
#include "consensus/validation.h"
#include "cc/importpayout.h"
#include "main.h"
#include "primitives/transaction.h"
#include "rpcserver.h"
@@ -621,6 +622,83 @@ UniValue height_MoM(const UniValue& params, bool fHelp)
return ret;
}
UniValue txMoMproof(const UniValue& params, bool fHelp)
{
uint256 hash, notarisationHash, MoM;
int32_t notarisedHeight, depth;
CBlockIndex* blockIndex;
std::vector<uint256> branch;
int nIndex;
// parse params and get notarisation data for tx
{
if ( fHelp || params.size() != 1)
throw runtime_error("txmomproof needs a txid");
uint256 hash(uint256S(params[0].get_str()));
uint256 blockHash;
CTransaction tx;
if (!GetTransaction(hash, tx, blockHash, true))
throw runtime_error("cannot find transaction");
blockIndex = mapBlockIndex[blockHash];
depth = komodo_MoM(&notarisedHeight, &MoM, &notarisationHash, blockIndex->nHeight);
if (!depth)
throw runtime_error("notarisation not found");
// index of block in MoM leaves
nIndex = notarisedHeight - blockIndex->nHeight;
}
// 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;
for (int i=0; i<depth; i++) {
uint256 mRoot = chainActive[notarisedHeight - i]->hashMerkleRoot;
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);
}
// Now get the tx merkle branch
{
CBlock block;
if (fHavePruned && !(blockIndex->nStatus & BLOCK_HAVE_DATA) && blockIndex->nTx > 0)
throw JSONRPCError(RPC_INTERNAL_ERROR, "Block not available (pruned data)");
if(!ReadBlockFromDisk(block, blockIndex))
throw JSONRPCError(RPC_INTERNAL_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 JSONRPCError(RPC_INTERNAL_ERROR, "Error locating tx in block");
// concatenate branches
std::vector<uint256> txBranch = block.GetMerkleBranch(nTxIndex);
nIndex = nIndex << txBranch.size() + nTxIndex;
branch.insert(branch.begin(), txBranch.begin(), txBranch.end());
}
// Encode and return
CDataStream ssProof(SER_NETWORK, PROTOCOL_VERSION);
ssProof << MoMProof(nIndex, branch, notarisationHash);
return HexStr(ssProof.begin(), ssProof.end());
}
UniValue minerids(const UniValue& params, bool fHelp)
{
uint32_t timestamp = 0; UniValue ret(UniValue::VOBJ); UniValue a(UniValue::VARR); uint8_t minerids[2000],pubkeys[65][33]; int32_t i,j,n,numnotaries,tally[129];