Skip to content

Commit

Permalink
create the "spec" library project
Browse files Browse the repository at this point in the history
  • Loading branch information
mrnaveira committed Jul 2, 2022
1 parent 211e916 commit 7f4314a
Show file tree
Hide file tree
Showing 18 changed files with 63 additions and 32 deletions.
15 changes: 15 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

members = [
"node",
"spec",
]
2 changes: 1 addition & 1 deletion coverage_report.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ cargo build
cargo test

# Generate a HTML report in the coverage/ directory.
grcov . --binary-path ./target/debug/ -s . -t html --branch --ignore-not-existing --ignore "tests/*" -o ./coverage/
grcov . --binary-path ./target/debug/ -s . -t html --branch --ignore-not-existing --ignore "*/tests/*" -o ./coverage/

rm *.prof*
2 changes: 2 additions & 0 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
spec = { path = "../spec" }

actix-web = "4.1.0"
anyhow = "1.0.58"
chrono = "0.4.19"
Expand Down
3 changes: 2 additions & 1 deletion node/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::{
model::{Block, Blockchain, Transaction, TransactionPool},
transaction_pool::TransactionPool,
util::{execution::Runnable, Context},
};
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
use anyhow::Result;
use spec::{Block, Blockchain, Transaction};

struct ApiState {
blockchain: Blockchain,
Expand Down
5 changes: 3 additions & 2 deletions node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ extern crate log;

mod api;
mod miner;
mod model;
mod peer;
mod transaction_pool;
mod util;

use api::Api;
use miner::Miner;
use model::{Blockchain, TransactionPool};
use peer::Peer;
use spec::Blockchain;
use transaction_pool::TransactionPool;
use util::{execution, initialize_logger, termination, Config, Context};

fn main() {
Expand Down
17 changes: 7 additions & 10 deletions node/src/miner.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use crate::{
model::{
Address, Block, BlockHash, Blockchain, Transaction, TransactionPool, TransactionVec,
BLOCK_SUBSIDY,
},
transaction_pool::{TransactionPool, TransactionVec},
util::{
execution::{sleep_millis, Runnable},
Context,
},
};
use anyhow::Result;
use spec::{Address, Block, BlockHash, Blockchain, Transaction, BLOCK_SUBSIDY};
use thiserror::Error;

#[derive(Error, Debug)]
Expand Down Expand Up @@ -151,10 +149,6 @@ impl Miner {
#[cfg(test)]
mod tests {
use super::*;
use crate::model::{
test_util::{alice, bob},
Transaction,
};

// We use SHA 256 hashes
const MAX_DIFFICULTY: u32 = 256;
Expand Down Expand Up @@ -283,7 +277,10 @@ mod tests {
}

fn miner_address() -> Address {
alice()
Address::try_from(
"f780b958227ff0bf5795ede8f9f7eaac67e7e06666b043a400026cbd421ce28e".to_string(),
)
.unwrap()
}

fn create_miner(difficulty: u32, max_nonce: u64) -> Miner {
Expand Down Expand Up @@ -315,7 +312,7 @@ mod tests {
// so that address can be a sender of funds to other addresses
let transaction = Transaction {
sender: miner_address(),
recipient: bob(),
recipient: Address::default(),
amount: 3,
};
pool.add_transaction(transaction.clone());
Expand Down
13 changes: 5 additions & 8 deletions node/src/peer.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use std::panic;

use crate::{
model::{Block, Blockchain},
util::{
execution::{sleep_millis, Runnable},
Context,
},
use crate::util::{
execution::{sleep_millis, Runnable},
Context,
};
use anyhow::Result;
use isahc::{ReadResponseExt, Request};
use spec::{Block, Blockchain};
use std::panic;

pub struct Peer {
peer_addresses: Vec<String>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::Transaction;
use spec::Transaction;
use std::sync::{Arc, Mutex};

pub type TransactionVec = Vec<Transaction>;
Expand Down Expand Up @@ -47,7 +47,7 @@ impl TransactionPool {

#[cfg(test)]
mod tests {
use crate::model::test_util::{alice, bob};
use spec::Address;

use super::*;

Expand Down Expand Up @@ -100,8 +100,8 @@ mod tests {

fn create_mock_transaction(amount: u64) -> Transaction {
Transaction {
sender: alice(),
recipient: bob(),
sender: Address::default(),
recipient: Address::default(),
amount: amount,
}
}
Expand Down
2 changes: 1 addition & 1 deletion node/src/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use dotenv::dotenv;
use std::env;
use std::str::FromStr;

use crate::model::Address;
use spec::Address;

type StringVec = Vec<String>;

Expand Down
5 changes: 4 additions & 1 deletion node/src/util/context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use spec::Blockchain;

use crate::transaction_pool::TransactionPool;

use super::Config;
use crate::model::{Blockchain, TransactionPool};

pub struct Context {
pub config: Config,
Expand Down
16 changes: 16 additions & 0 deletions spec/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "spec"
version = "0.4.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.58"
chrono = "0.4.19"
ethereum-types = "0.13.1"
hex = "0.4.3"
rust-crypto = "0.2.36"
serde = { version = "1.0.137", features = ["derive"] }
serde_json = "1.0.81"
thiserror = "1.0.31"
File renamed without changes.
2 changes: 1 addition & 1 deletion node/src/model/address.rs → spec/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub mod test_util {
mod tests {
use std::{convert::TryFrom, str::FromStr};

use crate::model::Address;
use crate::Address;

use super::AddressError;

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion node/src/model/blockchain.rs → spec/src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl Blockchain {

#[cfg(test)]
mod tests {
use crate::model::{
use crate::{
account_balance_map::AccountBalanceMapError,
test_util::{alice, bob, carol},
Address, Transaction,
Expand Down
2 changes: 0 additions & 2 deletions node/src/model.rs → spec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ mod address;
mod block;
mod blockchain;
mod transaction;
mod transaction_pool;

// Explicitly controlling which individual identifiers we export
// It also avoids verbose module imports from other files
pub use address::Address;
pub use block::{Block, BlockHash};
pub use blockchain::{Blockchain, BlockchainError, BLOCK_SUBSIDY};
pub use transaction::Transaction;
pub use transaction_pool::{TransactionPool, TransactionVec};

#[cfg(test)]
pub use address::test_util;
File renamed without changes.

0 comments on commit 7f4314a

Please sign in to comment.