~ubuntu-branches/debian/stretch/bitcoin/stretch

« back to all changes in this revision

Viewing changes to src/policy/policy.cpp

  • Committer: Package Import Robot
  • Author(s): Anthony Towns
  • Date: 2016-10-21 17:13:13 UTC
  • mfrom: (1.3.2)
  • Revision ID: package-import@ubuntu.com-20161021171313-7eu2ltpbk0xag3q1
Tags: 0.13.0-0.1
* Non-maintainer upload.
* New upstream release.
* Allow compilation with gcc/g++ 6. (Closes: Bug#835963)
* Additional fixes for openssl 1.1 compatibility. (See Bug#828248)
* Check if -latomic is needed (it is on mips*).
* Remove reproducible build patch, since leveldb build system is
  no longer used in 0.13. (See Bug#791834)
* Update description since the blockchain is much more than "several GB"
  now. (Closes: Bug#835809)

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
     * 2. P2SH scripts with a crazy number of expensive
24
24
     *    CHECKSIG/CHECKMULTISIG operations
25
25
     *
26
 
     * Check transaction inputs, and make sure any
27
 
     * pay-to-script-hash transactions are evaluating IsStandard scripts
28
 
     * 
29
26
     * Why bother? To avoid denial-of-service attacks; an attacker
30
27
     * can submit a standard HASH... OP_EQUAL transaction,
31
28
     * which will get accepted into blocks. The redemption
34
31
     *   DUP CHECKSIG DROP ... repeated 100 times... OP_1
35
32
     */
36
33
 
37
 
bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType)
 
34
bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType, const bool witnessEnabled)
38
35
{
39
36
    std::vector<std::vector<unsigned char> > vSolutions;
40
37
    if (!Solver(scriptPubKey, whichType, vSolutions))
53
50
               (!fAcceptDatacarrier || scriptPubKey.size() > nMaxDatacarrierBytes))
54
51
          return false;
55
52
 
 
53
    else if (!witnessEnabled && (whichType == TX_WITNESS_V0_KEYHASH || whichType == TX_WITNESS_V0_SCRIPTHASH))
 
54
        return false;
 
55
 
56
56
    return whichType != TX_NONSTANDARD;
57
57
}
58
58
 
59
 
bool IsStandardTx(const CTransaction& tx, std::string& reason)
 
59
bool IsStandardTx(const CTransaction& tx, std::string& reason, const bool witnessEnabled)
60
60
{
61
61
    if (tx.nVersion > CTransaction::MAX_STANDARD_VERSION || tx.nVersion < 1) {
62
62
        reason = "version";
67
67
    // almost as much to process as they cost the sender in fees, because
68
68
    // computing signature hashes is O(ninputs*txsize). Limiting transactions
69
69
    // to MAX_STANDARD_TX_SIZE mitigates CPU exhaustion attacks.
70
 
    unsigned int sz = tx.GetSerializeSize(SER_NETWORK, CTransaction::CURRENT_VERSION);
71
 
    if (sz >= MAX_STANDARD_TX_SIZE) {
 
70
    unsigned int sz = GetTransactionWeight(tx);
 
71
    if (sz >= MAX_STANDARD_TX_WEIGHT) {
72
72
        reason = "tx-size";
73
73
        return false;
74
74
    }
76
76
    BOOST_FOREACH(const CTxIn& txin, tx.vin)
77
77
    {
78
78
        // Biggest 'standard' txin is a 15-of-15 P2SH multisig with compressed
79
 
        // keys. (remember the 520 byte limit on redeemScript size) That works
 
79
        // keys (remember the 520 byte limit on redeemScript size). That works
80
80
        // out to a (15*(33+1))+3=513 byte redeemScript, 513+1+15*(73+1)+3=1627
81
81
        // bytes of scriptSig, which we round off to 1650 bytes for some minor
82
82
        // future-proofing. That's also enough to spend a 20-of-20
83
83
        // CHECKMULTISIG scriptPubKey, though such a scriptPubKey is not
84
 
        // considered standard)
 
84
        // considered standard.
85
85
        if (txin.scriptSig.size() > 1650) {
86
86
            reason = "scriptsig-size";
87
87
            return false;
95
95
    unsigned int nDataOut = 0;
96
96
    txnouttype whichType;
97
97
    BOOST_FOREACH(const CTxOut& txout, tx.vout) {
98
 
        if (!::IsStandard(txout.scriptPubKey, whichType)) {
 
98
        if (!::IsStandard(txout.scriptPubKey, whichType, witnessEnabled)) {
99
99
            reason = "scriptpubkey";
100
100
            return false;
101
101
        }
140
140
        {
141
141
            std::vector<std::vector<unsigned char> > stack;
142
142
            // convert the scriptSig into a stack, so we can inspect the redeemScript
143
 
            if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), 0))
 
143
            if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SIGVERSION_BASE))
144
144
                return false;
145
145
            if (stack.empty())
146
146
                return false;
153
153
 
154
154
    return true;
155
155
}
 
156
 
 
157
unsigned int nBytesPerSigOp = DEFAULT_BYTES_PER_SIGOP;
 
158
 
 
159
int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost)
 
160
{
 
161
    return (std::max(nWeight, nSigOpCost * nBytesPerSigOp) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR;
 
162
}
 
163
 
 
164
int64_t GetVirtualTransactionSize(const CTransaction& tx, int64_t nSigOpCost)
 
165
{
 
166
    return GetVirtualTransactionSize(GetTransactionWeight(tx), nSigOpCost);
 
167
}