1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2019 The Exonum Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Blockchain implementation details for the BTC anchoring service.

pub use self::{schema::Schema, transactions::BtcAnchoringInterface};
pub use crate::proto::{AddFunds, SignInput};

use bitcoin::blockdata::script::Script;
use btc_transaction_utils::{multisig::RedeemScript, p2wsh};
use exonum::helpers::Height;

use crate::{btc::Address, config::Config};

pub mod data_layout;
pub mod errors;
pub mod schema;
pub mod transactions;

/// Current state of the BTC anchoring service.
#[derive(Debug, Clone)]
pub enum BtcAnchoringState {
    /// The usual anchoring workflow.
    Regular {
        /// Current anchoring configuration.
        actual_configuration: Config,
    },
    /// The transition from the current anchoring address to the following one.
    Transition {
        /// Current anchoring configuration.
        actual_configuration: Config,
        /// Following anchoring configuration.
        following_configuration: Config,
    },
}

impl BtcAnchoringState {
    /// Returns the redeem script corresponding to the address to which the anchoring
    /// transaction will be sent.
    pub fn redeem_script(&self) -> RedeemScript {
        match self {
            BtcAnchoringState::Regular {
                actual_configuration,
            } => actual_configuration.redeem_script(),
            BtcAnchoringState::Transition {
                following_configuration,
                ..
            } => following_configuration.redeem_script(),
        }
    }

    /// Returns the `script_pubkey` for the corresponding redeem script.
    pub fn script_pubkey(&self) -> Script {
        self.redeem_script().as_ref().to_v0_p2wsh()
    }

    /// Returns the output address for the corresponding redeem script.
    pub fn output_address(&self) -> Address {
        p2wsh::address(&self.redeem_script(), self.actual_config().network).into()
    }

    /// Checks that anchoring state is regular.
    pub fn is_regular(&self) -> bool {
        if let BtcAnchoringState::Regular { .. } = self {
            true
        } else {
            false
        }
    }

    /// Checks that anchoring is in the transition state.
    pub fn is_transition(&self) -> bool {
        if let BtcAnchoringState::Transition { .. } = self {
            true
        } else {
            false
        }
    }

    /// Returns the actual anchoring configuration.
    pub fn actual_config(&self) -> &Config {
        match self {
            BtcAnchoringState::Regular {
                ref actual_configuration,
            } => actual_configuration,
            BtcAnchoringState::Transition {
                ref actual_configuration,
                ..
            } => actual_configuration,
        }
    }

    /// Returns the following anchoring configuration if anchoring is in transition state.
    pub fn following_config(&self) -> Option<&Config> {
        match self {
            BtcAnchoringState::Regular { .. } => None,
            BtcAnchoringState::Transition {
                ref following_configuration,
                ..
            } => Some(following_configuration),
        }
    }

    /// Returns the nearest following anchoring height for the given height.
    pub fn following_anchoring_height(&self, latest_anchored_height: Option<Height>) -> Height {
        latest_anchored_height.map_or_else(Height::zero, |height| match self {
            BtcAnchoringState::Regular {
                ref actual_configuration,
            } => actual_configuration.following_anchoring_height(height),
            BtcAnchoringState::Transition { .. } => height,
        })
    }
}