Skip to content

Commit 3ac97bc

Browse files
committed
Merge #54: Introduce rustfmt
e5030e7 Run formatter in CI (Tobin C. Harding) 5a8ec75 Introduce rustfmt (Tobin C. Harding) Pull request description: As we did in `rust-bitcoin` introduce `rustfmt` by adding configuration options in `rustfmt.toml`. Run `rustfmt` on the whole crate. ACKs for top commit: apoelstra: ACK e5030e7 Tree-SHA512: 078a75d153e63923e59e23af7851c6829122101f8c660e688ab94c5ac9ce836a9a8bddd95e395b699c2ce06578a85ec4200c218153b0e4b9e4f21a9fbb6328d8
2 parents dbc9804 + e5030e7 commit 3ac97bc

File tree

4 files changed

+142
-37
lines changed

4 files changed

+142
-37
lines changed

.github/workflows/rust.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,19 @@ jobs:
104104
with:
105105
command: clippy
106106
args: --all-targets -- -D warnings
107+
108+
Fmt:
109+
name: Fmt
110+
runs-on: ubuntu-latest
111+
steps:
112+
- uses: actions/checkout@v2
113+
- uses: actions-rs/toolchain@v1
114+
with:
115+
profile: minimal
116+
toolchain: nightly
117+
override: true
118+
- run: rustup component add rustfmt
119+
- uses: actions-rs/cargo@v1
120+
with:
121+
command: fmt
122+
args: --check

build.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ fn main() {
5151
.define("USE_SCALAR_4X64", "1")
5252
.define("HAVE___INT128", "1");
5353
} else {
54-
base_config
55-
.define("USE_FIELD_10X26", "1")
56-
.define("USE_SCALAR_8X32", "1");
54+
base_config.define("USE_FIELD_10X26", "1").define("USE_SCALAR_8X32", "1");
5755
}
5856
}
5957

rustfmt.toml

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,79 @@
1-
disable_all_formatting = true
1+
ignore = []
2+
3+
hard_tabs = false
4+
tab_spaces = 4
5+
newline_style = "Auto"
6+
indent_style = "Block"
7+
8+
max_width = 100 # This is number of characters.
9+
# `use_small_heuristics` is ignored if the granular width config values are explicitly set.
10+
use_small_heuristics = "Max" # "Max" == All granular width settings same as `max_width`.
11+
# # Granular width configuration settings. These are percentages of `max_width`.
12+
# fn_call_width = 60
13+
# attr_fn_like_width = 70
14+
# struct_lit_width = 18
15+
# struct_variant_width = 35
16+
# array_width = 60
17+
# chain_width = 60
18+
# single_line_if_else_max_width = 50
19+
20+
wrap_comments = false
21+
format_code_in_doc_comments = false
22+
comment_width = 100 # Default 80
23+
normalize_comments = false
24+
normalize_doc_attributes = false
25+
format_strings = false
26+
format_macro_matchers = false
27+
format_macro_bodies = true
28+
hex_literal_case = "Preserve"
29+
empty_item_single_line = true
30+
struct_lit_single_line = true
31+
fn_single_line = true # Default false
32+
where_single_line = false
33+
imports_indent = "Block"
34+
imports_layout = "Mixed"
35+
imports_granularity = "Module" # Default "Preserve"
36+
group_imports = "StdExternalCrate" # Default "Preserve"
37+
reorder_imports = true
38+
reorder_modules = true
39+
reorder_impl_items = false
40+
type_punctuation_density = "Wide"
41+
space_before_colon = false
42+
space_after_colon = true
43+
spaces_around_ranges = false
44+
binop_separator = "Front"
45+
remove_nested_parens = true
46+
combine_control_expr = true
47+
overflow_delimited_expr = false
48+
struct_field_align_threshold = 0
49+
enum_discrim_align_threshold = 0
50+
match_arm_blocks = false # Default true
51+
match_arm_leading_pipes = "Never"
52+
force_multiline_blocks = false
53+
fn_args_layout = "Tall"
54+
brace_style = "SameLineWhere"
55+
control_brace_style = "AlwaysSameLine"
56+
trailing_semicolon = true
57+
trailing_comma = "Vertical"
58+
match_block_trailing_comma = false
59+
blank_lines_upper_bound = 1
60+
blank_lines_lower_bound = 0
61+
edition = "2018"
62+
version = "One"
63+
inline_attribute_width = 0
64+
format_generated_files = true
65+
merge_derives = true
66+
use_try_shorthand = false
67+
use_field_init_shorthand = false
68+
force_explicit_abi = true
69+
condense_wildcard_suffixes = false
70+
color = "Auto"
71+
required_version = "1.5.1"
72+
unstable_features = false
73+
disable_all_formatting = false
74+
skip_children = false
75+
hide_parse_errors = false
76+
error_on_line_overflow = false
77+
error_on_unformatted = false
78+
emit_mode = "Files"
79+
make_backup = false

src/lib.rs

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
2020
extern crate libc;
2121

22-
use libc::{c_int,c_uchar, c_uint};
2322
use core::fmt;
2423

24+
use libc::{c_int, c_uchar, c_uint};
25+
2526
/// Errors returned by `libbitcoinconsensus` (see github.com/bitcoin/bitcoin/doc/shared-libraries.md).
2627
#[allow(non_camel_case_types)]
2728
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
@@ -38,7 +39,7 @@ pub enum Error {
3839
/// Input amount is required if WITNESS is used.
3940
ERR_AMOUNT_REQUIRED,
4041
/// Script verification `flags` are invalid (i.e. not part of the libconsensus interface).
41-
ERR_INVALID_FLAGS
42+
ERR_INVALID_FLAGS,
4243
}
4344

4445
impl fmt::Display for Error {
@@ -63,33 +64,33 @@ impl std::error::Error for Error {
6364
use self::Error::*;
6465

6566
match *self {
66-
ERR_SCRIPT
67-
| ERR_TX_INDEX
68-
| ERR_TX_SIZE_MISMATCH
69-
| ERR_TX_DESERIALIZE
70-
| ERR_AMOUNT_REQUIRED
71-
| ERR_INVALID_FLAGS => None,
67+
ERR_SCRIPT | ERR_TX_INDEX | ERR_TX_SIZE_MISMATCH | ERR_TX_DESERIALIZE
68+
| ERR_AMOUNT_REQUIRED | ERR_INVALID_FLAGS => None,
7269
}
7370
}
7471
}
7572

7673
/// Do not enable any verification.
77-
pub const VERIFY_NONE : c_uint = 0;
74+
pub const VERIFY_NONE: c_uint = 0;
7875
/// Evaluate P2SH (BIP16) subscripts.
79-
pub const VERIFY_P2SH : c_uint = 1 << 0;
76+
pub const VERIFY_P2SH: c_uint = 1 << 0;
8077
/// Enforce strict DER (BIP66) compliance.
81-
pub const VERIFY_DERSIG : c_uint = 1 << 2;
78+
pub const VERIFY_DERSIG: c_uint = 1 << 2;
8279
/// Enforce NULLDUMMY (BIP147).
83-
pub const VERIFY_NULLDUMMY : c_uint = 1 << 4;
80+
pub const VERIFY_NULLDUMMY: c_uint = 1 << 4;
8481
/// Enable CHECKLOCKTIMEVERIFY (BIP65).
85-
pub const VERIFY_CHECKLOCKTIMEVERIFY : c_uint = 1 << 9;
82+
pub const VERIFY_CHECKLOCKTIMEVERIFY: c_uint = 1 << 9;
8683
/// Enable CHECKSEQUENCEVERIFY (BIP112).
87-
pub const VERIFY_CHECKSEQUENCEVERIFY : c_uint = 1 << 10;
84+
pub const VERIFY_CHECKSEQUENCEVERIFY: c_uint = 1 << 10;
8885
/// Enable WITNESS (BIP141).
89-
pub const VERIFY_WITNESS : c_uint = 1 << 11;
86+
pub const VERIFY_WITNESS: c_uint = 1 << 11;
9087

91-
pub const VERIFY_ALL : c_uint = VERIFY_P2SH | VERIFY_DERSIG | VERIFY_NULLDUMMY |
92-
VERIFY_CHECKLOCKTIMEVERIFY | VERIFY_CHECKSEQUENCEVERIFY | VERIFY_WITNESS;
88+
pub const VERIFY_ALL: c_uint = VERIFY_P2SH
89+
| VERIFY_DERSIG
90+
| VERIFY_NULLDUMMY
91+
| VERIFY_CHECKLOCKTIMEVERIFY
92+
| VERIFY_CHECKSEQUENCEVERIFY
93+
| VERIFY_WITNESS;
9394

9495
extern "C" {
9596
/// Returns `libbitcoinconsensus` version.
@@ -98,14 +99,15 @@ extern "C" {
9899
/// Verifies that the transaction input correctly spends the previous
99100
/// output, considering any additional constraints specified by flags.
100101
pub fn bitcoinconsensus_verify_script_with_amount(
101-
script_pubkey: *const c_uchar,
102+
script_pubkey: *const c_uchar,
102103
script_pubkeylen: c_uint,
103104
amount: u64,
104105
tx_to: *const c_uchar,
105106
tx_tolen: c_uint,
106107
n_in: c_uint,
107108
flags: c_uint,
108-
err: *mut Error) -> c_int;
109+
err: *mut Error,
110+
) -> c_int;
109111
}
110112

111113
/// Computes flags for soft fork activation heights on the Bitcoin network.
@@ -132,9 +134,7 @@ pub fn height_to_flags(height: u32) -> u32 {
132134
}
133135

134136
/// Returns `libbitcoinconsensus` version.
135-
pub fn version () -> u32 {
136-
unsafe { bitcoinconsensus_version() as u32 }
137-
}
137+
pub fn version() -> u32 { unsafe { bitcoinconsensus_version() as u32 } }
138138

139139
/// Verifies a single spend (input) of a Bitcoin transaction.
140140
///
@@ -174,12 +174,23 @@ pub fn version () -> u32 {
174174
/// should return `Ok(())`.
175175
///
176176
/// **Note** since the spent amount will only be checked for Segwit transactions and the above example is not segwit, `verify` will succeed with any amount.
177-
pub fn verify (spent_output: &[u8], amount: u64, spending_transaction: &[u8], input_index: usize) -> Result<(), Error> {
178-
verify_with_flags (spent_output, amount, spending_transaction, input_index, VERIFY_ALL)
177+
pub fn verify(
178+
spent_output: &[u8],
179+
amount: u64,
180+
spending_transaction: &[u8],
181+
input_index: usize,
182+
) -> Result<(), Error> {
183+
verify_with_flags(spent_output, amount, spending_transaction, input_index, VERIFY_ALL)
179184
}
180185

181186
/// Same as verify but with flags that turn past soft fork features on or off.
182-
pub fn verify_with_flags (spent_output_script: &[u8], amount: u64, spending_transaction: &[u8], input_index: usize, flags: u32) -> Result<(), Error> {
187+
pub fn verify_with_flags(
188+
spent_output_script: &[u8],
189+
amount: u64,
190+
spending_transaction: &[u8],
191+
input_index: usize,
192+
flags: u32,
193+
) -> Result<(), Error> {
183194
unsafe {
184195
let mut error = Error::ERR_SCRIPT;
185196

@@ -191,7 +202,7 @@ pub fn verify_with_flags (spent_output_script: &[u8], amount: u64, spending_tran
191202
spending_transaction.len() as c_uint,
192203
input_index as c_uint,
193204
flags as c_uint,
194-
&mut error
205+
&mut error,
195206
);
196207
if ret != 1 {
197208
Err(error)
@@ -205,8 +216,8 @@ pub fn verify_with_flags (spent_output_script: &[u8], amount: u64, spending_tran
205216
mod tests {
206217
extern crate rustc_serialize as serialize;
207218

208-
use super::*;
209219
use self::serialize::hex::FromHex;
220+
use super::*;
210221

211222
#[test]
212223
fn bitcoinconsensus_test() {
@@ -250,15 +261,17 @@ mod tests {
250261
"010000000001011f97548fbbe7a0db7588a66e18d803d0089315aa7d4cc28360b6ec50ef36718a0100000000ffffffff02df1776000000000017a9146c002a686959067f4866b8fb493ad7970290ab728757d29f0000000000220020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d04004730440220565d170eed95ff95027a69b313758450ba84a01224e1f7f130dda46e94d13f8602207bdd20e307f062594022f12ed5017bbf4a055a06aea91c10110a0e3bb23117fc014730440220647d2dc5b15f60bc37dc42618a370b2a1490293f9e5c8464f53ec4fe1dfe067302203598773895b4b16d37485cbe21b337f4e4b650739880098c592553add7dd4355016952210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae00000000",
251262
18393430 , 0
252263
).is_err());
253-
254264
}
255265

256-
fn verify_test (spent : &str, spending :&str, amount :u64, input: usize) -> Result<(),Error> {
257-
verify (spent.from_hex().unwrap().as_slice(), amount, spending.from_hex().unwrap().as_slice(), input)
266+
fn verify_test(spent: &str, spending: &str, amount: u64, input: usize) -> Result<(), Error> {
267+
verify(
268+
spent.from_hex().unwrap().as_slice(),
269+
amount,
270+
spending.from_hex().unwrap().as_slice(),
271+
input,
272+
)
258273
}
259274

260275
#[test]
261-
fn invalid_flags_test() {
262-
verify_with_flags(&[], 0, &[], 0, VERIFY_ALL + 1).unwrap_err();
263-
}
276+
fn invalid_flags_test() { verify_with_flags(&[], 0, &[], 0, VERIFY_ALL + 1).unwrap_err(); }
264277
}

0 commit comments

Comments
 (0)