ImportPayout cc eval code and alot of general cc polish. tests to write

This commit is contained in:
Scott Sadler
2018-03-30 15:46:41 -03:00
parent 991c422a9d
commit 2c8d8268dd
25 changed files with 389 additions and 116 deletions

View File

@@ -13,7 +13,7 @@
#include "pubkey.h"
#include "script/script.h"
#include "uint256.h"
#include "komodo_cryptoconditions.h"
#include "cryptoconditions/include/cryptoconditions.h"
using namespace std;
@@ -1295,20 +1295,10 @@ bool TransactionSignatureChecker::CheckSig(
}
bool TransactionSignatureChecker::CheckEvalCondition(const CC *cond) const
{
return EvalConditionValidity(cond, txTo);
}
static int komodoCCEval(CC *cond, void *checker)
{
return ((TransactionSignatureChecker*)checker)->CheckEvalCondition(cond);
}
bool TransactionSignatureChecker::CheckCryptoCondition(const CC *cond, const std::vector<unsigned char>& condBin, const CScript& scriptCode, uint32_t consensusBranchId) const
{
if (!IsAcceptableCryptoCondition(cond)) return false;
uint256 sighash;
try {
sighash = SignatureHash(scriptCode, *txTo, nIn, SIGHASH_ALL, amount, consensusBranchId, this->txdata);
@@ -1316,7 +1306,15 @@ bool TransactionSignatureChecker::CheckCryptoCondition(const CC *cond, const std
return false;
}
return cc_verify(cond, (const unsigned char*)&sighash, 32, 0,
condBin.data(), condBin.size(), komodoCCEval, (void*)this);
condBin.data(), condBin.size(), GetCCEval(), (void*)this);
}
VerifyEval TransactionSignatureChecker::GetCCEval() const {
return [] (CC *cond, void *checker) {
fprintf(stderr, "Cannot check crypto-condition Eval outside of server\n");
return 0;
};
}

View File

@@ -8,7 +8,7 @@
#include "script_error.h"
#include "primitives/transaction.h"
#include "komodo_cryptoconditions.h"
#include "komodo_cc.h"
#include <vector>
#include <stdint.h>
@@ -153,7 +153,7 @@ public:
bool CheckSig(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, uint32_t consensusBranchId) const;
bool CheckLockTime(const CScriptNum& nLockTime) const;
bool CheckCryptoCondition(const CC *cond, const std::vector<unsigned char>& condBin, const CScript& scriptCode, uint32_t consensusBranchId) const;
bool CheckEvalCondition(const CC *cond) const;
VerifyEval GetCCEval() const;
};
class MutableTransactionSignatureChecker : public TransactionSignatureChecker

View File

@@ -7,6 +7,8 @@
#include "tinyformat.h"
#include "utilstrencodings.h"
#include "komodo_cc.h"
#include "cryptoconditions/include/cryptoconditions.h"
namespace {
inline std::string ValueString(const std::vector<unsigned char>& vch)
@@ -238,6 +240,20 @@ bool CScript::IsPayToCryptoCondition() const
return 0;
}
bool CScript::MayAcceptCryptoCondition() const
{
// Get the type mask of the condition
const_iterator pc = this->begin();
vector<unsigned char> data;
opcodetype opcode;
if (!this->GetOp(pc, opcode, data)) return false;
if (!(opcode > OP_0 && opcode < OP_PUSHDATA1)) return false;
CC *cond = cc_readConditionBinary(data.data(), data.size());
if (!cond) return false;
bool accept = IsAcceptableCryptoCondition(cond);
return accept;
}
bool CScript::IsPushOnly() const
{
const_iterator pc = begin();

View File

@@ -565,6 +565,7 @@ public:
bool IsPayToScriptHash() const;
bool IsPayToCryptoCondition() const;
bool MayAcceptCryptoCondition() const;
/** Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical). */
bool IsPushOnly() const;

View File

@@ -3,7 +3,9 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "sigcache.h"
#include "serverchecker.h"
#include "komodo_cc.h"
#include "cc/eval.h"
#include "pubkey.h"
#include "random.h"
@@ -26,13 +28,13 @@ private:
//! sigdata_type is (signature hash, signature, public key):
typedef boost::tuple<uint256, std::vector<unsigned char>, CPubKey> sigdata_type;
std::set< sigdata_type> setValid;
boost::shared_mutex cs_sigcache;
boost::shared_mutex cs_serverchecker;
public:
bool
Get(const uint256 &hash, const std::vector<unsigned char>& vchSig, const CPubKey& pubKey)
{
boost::shared_lock<boost::shared_mutex> lock(cs_sigcache);
boost::shared_lock<boost::shared_mutex> lock(cs_serverchecker);
sigdata_type k(hash, vchSig, pubKey);
std::set<sigdata_type>::iterator mi = setValid.find(k);
@@ -47,10 +49,10 @@ public:
// (~200 bytes per cache entry times 50,000 entries)
// Since there can be no more than 20,000 signature operations per block
// 50,000 is a reasonable default.
int64_t nMaxCacheSize = GetArg("-maxsigcachesize", 50000);
int64_t nMaxCacheSize = GetArg("-maxservercheckersize", 50000);
if (nMaxCacheSize <= 0) return;
boost::unique_lock<boost::shared_mutex> lock(cs_sigcache);
boost::unique_lock<boost::shared_mutex> lock(cs_serverchecker);
while (static_cast<int64_t>(setValid.size()) > nMaxCacheSize)
{
@@ -74,7 +76,7 @@ public:
}
bool CachingTransactionSignatureChecker::VerifySignature(const std::vector<unsigned char>& vchSig, const CPubKey& pubkey, const uint256& sighash) const
bool ServerTransactionSignatureChecker::VerifySignature(const std::vector<unsigned char>& vchSig, const CPubKey& pubkey, const uint256& sighash) const
{
static CSignatureCache signatureCache;
@@ -88,3 +90,24 @@ bool CachingTransactionSignatureChecker::VerifySignature(const std::vector<unsig
signatureCache.Set(sighash, vchSig, pubkey);
return true;
}
/*
* The reason that these functions are here is that the what used to be the
* CachingTransactionSignatureChecker, now the ServerTransactionSignatureChecker,
* is an entry point that the server uses to validate signatures and which is not
* included as part of bitcoin common libs. Since Crypto-Condtions eval methods
* may call server code (GetTransaction etc), the best way to get it to run this
* code without pulling the whole bitcoin server code into bitcoin common was
* using this class. Thus it has been renamed to ServerTransactionSignatureChecker.
*/
VerifyEval ServerTransactionSignatureChecker::GetCCEval() const
{
return [] (CC *cond, void *checker) {
return ((ServerTransactionSignatureChecker*)checker)->CheckEvalCondition(cond);
};
}
int ServerTransactionSignatureChecker::CheckEvalCondition(CC *cond) const
{
return EvalConditionValidity(cond, txTo, nIn);
}

View File

@@ -0,0 +1,30 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_SCRIPT_SERVERCHECKER_H
#define BITCOIN_SCRIPT_SERVERCHECKER_H
#include "script/interpreter.h"
#include <vector>
class CPubKey;
class ServerTransactionSignatureChecker : public TransactionSignatureChecker
{
private:
bool store;
const CTransaction* txTo;
unsigned int nIn;
public:
ServerTransactionSignatureChecker(const CTransaction* txToIn, unsigned int nInIn, const CAmount& amount, bool storeIn, PrecomputedTransactionData& txdataIn) : TransactionSignatureChecker(txToIn, nInIn, amount, txdataIn), store(storeIn) {}
bool VerifySignature(const std::vector<unsigned char>& vchSig, const CPubKey& vchPubKey, const uint256& sighash) const;
int CheckEvalCondition(CC *cond) const;
VerifyEval GetCCEval() const;
};
#endif // BITCOIN_SCRIPT_SERVERCHECKER_H

View File

@@ -1,26 +0,0 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_SCRIPT_SIGCACHE_H
#define BITCOIN_SCRIPT_SIGCACHE_H
#include "script/interpreter.h"
#include <vector>
class CPubKey;
class CachingTransactionSignatureChecker : public TransactionSignatureChecker
{
private:
bool store;
public:
CachingTransactionSignatureChecker(const CTransaction* txToIn, unsigned int nInIn, const CAmount& amount, bool storeIn, PrecomputedTransactionData& txdataIn) : TransactionSignatureChecker(txToIn, nInIn, amount, txdataIn), store(storeIn) {}
bool VerifySignature(const std::vector<unsigned char>& vchSig, const CPubKey& vchPubKey, const uint256& sighash) const;
};
#endif // BITCOIN_SCRIPT_SIGCACHE_H

View File

@@ -9,7 +9,7 @@
#include "script/script.h"
#include "util.h"
#include "utilstrencodings.h"
#include "komodo_cryptoconditions.h"
#include "komodo_cc.h"
#include <boost/foreach.hpp>
@@ -72,9 +72,11 @@ bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, vector<vector<unsi
if (IsCryptoConditionsEnabled()) {
// Shortcut for pay-to-crypto-condition
if (scriptPubKey.IsPayToCryptoCondition()) {
typeRet = TX_CRYPTOCONDITION;
// TODO: Extract solutions
return true;
if (scriptPubKey.MayAcceptCryptoCondition()) {
typeRet = TX_CRYPTOCONDITION;
return true;
}
return false;
}
}