Support BIP16

This commit is contained in:
Svyatoslav Nikolsky 2016-11-16 11:52:00 +03:00
parent d8c06007ac
commit 756bb10fcc
3 changed files with 29 additions and 3 deletions

View File

@ -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,
},
}

View File

@ -930,10 +930,20 @@ impl<T> SynchronizationClient<T> where T: TaskExecutor {
/// Thread procedure for handling verification tasks
fn verification_worker_proc(sync: Arc<Mutex<Self>>, mut verifier: ChainVerifier, work_receiver: Receiver<VerificationTask>) {
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<T> SynchronizationClient<T> 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);

View File

@ -14,6 +14,7 @@ const MAX_BLOCK_SIZE: usize = 1000000;
pub struct ChainVerifier {
store: Arc<db::Store>,
verify_p2sh: bool,
verify_clocktimeverify: bool,
skip_pow: bool,
skip_sig: bool,
@ -23,6 +24,7 @@ impl ChainVerifier {
pub fn new(store: Arc<db::Store>) -> 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