diff --git a/message/src/common/consensus.rs b/message/src/common/consensus.rs index c92c14de..cc24791c 100644 --- a/message/src/common/consensus.rs +++ b/message/src/common/consensus.rs @@ -3,6 +3,9 @@ use super::Magic; #[derive(Debug, Clone)] /// Parameters that influence chain consensus. pub struct ConsensusParams { + /// Time when BIP16 becomes active. + /// See https://github.com/bitcoin/bips/blob/master/bip-0016.mediawiki + pub bip16_time: u32, /// Block height at which BIP65 becomes active. /// See https://github.com/bitcoin/bips/blob/master/bip-0065.mediawiki pub bip65_height: u32, @@ -12,12 +15,15 @@ impl ConsensusParams { pub fn with_magic(magic: Magic) -> Self { match magic { Magic::Mainnet => ConsensusParams { - bip65_height: 388381, // 000000000000000004c2b624ed5d7756c508d90fd0da2c7c679febfa6c4735f0 + bip16_time: 1333238400, // Apr 1 2012 + bip65_height: 388381, // 000000000000000004c2b624ed5d7756c508d90fd0da2c7c679febfa6c4735f0 }, Magic::Testnet => ConsensusParams { - bip65_height: 581885, // 00000000007f6655f22f98e72ed80d8b06dc761d5da09df0fa1dc4be4f861eb6 + bip16_time: 1333238400, // Apr 1 2012 + bip65_height: 581885, // 00000000007f6655f22f98e72ed80d8b06dc761d5da09df0fa1dc4be4f861eb6 }, Magic::Regtest => ConsensusParams { + bip16_time: 1333238400, // Apr 1 2012 bip65_height: 1351, }, } diff --git a/sync/src/synchronization_client.rs b/sync/src/synchronization_client.rs index bbaaea89..dbe2dffa 100644 --- a/sync/src/synchronization_client.rs +++ b/sync/src/synchronization_client.rs @@ -930,10 +930,20 @@ impl SynchronizationClient where T: TaskExecutor { /// Thread procedure for handling verification tasks fn verification_worker_proc(sync: Arc>, mut verifier: ChainVerifier, work_receiver: Receiver) { + let bip16_time_border = { sync.lock().config().consensus_params.bip16_time }; + let mut is_bip16_active = false; let mut parameters_change_steps = Some(0); + while let Ok(task) = work_receiver.recv() { match task { VerificationTask::VerifyBlock(block) => { + // for changes that are not relying on block# + let is_bip16_active_on_block = block.block_header.time >= bip16_time_border; + let force_parameters_change = is_bip16_active_on_block != is_bip16_active; + if force_parameters_change { + parameters_change_steps = Some(0); + } + // change verifier parameters, if needed if let Some(steps_left) = parameters_change_steps { if steps_left == 0 { @@ -941,6 +951,9 @@ impl SynchronizationClient where T: TaskExecutor { let config = sync.config(); let best_storage_block = sync.chain.read().best_storage_block(); + is_bip16_active = is_bip16_active_on_block; + verifier = verifier.verify_p2sh(is_bip16_active); + let is_bip65_active = best_storage_block.number >= config.consensus_params.bip65_height; verifier = verifier.verify_clocktimeverify(is_bip65_active); diff --git a/verification/src/chain_verifier.rs b/verification/src/chain_verifier.rs index e5b6b382..06be939d 100644 --- a/verification/src/chain_verifier.rs +++ b/verification/src/chain_verifier.rs @@ -14,6 +14,7 @@ const MAX_BLOCK_SIZE: usize = 1000000; pub struct ChainVerifier { store: Arc, + verify_p2sh: bool, verify_clocktimeverify: bool, skip_pow: bool, skip_sig: bool, @@ -23,6 +24,7 @@ impl ChainVerifier { pub fn new(store: Arc) -> Self { ChainVerifier { store: store, + verify_p2sh: false, verify_clocktimeverify: false, skip_pow: false, skip_sig: false @@ -41,6 +43,11 @@ impl ChainVerifier { self } + pub fn verify_p2sh(mut self, verify: bool) -> Self { + self.verify_p2sh = verify; + self + } + pub fn verify_clocktimeverify(mut self, verify: bool) -> Self { self.verify_clocktimeverify = verify; self @@ -141,7 +148,7 @@ impl ChainVerifier { let output: Script = paired_output.script_pubkey.to_vec().into(); let flags = VerificationFlags::default() - .verify_p2sh(true) + .verify_p2sh(self.verify_p2sh) .verify_clocktimeverify(self.verify_clocktimeverify); // for tests only, skips as late as possible