-
Notifications
You must be signed in to change notification settings - Fork 23
/
consensus_storage.rs
172 lines (154 loc) · 5.65 KB
/
consensus_storage.rs
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2017 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.
use std::borrow::Cow;
use serde::{Deserialize, Deserializer};
use serde_json;
use exonum::crypto::{hash, CryptoHash, Hash};
use exonum::helpers::Height;
use exonum::storage::StorageValue;
use details::btc;
use details::btc::transactions::FundingTx;
/// Public part of anchoring service configuration stored in blockchain.
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
pub struct AnchoringConfig {
/// Validators' public keys from which the current anchoring address can be calculated.
pub anchoring_keys: Vec<btc::PublicKey>,
/// The transaction that funds anchoring address.
/// If the anchoring transactions chain is empty, it will be the first transaction in the chain.
/// Note: you must specify a suitable transaction before the network launching.
pub funding_tx: Option<FundingTx>,
/// Fee for each transaction in chain.
pub fee: u64,
/// The frequency in blocks with which the generation of new anchoring
/// transactions in the chain occurs.
pub frequency: u64,
/// The minimum number of confirmations in bitcoin network for the transition to a
/// new anchoring address.
pub utxo_confirmations: u64,
/// The current bitcoin network type.
#[serde(serialize_with = "btc_network_to_str", deserialize_with = "btc_network_from_str")]
pub network: btc::Network,
}
impl Default for AnchoringConfig {
fn default() -> AnchoringConfig {
AnchoringConfig {
anchoring_keys: vec![],
funding_tx: None,
fee: 1000,
frequency: 500,
utxo_confirmations: 5,
network: btc::Network::Testnet,
}
}
}
impl AnchoringConfig {
/// Creates anchoring configuration for the given `anchoring_keys` without funding transaction.
/// This is usable for deploying procedure when the network participants exchange
/// the public configuration before launching.
/// Do not forget to send funding transaction to the final multisig address
/// and add it to the final configuration.
pub fn new<I>(network: btc::Network, anchoring_keys: I) -> AnchoringConfig
where
I: IntoIterator<Item = btc::PublicKey>,
{
AnchoringConfig {
anchoring_keys: anchoring_keys.into_iter().collect(),
network,
..Default::default()
}
}
/// Creates default anchoring configuration from given public keys and funding transaction
/// which were created earlier by other way.
pub fn new_with_funding_tx<I>(
network: btc::Network,
anchoring_keys: I,
tx: FundingTx,
) -> AnchoringConfig
where
I: IntoIterator<Item = btc::PublicKey>,
{
AnchoringConfig {
anchoring_keys: anchoring_keys.into_iter().collect(),
funding_tx: Some(tx),
network,
..Default::default()
}
}
#[doc(hidden)]
/// Creates compressed `RedeemScript` from public keys in config.
pub fn redeem_script(&self) -> (btc::RedeemScript, btc::Address) {
let majority_count = self.majority_count();
let redeem_script = btc::RedeemScriptBuilder::with_public_keys(
self.anchoring_keys.iter().map(|x| x.0),
).quorum(majority_count as usize)
.to_script()
.unwrap();
let addr = btc::Address::from_script(&redeem_script, self.network);
(redeem_script, addr)
}
#[doc(hidden)]
/// Returns the latest height below the given `height` which needs to be anchored.
pub fn latest_anchoring_height(&self, height: Height) -> Height {
Height(height.0 - height.0 % self.frequency as u64)
}
#[doc(hidden)]
pub fn majority_count(&self) -> u8 {
::majority_count(self.anchoring_keys.len() as u8)
}
/// Returns the funding transaction.
///
/// # Panics
///
/// If funding transaction is not specified.
pub fn funding_tx(&self) -> &FundingTx {
self.funding_tx.as_ref().expect(
"You need to specify suitable \
funding_tx",
)
}
}
fn btc_network_to_str<S>(network: &btc::Network, ser: S) -> Result<S::Ok, S::Error>
where
S: ::serde::Serializer,
{
match *network {
btc::Network::Bitcoin => ser.serialize_str("bitcoin"),
btc::Network::Testnet => ser.serialize_str("testnet"),
}
}
fn btc_network_from_str<'de, D>(deserializer: D) -> Result<btc::Network, D::Error>
where
D: Deserializer<'de>,
{
let s: String = Deserialize::deserialize(deserializer)?;
const VARIANTS: &[&str] = &["bitcoin", "testnet"];
match s.as_str() {
"bitcoin" => Ok(btc::Network::Bitcoin),
"testnet" => Ok(btc::Network::Testnet),
other => Err(::serde::de::Error::unknown_variant(other, VARIANTS)),
}
}
impl StorageValue for AnchoringConfig {
fn into_bytes(self) -> Vec<u8> {
serde_json::to_vec(&self).unwrap()
}
fn from_bytes(value: Cow<[u8]>) -> Self {
serde_json::from_slice(value.as_ref()).unwrap()
}
}
impl CryptoHash for AnchoringConfig {
fn hash(&self) -> Hash {
hash(serde_json::to_vec(&self).unwrap().as_slice())
}
}