shohu / c0ban

c0ban source tree
MIT License
0 stars 0 forks source link

Pass signlogic for bipdersig-p2p.py #62

Closed shohu closed 6 years ago

shohu commented 6 years ago
# test/functional/bipdersig-p2p.py
2018-07-11 05:58:53.252000 TestFramework (INFO): Initializing test directory /tmp/testtmrec8fr
2018-07-11 05:58:53.519000 TestFramework.mininode (INFO): Connecting to Bitcoin Node: 127.0.0.1:11467
2018-07-11 05:58:53.570000 TestFramework (INFO): Mining 1249 blocks
2018-07-11 05:58:55.539000 TestFramework (INFO): Test that a transaction with non-DER signature can still appear in a block
2018-07-11 05:58:55.597000 TestFramework (ERROR): Assertion failed
Traceback (most recent call last):
  File "/c0ban/test/functional/test_framework/test_framework.py", line 120, in main
    self.run_test()
  File "test/functional/bipdersig-p2p.py", line 87, in run_test
    assert_equal(self.nodes[0].getbestblockhash(), block.hash)
  File "/c0ban/test/functional/test_framework/util.py", line 38, in assert_equal
    raise AssertionError("not(%s)" % " == ".join(str(arg) for arg in (thing1, thing2) + args))
AssertionError: not(5c153b120d824cad648aaf1ef25e5b173cbd855cd00e8c7e70ed82b13a294e8b == 421933a79bafcd724249082982417d9c6723917e076e801844b3fedfb92dfa10)
2018-07-11 05:58:55.610000 TestFramework (INFO): Stopping nodes
2018-07-11 05:58:57.865000 TestFramework (WARNING): Not cleaning up dir /tmp/testtmrec8fr
2018-07-11 05:58:57.865000 TestFramework (ERROR): Test failed. Test logging available at /tmp/testtmrec8fr/test_framework.log
shohu commented 6 years ago

debug.log

2018-07-12 06:45:47.678889 ERROR: ConnectBlock: CheckQueue failed
2018-07-12 06:45:47.678922 Misbehaving: 127.0.0.1:39278 peer=0 (0 -> 100) BAN THRESHOLD EXCEEDED
2018-07-12 06:45:47.678968 InvalidChainFound: invalid block=2313365973c6791c0f40185f8eabff99b6994d4f1f9a24e4338e6f6d25123fb5  height=1250  log2_work=11.288866  date=2018-07-12 06:48:42
2018-07-12 06:45:47.679109 InvalidChainFound:  current best=6509e441d8880072273cb7415831c36b8d68820db16aaecc0c1f1d08b6802156  height=1249  log2_work=11.287712  date=2018-07-12 06:48:41
2018-07-12 06:45:47.679127 ERROR: ConnectTip(): ConnectBlock 2313365973c6791c0f40185f8eabff99b6994d4f1f9a24e4338e6f6d25123fb5 failed
2018-07-12 06:45:47.679150 InvalidChainFound: invalid block=2313365973c6791c0f40185f8eabff99b6994d4f1f9a24e4338e6f6d25123fb5  height=1250  log2_work=11.288866  date=2018-07-12 06:48:42
2018-07-12 06:45:47.679170 InvalidChainFound:  current best=6509e441d8880072273cb7415831c36b8d68820db16aaecc0c1f1d08b6802156  height=1249  log2_work=11.287712  date=2018-07-12 06:48:41
2018-07-12 06:45:47.679189 Checking mempool with 0 transactions and 0 inputs
                case OP_CHECKSIG:
                case OP_CHECKSIGVERIFY:
                {
                    // (sig pubkey -- bool)
                    if (stack.size() < 2)
                        return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);

                    valtype& vchSig    = stacktop(-2);
                    valtype& vchPubKey = stacktop(-1);

                    if (!CheckSignatureEncoding(vchSig, flags, serror) ||
                        !CheckPubKeyEncoding(vchPubKey, flags, sigversion, serror)) {
                        // serror is set
                        return false;
                    }
bool CheckSignatureEncoding(const std::vector<uint8_t> &vchSig, uint32_t flags,
                            ScriptError *serror) {
    // Empty signature. Not strictly DER encoded, but allowed to provide a
    // compact way to provide an invalid signature for use with CHECK(MULTI)SIG
    if (vchSig.size() == 0) {
        return true;
    }
    if ((flags & (SCRIPT_VERIFY_DERSIG | SCRIPT_VERIFY_LOW_S |
                  SCRIPT_VERIFY_STRICTENC)) != 0 &&
        !IsValidSignatureEncoding(vchSig)) {
        return set_error(serror, SCRIPT_ERR_SIG_DER);   // ★★★★★★★★★ Error 
    }
bool static IsValidSignatureEncoding(const std::vector<unsigned char> &sig) {
    // Format: 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S] [sighash]
    // * total-length: 1-byte length descriptor of everything that follows,
    //   excluding the sighash byte.
    // * R-length: 1-byte length descriptor of the R value that follows.
    // * R: arbitrary-length big-endian encoded R value. It must use the shortest
    //   possible encoding for a positive integers (which means no null bytes at
    //   the start, except a single one when the next byte has its highest bit set).
    // * S-length: 1-byte length descriptor of the S value that follows.
    // * S: arbitrary-length big-endian encoded S value. The same rules apply.
    // * sighash: 1-byte value indicating what data is hashed (not part of the DER
    //   signature)

    // Minimum and maximum size constraints.
    if (sig.size() < 9) return false;
    if (sig.size() > 73) return false;

    // A signature is of type 0x30 (compound).
    if (sig[0] != 0x30) return false;

    // Make sure the length covers the entire signature.
    if (sig[1] != sig.size() - 3) return false;

    // Extract the length of the R element.
    unsigned int lenR = sig[3];

    // Make sure the length of the S element is still inside the signature.
    if (5 + lenR >= sig.size()) return false;

    // Extract the length of the S element.
    unsigned int lenS = sig[5 + lenR];

    // Verify that the length of the signature matches the sum of the length
    // of the elements.
    if ((size_t)(lenR + lenS + 7) != sig.size()) return false;

    // Check whether the R element is an integer.
    if (sig[2] != 0x02) return false;

    // Zero-length integers are not allowed for R.
    if (lenR == 0) return false;

    // Negative numbers are not allowed for R.
    if (sig[4] & 0x80) return false;

    // Null bytes at the start of R are not allowed, unless R would
    // otherwise be interpreted as a negative number.
    if (lenR > 1 && (sig[4] == 0x00) && !(sig[5] & 0x80)) return false;

    // Check whether the S element is an integer.
    if (sig[lenR + 4] != 0x02) return false;

    // Zero-length integers are not allowed for S.
    if (lenS == 0) return false;

    // Negative numbers are not allowed for S.
    if (sig[lenR + 6] & 0x80) return false;

    // Null bytes at the start of S are not allowed, unless S would otherwise be
    // interpreted as a negative number.
    if (lenS > 1 && (sig[lenR + 6] == 0x00) && !(sig[lenR + 7] & 0x80)) return false;

    return true;
}
shohu commented 6 years ago

sign

2018-07-13 05:14:38.986695 received: block (304 bytes) peer=0
2018-07-13 05:14:38.986766 received block ae4c7dc4c46e487274b3bf1e2785cc5239b398ac12e54b69b5e74800c33fc8ae peer=0
2018-07-13 05:14:38.988430   - Load block from disk: 0.01ms [0.01s]
2018-07-13 05:14:38.988483     - Sanity checks: 0.01ms [0.01s]
2018-07-13 05:14:38.988520     - Fork checks: 0.04ms [0.07s]
2018-07-13 05:14:38.988593       - Connect 2 transactions: 0.07ms (0.036ms/tx, 0.072ms/txin) [0.07s]
2018-07-13 05:14:38.988634 INNNNNNNNN VerifyScript 1
2018-07-13 05:14:38.988680 INNNNNNNNN VerifyScript 2
2018-07-13 05:14:38.988705 INNNNNNNNN EvalScript opcode = 0049
2018-07-13 05:14:38.988733 INNNNNNNNN EvalScript push_back(vchPushValue)
2018-07-13 05:14:38.988754 INNNNNNNNN EvalScript opcode = 0021
2018-07-13 05:14:38.988840 INNNNNNNNN EvalScript push_back(vchPushValue)
2018-07-13 05:14:38.988856 INNNNNNNNN EvalScript opcode = 00ac
2018-07-13 05:14:38.988870 INNNNNNNNN OP_CHECKSIG || OP_CHECKSIGVERIFY
2018-07-13 05:14:38.988882 INNNNNNNNN CheckSignatureEncoding 1
2018-07-13 05:14:38.988898 INNNNNNNNN CheckSignatureEncoding 2. flags = 10c13, if = 2, IsValidSignatureEncoding = 0
2018-07-13 05:14:38.988914 INNNNNNNNN ERR CheckSignatureEncoding, CheckPubKeyEncoding ERROR 1
2018-07-13 05:14:38.988930 INNNNNNNNN VerifyScript 2.5
2018-07-13 05:14:38.988949 ERROR: ConnectBlock: CheckQueue failed
2018-07-13 05:14:38.988973 Misbehaving: 127.0.0.1:54702 peer=0 (0 -> 100) BAN THRESHOLD EXCEEDED
2018-07-13 05:14:38.989016 InvalidChainFound: invalid block=ae4c7dc4c46e487274b3bf1e2785cc5239b398ac12e54b69b5e74800c33fc8ae  height=1250  log2_work=11.288866  date=2018-07-13 05:17:53
2018-07-13 05:14:38.989042 InvalidChainFound:  current best=83c1509552b014cc9a554dcaac18e62e003773d5f23d7f5e906a611df433379a  height=1249  log2_work=11.287712  date=2018-07-13 05:17:52
2018-07-13 05:14:38.989062 ERROR: ConnectTip(): ConnectBlock ae4c7dc4c46e487274b3bf1e2785cc5239b398ac12e54b69b5e74800c33fc8ae failed
2018-07-13 05:14:38.989087 InvalidChainFound: invalid block=ae4c7dc4c46e487274b3bf1e2785cc5239b398ac12e54b69b5e74800c33fc8ae  height=1250  log2_work=11.288866  date=2018-07-13 05:17:53
2018-07-13 05:14:38.989110 InvalidChainFound:  current best=83c1509552b014cc9a554dcaac18e62e003773d5f23d7f5e906a611df433379a  height=1249  log2_work=11.287712  date=2018-07-13 05:17:52
2018-07-13 05:14:38.989131 Checking mempool with 0 transactions and 0 inputs
2018-07-13 05:14:38.989166 sending reject (63 bytes) peer=0
2018-07-13 05:14:38.989275 Warning: not punishing whitelisted peer 127.0.0.1:54702!
2018-07-13 05:14:38.989325 received: ping (8 bytes) peer=0
2018-07-13 05:14:38.989348 sending pong (8 bytes) peer=0

not sign

2018-07-13 05:07:50.066692 received: block (304 bytes) peer=0
2018-07-13 05:07:50.066833 received block a74008734fd8ff5bf38bc96d292df0fcc92b30f0f7e238b22947b27db6451fab peer=0
2018-07-13 05:07:50.068933   - Load block from disk: 0.01ms [0.01s]
2018-07-13 05:07:50.068991     - Sanity checks: 0.01ms [0.02s]
2018-07-13 05:07:50.069033     - Fork checks: 0.04ms [0.07s]
2018-07-13 05:07:50.069113       - Connect 2 transactions: 0.08ms (0.038ms/tx, 0.076ms/txin) [0.07s]
2018-07-13 05:07:50.069161 INNNNNNNNN VerifyScript 1
2018-07-13 05:07:50.069187 INNNNNNNNN VerifyScript 2
2018-07-13 05:07:50.069206 INNNNNNNNN EvalScript opcode = 0049
2018-07-13 05:07:50.069227 INNNNNNNNN EvalScript push_back(vchPushValue)
2018-07-13 05:07:50.069247 INNNNNNNNN EvalScript opcode = 0021
2018-07-13 05:07:50.069266 INNNNNNNNN EvalScript push_back(vchPushValue)
2018-07-13 05:07:50.069357 INNNNNNNNN EvalScript opcode = 00ac
2018-07-13 05:07:50.069377 INNNNNNNNN OP_CHECKSIG || OP_CHECKSIGVERIFY
2018-07-13 05:07:50.069397 INNNNNNNNN CheckSignatureEncoding 1
2018-07-13 05:11:55.704551 INNNNNNNNN CheckSignatureEncoding 2. flags = 0c11, if = 0, IsValidSignatureEncoding = 0
2018-07-13 05:07:50.069563 INNNNNNNNN VerifyScript 3
2018-07-13 05:07:50.069584 INNNNNNNNN VerifyScript 4
2018-07-13 05:07:50.069602 INNNNNNNNN VerifyScript 5
2018-07-13 05:07:50.069619 INNNNNNNNN VerifyScript 6
2018-07-13 05:07:50.069648     - Verify 1 txins: 0.61ms (0.613ms/txin) [0.15s]
2018-07-13 05:07:50.069718     - Index writing: 0.07ms [0.05s]
2018-07-13 05:07:50.069748     - Callbacks: 0.03ms [0.03s]
2018-07-13 05:07:50.069781   - Connect total: 0.86ms [0.27s]
2018-07-13 05:07:50.069811   - Flush: 0.03ms [0.03s]
2018-07-13 05:07:50.069844   - Writing chainstate: 0.03ms [0.03s]
2018-07-13 05:07:50.069942 Blockpolicy estimates updated by 0 of 0 block txs, since last block 0 of 0 tracked, mempool map size 0, max target 0 from current
2018-07-13 05:07:50.070012 UpdateTip: new best=a74008734fd8ff5bf38bc96d292df0fcc92b30f0f7e238b22947b27db6451fab height=1250 version=0x00000002 log2_work=11.288866 tx=1252 date='2018-07-13 05:10:56' progress=1.000000 cache=0.2MiB(1251txo)
2018-07-13 05:07:50.070044   - Connect postprocess: 0.20ms [0.18s]
2018-07-13 05:07:50.070064 - Connect block: 1.13ms [0.51s]
2018-07-13 05:07:50.070088 Checking mempool with 0 transactions and 0 inputs
2018-07-13 05:07:50.070294 AddToWallet 148fa957a3fc4fa85c9c8bd8ce3f5ce776800e9a5ab4a4a12db0e9f12383b2b7  new
2018-07-13 05:07:50.071093 SendMessages: sending inv peer=0 hash=a74008734fd8ff5bf38bc96d292df0fcc92b30f0f7e238b22947b27db6451fab
2018-07-13 05:07:50.071157 sending inv (37 bytes) peer=0
2018-07-13 05:07:50.071289 received: ping (8 bytes) peer=0
2018-07-13 05:07:50.071320 sending pong (8 bytes) peer=0
2018-07-13 05:07:50.072205 received: getdata (37 bytes) peer=0
2018-07-13 05:07:50.072244 received getdata (1 invsz) peer=0
2018-07-13 05:07:50.072268 received getdata for: block a74008734fd8ff5bf38bc96d292df0fcc92b30f0f7e238b22947b27db6451fab peer=0
2018-07-13 05:07:50.072295 sending block (304 bytes) peer=0

sign detail log

2018-07-13 06:05:48.323639 received: block (303 bytes) peer=0
2018-07-13 06:05:48.323740 received block 8c5361856406958b294d48bce4ae4632ee15c6bda43b27b85470ee2abcc73c16 peer=0
2018-07-13 06:05:48.326342   - Load block from disk: 0.01ms [0.01s]
2018-07-13 06:05:48.326430     - Sanity checks: 0.01ms [0.02s]
2018-07-13 06:05:48.326491 INNNNNNNNN GetBlockScriptFlags IsUAHFenabled yes!
2018-07-13 06:05:48.326512 INNNNNNNNN ConnectBlock flags = 10c11
2018-07-13 06:05:48.326538     - Fork checks: 0.12ms [0.14s]
2018-07-13 06:05:48.326575 INNNNNNNNN CheckInputs ConnectBlock. flags = 10c11
2018-07-13 06:05:48.326598 INNNNNNNNN CheckInputs 1 flags = 10c11
2018-07-13 06:05:48.326618 INNNNNNNNN CheckInputs 2 flags = 10c11
2018-07-13 06:05:48.326639 INNNNNNNNN CheckInputs 3 flags = 10c11
2018-07-13 06:05:48.326700       - Connect 2 transactions: 0.16ms (0.080ms/tx, 0.160ms/txin) [0.07s]
2018-07-13 06:05:48.326748 INNNNNNNNN CScriptCheck::operator() VerifyScript
2018-07-13 06:05:48.326784 INNNNNNNNN VerifyScript 1
2018-07-13 06:05:48.326805 INNNNNNNNN VerifyScript 2
2018-07-13 06:05:48.326826 INNNNNNNNN EvalScript opcode = 0048
2018-07-13 06:05:48.326848 INNNNNNNNN EvalScript push_back(vchPushValue)
2018-07-13 06:05:48.326869 INNNNNNNNN EvalScript opcode = 0021
2018-07-13 06:05:48.326887 INNNNNNNNN EvalScript push_back(vchPushValue)
2018-07-13 06:05:48.326905 INNNNNNNNN EvalScript opcode = 00ac
2018-07-13 06:05:48.326924 INNNNNNNNN OP_CHECKSIG || OP_CHECKSIGVERIFY
2018-07-13 06:05:48.326942 INNNNNNNNN CheckSignatureEncoding 1
2018-07-13 06:05:48.326959 INNNNNNNNN IsValidSignatureEncoding 1
2018-07-13 06:05:48.326977 INNNNNNNNN IsValidSignatureEncoding 10
2018-07-13 06:05:48.326994 INNNNNNNNN IsValidSignatureEncoding 20
2018-07-13 06:05:48.327012 INNNNNNNNN IsValidSignatureEncoding 30
2018-07-13 06:05:48.327029 INNNNNNNNN IsValidSignatureEncoding 1
2018-07-13 06:05:48.327046 INNNNNNNNN IsValidSignatureEncoding 10
2018-07-13 06:05:48.327063 INNNNNNNNN IsValidSignatureEncoding 20
2018-07-13 06:05:48.327080 INNNNNNNNN IsValidSignatureEncoding 30
2018-07-13 06:05:48.327099 INNNNNNNNN CheckSignatureEncoding 2. flags = 10c13, if = 2, IsValidSignatureEncoding = 0
2018-07-13 06:05:48.327117 INNNNNNNNN ERR CheckSignatureEncoding, CheckPubKeyEncoding ERROR 1
2018-07-13 06:05:48.327135 INNNNNNNNN VerifyScript 2.5
2018-07-13 06:05:48.327157 ERROR: ConnectBlock: CheckQueue failed
2018-07-13 06:05:48.327186 Misbehaving: 127.0.0.1:55678 peer=0 (0 -> 100) BAN THRESHOLD EXCEEDED
2018-07-13 06:05:48.327225 InvalidChainFound: invalid block=8c5361856406958b294d48bce4ae4632ee15c6bda43b27b85470ee2abcc73c16  height=1250  log2_work=11.288866  date=2018-07-13 06:09:12
2018-07-13 06:05:48.327254 InvalidChainFound:  current best=a6a13f48fabe8876ddeefd6b31b678421de52d59261d8ab5ca932d38e59703d9  height=1249  log2_work=11.287712  date=2018-07-13 06:09:11
2018-07-13 06:05:48.327277 ERROR: ConnectTip(): ConnectBlock 8c5361856406958b294d48bce4ae4632ee15c6bda43b27b85470ee2abcc73c16 failed
2018-07-13 06:05:48.327306 InvalidChainFound: invalid block=8c5361856406958b294d48bce4ae4632ee15c6bda43b27b85470ee2abcc73c16  height=1250  log2_work=11.288866  date=2018-07-13 06:09:12
2018-07-13 06:05:48.327332 InvalidChainFound:  current best=a6a13f48fabe8876ddeefd6b31b678421de52d59261d8ab5ca932d38e59703d9  height=1249  log2_work=11.287712  date=2018-07-13 06:09:11
2018-07-13 06:05:48.327357 Checking mempool with 0 transactions and 0 inputs
2018-07-13 06:05:48.327398 sending reject (63 bytes) peer=0
2018-07-13 06:05:48.327512 Warning: not punishing whitelisted peer 127.0.0.1:55678!
2018-07-13 06:05:48.327570 received: ping (8 bytes) peer=0
2018-07-13 06:05:48.327597 sending pong (8 bytes) peer=0
shohu commented 6 years ago

I passed this test 👍

# test/functional/bipdersig-p2p.py
2018-07-13 06:16:06.685000 TestFramework (INFO): Initializing test directory /tmp/test5az3z5_5
2018-07-13 06:16:06.950000 TestFramework.mininode (INFO): Connecting to Bitcoin Node: 127.0.0.1:15159
2018-07-13 06:16:07.002000 TestFramework (INFO): Mining 1249 blocks
2018-07-13 06:16:09.178000 TestFramework (INFO): Test that a transaction with non-DER signature can still appear in a block
2018-07-13 06:16:09.290000 TestFramework (INFO): Test that blocks must now be at least version 3
2018-07-13 06:16:09.395000 TestFramework (INFO): Test that transactions with non-DER signatures cannot appear in a block
2018-07-13 06:16:09.612000 TestFramework (INFO): Test that a version 3 block with a DERSIG-compliant transaction is accepted
2018-07-13 06:16:09.722000 TestFramework (INFO): Stopping nodes
2018-07-13 06:16:11.916000 TestFramework (INFO): Cleaning up
2018-07-13 06:16:11.919000 TestFramework (INFO): Tests successful