Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rustfmt, Init CI #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions .github/workflows/check_and_lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
on: [push]

name: Check, Lint, Build

env:
CARGO_TERM_COLOR: always

jobs:
check-lint-build-stable:
name: Check, Lint, Build (stable)
runs-on: ubuntu-latest
timeout-minutes: 20
# env:
# RUSTFLAGS: -D warnings
steps:
- uses: actions/checkout@v2
- name: Install latest stable toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
components: rustfmt, clippy
override: true
target: x86_64-pc-windows-gnu

- name: Rust Cache
uses: Swatinem/[email protected]

- name: Rustfmt
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check

- name: Cargo check
uses: actions-rs/cargo@v1
with:
command: check

- name: Clippy
uses: actions-rs/cargo@v1
with:
command: clippy
args: --all-targets --all-features

- name: Build
uses: actions-rs/cargo@v1
with:
command: build
args: --release
116 changes: 116 additions & 0 deletions .github/workflows/comment_coverage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# This workflow should enforce monotonically increasing comment coverage

on: [pull_request]

name: Comment Coverage

#env:
# CARGO_TERM_COLOR: always

jobs:
check-lint-build-stable:
name: Comment Coverage (stable)
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- name: Install Protoc
uses: arduino/setup-protoc@v2

- name: Install latest stable toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
components: rustfmt, clippy
override: true
target: x86_64-pc-windows-gnu

- name: Rust Cache
uses: Swatinem/[email protected]

- name: Checkout PR branch
uses: actions/checkout@v2

- name: Missing docs warnings (PR)
id: missing_docs_warnings_pr
run: |
# use a random EOF, as per GitHub security recommendations
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
WARNINGS=$(\
cargo -q clippy --message-format=short -- \
-Aclippy::all \
-Wclippy::missing_errors_doc \
-Wclippy::missing_panics_doc \
-Wclippy::missing_safety_doc \
-Wclippy::missing_docs_in_private_items \
-Wmissing_docs \
2>&1)
echo "$WARNINGS"
AWKSTR='/warning: `.+` \(lib\) generated [0-9]+ warnings?/ { print $3 ": " $7 }'
WARNINGS=$(echo "$WARNINGS" | awk -F"[\` ]" "$AWKSTR" | sort)
echo "PR_WARNINGS<<$EOF" >> "$GITHUB_OUTPUT"
echo "$WARNINGS" >> "$GITHUB_OUTPUT"
echo "$EOF" >> "$GITHUB_OUTPUT"

- name: Checkout target branch
uses: actions/checkout@v2
with:
ref: ${{ github.base_ref }}

- name: Missing docs warnings (Target)
id: missing_docs_warnings_target
run: |
# use a random EOF, as per GitHub security recommendations
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
WARNINGS=$(\
cargo -q clippy --message-format=short -- \
-Aclippy::all \
-Wclippy::missing_errors_doc \
-Wclippy::missing_panics_doc \
-Wclippy::missing_safety_doc \
-Wclippy::missing_docs_in_private_items \
-Wmissing_docs \
2>&1)
echo "$WARNINGS"
AWKSTR='/warning: `.+` \(lib\) generated [0-9]+ warnings?/ { print $3 ": " $7 }'
WARNINGS=$(echo "$WARNINGS" | awk -F"[\` ]" "$AWKSTR" | sort)
echo "TARGET_WARNINGS<<$EOF" >> "$GITHUB_OUTPUT"
echo "$WARNINGS" >> "$GITHUB_OUTPUT"
echo "$EOF" >> "$GITHUB_OUTPUT"

- name: Compare comment coverage
run: |
PR_WARNINGS="${{steps.missing_docs_warnings_pr.outputs.PR_WARNINGS}}"
TARGET_WARNINGS="${{ steps.missing_docs_warnings_target.outputs.TARGET_WARNINGS }}"
readarray -t missing_docs_warnings_pr_arr <<< "$PR_WARNINGS"
readarray -t missing_docs_warnings_target_arr <<< "$TARGET_WARNINGS"
for pr_warnings_line in "${missing_docs_warnings_pr_arr[@]}"
do
# Extract the libname and number of warnings from the line
IFS=': ' read -r libname nwarnings_pr <<< "$pr_warnings_line"
# Look for the libname in the target warnings
target_warning_line=""
for target_warnings_line in "${missing_docs_warnings_target_arr[@]}"
do
if [[ $target_warnings_line == "$libname:"* ]]; then
target_warning_line=$target_warnings_line
break
fi
done

if [ -z "$target_warning_line" ]
then
echo "New warnings found for \`${libname}\`"
exit 1
fi

# Find the number of warnings for the target branch
IFS=': ' read -r _ nwarnings_target <<< "$target_warning_line"

# Compare the values
if [ "$nwarnings_target" -gt "$nwarnings_pr" ]
then
echo "Too many warnings for \`${libname}\` (${nwarnings_pr}): must be less than $nwarnings_target"
exit 1
fi
done
6 changes: 6 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
edition = "2021"
max_width = 80

# unstable features
# unstable_features = true
# reorder_impl_items = true
# group_imports = "StdExternalCrate"
18 changes: 15 additions & 3 deletions src/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,21 @@ impl<
})
}

pub fn get_header(&self, txn: &RoTxn, height: u32) -> Result<Option<Header>, Error> {
pub fn get_header(
&self,
txn: &RoTxn,
height: u32,
) -> Result<Option<Header>, Error> {
let height = height.to_be_bytes();
let header = self.headers.get(txn, &height)?;
Ok(header)
}

pub fn get_body(&self, txn: &RoTxn, height: u32) -> Result<Option<Body<A, C>>, Error> {
pub fn get_body(
&self,
txn: &RoTxn,
height: u32,
) -> Result<Option<Body<A, C>>, Error> {
let height = height.to_be_bytes();
let header = self.bodies.get(txn, &height)?;
Ok(header)
Expand Down Expand Up @@ -77,7 +85,11 @@ impl<
Ok(())
}

pub fn append_header(&self, txn: &mut RwTxn, header: &Header) -> Result<(), Error> {
pub fn append_header(
&self,
txn: &mut RwTxn,
header: &Header,
) -> Result<(), Error> {
let height = self.get_height(txn)?;
let best_hash = self.get_best_hash(txn)?;
if header.prev_side_hash != best_hash {
Expand Down
51 changes: 33 additions & 18 deletions src/authorization.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::types::blake3;
use crate::types::{Address, AuthorizedTransaction, Body, GetAddress, Transaction, Verify};
pub use ed25519_dalek::{Keypair, PublicKey, Signature, SignatureError, Signer, Verifier};
use crate::types::{
Address, AuthorizedTransaction, Body, GetAddress, Transaction, Verify,
};
pub use ed25519_dalek::{
Keypair, PublicKey, Signature, SignatureError, Signer, Verifier,
};
use rayon::prelude::*;
use serde::{Deserialize, Serialize};

Expand All @@ -16,9 +20,13 @@ impl GetAddress for Authorization {
}
}

impl<C: Clone + Serialize + for<'de> Deserialize<'de> + Send + Sync> Verify<C> for Authorization {
impl<C: Clone + Serialize + for<'de> Deserialize<'de> + Send + Sync> Verify<C>
for Authorization
{
type Error = Error;
fn verify_transaction(transaction: &AuthorizedTransaction<Self, C>) -> Result<(), Self::Error>
fn verify_transaction(
transaction: &AuthorizedTransaction<Self, C>,
) -> Result<(), Self::Error>
where
Self: Sized,
{
Expand Down Expand Up @@ -56,16 +64,17 @@ pub fn verify_authorized_transaction<C: Clone + Serialize + Sync>(
let messages: Vec<_> = std::iter::repeat(serialized_transaction.as_slice())
.take(transaction.authorizations.len())
.collect();
let (public_keys, signatures): (Vec<PublicKey>, Vec<Signature>) = transaction
.authorizations
.iter()
.map(
|Authorization {
public_key,
signature,
}| (public_key, signature),
)
.unzip();
let (public_keys, signatures): (Vec<PublicKey>, Vec<Signature>) =
transaction
.authorizations
.iter()
.map(
|Authorization {
public_key,
signature,
}| (public_key, signature),
)
.unzip();
ed25519_dalek::verify_batch(&messages, &signatures, &public_keys)?;
Ok(())
}
Expand All @@ -82,7 +91,8 @@ pub fn verify_authorizations<C: Clone + Serialize + Sync>(
.par_iter()
.map(bincode::serialize)
.collect::<Result<_, _>>()?;
let serialized_transactions = serialized_transactions.iter().map(Vec::as_slice);
let serialized_transactions =
serialized_transactions.iter().map(Vec::as_slice);
let messages = input_numbers.zip(serialized_transactions).flat_map(
|(input_number, serialized_transaction)| {
std::iter::repeat(serialized_transaction).take(input_number)
Expand All @@ -101,7 +111,9 @@ pub fn verify_authorizations<C: Clone + Serialize + Sync>(
signatures: Vec::with_capacity(package_size),
public_keys: Vec::with_capacity(package_size),
};
for (authorization, message) in &pairs[i * package_size..(i + 1) * package_size] {
for (authorization, message) in
&pairs[i * package_size..(i + 1) * package_size]
{
package.messages.push(*message);
package.signatures.push(authorization.signature);
package.public_keys.push(authorization.public_key);
Expand All @@ -128,7 +140,9 @@ pub fn verify_authorizations<C: Clone + Serialize + Sync>(
messages,
signatures,
public_keys,
}| ed25519_dalek::verify_batch(messages, signatures, public_keys),
}| {
ed25519_dalek::verify_batch(messages, signatures, public_keys)
},
)
.collect::<Result<(), SignatureError>>()?;
Ok(())
Expand All @@ -146,7 +160,8 @@ pub fn authorize<C: Clone + Serialize>(
addresses_keypairs: &[(Address, &Keypair)],
transaction: Transaction<C>,
) -> Result<AuthorizedTransaction<Authorization, C>, Error> {
let mut authorizations: Vec<Authorization> = Vec::with_capacity(addresses_keypairs.len());
let mut authorizations: Vec<Authorization> =
Vec::with_capacity(addresses_keypairs.len());
let message = bincode::serialize(&transaction)?;
for (address, keypair) in addresses_keypairs {
let hash_public_key = get_address(&keypair.public);
Expand Down
22 changes: 17 additions & 5 deletions src/drivechain/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,19 @@ pub trait Main {
nsidechain: u8,
) -> Result<Vec<WithdrawalStatus>, jsonrpsee::core::Error>;
#[method(name = "listspentwithdrawals")]
async fn listspentwithdrawals(&self) -> Result<Vec<SpentWithdrawal>, jsonrpsee::core::Error>;
async fn listspentwithdrawals(
&self,
) -> Result<Vec<SpentWithdrawal>, jsonrpsee::core::Error>;
#[method(name = "listfailedwithdrawals")]
async fn listfailedwithdrawals(&self) -> Result<Vec<FailedWithdrawal>, jsonrpsee::core::Error>;
async fn listfailedwithdrawals(
&self,
) -> Result<Vec<FailedWithdrawal>, jsonrpsee::core::Error>;
#[method(name = "getblockcount")]
async fn getblockcount(&self) -> Result<usize, jsonrpsee::core::Error>;
#[method(name = "getbestblockhash")]
async fn getbestblockhash(&self) -> Result<bitcoin::BlockHash, jsonrpsee::core::Error>;
async fn getbestblockhash(
&self,
) -> Result<bitcoin::BlockHash, jsonrpsee::core::Error>;
#[method(name = "getblock")]
async fn getblock(
&self,
Expand Down Expand Up @@ -121,14 +127,20 @@ pub trait Main {
) -> Result<serde_json::Value, jsonrpsee::core::Error>;

#[method(name = "generate")]
async fn generate(&self, num: u32) -> Result<serde_json::Value, jsonrpsee::core::Error>;
async fn generate(
&self,
num: u32,
) -> Result<serde_json::Value, jsonrpsee::core::Error>;

#[method(name = "getnewaddress")]
async fn getnewaddress(
&self,
account: &str,
address_type: &str,
) -> Result<bitcoin::Address<bitcoin::address::NetworkUnchecked>, jsonrpsee::core::Error>;
) -> Result<
bitcoin::Address<bitcoin::address::NetworkUnchecked>,
jsonrpsee::core::Error,
>;

#[method(name = "createsidechaindeposit")]
async fn createsidechaindeposit(
Expand Down
Loading
Loading