Skip to content

Commit 73e8b54

Browse files
committed
Add testnet blockspacing rule
1 parent 51038f5 commit 73e8b54

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/blockdata/blockchain.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ use std::kinds::marker;
3030

3131
use blockdata::block::{Block, BlockHeader};
3232
use blockdata::transaction::Transaction;
33-
use blockdata::constants::{DIFFCHANGE_INTERVAL, DIFFCHANGE_TIMESPAN, max_target, genesis_block};
34-
use network::constants::Network;
33+
use blockdata::constants::{DIFFCHANGE_INTERVAL, DIFFCHANGE_TIMESPAN,
34+
TARGET_BLOCK_SPACING, max_target, genesis_block};
35+
use network::constants::{Network, BitcoinTestnet};
3536
use network::serialize::{Serializable, SerializeIter};
3637
use util::BitArray;
3738
use util::error::{BitcoinResult, BlockNotFound, DuplicateHash, PrevHashNotFound};
@@ -491,9 +492,25 @@ impl Blockchain {
491492
if target > max { target = max };
492493
// Compactify (make expressible in the 8+24 nBits float format
493494
satoshi_the_precision(&target)
494-
} else {
495+
// On non-diffchange blocks, Testnet has a rule that any 20-minute-long
496+
// block intervals result the difficulty
497+
} else if self.network == BitcoinTestnet &&
498+
block.header.time > prev.block.header.time + 2*TARGET_BLOCK_SPACING {
499+
max_target(self.network)
500+
// On the other hand, if we are in Testnet and the block interval is less
501+
// than 20 minutes, we need to scan backward to find a block for which the
502+
// previous rule did not apply, to find the "real" difficulty.
503+
} else if self.network == BitcoinTestnet {
504+
// Scan back DIFFCHANGE_INTERVAL blocks
505+
let mut scan = prev.clone();
506+
while scan.height % DIFFCHANGE_INTERVAL != 0 &&
507+
scan.required_difficulty == max_target(self.network) {
508+
scan = scan.prev(&self.tree).unwrap();
509+
}
510+
scan.required_difficulty
495511
// Otherwise just use the last block's difficulty
496-
prev.required_difficulty
512+
} else {
513+
prev.required_difficulty
497514
};
498515
// Create node
499516
let ret = Rc::new(BlockchainNode {

src/blockdata/constants.rs

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use util::uint::Uint256;
3333

3434
pub static MAX_SEQUENCE: u32 = 0xFFFFFFFF;
3535
pub static COIN_VALUE: u64 = 100000000;
36+
pub static TARGET_BLOCK_SPACING: u32 = 600;
3637
pub static DIFFCHANGE_INTERVAL: u32 = 2016;
3738
pub static DIFFCHANGE_TIMESPAN: u32 = 14 * 24 * 3600;
3839

0 commit comments

Comments
 (0)