regtest : specify timestamp of created blocks #5155

pull gavinandresen wants to merge 1 commits into bitcoin:master from gavinandresen:regtest_set_blocktimes changing 4 files +36 −7
  1. gavinandresen commented at 3:38 PM on October 28, 2014: contributor

    Add a third parameter to setgenerate in -regtest mode that sets the timestamp for created blocks.

    Also returns the hashes of blocks created in -regtest mode instead of null (only possible in regtest mode, because the RPC calls waits until the blocks are generated before returning).

    I decided to submit this as a separate pull request because I'm in the middle of writing a couple of regression tests that depend on it.

  2. laanwj added the label Tests on Oct 29, 2014
  3. laanwj commented at 2:24 PM on October 29, 2014: member

    Can't get this to work; when I try to generate a block far in the future, I end up with a block with time=now (whereas 3000000000='Sat Jan 24 06:20:00 2065').

    $ src/bitcoin-cli -regtest -datadir=/store/tmp/testbtc setgenerate true 1 3000000000
    [
        "00009367006078e783947d0c4dcc429a98935ad22bc579ff68de99640e5e85c2"
    ]
    
    $ src/bitcoin-cli -regtest -datadir=/store/tmp/testbtc getblock 00009367006078e783947d0c4dcc429a98935ad22bc579ff68de99640e5e85c2
    {
        "hash" : "00009367006078e783947d0c4dcc429a98935ad22bc579ff68de99640e5e85c2",
        "confirmations" : 1,
        "size" : 186,
        "height" : 1,
        "version" : 2,
        "merkleroot" : "ac078febf686644d7ce7379266811d22eb18ba9eb7cb94d9b4d0ac5d3012d0a2",
        "tx" : [
            "ac078febf686644d7ce7379266811d22eb18ba9eb7cb94d9b4d0ac5d3012d0a2"
        ],
        "time" : 1414592465,
        "nonce" : 81801,
        "bits" : "207fffff",
        "difficulty" : 0.00000000,
        "chainwork" : "0000000000000000000000000000000000000000000000000000000000000004",
        "previousblockhash" : "0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206"
    }
    

    However, when picking a random invalid timestamp far in the past (for ex. 200000000 = 'Mon May 3 20:33:20 1976') it appears to hang forever

    src/bitcoin-cli -regtest -datadir=/store/tmp/testbtc setgenerate true 10 200000000
    

    Debug.log output:

    2014-10-29 14:02:09 BitcoinMiner started
    2014-10-29 14:02:09 keypool reserve 2
    2014-10-29 14:02:09 CreateNewBlock(): total size 1000
    2014-10-29 14:02:09 Running BitcoinMiner with 1 transactions in block (186 bytes)
    2014-10-29 14:02:09 BitcoinMiner:
    2014-10-29 14:02:09 proof-of-work found  
      hash: 0000c82311519c9d0f20b0407ce56a1a8a41b06b27f34d5a38e6b9ad1c4bb0a3  
    target: 7fffff0000000000000000000000000000000000000000000000000000000000
    2014-10-29 14:02:09 CBlock(hash=0000c82311519c9d0f20b0407ce56a1a8a41b06b27f34d5a38e6b9ad1c4bb0a3, ver=2, hashPrevBlock=0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206, hashMerkleRoot=8a254f776e50b0c8f2a1c0f0478f950647703676130dec10f1aade13f523a25e, nTime=200000000, nBits=207fffff, nNonce=122518, vtx=1)
      CTransaction(hash=8a254f776e, ver=1, vin.size=1, vout.size=1, nLockTime=0)
        CTxIn(COutPoint(0000000000, 4294967295), coinbase 510101062f503253482f)
        CTxOut(nValue=50.00000000, scriptPubKey=027e94a912a9922094d5aec686133c)
    
      vMerkleTree:  8a254f776e50b0c8f2a1c0f0478f950647703676130dec10f1aade13f523a25e
    
    2014-10-29 14:02:09 generated 50.00
    2014-10-29 14:02:09 keypool keep 2
    2014-10-29 14:02:09 ERROR: AcceptBlockHeader : block's timestamp is too early
    2014-10-29 14:02:09 ERROR: ProcessBlock() : AcceptBlock FAILED
    2014-10-29 14:02:09 ERROR: BitcoinMiner : ProcessBlock, block not accepted
    2014-10-29 14:02:09 BitcoinMiner terminated
    
  4. laanwj referenced this in commit 57ec4ed17d on Oct 29, 2014
  5. gavinandresen commented at 3:11 PM on October 29, 2014: contributor

    I'll add checks for out-of-range timestamps: timestamp will have to be greater than genesis block, and less than now + 2_60_60

  6. sipa commented at 3:16 PM on October 29, 2014: member

    Would it be possible to have a dedicated generate RPC which just calls the miner (without a new thread) to create a block, with all the parameters needed?

    ... and un-magicify the regtest behaviour of setgenerate.

  7. gavinandresen commented at 3:43 PM on October 29, 2014: contributor

    @sipa: we should've done that when we added the magic setgenerate behavior.

    Adding a regression-test-only RPC like... uh:

    generate previousblockhash n_blocks block_timestamp [coinbase_scriptpubkeys] 'hex_coinbase_extradata'
    

    ... that returns an array of raw blocks (which could be submitted with submitblock) would be darn handy for writing regression tests, benchmarking, .....

  8. gavinandresen force-pushed on Oct 29, 2014
  9. gavinandresen commented at 4:43 PM on October 29, 2014: contributor

    So now I'm thinking using RPC to generate -regtest-mode-blocks is stupid.

    We already have python code for mining, I'm thinking that is the right approach rather than a 'generate' RPC call. The setgenerate RPC call already does 100 times as much work as it should in -regtest mode (because its mining inner loop looks for a zero byte), so generating -regtest chains in python (and submitting via submitblock) should be faster than what we've been doing, too.....

  10. regtest : specify timestamp of created blocks
    Add a third parameter to setgenerate in -regtest mode that
    sets the timestamp for created blocks.
    
    And returns hashes of generated blocks in -regtest mode.
    15e85a03e2
  11. gavinandresen force-pushed on Oct 29, 2014
  12. petertodd commented at 7:52 PM on October 29, 2014: contributor

    @gavinandresen Yeah, I generate regtest blocks w/ Python myself any time I need something non-generic; I gotta get around to cleaning up my code to do that and adding it to python-bitcoinlib officially.

    What would be useful however is a way to force-set the time your Bitcoin node uses from the RPC interface; I looked for a way to do that the other day and didn't find it.

  13. laanwj commented at 9:59 AM on October 31, 2014: member

    Agree @sipa; the setgenerate call is getting cluttered this way. regtest_generate would have been better as a wholly separate call. I also thought about noting that, but didn't bother because it's just the internal miner.

    Having the generate call internally in RPC is useful though (instead of having it depend on external Python code). I regularly use regtest mode to do manual testing, and then it's super-useful to just be able to generate blocks in the RPC console.

  14. laanwj commented at 11:30 AM on November 3, 2014: member

    So I vote for keeping setgenerate as it is now - convenient for generating blocks and not much more - and doing more advanced, specific test cases like changing the timestamp, previous block hash and coinbase script from the testing Python code. That's much more flexible and keeps the RPC interface clean.

  15. gavinandresen commented at 5:34 PM on November 10, 2014: contributor

    OK, will do this a different way.

  16. gavinandresen closed this on Nov 10, 2014

  17. MarcoFalke locked this on Sep 8, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-05-02 03:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me