shohu / c0ban

c0ban source tree
MIT License
0 stars 0 forks source link

Pass for test bip68-112-113-p2p.py #24

Open shohu opened 6 years ago

shohu commented 6 years ago
# test/functional/bip68-112-113-p2p.py
2018-03-22 06:54:05.307000 TestFramework (INFO): Initializing test directory /tmp/test9v2gvofb
2018-03-22 06:54:05.570000 TestFramework.mininode (INFO): Connecting to Bitcoin Node: 127.0.0.1:12346
2018-03-22 06:54:06.100000 TestFramework.comptool (INFO): Test 1: PASS
2018-03-22 06:54:06.673000 TestFramework.comptool (INFO): Test 2: PASS
2018-03-22 06:54:07.355000 TestFramework.comptool (INFO): Test 3: PASS
2018-03-22 06:54:07.943000 TestFramework.comptool (INFO): Test 4: PASS
2018-03-22 06:54:08.512000 TestFramework.comptool (INFO): Test 5: PASS
2018-03-22 06:54:09.037000 TestFramework.comptool (INFO): Test 6: PASS
2018-03-22 06:54:09.221000 TestFramework.comptool (INFO): Test 7: PASS
2018-03-22 06:54:09.481000 TestFramework.comptool (INFO): Test 8: PASS
2018-03-22 06:54:09.539000 TestFramework.comptool (INFO): Test 9: PASS
2018-03-22 06:54:09.643000 TestFramework.comptool (INFO): Test 10: PASS
2018-03-22 06:54:09.855000 TestFramework.comptool (INFO): Test 11: PASS
2018-03-22 06:54:10.065000 TestFramework.comptool (INFO): Test 12: PASS
2018-03-22 06:54:10.318000 TestFramework.comptool (INFO): Test 13: PASS
2018-03-22 06:54:10.526000 TestFramework.comptool (INFO): Test 14: PASS
2018-03-22 06:54:10.686000 TestFramework.comptool (INFO): Test 15: PASS
2018-03-22 06:54:10.793000 TestFramework.comptool (INFO): Test 16: PASS
2018-03-22 06:54:10.896000 TestFramework.comptool (INFO): Test 17: PASS
2018-03-22 06:54:11.000000 TestFramework.comptool (INFO): Test 18: PASS
2018-03-22 06:54:11.103000 TestFramework.comptool (INFO): Test 19: PASS
2018-03-22 06:54:11.207000 TestFramework.comptool (INFO): Test 20: PASS
2018-03-22 06:54:11.311000 TestFramework.comptool (INFO): Test 21: PASS
2018-03-22 06:54:11.416000 TestFramework.comptool (INFO): Test 22: PASS
2018-03-22 06:54:11.520000 TestFramework.comptool (INFO): Test 23: PASS
2018-03-22 06:54:11.768000 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/bip68-112-113-p2p.py", line 104, in run_test
    test.run()
  File "/c0ban/test/functional/test_framework/comptool.py", line 391, in run
    raise AssertionError("Block test failed at test %d" % test_number)
AssertionError: Block test failed at test 24
2018-03-22 06:54:11.783000 TestFramework (INFO): Stopping nodes
2018-03-22 06:54:13.907000 TestFramework (WARNING): Not cleaning up dir /tmp/test9v2gvofb
2018-03-22 06:54:13.907000 TestFramework (ERROR): Test failed. Test logging available at /tmp/test9v2gvofb/test_framework.log
shohu commented 6 years ago

c0ban log

2018-03-22 07:18:47.150430 got inv: block 553e459fd14fbc07bec51c8883b4caf6b9a0fc8a6f66238800cef4ac476a6810  have peer=0
2018-03-22 07:18:47.150449 got inv: block 5b1716aa2275f515469a8691dc2f2f4afdca292bc844fceb28e18de6de38d95b  new peer=0
2018-03-22 07:18:47.150477 sending getheaders (709 bytes) peer=0
2018-03-22 07:18:47.150608 getheaders (580) 5b1716aa2275f515469a8691dc2f2f4afdca292bc844fceb28e18de6de38d95b to peer=0
2018-03-22 07:18:47.151625 received: headers (163 bytes) peer=0
2018-03-22 07:18:47.152013 Requesting block 5b1716aa2275f515469a8691dc2f2f4afdca292bc844fceb28e18de6de38d95b (580) peer=0
2018-03-22 07:18:47.152040 sending getdata (37 bytes) peer=0
2018-03-22 07:18:47.152854 received: block (145 bytes) peer=0
2018-03-22 07:18:47.152929 received block 5b1716aa2275f515469a8691dc2f2f4afdca292bc844fceb28e18de6de38d95b peer=0
2018-03-22 07:18:47.255728 received: getheaders (293 bytes) peer=0
2018-03-22 07:18:47.255820 getheaders 580 to end from peer=0
2018-03-22 07:18:47.255857 sending headers (82 bytes) peer=0
2018-03-22 07:18:47.152980 CheckBlock is False
2018-03-22 07:18:47.153123 AcceptBlock ret = 1

bitcoin log

2018-03-22 07:19:14.262782 got inv: block 16fb06ef99f5ec99ecdcb42ac8c7f28bcdd1f49c8487ac03b1fca1a57410727f  have peer=0
2018-03-22 07:19:14.262799 got inv: block 0f1b8609deedab574b33f0f51a3c5f0c3bf442b2cc4f18b654827a809ece9dc0  new peer=0
2018-03-22 07:19:14.262824 sending getheaders (709 bytes) peer=0
2018-03-22 07:19:14.263016 getheaders (580) 0f1b8609deedab574b33f0f51a3c5f0c3bf442b2cc4f18b654827a809ece9dc0 to peer=0
2018-03-22 07:19:14.263897 received: headers (163 bytes) peer=0
2018-03-22 07:19:14.264195 Requesting block 0f1b8609deedab574b33f0f51a3c5f0c3bf442b2cc4f18b654827a809ece9dc0 (580) peer=0
2018-03-22 07:19:14.264218 sending getdata (37 bytes) peer=0
2018-03-22 07:19:14.264890 received: block (145 bytes) peer=0
2018-03-22 07:19:14.264927 received block 0f1b8609deedab574b33f0f51a3c5f0c3bf442b2cc4f18b654827a809ece9dc0 peer=0
2018-03-22 07:19:14.265231   - Load block from disk: 0.00ms [0.00s]
2018-03-22 07:19:14.265266     - Sanity checks: 0.01ms [0.00s]
2018-03-22 07:19:14.265290     - Fork checks: 0.02ms [0.02s]
2018-03-22 07:19:14.265317       - Connect 1 transactions: 0.03ms (0.026ms/tx, 0.000ms/txin) [0.02s]
2018-03-22 07:19:14.265348     - Verify 0 txins: 0.06ms (0.000ms/txin) [0.05s]
2018-03-22 07:19:14.265390     - Index writing: 0.04ms [0.03s]
2018-03-22 07:19:14.265411     - Callbacks: 0.02ms [0.01s]
2018-03-22 07:19:14.265434   - Connect total: 0.21ms [0.14s]
2018-03-22 07:19:14.265453   - Flush: 0.02ms [0.01s]
2018-03-22 07:19:14.265476   - Writing chainstate: 0.02ms [0.02s]
2018-03-22 07:19:14.265538 UpdateTip: new best=0f1b8609deedab574b33f0f51a3c5f0c3bf442b2cc4f18b654827a809ece9dc0 height=580 version=0x000004d2 log2_work=10.182394 tx=663 date='2018-03-18 19:35:41' progress=1.000000 cache=0.1MiB(662txo) warning='5 of last 100 blocks have unexpected version'
2018-03-22 07:19:14.265564   - Connect postprocess: 0.09ms [0.09s]
2018-03-22 07:19:14.265578 - Connect block: 0.34ms [0.26s]
2018-03-22 07:19:14.265595 Checking mempool with 17 transactions and 17 inputs
2018-03-22 07:19:14.365585 received: getheaders (293 bytes) peer=0
2018-03-22 07:19:14.365697 getheaders 580 to end from peer=0
2018-03-22 07:19:14.365751 sending headers (82 bytes) peer=0
2018-03-22 07:19:14.366013 received: ping (8 bytes) peer=0

received: block

    else if (strCommand == NetMsgType::BLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
    {
        std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
        vRecv >> *pblock;

        LogPrint(BCLog::NET, "received block %s peer=%d\n", pblock->GetHash().ToString(), pfrom->GetId());

received block

    else if (strCommand == NetMsgType::BLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
    {
        std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
        vRecv >> *pblock;

        LogPrint(BCLog::NET, "received block %s peer=%d\n", pblock->GetHash().ToString(), pfrom->GetId());
bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW, bool fCheckMerkleRoot)
{
    // These are checks that are independent of context.

    if (block.fChecked)
        return true;

    // Check that the header is valid (particularly PoW).  This is mostly
    // redundant with the call in AcceptBlockHeader.
    if (!CheckBlockHeader(block, state, consensusParams, fCheckPOW))
        return false;
static bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW = true)
{
    // Check proof of work matches claimed amount
    if (fCheckPOW && !CheckProofOfWork(block.GetHash(), block.nBits, consensusParams))
        return state.DoS(50, false, REJECT_INVALID, "high-hash", false, "proof of work failed");

    return true;
}
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params)
{
    bool fNegative;
    bool fOverflow;
    arith_uint256 bnTarget;

    bnTarget.SetCompact(nBits, &fNegative, &fOverflow);

    // Check range
    if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(params.powLimit))
        return false;

    // Check proof of work matches claimed amount
    if (UintToArith256(hash) > bnTarget)
        return false;

    return true;
}
249         def check_results(self, blockhash, outcome):
250             with mininode_lock:
251                 for c in self.connections:
252                     if outcome is None:
253                         if c.cb.bestblockhash != self.connections[0].cb.bestblockhash:
254                             return False
255                     elif isinstance(outcome, RejectResult): # Check that block was rejected w/ code
256                         if c.cb.bestblockhash == blockhash:
257                             return False
258                         if blockhash not in c.cb.block_reject_map:
259                             logger.error('Block not in reject map: %064x' % (blockhash))
260                             return False
261                         if not outcome.match(c.cb.block_reject_map[blockhash]):
262                             logger.error('Block rejected with %s instead of expected %s: %064x' % (c.cb.block_reject_map[blockhash], outcome, blockhash))
263                             return False
264                     elif ((c.cb.bestblockhash == blockhash) != outcome):
265  ->                     return False   ##### error occured
shohu commented 6 years ago

testcode

        # Advance one block to 581
        test_blocks = self.generate_blocks(1, 1234)
        yield TestInstance(test_blocks, sync_every_block=False) # 24

c0band can't sync generate

(Pdb) n
> /c0ban/test/functional/test_framework/comptool.py(389)run()
-> invqueue = []
(Pdb) n
> /c0ban/test/functional/test_framework/comptool.py(390)run()
-> self.sync_blocks(block.sha256, len(test_instance.blocks_and_transactions))
(Pdb) n
!!!!!!! bestblockhash = 7254295806296385607396220691757541387140929673216191150035359653484563132933
> /c0ban/test/functional/test_framework/comptool.py(391)run()
-> if (not self.check_results(tip, block_outcome)):

I tried following source... but can't pass test... orz

2018-03-26 17 34 34

shohu commented 6 years ago

changed bestblockhash when send_message

            # Do final sync if we weren't syncing on every block or every tx.
            if (not test_instance.sync_every_block and block is not None):
                if len(invqueue) > 0:
                    [ c.send_message(msg_inv(invqueue)) for c in self.connections ]
                    invqueue = []
                self.sync_blocks(block.sha256, len(test_instance.blocks_and_transactions))
                if (not self.check_results(tip, block_outcome)):
                    self.check_results(tip, block_outcome)
                    raise AssertionError("Block test failed at test %d" % test_number)
shohu commented 6 years ago

I realized 600 is block generation time. I will try to change 600(60 sec * 10 minute) -> 32 sec

testcode

    def generate_blocks(self, number, version, test_blocks = []):
        for i in range(number):
            block = self.create_test_block([], version)
            test_blocks.append([block, True])
            # self.last_block_time += 600
            self.last_block_time += 32
            self.tip = block.sha256
            self.tipheight += 1
        return test_blocks

But I got new error...

# test/functional/bip68-112-113-p2p.py
2018-04-05 04:50:33.765000 TestFramework (INFO): Initializing test directory /tmp/test2vsii2i7
2018-04-05 04:50:34.029000 TestFramework.mininode (INFO): Connecting to Bitcoin Node: 127.0.0.1:11825
2018-04-05 04:50:34.540000 TestFramework.comptool (INFO): Test 1: PASS
2018-04-05 04:50:35.046000 TestFramework.comptool (INFO): Test 2: PASS
2018-04-05 04:50:35.626000 TestFramework.comptool (INFO): Test 3: PASS
2018-04-05 04:50:36.221000 TestFramework.comptool (INFO): Test 4: PASS
2018-04-05 04:50:36.779000 TestFramework.comptool (INFO): Test 5: PASS
2018-04-05 04:50:37.303000 TestFramework.comptool (INFO): Test 6: PASS
2018-04-05 04:50:37.484000 TestFramework.comptool (INFO): Test 7: PASS
2018-04-05 04:50:37.696000 TestFramework.comptool (INFO): Test 8: PASS
2018-04-05 04:50:37.806000 TestFramework.comptool (INFO): Test 9: PASS
2018-04-05 04:50:37.910000 TestFramework.comptool (INFO): Test 10: PASS
2018-04-05 04:50:38.121000 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/bip68-112-113-p2p.py", line 104, in run_test
    test.run()
  File "/c0ban/test/functional/test_framework/comptool.py", line 355, in run
    raise AssertionError("Test failed at test %d" % test_number)
AssertionError: Test failed at test 11
2018-04-05 04:50:38.138000 TestFramework (INFO): Stopping nodes
2018-04-05 04:50:40.427000 TestFramework (WARNING): Not cleaning up dir /tmp/test2vsii2i7
2018-04-05 04:50:40.428000 TestFramework (ERROR): Test failed. Test logging available at /tmp/test2vsii2i7/test_framework.log
shohu commented 6 years ago

2018-04-05 13 56 29

I changed all block generation time in this test, but I got new error...

# test/functional/bip68-112-113-p2p.py
2018-04-05 05:38:05.680000 TestFramework (INFO): Initializing test directory /tmp/test8z0ljt77
2018-04-05 05:38:05.944000 TestFramework.mininode (INFO): Connecting to Bitcoin Node: 127.0.0.1:13481
2018-04-05 05:38:06.440000 TestFramework.comptool (INFO): Test 1: PASS
2018-04-05 05:38:06.991000 TestFramework.comptool (INFO): Test 2: PASS
2018-04-05 05:38:07.520000 TestFramework.comptool (INFO): Test 3: PASS
2018-04-05 05:38:08.113000 TestFramework.comptool (INFO): Test 4: PASS
2018-04-05 05:38:08.688000 TestFramework.comptool (INFO): Test 5: PASS
2018-04-05 05:38:09.198000 TestFramework.comptool (INFO): Test 6: PASS
2018-04-05 05:38:09.431000 TestFramework.comptool (INFO): Test 7: PASS
2018-04-05 05:38:09.642000 TestFramework.comptool (INFO): Test 8: PASS
2018-04-05 05:38:09.752000 TestFramework.comptool (INFO): Test 9: PASS
2018-04-05 05:38:09.807000 TestFramework.comptool (INFO): Test 10: PASS
2018-04-05 05:38:10.018000 TestFramework.comptool (INFO): Test 11: PASS
2018-04-05 05:38:10.227000 TestFramework.comptool (INFO): Test 12: PASS
2018-04-05 05:38:10.480000 TestFramework.comptool (INFO): Test 13: PASS
2018-04-05 05:38:10.689000 TestFramework.comptool (INFO): Test 14: PASS
2018-04-05 05:38:10.901000 TestFramework.comptool (INFO): Test 15: PASS
2018-04-05 05:38:10.958000 TestFramework.comptool (INFO): Test 16: PASS
2018-04-05 05:38:11.062000 TestFramework.comptool (INFO): Test 17: PASS
2018-04-05 05:38:11.168000 TestFramework.comptool (INFO): Test 18: PASS
2018-04-05 05:38:11.272000 TestFramework.comptool (INFO): Test 19: PASS
2018-04-05 05:38:11.376000 TestFramework.comptool (INFO): Test 20: PASS
2018-04-05 05:38:11.481000 TestFramework.comptool (INFO): Test 21: PASS
2018-04-05 05:38:11.586000 TestFramework.comptool (INFO): Test 22: PASS
2018-04-05 05:38:11.690000 TestFramework.comptool (INFO): Test 23: PASS
2018-04-05 05:38:11.940000 TestFramework.comptool (INFO): Test 24: PASS
2018-04-05 05:38:12.149000 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/bip68-112-113-p2p.py", line 105, in run_test
    test.run()
  File "/c0ban/test/functional/test_framework/comptool.py", line 357, in run
    raise AssertionError("Test failed at test %d" % test_number)
AssertionError: Test failed at test 25
2018-04-05 05:38:12.176000 TestFramework (INFO): Stopping nodes
2018-04-05 05:38:14.337000 TestFramework (WARNING): Not cleaning up dir /tmp/test8z0ljt77
2018-04-05 05:38:14.337000 TestFramework (ERROR): Test failed. Test logging available at /tmp/test8z0ljt77/test_framework.log

testcode

    # Either send inv's to each node and sync, or add
    # to invqueue for later inv'ing.
    if (test_instance.sync_every_block):
        # if we expect success, send inv and sync every block
        # if we expect failure, just push the block and see what happens.
        if outcome == True:
            [ c.cb.send_inv(block) for c in self.connections ]
            self.sync_blocks(block.sha256, 1)
        else:
            [ c.send_message(msg_block(block)) for c in self.connections ]
            [ c.cb.send_ping(self.ping_counter) for c in self.connections ]
            self.wait_for_pings(self.ping_counter)
            self.ping_counter += 1
        if (not self.check_results(tip, outcome)):
            raise AssertionError("Test failed at test %d" % test_number)   # <-- Error was occured !!!
shohu commented 6 years ago

testcode

        # Height txs should fail and time txs should now pass 9 * 600 > 10 * 512
        bip68success_txs.extend(bip68timetxs)
        yield TestInstance([[self.create_test_block(bip68success_txs), True]]) # 25

c0band

    /* In order to use the same number of bits to encode roughly the
     * same wall-clock duration, and because blocks are naturally
     * limited to occur every 600s on average, the minimum granularity
     * for time-based relative lock-time is fixed at 512 seconds.
     * Converting from CTxIn::nSequence to seconds is performed by
     * multiplying by 512 = 2^9, or equivalently shifting up by
     * 9 bits. */
    static const int SEQUENCE_LOCKTIME_GRANULARITY = 5;

c0ban was changed SEQUENCE_LOCKTIME_GRANULARITY from 9 -> 5 2018-04-05 15 19 22

shohu commented 6 years ago

base_relative_locktime = 10 -> 9 I passed 25 , but I got new error

# test/functional/bip68-112-113-p2p.py
2018-04-05 06:46:34.146000 TestFramework (INFO): Initializing test directory /tmp/testzxy19e0d
2018-04-05 06:46:34.408000 TestFramework.mininode (INFO): Connecting to Bitcoin Node: 127.0.0.1:15137
2018-04-05 06:46:34.901000 TestFramework.comptool (INFO): Test 1: PASS
2018-04-05 06:46:35.449000 TestFramework.comptool (INFO): Test 2: PASS
2018-04-05 06:46:36.012000 TestFramework.comptool (INFO): Test 3: PASS
2018-04-05 06:46:36.549000 TestFramework.comptool (INFO): Test 4: PASS
2018-04-05 06:46:37.060000 TestFramework.comptool (INFO): Test 5: PASS
2018-04-05 06:46:37.608000 TestFramework.comptool (INFO): Test 6: PASS
2018-04-05 06:46:37.789000 TestFramework.comptool (INFO): Test 7: PASS
2018-04-05 06:46:38.000000 TestFramework.comptool (INFO): Test 8: PASS
2018-04-05 06:46:38.109000 TestFramework.comptool (INFO): Test 9: PASS
2018-04-05 06:46:38.213000 TestFramework.comptool (INFO): Test 10: PASS
2018-04-05 06:46:38.421000 TestFramework.comptool (INFO): Test 11: PASS
2018-04-05 06:46:38.630000 TestFramework.comptool (INFO): Test 12: PASS
2018-04-05 06:46:38.829000 TestFramework.comptool (INFO): Test 13: PASS
2018-04-05 06:46:39.086000 TestFramework.comptool (INFO): Test 14: PASS
2018-04-05 06:46:39.249000 TestFramework.comptool (INFO): Test 15: PASS
2018-04-05 06:46:39.358000 TestFramework.comptool (INFO): Test 16: PASS
2018-04-05 06:46:39.461000 TestFramework.comptool (INFO): Test 17: PASS
2018-04-05 06:46:39.566000 TestFramework.comptool (INFO): Test 18: PASS
2018-04-05 06:46:39.671000 TestFramework.comptool (INFO): Test 19: PASS
2018-04-05 06:46:39.776000 TestFramework.comptool (INFO): Test 20: PASS
2018-04-05 06:46:39.881000 TestFramework.comptool (INFO): Test 21: PASS
2018-04-05 06:46:39.987000 TestFramework.comptool (INFO): Test 22: PASS
2018-04-05 06:46:40.091000 TestFramework.comptool (INFO): Test 23: PASS
2018-04-05 06:46:40.291000 TestFramework.comptool (INFO): Test 24: PASS
2018-04-05 06:46:40.497000 TestFramework.comptool (INFO): Test 25: PASS
2018-04-05 06:46:40.605000 TestFramework.comptool (INFO): Test 26: PASS
2018-04-05 06:46:40.709000 TestFramework.comptool (INFO): Test 27: PASS
2018-04-05 06:46:40.812000 TestFramework.comptool (INFO): Test 28: PASS
2018-04-05 06:46:40.917000 TestFramework.comptool (INFO): Test 29: PASS
2018-04-05 06:46:41.165000 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/bip68-112-113-p2p.py", line 105, in run_test
    test.run()
  File "/c0ban/test/functional/test_framework/comptool.py", line 394, in run
    raise AssertionError("Block test failed at test %d" % test_number)
AssertionError: Block test failed at test 30
2018-04-05 06:46:41.176000 TestFramework (INFO): Stopping nodes
2018-04-05 06:46:43.377000 TestFramework (WARNING): Not cleaning up dir /tmp/testzxy19e0d
2018-04-05 06:46:43.377000 TestFramework (ERROR): Test failed. Test logging available at /tmp/testzxy19e0d/test_framework.log
shohu commented 6 years ago

I found error in debug.log

2018-04-05 06:49:33.210010   - Load block from disk: 0.01ms [0.00s]
2018-04-05 06:49:33.210136     - Sanity checks: 0.04ms [0.00s]
2018-04-05 06:49:33.210196     - Fork checks: 0.06ms [0.02s]
2018-04-05 06:49:33.210303 ERROR: ConnectBlock: contains a non-BIP68-final transaction
2018-04-05 06:49:33.210411 Misbehaving: 127.0.0.1:33426 peer=0 (820 -> 920)

c0band

static bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pindex,
                  CCoinsViewCache& view, const CChainParams& chainparams, bool fJustCheck = false)
{
  :
  :
        if (!tx.IsCoinBase())
        {
            if (!view.HaveInputs(tx))
                return state.DoS(100, error("ConnectBlock(): inputs missing/spent"),
                                 REJECT_INVALID, "bad-txns-inputs-missingorspent");

            // Check that transaction is BIP68 final
            // BIP68 lock checks (as opposed to nLockTime checks) must
            // be in ConnectBlock because they require the UTXO set
            prevheights.resize(tx.vin.size());
            for (size_t j = 0; j < tx.vin.size(); j++) {
                prevheights[j] = view.AccessCoin(tx.vin[j].prevout).nHeight;
            }

            if (!SequenceLocks(tx, nLockTimeFlags, &prevheights, *pindex)) {
                return state.DoS(100, error("%s: contains a non-BIP68-final transaction", __func__),
                                 REJECT_INVALID, "bad-txns-nonfinal");
            }
        }
bool SequenceLocks(const CTransaction &tx, int flags, std::vector<int>* prevHeights, const CBlockIndex& block)
{
    return EvaluateSequenceLocks(block, CalculateSequenceLocks(tx, flags, prevHeights, block));
}

bool EvaluateSequenceLocks(const CBlockIndex& block, std::pair<int, int64_t> lockPair)
{
    assert(block.pprev);
    int64_t nBlockTime = block.pprev->GetMedianTimePast();
    if (lockPair.first >= block.nHeight || lockPair.second >= nBlockTime)
        return false;

    return true;
}

std::pair<int, int64_t> CalculateSequenceLocks(const CTransaction &tx, int flags, std::vector<int>* prevHeights, const CBlockIndex& block)
{
    assert(prevHeights->size() == tx.vin.size());

    // Will be set to the equivalent height- and time-based nLockTime
    // values that would be necessary to satisfy all relative lock-
    // time constraints given our view of block chain history.
    // The semantics of nLockTime are the last invalid height/time, so
    // use -1 to have the effect of any height or time being valid.
    int nMinHeight = -1;
    int64_t nMinTime = -1;

    // tx.nVersion is signed integer so requires cast to unsigned otherwise
    // we would be doing a signed comparison and half the range of nVersion
    // wouldn't support BIP 68.
    bool fEnforceBIP68 = static_cast<uint32_t>(tx.nVersion) >= 2
                      && flags & LOCKTIME_VERIFY_SEQUENCE;

    // Do not enforce sequence numbers as a relative lock time
    // unless we have been instructed to
    if (!fEnforceBIP68) {
        return std::make_pair(nMinHeight, nMinTime);
    }

    for (size_t txinIndex = 0; txinIndex < tx.vin.size(); txinIndex++) {
        const CTxIn& txin = tx.vin[txinIndex];

        // Sequence numbers with the most significant bit set are not
        // treated as relative lock-times, nor are they given any
        // consensus-enforced meaning at this point.
        if (txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_DISABLE_FLAG) {
            // The height of this input is not relevant for sequence locks
            (*prevHeights)[txinIndex] = 0;
            continue;
        }

        int nCoinHeight = (*prevHeights)[txinIndex];

        if (txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG) {
            int64_t nCoinTime = block.GetAncestor(std::max(nCoinHeight-1, 0))->GetMedianTimePast();
            // NOTE: Subtract 1 to maintain nLockTime semantics
            // BIP 68 relative lock times have the semantics of calculating
            // the first block or time at which the transaction would be
            // valid. When calculating the effective block time or height
            // for the entire transaction, we switch to using the
            // semantics of nLockTime which is the last invalid block
            // time or height.  Thus we subtract 1 from the calculated
            // time or height.

            // Time-based relative lock-times are measured from the
            // smallest allowed timestamp of the block containing the
            // txout being spent, which is the median time past of the
            // block prior.
            nMinTime = std::max(nMinTime, nCoinTime + (int64_t)((txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_MASK) << CTxIn::SEQUENCE_LOCKTIME_GRANULARITY) - 1);
        } else {
            nMinHeight = std::max(nMinHeight, nCoinHeight + (int)(txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_MASK) - 1);
        }
    }

    return std::make_pair(nMinHeight, nMinTime);
}
class CBlockIndex
{
public:
 :
 :
    enum { nMedianTimeSpan=11 };

    int64_t GetMedianTimePast() const
    {
        int64_t pmedian[nMedianTimeSpan];
        int64_t* pbegin = &pmedian[nMedianTimeSpan];
        int64_t* pend = &pmedian[nMedianTimeSpan];

        const CBlockIndex* pindex = this;
        for (int i = 0; i < nMedianTimeSpan && pindex; i++, pindex = pindex->pprev)
            *(--pbegin) = pindex->GetBlockTime();

        std::sort(pbegin, pend);
        return pbegin[(pend - pbegin)/2];
    }
shohu commented 6 years ago
bool EvaluateSequenceLocks(const CBlockIndex& block, std::pair<int, int64_t> lockPair)
{
    assert(block.pprev);
    int64_t nBlockTime = block.pprev->GetMedianTimePast();
    LogPrintf("!!!!!! [%d] lockPair.first(%d) >= block.nHeight(%d)\n", lockPair.first >= block.nHeight, lockPair.first, block.nHeight);
    LogPrintf("!!!!!! [%d] lockPair.second(%d) >= nBlockTime(%d)\n", lockPair.second >= nBlockTime, lockPair.second, nBlockTime);
    if (lockPair.first >= block.nHeight || lockPair.second >= nBlockTime)
        return false;

    return true;
}
2018-04-05 07:48:50.273070     - Fork checks: 0.05ms [0.02s]
2018-04-05 07:48:50.273117 !!!!!! [0] lockPair.first(-1) >= block.nHeight(581)
2018-04-05 07:48:50.273140 !!!!!! [0] lockPair.second(-1) >= nBlockTime(1522898300)
2018-04-05 07:48:50.273171 !!!!!! [0] lockPair.first(-1) >= block.nHeight(581)
2018-04-05 07:48:50.273191 !!!!!! [0] lockPair.second(-1) >= nBlockTime(1522898300)
2018-04-05 07:48:50.273218 !!!!!! [0] lockPair.first(-1) >= block.nHeight(581)
2018-04-05 07:48:50.273237 !!!!!! [0] lockPair.second(-1) >= nBlockTime(1522898300)
2018-04-05 07:48:50.273263 !!!!!! [0] lockPair.first(-1) >= block.nHeight(581)
2018-04-05 07:48:50.273282 !!!!!! [0] lockPair.second(-1) >= nBlockTime(1522898300)
2018-04-05 07:48:50.273308 !!!!!! [0] lockPair.first(-1) >= block.nHeight(581)
2018-04-05 07:48:50.273327 !!!!!! [0] lockPair.second(-1) >= nBlockTime(1522898300)
2018-04-05 07:48:50.273354 !!!!!! [0] lockPair.first(-1) >= block.nHeight(581)
2018-04-05 07:48:50.273373 !!!!!! [0] lockPair.second(-1) >= nBlockTime(1522898300)
2018-04-05 07:48:50.273399 !!!!!! [0] lockPair.first(-1) >= block.nHeight(581)
2018-04-05 07:48:50.273418 !!!!!! [0] lockPair.second(-1) >= nBlockTime(1522898300)
2018-04-05 07:48:50.273443 !!!!!! [0] lockPair.first(-1) >= block.nHeight(581)
2018-04-05 07:48:50.273463 !!!!!! [0] lockPair.second(-1) >= nBlockTime(1522898300)
2018-04-05 07:48:50.273489 !!!!!! [0] lockPair.first(-1) >= block.nHeight(581)
2018-04-05 07:48:50.273508 !!!!!! [1] lockPair.second(1522898331) >= nBlockTime(1522898300)
2018-04-05 07:48:50.273528 ERROR: ConnectBlock: contains a non-BIP68-final transaction

There are 31 differences...

 lockPair.second(1522898331) >= nBlockTime(1522898300)
shohu commented 6 years ago
type nPowTargetSpacing SEQUENCE_LOCKTIME_GRANULARITY
c0band 32 5 (32)
bitcoin 600 9 (512)

original test

# Height txs should fail and time txs should now pass 9 * 600 > 10 * 512

c0band test is impossible

# Height txs should fail and time txs should now pass 9 * 32 > 10 * 32

testcode

        # Height txs should fail and time txs should now pass 9 * 600 > 10 * 512
        bip68success_txs.extend(bip68timetxs)
        import pdb; pdb.set_trace()
        yield TestInstance([[self.create_test_block(bip68success_txs), True]]) # 25
        self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
        for tx in bip68heighttxs:
            yield TestInstance([[self.create_test_block([tx]), False]]) # 26 - 29
shohu commented 6 years ago

If c0ban nPowTargetSpacing is 40, I may have tested... c0ban is equal nPowTargetSpacing to SEQUENCE_LOCKTIME_GRANULARITY. So this test has no meaning