Merge pull request #555 from libscott/cryptoconditions

Various cleanups
This commit is contained in:
jl777
2018-04-09 18:57:14 +03:00
committed by GitHub
16 changed files with 38 additions and 63 deletions

View File

@@ -381,13 +381,13 @@ libbitcoin_common_a_SOURCES = \
hash.cpp \ hash.cpp \
key.cpp \ key.cpp \
keystore.cpp \ keystore.cpp \
komodo_cc.cpp \
netbase.cpp \ netbase.cpp \
primitives/block.cpp \ primitives/block.cpp \
primitives/transaction.cpp \ primitives/transaction.cpp \
protocol.cpp \ protocol.cpp \
pubkey.cpp \ pubkey.cpp \
scheduler.cpp \ scheduler.cpp \
script/cc.cpp \
script/interpreter.cpp \ script/interpreter.cpp \
script/script.cpp \ script/script.cpp \
script/script_error.cpp \ script/script_error.cpp \

View File

@@ -1,7 +1,7 @@
#include <cryptoconditions.h> #include <cryptoconditions.h>
#include "streams.h" #include "streams.h"
#include "komodo_cc.h" #include "script/cc.h"
#include "cc/eval.h" #include "cc/eval.h"
#include "cc/betprotocol.h" #include "cc/betprotocol.h"
#include "primitives/transaction.h" #include "primitives/transaction.h"

View File

@@ -3,7 +3,7 @@
#include "hash.h" #include "hash.h"
#include "chain.h" #include "chain.h"
#include "version.h" #include "version.h"
#include "komodo_cc.h" #include "script/cc.h"
#include "cc/eval.h" #include "cc/eval.h"
#include "cc/betprotocol.h" #include "cc/betprotocol.h"
#include "primitives/transaction.h" #include "primitives/transaction.h"
@@ -61,7 +61,7 @@ bool Eval::DisputePayout(AppVM &vm, std::vector<uint8_t> params, const CTransact
for (int i=1; i<spends.size(); i++) for (int i=1; i<spends.size(); i++)
{ {
std::vector<unsigned char> vmState; std::vector<unsigned char> vmState;
if (!spends[i].vout.size() > 0) continue; if (spends[i].vout.size() == 0) continue;
if (!GetOpReturnData(spends[i].vout[0].scriptPubKey, vmState)) continue; if (!GetOpReturnData(spends[i].vout[0].scriptPubKey, vmState)) continue;
auto out = vm.evaluate(vmParams, vmState); auto out = vm.evaluate(vmParams, vmState);
uint256 resultHash = SerializeHash(out.second); uint256 resultHash = SerializeHash(out.second);

View File

@@ -2,7 +2,7 @@
#include <cryptoconditions.h> #include <cryptoconditions.h>
#include "primitives/transaction.h" #include "primitives/transaction.h"
#include "komodo_cc.h" #include "script/cc.h"
#include "cc/eval.h" #include "cc/eval.h"
#include "main.h" #include "main.h"
#include "chain.h" #include "chain.h"

View File

@@ -22,4 +22,3 @@
converter-sample.c converter-sample.c
config.* config.*
.pytest_cache .pytest_cache
src/asn/asn_system.h

View File

@@ -633,7 +633,7 @@ UniValue txMoMproof(const UniValue& params, bool fHelp)
// parse params and get notarisation data for tx // parse params and get notarisation data for tx
{ {
if ( fHelp || params.size() != 1) if ( fHelp || params.size() != 1)
throw runtime_error("txmomproof needs a txid"); throw runtime_error("txMoMproof needs a txid");
hash = uint256S(params[0].get_str()); hash = uint256S(params[0].get_str());

View File

@@ -1,5 +1,5 @@
#include "cryptoconditions/include/cryptoconditions.h" #include "cryptoconditions/include/cryptoconditions.h"
#include "komodo_cc.h" #include "script/cc.h"
bool IsCryptoConditionsEnabled() bool IsCryptoConditionsEnabled()

View File

@@ -1,5 +1,5 @@
#ifndef KOMODO_CC_H #ifndef SCRIPT_CC_H
#define KOMODO_CC_H #define SCRIPT_CC_H
#include "pubkey.h" #include "pubkey.h"
#include "script/script.h" #include "script/script.h"
@@ -80,4 +80,4 @@ bool GetPushData(const CScript &sig, std::vector<unsigned char> &data);
bool GetOpReturnData(const CScript &sig, std::vector<unsigned char> &data); bool GetOpReturnData(const CScript &sig, std::vector<unsigned char> &data);
#endif /* KOMODO_CC_H */ #endif /* SCRIPT_CC_H */

View File

@@ -8,7 +8,7 @@
#include "script_error.h" #include "script_error.h"
#include "primitives/transaction.h" #include "primitives/transaction.h"
#include "komodo_cc.h" #include "script/cc.h"
#include <vector> #include <vector>
#include <stdint.h> #include <stdint.h>

View File

@@ -7,7 +7,7 @@
#include "tinyformat.h" #include "tinyformat.h"
#include "utilstrencodings.h" #include "utilstrencodings.h"
#include "komodo_cc.h" #include "script/cc.h"
#include "cryptoconditions/include/cryptoconditions.h" #include "cryptoconditions/include/cryptoconditions.h"
namespace { namespace {

View File

@@ -4,7 +4,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "serverchecker.h" #include "serverchecker.h"
#include "komodo_cc.h" #include "script/cc.h"
#include "cc/eval.h" #include "cc/eval.h"
#include "pubkey.h" #include "pubkey.h"

View File

@@ -9,7 +9,7 @@
#include "script/script.h" #include "script/script.h"
#include "util.h" #include "util.h"
#include "utilstrencodings.h" #include "utilstrencodings.h"
#include "komodo_cc.h" #include "script/cc.h"
#include <boost/foreach.hpp> #include <boost/foreach.hpp>

View File

@@ -3,7 +3,7 @@
#include "base58.h" #include "base58.h"
#include "key.h" #include "key.h"
#include "komodo_cc.h" #include "script/cc.h"
#include "cc/eval.h" #include "cc/eval.h"
#include "primitives/transaction.h" #include "primitives/transaction.h"
#include "script/interpreter.h" #include "script/interpreter.h"
@@ -93,20 +93,21 @@ TEST_F(CCTest, testMayAcceptCryptoCondition)
} }
static bool CCVerify(const CMutableTransaction &mtxTo, const CC *cond) {
CAmount amount;
ScriptError error;
CTransaction txTo(mtxTo);
PrecomputedTransactionData txdata(txTo);
auto checker = ServerTransactionSignatureChecker(&txTo, 0, amount, false, txdata);
return VerifyScript(CCSig(cond), CCPubKey(cond), 0, checker, 0, &error);
};
TEST_F(CCTest, testVerifyCryptoCondition) TEST_F(CCTest, testVerifyCryptoCondition)
{ {
CC *cond; CC *cond;
ScriptError error;
CMutableTransaction mtxTo; CMutableTransaction mtxTo;
auto Verify = [&] (const CC *cond) {
CAmount amount;
CTransaction txTo(mtxTo);
PrecomputedTransactionData txdata(txTo);
auto checker = ServerTransactionSignatureChecker(&txTo, 0, amount, false, txdata);
return VerifyScript(CCSig(cond), CCPubKey(cond), 0, checker, 0, &error);
};
// ok // ok
cond = CCNewSecp256k1(notaryKey.GetPubKey()); cond = CCNewSecp256k1(notaryKey.GetPubKey());
CCFromJson(cond, R"!!({ CCFromJson(cond, R"!!({
@@ -114,7 +115,7 @@ TEST_F(CCTest, testVerifyCryptoCondition)
"publicKey": "0205a8ad0c1dbc515f149af377981aab58b836af008d4d7ab21bd76faf80550b47" "publicKey": "0205a8ad0c1dbc515f149af377981aab58b836af008d4d7ab21bd76faf80550b47"
})!!"); })!!");
CCSign(mtxTo, cond); CCSign(mtxTo, cond);
ASSERT_TRUE(Verify(cond)); ASSERT_TRUE(CCVerify(mtxTo, cond));
// has signature nodes // has signature nodes
@@ -128,18 +129,18 @@ TEST_F(CCTest, testVerifyCryptoCondition)
})!!"); })!!");
cond->threshold = 2; cond->threshold = 2;
CCSign(mtxTo, cond); CCSign(mtxTo, cond);
ASSERT_TRUE(Verify(cond)); ASSERT_TRUE(CCVerify(mtxTo, cond));
// no signatures; the preimage will get encoded as a fulfillment because it's cheaper // no signatures; the preimage will get encoded as a fulfillment because it's cheaper
// and the secp256k1 node will get encoded as a condition // and the secp256k1 node will get encoded as a condition
cond->threshold = 1; cond->threshold = 1;
ASSERT_FALSE(Verify(cond)); ASSERT_FALSE(CCVerify(mtxTo, cond));
// here the signature is set wrong // here the signature is set wrong
cond->threshold = 2; cond->threshold = 2;
ASSERT_TRUE(Verify(cond)); ASSERT_TRUE(CCVerify(mtxTo, cond));
memset(cond->subconditions[1]->signature, 0, 32); memset(cond->subconditions[1]->signature, 0, 32);
ASSERT_FALSE(Verify(cond)); ASSERT_FALSE(CCVerify(mtxTo, cond));
} }
extern Eval* EVAL_TEST; extern Eval* EVAL_TEST;
@@ -159,24 +160,15 @@ TEST_F(CCTest, testVerifyEvalCondition)
CC *cond; CC *cond;
ScriptError error;
CMutableTransaction mtxTo; CMutableTransaction mtxTo;
auto Verify = [&] (const CC *cond) {
CAmount amount;
CTransaction txTo(mtxTo);
PrecomputedTransactionData txdata(txTo);
auto checker = ServerTransactionSignatureChecker(&txTo, 0, amount, false, txdata);
return VerifyScript(CCSig(cond), CCPubKey(cond), 0, checker, 0, &error);
};
// ok // ok
cond = CCNewThreshold(2, { CCNewSecp256k1(notaryKey.GetPubKey()), CCNewEval({1}) }); cond = CCNewThreshold(2, { CCNewSecp256k1(notaryKey.GetPubKey()), CCNewEval({1}) });
CCSign(mtxTo, cond); CCSign(mtxTo, cond);
ASSERT_TRUE(Verify(cond)); ASSERT_TRUE(CCVerify(mtxTo, cond));
cond->subconditions[1]->code[0] = 0; cond->subconditions[1]->code[0] = 0;
ASSERT_FALSE(Verify(cond)); ASSERT_FALSE(CCVerify(mtxTo, cond));
} }
@@ -186,24 +178,16 @@ TEST_F(CCTest, testCryptoConditionsDisabled)
ScriptError error; ScriptError error;
CMutableTransaction mtxTo; CMutableTransaction mtxTo;
auto Verify = [&] (const CC *cond) {
CAmount amount;
CTransaction txTo(mtxTo);
PrecomputedTransactionData txdata(txTo);
auto checker = ServerTransactionSignatureChecker(&txTo, 0, amount, false, txdata);
return VerifyScript(CCSig(cond), CCPubKey(cond), 0, checker, 0, &error);
};
// ok // ok
CCFromJson(cond, R"!!({ CCFromJson(cond, R"!!({
"type": "secp256k1-sha-256", "type": "secp256k1-sha-256",
"publicKey": "0205a8ad0c1dbc515f149af377981aab58b836af008d4d7ab21bd76faf80550b47" "publicKey": "0205a8ad0c1dbc515f149af377981aab58b836af008d4d7ab21bd76faf80550b47"
})!!"); })!!");
CCSign(mtxTo, cond); CCSign(mtxTo, cond);
ASSERT_TRUE(Verify(cond)); ASSERT_TRUE(CCVerify(mtxTo, cond));
ASSETCHAINS_CC = 0; ASSETCHAINS_CC = 0;
ASSERT_FALSE(Verify(cond)); ASSERT_FALSE(CCVerify(mtxTo, cond));
} }
@@ -213,14 +197,6 @@ TEST_F(CCTest, testLargeCondition)
ScriptError error; ScriptError error;
CMutableTransaction mtxTo; CMutableTransaction mtxTo;
auto Verify = [&] (const CC *cond) {
CAmount amount;
CTransaction txTo(mtxTo);
PrecomputedTransactionData txdata(txTo);
auto checker = ServerTransactionSignatureChecker(&txTo, 0, amount, false, txdata);
return VerifyScript(CCSig(cond), CCPubKey(cond), 0, checker, 0, &error);
};
std::vector<CC*> ccs; std::vector<CC*> ccs;
for (int i=0; i<18; i++) { for (int i=0; i<18; i++) {
ccs.push_back(CCNewSecp256k1(notaryKey.GetPubKey())); ccs.push_back(CCNewSecp256k1(notaryKey.GetPubKey()));
@@ -230,5 +206,5 @@ TEST_F(CCTest, testLargeCondition)
EXPECT_EQ("(16 of 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,A5,A5)", EXPECT_EQ("(16 of 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,A5,A5)",
CCShowStructure(CCPrune(cond))); CCShowStructure(CCPrune(cond)));
EXPECT_EQ(1744, CCSig(cond).size()); EXPECT_EQ(1744, CCSig(cond).size());
ASSERT_TRUE(Verify(cond)); ASSERT_TRUE(CCVerify(mtxTo, cond));
} }

View File

@@ -6,7 +6,7 @@
#include "base58.h" #include "base58.h"
#include "key.h" #include "key.h"
#include "main.h" #include "main.h"
#include "komodo_cc.h" #include "script/cc.h"
#include "primitives/transaction.h" #include "primitives/transaction.h"
#include "script/interpreter.h" #include "script/interpreter.h"
#include "script/serverchecker.h" #include "script/serverchecker.h"

View File

@@ -7,7 +7,7 @@
#include "core_io.h" #include "core_io.h"
#include "key.h" #include "key.h"
#include "main.h" #include "main.h"
#include "komodo_cc.h" #include "script/cc.h"
#include "primitives/transaction.h" #include "primitives/transaction.h"
#include "script/interpreter.h" #include "script/interpreter.h"
#include "script/serverchecker.h" #include "script/serverchecker.h"

View File

@@ -1,7 +1,7 @@
#ifndef TESTUTILS_H #ifndef TESTUTILS_H
#define TESTUTILS_H #define TESTUTILS_H
#include "komodo_cc.h" #include "script/cc.h"
#define VCH(a,b) std::vector<unsigned char>(a, a + b) #define VCH(a,b) std::vector<unsigned char>(a, a + b)