Skip to content

Commit

Permalink
Merge pull request #1137 from gregdhill/chore/bitcoin-dep
Browse files Browse the repository at this point in the history
chore: remove unused bitcoin code, cleanup deps, feature gate parser
  • Loading branch information
sander2 authored Jul 24, 2023
2 parents ac05c24 + 7df0652 commit 66ef87c
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 330 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 15 additions & 11 deletions crates/bitcoin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@ serde = { version = "1.0.130", default-features = false, features = ["derive"],
impl-serde = { version = "0.3.1", default-features = false, optional = true }
sha2 = { version = "0.8.2", default-features = false }
hex = { version = "0.4.2", default-features = false }
spin = { version = "0.7.1", default-features = false }
primitive-types = { version = "0.12.1", default-features = false, features = ["codec", "scale-info"] }
bitcoin_hashes = { version = "0.7.3", default-features = false }
secp256k1 = { package = "secp256k1", git = "https://github.com/rust-bitcoin/rust-secp256k1", rev = "8e61874", default-features = false }
spin = { version = "0.7.1", default-features = false }

# Substrate dependencies
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false }

[dev-dependencies]
mocktopus = "0.8.0"
secp256k1 = { package = "secp256k1", git = "https://github.com/rust-bitcoin/rust-secp256k1", rev = "8e61874", default-features = false, features = ["rand-std"] }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31" }

[features]
default = ["std"]
Expand All @@ -33,10 +30,17 @@ std = [
"impl-serde",
"sha2/std",
"hex/alloc",
"primitive-types/std",
"primitive-types/serde",
"secp256k1/std",

"sp-core/std",
"sp-std/std",
"frame-support/std",
]
runtime-benchmarks = []
parser = []
runtime-benchmarks = []

[[example]]
name = "parse-transaction"
required-features = ["parser"]

[[example]]
name = "run-proof"
required-features = ["parser"]
2 changes: 1 addition & 1 deletion crates/bitcoin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ To add this library to your crate, simply include the following in your crate's
```TOML
[dependencies.bitcoin]
default_features = false
git = '../creates/bitcoin'
git = '../crates/bitcoin'
```

Update your crate's `std` feature to include this pallet:
Expand Down
5 changes: 1 addition & 4 deletions crates/bitcoin/examples/parse-transaction.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
extern crate bitcoin;
extern crate hex;
use bitcoin::parser::parse_transaction;

const RAW_TRANSACTION: &str = "020000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff0502cb000101ffffffff02400606950000000017a91466c7060feb882664ae62ffad0051fe843e318e85870000000000000000266a24aa21a9ede5c17d15b8b1fa2811b7e6da66ffa5e1aaa05922c69068bf90cd585b95bb46750120000000000000000000000000000000000000000000000000000000000000000000000000";

use bitcoin::parser::parse_transaction;

fn main() {
let raw_tx = hex::decode(RAW_TRANSACTION).unwrap();
let tx = parse_transaction(&raw_tx).unwrap();
Expand Down
2 changes: 0 additions & 2 deletions crates/bitcoin/examples/run-proof.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate bitcoin;

use bitcoin::{
merkle::{MerkleProof, PartialTransactionProof},
parser::parse_transaction,
Expand Down
10 changes: 7 additions & 3 deletions crates/bitcoin/src/address.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::{types::*, Error, Script};
use bitcoin_hashes::{hash160::Hash as Hash160, Hash};
use codec::{Decode, Encode, MaxEncodedLen};
use primitive_types::{H160, H256};
use scale_info::TypeInfo;
use sha2::{Digest, Sha256};
use sp_core::{H160, H256};
use sp_std::vec::Vec;

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use secp256k1::{constants::PUBLIC_KEY_SIZE, Error as Secp256k1Error, PublicKey as Secp256k1PublicKey};

Expand Down Expand Up @@ -175,8 +177,10 @@ impl<'de> serde::Deserialize<'de> for PublicKey {
}

pub mod global {
#[cfg(not(feature = "std"))]
use alloc::{vec, vec::Vec};
use core::ops::Deref;
use secp256k1::{ffi::types::AlignedType, AllPreallocated, Secp256k1};
use sp_std::{ops::Deref, vec, vec::Vec};
// this is what lazy_static uses internally
use spin::Once;

Expand Down
6 changes: 4 additions & 2 deletions crates/bitcoin/src/formatter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use sp_core::U256;
use sp_std::{prelude::*, vec, vec::Vec};
use primitive_types::U256;

#[cfg(not(feature = "std"))]
use alloc::{vec, vec::Vec};

use crate::{merkle::MerkleProof, script::*, types::*, Error, GetCompact};

Expand Down
7 changes: 7 additions & 0 deletions crates/bitcoin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@
#![cfg_attr(test, feature(proc_macro_hygiene))]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(not(feature = "std"))]
extern crate alloc;

#[cfg(test)]
extern crate mocktopus;

extern crate hex;

mod error;
pub use error::Error;

Expand All @@ -36,6 +41,8 @@ pub use script::Script;
pub mod types;

pub mod formatter;

#[cfg(any(feature = "parser", test))]
pub mod parser;

pub mod utils;
Expand Down
2 changes: 1 addition & 1 deletion crates/bitcoin/src/math.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use sp_core::U256;
use primitive_types::U256;

pub trait GetCompact {
fn get_compact(self) -> Option<u32>;
Expand Down
22 changes: 10 additions & 12 deletions crates/bitcoin/src/merkle.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
#[cfg(test)]
extern crate mocktopus;

#[cfg(test)]
use mocktopus::macros::mockable;

use crate::{
parser::BytesParser,
types::{BlockHeader, CompactUint, H256Le, Transaction},
types::{BlockHeader, H256Le, Transaction},
utils::hash256_merkle_step,
Error,
};
use codec::{Decode, Encode};
use scale_info::TypeInfo;
use sp_std::prelude::*;

#[cfg(any(feature = "parser", test))]
use crate::{parser::BytesParser, types::CompactUint};

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

// https://github.com/bitcoin/bitcoin/blob/78dae8caccd82cfbfd76557f1fb7d7557c7b5edb/src/consensus/consensus.h
const MAX_BLOCK_WEIGHT: u32 = 4_000_000;
Expand Down Expand Up @@ -105,7 +103,6 @@ impl MerkleTree {
}
}

#[cfg_attr(test, mockable)]
impl PartialTransactionProof {
/// Computes the merkle root of the proof partial merkle tree
pub fn verify_proof(self) -> Result<ProofResult, Error> {
Expand Down Expand Up @@ -287,6 +284,7 @@ impl MerkleProof {
/// # Arguments
///
/// * `merkle_proof` - Raw bytes of the merkle proof
#[cfg(any(feature = "parser", test))]
pub fn parse(merkle_proof: &[u8]) -> Result<MerkleProof, Error> {
let mut proof_parser = BytesParser::new(merkle_proof);
let block_header = proof_parser.parse()?;
Expand Down Expand Up @@ -319,8 +317,8 @@ mod tests {

use super::*;

use sp_core::H256;
use sp_std::str::FromStr;
use primitive_types::H256;
use std::str::FromStr;

// curl -s -H 'content-type: application/json' http://satoshi.doc.ic.ac.uk:8332 -d '{
// "jsonrpc": "1.0",
Expand Down
Loading

0 comments on commit 66ef87c

Please sign in to comment.