diff --git a/qa/rpc-tests/cryptoconditions_oracles.py b/qa/rpc-tests/cryptoconditions_oracles.py index ded5166ed..31712abbc 100755 --- a/qa/rpc-tests/cryptoconditions_oracles.py +++ b/qa/rpc-tests/cryptoconditions_oracles.py @@ -115,13 +115,163 @@ class CryptoconditionsOraclesTest(BitcoinTestFramework): too_long_description = generate_random_string(4100) result = rpc.oraclescreate("Test", too_long_description, "s") assert_error(result) - # # valid creating oracles of different types - # # using such naming to re-use it for data publishing / reading (e.g. oracle_s for s type) - # valid_formats = ["s", "S", "d", "D", "c", "C", "t", "T", "i", "I", "l", "L", "h", "Ihh"] - # for f in valid_formats: - # result = rpc.oraclescreate("Test", "Test", f) - # assert_success(result) - # globals()["oracle_{}".format(f)] = self.send_and_mine(result['hex'], rpc) + # valid creating oracles of different types + # using such naming to re-use it for data publishing / reading (e.g. oracle_s for s type) + valid_formats = ["s", "S", "d", "D", "c", "C", "t", "T", "i", "I", "l", "L", "h", "Ihh"] + for f in valid_formats: + result = rpc.oraclescreate("Test", "Test", f) + assert_success(result) + globals()["oracle_{}".format(f)] = self.send_and_mine(result['hex'], rpc) + + # trying to register with negative datafee + for f in valid_formats: + result = rpc.oraclesregister(globals()["oracle_{}".format(f)], "-100") + assert_error(result) + + # trying to register with zero datafee + for f in valid_formats: + result = rpc.oraclesregister(globals()["oracle_{}".format(f)], "0") + assert_error(result) + + # trying to register with datafee less than txfee + for f in valid_formats: + result = rpc.oraclesregister(globals()["oracle_{}".format(f)], "500") + assert_error(result) + + # trying to register valid + for f in valid_formats: + result = rpc.oraclesregister(globals()["oracle_{}".format(f)], "10000") + assert_success(result) + register_txid = self.send_and_mine(result["hex"], rpc) + assert register_txid, "got txid" + + # TODO: for most of the non valid oraclesregister and oraclessubscribe transactions generating and broadcasting now + # so trying only valid oraclessubscribe atm + for f in valid_formats: + result = rpc.oraclessubscribe(globals()["oracle_{}".format(f)], self.pubkey, "1") + assert_success(result) + subscribe_txid = self.send_and_mine(result["hex"], rpc) + assert register_txid, "got txid" + + # now lets publish and read valid data for each oracle type + + # s type + result = rpc.oraclesdata(globals()["oracle_{}".format("s")], "05416e746f6e") + assert_success(result) + # baton + oraclesdata_s = self.send_and_mine(result["hex"], rpc) + result = rpc.oraclessamples(globals()["oracle_{}".format("s")], oraclesdata_s, "1") + assert_equal("[u'Anton']", str(result["samples"][0]), "Data match") + + # S type + result = rpc.oraclesdata(globals()["oracle_{}".format("S")], "000161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161") + assert_success(result) + # baton + oraclesdata_S = self.send_and_mine(result["hex"], rpc) + result = rpc.oraclessamples(globals()["oracle_{}".format("S")], oraclesdata_S, "1") + assert_equal("[u'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa']", str(result["samples"][0]), "Data match") + + # d type + result = rpc.oraclesdata(globals()["oracle_{}".format("d")], "0101") + assert_success(result) + # baton + oraclesdata_d = self.send_and_mine(result["hex"], rpc) + result = rpc.oraclessamples(globals()["oracle_{}".format("d")], oraclesdata_d, "1") + # TODO: working not correct now! + #assert_equal("[u'01']", str(result["samples"][0]), "Data match") + + # D type + result = rpc.oraclesdata(globals()["oracle_{}".format("D")], "0101") + assert_success(result) + # baton + oraclesdata_D = self.send_and_mine(result["hex"], rpc) + result = rpc.oraclessamples(globals()["oracle_{}".format("D")], oraclesdata_D, "1") + # TODO: working not correct now! + #assert_equal("[u'01']", str(result["samples"][0]), "Data match") + + # c type + result = rpc.oraclesdata(globals()["oracle_{}".format("c")], "ff") + assert_success(result) + # baton + oraclesdata_c = self.send_and_mine(result["hex"], rpc) + result = rpc.oraclessamples(globals()["oracle_{}".format("c")], oraclesdata_c, "1") + assert_equal("[u'-1']", str(result["samples"][0]), "Data match") + + # C type + result = rpc.oraclesdata(globals()["oracle_{}".format("C")], "ff") + assert_success(result) + # baton + oraclesdata_C = self.send_and_mine(result["hex"], rpc) + result = rpc.oraclessamples(globals()["oracle_{}".format("C")], oraclesdata_C, "1") + assert_equal("[u'255']", str(result["samples"][0]), "Data match") + + # t type + result = rpc.oraclesdata(globals()["oracle_{}".format("t")], "ffff") + assert_success(result) + # baton + oraclesdata_t = self.send_and_mine(result["hex"], rpc) + result = rpc.oraclessamples(globals()["oracle_{}".format("t")], oraclesdata_t, "1") + assert_equal("[u'-1']", str(result["samples"][0]), "Data match") + + # T type + result = rpc.oraclesdata(globals()["oracle_{}".format("T")], "ffff") + assert_success(result) + # baton + oraclesdata_T = self.send_and_mine(result["hex"], rpc) + result = rpc.oraclessamples(globals()["oracle_{}".format("T")], oraclesdata_T, "1") + assert_equal("[u'65535']", str(result["samples"][0]), "Data match") + + # i type + result = rpc.oraclesdata(globals()["oracle_{}".format("i")], "ffffffff") + assert_success(result) + # baton + oraclesdata_i = self.send_and_mine(result["hex"], rpc) + result = rpc.oraclessamples(globals()["oracle_{}".format("i")], oraclesdata_i, "1") + assert_equal("[u'-1']", str(result["samples"][0]), "Data match") + + # I type + result = rpc.oraclesdata(globals()["oracle_{}".format("I")], "ffffffff") + assert_success(result) + # baton + oraclesdata_I = self.send_and_mine(result["hex"], rpc) + result = rpc.oraclessamples(globals()["oracle_{}".format("I")], oraclesdata_I, "1") + assert_equal("[u'4294967295']", str(result["samples"][0]), "Data match") + + # l type + result = rpc.oraclesdata(globals()["oracle_{}".format("l")], "00000000ffffffff") + assert_success(result) + # baton + oraclesdata_l = self.send_and_mine(result["hex"], rpc) + result = rpc.oraclessamples(globals()["oracle_{}".format("l")], oraclesdata_l, "1") + # TODO: working not correct now! + #assert_equal("[u'-4294967296']", str(result["samples"][0]), "Data match") + + # L type + result = rpc.oraclesdata(globals()["oracle_{}".format("L")], "00000000ffffffff") + assert_success(result) + # baton + oraclesdata_L = self.send_and_mine(result["hex"], rpc) + result = rpc.oraclessamples(globals()["oracle_{}".format("L")], oraclesdata_L, "1") + assert_equal("[u'18446744069414584320']", str(result["samples"][0]), "Data match") + + # h type + result = rpc.oraclesdata(globals()["oracle_{}".format("h")], "00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff") + assert_success(result) + # baton + oraclesdata_h = self.send_and_mine(result["hex"], rpc) + result = rpc.oraclessamples(globals()["oracle_{}".format("h")], oraclesdata_h, "1") + assert_equal("[u'ffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000']", str(result["samples"][0]), "Data match") + + # Ihh type + result = rpc.oraclesdata(globals()["oracle_{}".format("Ihh")], "00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff") + assert_success(result) + # baton + oraclesdata_Ihh = self.send_and_mine(result["hex"], rpc) + result = rpc.oraclessamples(globals()["oracle_{}".format("Ihh")], oraclesdata_Ihh, "1") + assert_equal("[u'0']", str(result["samples"][0]), "Data match") + assert_equal("[u'00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff']", str(result["samples"][1]), "Data match") + assert_equal("[u'00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff']", str(result["samples"][2]), "Data match") + def run_test(self): print("Mining blocks...") @@ -138,5 +288,6 @@ class CryptoconditionsOraclesTest(BitcoinTestFramework): rpc1.importprivkey(self.privkey1) self.run_oracles_tests() + if __name__ == '__main__': CryptoconditionsOraclesTest().main() diff --git a/src/Makefile.ktest.include b/src/Makefile.ktest.include index 07c64caa0..640c154e4 100644 --- a/src/Makefile.ktest.include +++ b/src/Makefile.ktest.include @@ -10,7 +10,6 @@ komodo_test_SOURCES = \ test-komodo/test_coinimport.cpp \ test-komodo/test_eval_bet.cpp \ test-komodo/test_eval_notarisation.cpp \ - test-komodo/test_crosschain.cpp \ test-komodo/test_parse_notarisation.cpp komodo_test_CPPFLAGS = $(komodod_CPPFLAGS) diff --git a/src/cc/eval.cpp b/src/cc/eval.cpp index 4c3ed5ee4..35644a82a 100644 --- a/src/cc/eval.cpp +++ b/src/cc/eval.cpp @@ -192,17 +192,6 @@ bool Eval::GetNotarisationData(const uint256 notaryHash, NotarisationData &data) return true; } -/* - * Get MoMoM corresponding to a notarisation tx hash (on assetchain) - */ -bool Eval::GetProofRoot(uint256 kmdNotarisationHash, uint256 &momom) const -{ - std::pair out; - if (!GetNextBacknotarisation(kmdNotarisationHash, out)) return false; - momom = out.second.MoMoM; - return true; -} - uint32_t Eval::GetAssetchainsCC() const { diff --git a/src/cc/eval.h b/src/cc/eval.h index 2a5f41b71..006fca950 100644 --- a/src/cc/eval.h +++ b/src/cc/eval.h @@ -104,7 +104,6 @@ public: virtual bool GetBlock(uint256 hash, CBlockIndex& blockIdx) const; virtual int32_t GetNotaries(uint8_t pubkeys[64][33], int32_t height, uint32_t timestamp) const; virtual bool GetNotarisationData(uint256 notarisationHash, NotarisationData &data) const; - virtual bool GetProofRoot(uint256 kmdNotarisationHash, uint256 &momom) const; virtual bool CheckNotaryInputs(const CTransaction &tx, uint32_t height, uint32_t timestamp) const; virtual uint32_t GetAssetchainsCC() const; virtual std::string GetAssetchainsSymbol() const; diff --git a/src/cc/import.cpp b/src/cc/import.cpp index ffc94ac43..b250bb7bf 100644 --- a/src/cc/import.cpp +++ b/src/cc/import.cpp @@ -16,6 +16,7 @@ #include "cc/eval.h" #include "cc/utils.h" #include "importcoin.h" +#include "crosschain.h" #include "primitives/transaction.h" @@ -75,12 +76,8 @@ bool Eval::ImportCoin(const std::vector params, const CTransaction &imp // Check proof confirms existance of burnTx { - uint256 momom, target; - if (!GetProofRoot(proof.first, momom)) - return Invalid("coudnt-load-momom"); - - target = proof.second.Exec(burnTx.GetHash()); - if (momom != proof.second.Exec(burnTx.GetHash())) + uint256 target = proof.second.Exec(burnTx.GetHash()); + if (!CheckMoMoM(proof.first, target)) return Invalid("momom-check-fail"); } diff --git a/src/crosschain.cpp b/src/crosschain.cpp index 4c0280646..56c310ce7 100644 --- a/src/crosschain.cpp +++ b/src/crosschain.cpp @@ -69,7 +69,7 @@ uint256 CalculateProofRoot(const char* symbol, uint32_t targetCCid, int kmdHeigh destNotarisationTxid = nota.first; else if (seenOwnNotarisations == 2) goto end; - break; + //break; } } @@ -96,8 +96,9 @@ template int ScanNotarisationsFromHeight(int nHeight, const IsTarget f, Notarisation &found) { int limit = std::min(nHeight + NOTARISATION_SCAN_LIMIT_BLOCKS, chainActive.Height()); - - for (int h=nHeight; hphashBlock, notarisations)) @@ -125,7 +126,7 @@ TxProof GetCrossChainProof(const uint256 txid, const char* targetSymbol, uint32_ */ EvalRef eval; uint256 MoM = assetChainProof.second.Exec(txid); - + // Get a kmd height for given notarisation Txid int kmdHeight; { @@ -157,7 +158,7 @@ TxProof GetCrossChainProof(const uint256 txid, const char* targetSymbol, uint32_ uint256 MoMoM = CalculateProofRoot(targetSymbol, targetCCid, kmdHeight, moms, targetChainNotarisationTxid); if (MoMoM.IsNull()) throw std::runtime_error("No MoMs found"); - + // Find index of source MoM in MoMoM int nIndex; for (nIndex=0; nIndexGetTxConfirmed(bn.first, tx, block)){ + fprintf(stderr, "Can't get height of backnotarisation, this should not happen\n"); + return false; + } + + Notarisation nota; + auto checkMoMoM = [&](Notarisation ¬a) { + return nota.second.MoMoM == momom; + }; + + return (bool) ScanNotarisationsFromHeight(block.nHeight-100, checkMoMoM, nota); + +} + + /* * On assetchain * in: txid @@ -278,7 +311,7 @@ TxProof GetAssetchainProof(uint256 hash) }; if (!ScanNotarisationsFromHeight(blockIndex->GetHeight(), isTarget, nota)) throw std::runtime_error("backnotarisation not yet confirmed"); - + // index of block in MoM leaves nIndex = nota.second.height - blockIndex->GetHeight(); } @@ -292,7 +325,7 @@ TxProof GetAssetchainProof(uint256 hash) } bool fMutated; BuildMerkleTree(&fMutated, leaves, tree); - branch = GetMerkleBranch(nIndex, leaves.size(), tree); + branch = GetMerkleBranch(nIndex, leaves.size(), tree); // Check branch uint256 ourResult = SafeCheckMerkleBranch(blockIndex->hashMerkleRoot, branch, nIndex); @@ -331,7 +364,7 @@ TxProof GetAssetchainProof(uint256 hash) } // Check the proof - if (nota.second.MoM != CBlock::CheckMerkleBranch(hash, branch, nIndex)) + if (nota.second.MoM != CBlock::CheckMerkleBranch(hash, branch, nIndex)) throw std::runtime_error("Failed validating MoM"); // All done! diff --git a/src/crosschain.h b/src/crosschain.h index 15452ac63..1fbd7603a 100644 --- a/src/crosschain.h +++ b/src/crosschain.h @@ -15,7 +15,7 @@ TxProof GetCrossChainProof(const uint256 txid, const char* targetSymbol, uint32_ void CompleteImportTransaction(CTransaction &importTx); /* On assetchain */ -bool GetNextBacknotarisation(uint256 txid, std::pair &bn); +bool CheckMoMoM(uint256 kmdNotarisationHash, uint256 momom); #endif /* CROSSCHAIN_H */ diff --git a/src/main.cpp b/src/main.cpp index edb8aa0fb..6c33feb08 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1731,7 +1731,8 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa if (pfMissingInputs) *pfMissingInputs = true; //fprintf(stderr,"missing inputs\n"); - return state.DoS(0, error("AcceptToMemoryPool: tx inputs not found"),REJECT_INVALID, "bad-txns-inputs-missing"); + //return state.DoS(0, error("AcceptToMemoryPool: tx inputs not found"),REJECT_INVALID, "bad-txns-inputs-missing"); + return(false); } } diff --git a/src/rpc/client.cpp b/src/rpc/client.cpp index 91b158f45..c4eb16e0e 100644 --- a/src/rpc/client.cpp +++ b/src/rpc/client.cpp @@ -136,7 +136,6 @@ static const CRPCConvertParam vRPCConvertParams[] = { "z_shieldcoinbase", 3}, { "z_getoperationstatus", 0}, { "z_getoperationresult", 0}, - { "z_importkey", 1 }, { "paxprice", 4 }, { "paxprices", 3 }, { "paxpending", 0 }, diff --git a/src/test-komodo/test_crosschain.cpp b/src/test-komodo/test_crosschain.cpp deleted file mode 100644 index 9d24b4d1d..000000000 --- a/src/test-komodo/test_crosschain.cpp +++ /dev/null @@ -1,213 +0,0 @@ -#include -#include -#include -#include -#include - -#include -#include - -#include "cc/eval.h" -#include "importcoin.h" -#include "base58.h" -#include "core_io.h" -#include "crosschain.h" -#include "key.h" -#include "komodo_structs.h" -#include "main.h" -#include "notarisationdb.h" -#include "primitives/block.h" -#include "primitives/transaction.h" -#include "script/cc.h" -#include "script/interpreter.h" -#include "script/serverchecker.h" -#include "txmempool.h" -#include "crosschain.h" - -#include "testutils.h" - - -extern uint256 komodo_calcMoM(int32_t height,int32_t MoMdepth); -extern bool KOMODO_TEST_ASSETCHAIN_SKIP_POW; - - -/* - * Tests for the whole process of creating and validating notary proofs - * using proof roots (MoMoMs). This is to support coin imports. - */ - -namespace TestCrossChainProof { - - -class TestCrossChain : public ::testing::Test, public Eval { -public: - bool CheckNotaryInputs(const CTransaction &tx, uint32_t height, uint32_t timestamp) const - { - NotarisationData data(2); - return ParseNotarisationOpReturn(tx, data); // If it parses it's valid - } -protected: - static void SetUpTestCase() { } - virtual void SetUp() { - KOMODO_TEST_ASSETCHAIN_SKIP_POW = 1; - ASSETCHAINS_CC = 1; - EVAL_TEST = this; - } -}; - - -uint256 endianHash(uint256 h) -{ - uint256 out; - for (int i=0; i<32; i++) { - out.begin()[31-i] = h.begin()[i]; - } - return out; -} - - -TEST_F(TestCrossChain, testCreateAndValidateImportProof) -{ - /* - * This tests the full process of creation of a cross chain proof. - * For the purposes of the test we will use one assetchain and a KMD chain. - * - * In order to do this test, we need 2 blockchains, so we'll fork and make a socket - * for IPC. - */ - - int childPid = fork(); - void *ctx = zmq_ctx_new(); - void *socket = zmq_socket(ctx, ZMQ_PAIR); - if (!childPid) - strcpy(ASSETCHAINS_SYMBOL, "PIZZA"); - setupChain(); - std::vector blocks; - blocks.resize(1000); - NotarisationData a2kmd(0), kmd2a(1); - int numTestNotarisations = 10; - - - auto SendIPC = [&] (std::vector v) { - assert(v.size() == zmq_send(socket, v.data(), v.size(), 0)); - }; - - auto RecvIPC = [&] () { - std::vector out; - out.resize(100000); - int len = zmq_recv(socket, out.data(), out.size(), 0); - assert(len != -1); - out.resize(len); - return out; - }; - - auto RecordNotarisation = [&] (CTransaction inputTx, NotarisationData data) { - CMutableTransaction mtx = spendTx(inputTx); - mtx.vout.resize(2); - mtx.vout[0].scriptPubKey << VCH(notaryKey.GetPubKey().begin(), 33) << OP_CHECKSIG; - mtx.vout[1].scriptPubKey << OP_RETURN << E_MARSHAL(ss << data); - mtx.vout[1].nValue = 0; - mtx.vin[0].scriptSig << getSig(mtx, inputTx.vout[0].scriptPubKey); - - acceptTxFail(CTransaction(mtx)); - return mtx.GetHash(); - }; - - auto RunTestAssetchain = [&] () - { - NotarisationData n(0), back(1); - strcpy(n.symbol, "PIZZA"); - n.ccId = 2; - int height = 0; - - /* - * Send notarisations and write backnotarisations - */ - for (int ni=0; ni> back)); - RecordNotarisation(blocks[height].vtx[0], back); - } - - /* - * Test a proof - */ - uint256 txid = blocks[7].vtx[0].GetHash(); - TxProof proof = GetAssetchainProof(txid); - SendIPC(E_MARSHAL(ss << txid; ss << proof)); - E_UNMARSHAL(RecvIPC(), ss >> proof); - - std::pair bn; - if (!GetNextBacknotarisation(proof.first, bn)) { - printf("GetNextBackNotarisation failed\n"); - return 1; - } - if (proof.second.Exec(txid) != bn.second.MoMoM) { - printf("MoMom incorrect\n"); - return 1; - } - return 0; - }; - - auto RunTestKmd = [&] () - { - NotarisationData n(0); - int height = 0; - - /* - * Write notarisations and send backnotarisations - */ - for (int ni=0; ni> n); - // Grab a coinbase input to fund notarisation - generateBlock(&blocks[++height]); - n.txHash = RecordNotarisation(blocks[height].vtx[0], n); - { - std::vector moms; - uint256 destNotarisationTxid; - n.MoMoM = CalculateProofRoot(n.symbol, 2, height, moms, destNotarisationTxid); - } - n.IsBackNotarisation = 1; - SendIPC(E_MARSHAL(ss << n)); - } - - /* - * Extend proof - */ - TxProof proof; - uint256 txid; - // Extend proof to MoMoM - assert(E_UNMARSHAL(RecvIPC(), ss >> txid; ss >> proof)); - proof = GetCrossChainProof(txid, (char*)"PIZZA", 2, proof); - SendIPC(E_MARSHAL(ss << proof)); - }; - - const char endpoint[] = "ipc://tmpKomodoTestCrossChainSock"; - - if (!childPid) { - assert(0 == zmq_connect(socket, endpoint)); - usleep(20000); - int out = RunTestAssetchain(); - if (!out) printf("Assetchain success\n"); - exit(out); - } - else { - assert(0 == zmq_bind(socket, endpoint)); - RunTestKmd(); - int returnStatus; - waitpid(childPid, &returnStatus, 0); - unlink("tmpKomodoTestCrossChainSock"); - ASSERT_EQ(0, returnStatus); - } - -} - - -} /* namespace TestCrossChainProof */