Skip to content

Commit 4a52f34

Browse files
committed
add rustfmt rules
1 parent 634a46b commit 4a52f34

14 files changed

Lines changed: 298 additions & 100 deletions

File tree

rustfmt.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
edition = "2021"
2+
max_width = 80
3+
4+
# unstable features
5+
# unstable_features = true
6+
# reorder_impl_items = true
7+
# group_imports = "StdExternalCrate"

src/archive.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,21 @@ impl<
3131
})
3232
}
3333

34-
pub fn get_header(&self, txn: &RoTxn, height: u32) -> Result<Option<Header>, Error> {
34+
pub fn get_header(
35+
&self,
36+
txn: &RoTxn,
37+
height: u32,
38+
) -> Result<Option<Header>, Error> {
3539
let height = height.to_be_bytes();
3640
let header = self.headers.get(txn, &height)?;
3741
Ok(header)
3842
}
3943

40-
pub fn get_body(&self, txn: &RoTxn, height: u32) -> Result<Option<Body<A, C>>, Error> {
44+
pub fn get_body(
45+
&self,
46+
txn: &RoTxn,
47+
height: u32,
48+
) -> Result<Option<Body<A, C>>, Error> {
4149
let height = height.to_be_bytes();
4250
let header = self.bodies.get(txn, &height)?;
4351
Ok(header)
@@ -77,7 +85,11 @@ impl<
7785
Ok(())
7886
}
7987

80-
pub fn append_header(&self, txn: &mut RwTxn, header: &Header) -> Result<(), Error> {
88+
pub fn append_header(
89+
&self,
90+
txn: &mut RwTxn,
91+
header: &Header,
92+
) -> Result<(), Error> {
8193
let height = self.get_height(txn)?;
8294
let best_hash = self.get_best_hash(txn)?;
8395
if header.prev_side_hash != best_hash {

src/authorization.rs

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
use crate::types::blake3;
2-
use crate::types::{Address, AuthorizedTransaction, Body, GetAddress, Transaction, Verify};
3-
pub use ed25519_dalek::{Keypair, PublicKey, Signature, SignatureError, Signer, Verifier};
2+
use crate::types::{
3+
Address, AuthorizedTransaction, Body, GetAddress, Transaction, Verify,
4+
};
5+
pub use ed25519_dalek::{
6+
Keypair, PublicKey, Signature, SignatureError, Signer, Verifier,
7+
};
48
use rayon::prelude::*;
59
use serde::{Deserialize, Serialize};
610

@@ -16,9 +20,13 @@ impl GetAddress for Authorization {
1620
}
1721
}
1822

19-
impl<C: Clone + Serialize + for<'de> Deserialize<'de> + Send + Sync> Verify<C> for Authorization {
23+
impl<C: Clone + Serialize + for<'de> Deserialize<'de> + Send + Sync> Verify<C>
24+
for Authorization
25+
{
2026
type Error = Error;
21-
fn verify_transaction(transaction: &AuthorizedTransaction<Self, C>) -> Result<(), Self::Error>
27+
fn verify_transaction(
28+
transaction: &AuthorizedTransaction<Self, C>,
29+
) -> Result<(), Self::Error>
2230
where
2331
Self: Sized,
2432
{
@@ -56,16 +64,17 @@ pub fn verify_authorized_transaction<C: Clone + Serialize + Sync>(
5664
let messages: Vec<_> = std::iter::repeat(serialized_transaction.as_slice())
5765
.take(transaction.authorizations.len())
5866
.collect();
59-
let (public_keys, signatures): (Vec<PublicKey>, Vec<Signature>) = transaction
60-
.authorizations
61-
.iter()
62-
.map(
63-
|Authorization {
64-
public_key,
65-
signature,
66-
}| (public_key, signature),
67-
)
68-
.unzip();
67+
let (public_keys, signatures): (Vec<PublicKey>, Vec<Signature>) =
68+
transaction
69+
.authorizations
70+
.iter()
71+
.map(
72+
|Authorization {
73+
public_key,
74+
signature,
75+
}| (public_key, signature),
76+
)
77+
.unzip();
6978
ed25519_dalek::verify_batch(&messages, &signatures, &public_keys)?;
7079
Ok(())
7180
}
@@ -82,7 +91,8 @@ pub fn verify_authorizations<C: Clone + Serialize + Sync>(
8291
.par_iter()
8392
.map(bincode::serialize)
8493
.collect::<Result<_, _>>()?;
85-
let serialized_transactions = serialized_transactions.iter().map(Vec::as_slice);
94+
let serialized_transactions =
95+
serialized_transactions.iter().map(Vec::as_slice);
8696
let messages = input_numbers.zip(serialized_transactions).flat_map(
8797
|(input_number, serialized_transaction)| {
8898
std::iter::repeat(serialized_transaction).take(input_number)
@@ -101,7 +111,9 @@ pub fn verify_authorizations<C: Clone + Serialize + Sync>(
101111
signatures: Vec::with_capacity(package_size),
102112
public_keys: Vec::with_capacity(package_size),
103113
};
104-
for (authorization, message) in &pairs[i * package_size..(i + 1) * package_size] {
114+
for (authorization, message) in
115+
&pairs[i * package_size..(i + 1) * package_size]
116+
{
105117
package.messages.push(*message);
106118
package.signatures.push(authorization.signature);
107119
package.public_keys.push(authorization.public_key);
@@ -128,7 +140,9 @@ pub fn verify_authorizations<C: Clone + Serialize + Sync>(
128140
messages,
129141
signatures,
130142
public_keys,
131-
}| ed25519_dalek::verify_batch(messages, signatures, public_keys),
143+
}| {
144+
ed25519_dalek::verify_batch(messages, signatures, public_keys)
145+
},
132146
)
133147
.collect::<Result<(), SignatureError>>()?;
134148
Ok(())
@@ -146,7 +160,8 @@ pub fn authorize<C: Clone + Serialize>(
146160
addresses_keypairs: &[(Address, &Keypair)],
147161
transaction: Transaction<C>,
148162
) -> Result<AuthorizedTransaction<Authorization, C>, Error> {
149-
let mut authorizations: Vec<Authorization> = Vec::with_capacity(addresses_keypairs.len());
163+
let mut authorizations: Vec<Authorization> =
164+
Vec::with_capacity(addresses_keypairs.len());
150165
let message = bincode::serialize(&transaction)?;
151166
for (address, keypair) in addresses_keypairs {
152167
let hash_public_key = get_address(&keypair.public);

src/drivechain/client.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,19 @@ pub trait Main {
7474
nsidechain: u8,
7575
) -> Result<Vec<WithdrawalStatus>, jsonrpsee::core::Error>;
7676
#[method(name = "listspentwithdrawals")]
77-
async fn listspentwithdrawals(&self) -> Result<Vec<SpentWithdrawal>, jsonrpsee::core::Error>;
77+
async fn listspentwithdrawals(
78+
&self,
79+
) -> Result<Vec<SpentWithdrawal>, jsonrpsee::core::Error>;
7880
#[method(name = "listfailedwithdrawals")]
79-
async fn listfailedwithdrawals(&self) -> Result<Vec<FailedWithdrawal>, jsonrpsee::core::Error>;
81+
async fn listfailedwithdrawals(
82+
&self,
83+
) -> Result<Vec<FailedWithdrawal>, jsonrpsee::core::Error>;
8084
#[method(name = "getblockcount")]
8185
async fn getblockcount(&self) -> Result<usize, jsonrpsee::core::Error>;
8286
#[method(name = "getbestblockhash")]
83-
async fn getbestblockhash(&self) -> Result<bitcoin::BlockHash, jsonrpsee::core::Error>;
87+
async fn getbestblockhash(
88+
&self,
89+
) -> Result<bitcoin::BlockHash, jsonrpsee::core::Error>;
8490
#[method(name = "getblock")]
8591
async fn getblock(
8692
&self,
@@ -121,14 +127,20 @@ pub trait Main {
121127
) -> Result<serde_json::Value, jsonrpsee::core::Error>;
122128

123129
#[method(name = "generate")]
124-
async fn generate(&self, num: u32) -> Result<serde_json::Value, jsonrpsee::core::Error>;
130+
async fn generate(
131+
&self,
132+
num: u32,
133+
) -> Result<serde_json::Value, jsonrpsee::core::Error>;
125134

126135
#[method(name = "getnewaddress")]
127136
async fn getnewaddress(
128137
&self,
129138
account: &str,
130139
address_type: &str,
131-
) -> Result<bitcoin::Address<bitcoin::address::NetworkUnchecked>, jsonrpsee::core::Error>;
140+
) -> Result<
141+
bitcoin::Address<bitcoin::address::NetworkUnchecked>,
142+
jsonrpsee::core::Error,
143+
>;
132144

133145
#[method(name = "createsidechaindeposit")]
134146
async fn createsidechaindeposit(

src/drivechain/mod.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ impl<C> Drivechain<C> {
2424
.nextblockhash
2525
.ok_or(Error::NoNextBlock { prev_main_hash })?;
2626
self.client
27-
.verifybmm(&block_hash, &header.hash().into(), self.sidechain_number)
27+
.verifybmm(
28+
&block_hash,
29+
&header.hash().into(),
30+
self.sidechain_number,
31+
)
2832
.await?;
2933
Ok(())
3034
}
@@ -38,7 +42,8 @@ impl<C> Drivechain<C> {
3842
end: bitcoin::BlockHash,
3943
start: Option<bitcoin::BlockHash>,
4044
) -> Result<TwoWayPegData<C>, Error> {
41-
let (deposits, deposit_block_hash) = self.get_deposit_outputs(end, start).await?;
45+
let (deposits, deposit_block_hash) =
46+
self.get_deposit_outputs(end, start).await?;
4247
let bundle_statuses = self.get_withdrawal_bundle_statuses().await?;
4348
let two_way_peg_data = TwoWayPegData {
4449
deposits,
@@ -65,18 +70,24 @@ impl<C> Drivechain<C> {
6570
&self,
6671
end: bitcoin::BlockHash,
6772
start: Option<bitcoin::BlockHash>,
68-
) -> Result<(HashMap<OutPoint, Output<C>>, Option<bitcoin::BlockHash>), Error> {
73+
) -> Result<(HashMap<OutPoint, Output<C>>, Option<bitcoin::BlockHash>), Error>
74+
{
6975
let deposits = self
7076
.client
71-
.listsidechaindepositsbyblock(self.sidechain_number, Some(end), start)
77+
.listsidechaindepositsbyblock(
78+
self.sidechain_number,
79+
Some(end),
80+
start,
81+
)
7282
.await?;
7383
let mut last_block_hash = None;
7484
let mut last_total = 0;
7585
let mut outputs = HashMap::new();
7686
for deposit in &deposits {
7787
let transaction = hex::decode(&deposit.txhex)?;
78-
let transaction =
79-
bitcoin::Transaction::consensus_decode(&mut std::io::Cursor::new(transaction))?;
88+
let transaction = bitcoin::Transaction::consensus_decode(
89+
&mut std::io::Cursor::new(transaction),
90+
)?;
8091
if let Some(start) = start {
8192
if deposit.hashblock == start {
8293
last_total = transaction.output[deposit.nburnindex].value;
@@ -125,7 +136,10 @@ impl<C> Drivechain<C> {
125136
Ok(statuses)
126137
}
127138

128-
pub fn new(sidechain_number: u8, main_addr: SocketAddr) -> Result<Self, Error> {
139+
pub fn new(
140+
sidechain_number: u8,
141+
main_addr: SocketAddr,
142+
) -> Result<Self, Error> {
129143
let mut headers = HeaderMap::new();
130144
let auth = format!("{}:{}", "user", "password");
131145
let header_value = format!(

src/mempool.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ use serde::{Deserialize, Serialize};
55

66
#[derive(Clone)]
77
pub struct MemPool<A, C> {
8-
pub transactions: Database<OwnedType<[u8; 32]>, SerdeBincode<AuthorizedTransaction<A, C>>>,
8+
pub transactions: Database<
9+
OwnedType<[u8; 32]>,
10+
SerdeBincode<AuthorizedTransaction<A, C>>,
11+
>,
912
pub spent_utxos: Database<SerdeBincode<OutPoint>, Unit>,
1013
}
1114

@@ -40,8 +43,11 @@ impl<
4043
}
4144
self.spent_utxos.put(txn, input, &())?;
4245
}
43-
self.transactions
44-
.put(txn, &transaction.transaction.txid().into(), &transaction)?;
46+
self.transactions.put(
47+
txn,
48+
&transaction.transaction.txid().into(),
49+
&transaction,
50+
)?;
4551
Ok(())
4652
}
4753

src/miner.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ pub struct Miner<A, C> {
1515
}
1616

1717
impl<A: Clone, C: Clone + GetValue + Serialize> Miner<A, C> {
18-
pub fn new(sidechain_number: u8, main_addr: SocketAddr) -> Result<Self, Error> {
18+
pub fn new(
19+
sidechain_number: u8,
20+
main_addr: SocketAddr,
21+
) -> Result<Self, Error> {
1922
let drivechain = Drivechain::new(sidechain_number, main_addr)?;
2023
Ok(Self {
2124
drivechain,
@@ -55,14 +58,18 @@ impl<A: Clone, C: Clone + GetValue + Serialize> Miner<A, C> {
5558
)
5659
.await
5760
.map_err(crate::drivechain::Error::from)?;
58-
bitcoin::Txid::from_str(value["txid"]["txid"].as_str().ok_or(Error::InvalidJson)?)
59-
.map_err(crate::drivechain::Error::from)?;
61+
bitcoin::Txid::from_str(
62+
value["txid"]["txid"].as_str().ok_or(Error::InvalidJson)?,
63+
)
64+
.map_err(crate::drivechain::Error::from)?;
6065
assert_eq!(header.merkle_root, body.compute_merkle_root());
6166
self.block = Some((header, body));
6267
Ok(())
6368
}
6469

65-
pub async fn confirm_bmm(&mut self) -> Result<Option<(Header, Body<A, C>)>, Error> {
70+
pub async fn confirm_bmm(
71+
&mut self,
72+
) -> Result<Option<(Header, Body<A, C>)>, Error> {
6673
if let Some((header, body)) = self.block.clone() {
6774
self.drivechain.verify_bmm(&header).await?;
6875
self.block = None;

src/net.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ impl Net {
117117
Ok(peer)
118118
}
119119

120-
pub async fn disconnect(&self, stable_id: usize) -> Result<Option<Peer>, Error> {
120+
pub async fn disconnect(
121+
&self,
122+
stable_id: usize,
123+
) -> Result<Option<Peer>, Error> {
121124
let peer = self.peers.write().await.remove(&stable_id);
122125
Ok(peer)
123126
}
@@ -139,7 +142,9 @@ pub fn make_client_endpoint(bind_addr: SocketAddr) -> Result<Endpoint, Error> {
139142
/// - a stream of incoming QUIC connections
140143
/// - server certificate serialized into DER format
141144
#[allow(unused)]
142-
pub fn make_server_endpoint(bind_addr: SocketAddr) -> Result<(Endpoint, Vec<u8>), Error> {
145+
pub fn make_server_endpoint(
146+
bind_addr: SocketAddr,
147+
) -> Result<(Endpoint, Vec<u8>), Error> {
143148
let (server_config, server_cert) = configure_server()?;
144149
let endpoint = Endpoint::server(server_config, bind_addr)?;
145150
Ok((endpoint, server_cert))
@@ -153,7 +158,8 @@ fn configure_server() -> Result<(ServerConfig, Vec<u8>), Error> {
153158
let priv_key = rustls::PrivateKey(priv_key);
154159
let cert_chain = vec![rustls::Certificate(cert_der.clone())];
155160

156-
let mut server_config = ServerConfig::with_single_cert(cert_chain, priv_key)?;
161+
let mut server_config =
162+
ServerConfig::with_single_cert(cert_chain, priv_key)?;
157163
let transport_config = Arc::get_mut(&mut server_config.transport).unwrap();
158164
transport_config.max_concurrent_uni_streams(1_u8.into());
159165

0 commit comments

Comments
 (0)