Skip to content

Commit

Permalink
0.1.0-pre.alpha.2
Browse files Browse the repository at this point in the history
  • Loading branch information
rustyspottedcatt committed Feb 6, 2025
1 parent d83a3f1 commit 8307170
Show file tree
Hide file tree
Showing 36 changed files with 985 additions and 659 deletions.
21 changes: 19 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nebulacrypto"
version = "0.2.0"
version = "0.1.0-pre.alpha.2"
edition = "2024"

[dependencies]
Expand All @@ -12,4 +12,21 @@ rand = "0.8"
chrono = { version = "0.4.39", features = ["serde"]}
sha2 = "0.10.8"
hex = "0.4.3"
warp = "0.3.7"
#libp2p = "0.55.0"


[lib]
name = "nebula"
path = "src/lib.rs"

[[test]]
name = "transactions"
path = "tests/transactions.rs"

[[test]]
name = "staking"
path = "tests/staking.rs"

[[test]]
name = "governance"
path = "tests/governance.rs"
44 changes: 28 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

---

# Nebula
# Nebula (Early Alpha Release v0.1.0-pre.alpha.1)

![Maintainer](https://img.shields.io/badge/maintainer-rustyspottedcatt-blue)
[![made-with-rust](https://img.shields.io/badge/Made%20with-Rust-1f425f.svg)](https://www.rust-lang.org/)
Expand Down Expand Up @@ -71,55 +71,67 @@ $ cargo run

### Creating a Wallet
```rust
let (signing_key, public_key, address) = core::api::v1::create_wallet();
println!("Address: {}", address); // Shareable
use core::api::v1::*;

let (signing_key, _public_key, address) = create_wallet();
println!("Wallet created: {:x?}", address); // Shareable (used to exchange)
println!("Public Key: {}", public_key); // Shareable
println!("Private Key: {}", signing_key); // Do not SHARE
```

### Creating and Signing Transactions
```rust
let recipient = "recipient_address";
use core::api::v1::*;

let recipient = "recipient_address".to_string();
let amount = 100;

let mut tx = core::api::v1::build_transaction(&mut consensus_engine, address, recipient, amount);
core::api::v1::finalize_transaction(&mut tx)?;
core::api::v1::submit_transaction(&mut consensus_engine, tx)?;
let mut tx = build_transaction(&mut consensus_engine, address, recipient, amount);
finalize_transaction(&mut tx)?;
submit_transaction(&mut consensus_engine, tx)?;
```

### Producing a Block
```rust
let block = consensus_engine.produce_block(&signing_key)?;
use crate::core::consensus::block::produce_block;

let block = produce_block(&mut consensus_engine, &signing_key)?;
println!("Block produced with {} transaction(s)", block.transactions.len());
println!("Block timestamp: {}", block.header.timestamp);
```

### Neuron Management
```rust
let neuron_id = nervous_system.create_neuron(&signing_key, "Test Neuron".to_string(), 30)?;
use crate::core::nervous::neuron_handler::create_neuron;

let neuron_id = create_neuron(&nervous_system, &signing_key, "Test Neuron".to_string(), 30)?;
println!("Neuron created with id: {}", neuron_id);
```

### Staking
```rust
let mut staking_module = core::staking::StakingModule::new(nervous_system.neurons.clone());
staking_module.stake(&signing_key, neuron_id, 50)?;
use crate::core::staking::staking_handler::{stake, unstake};

let mut staking_module = core::staking::staking_module::StakingModule::new(nervous_system.neurons.clone());
stake(&mut staking_module, &signing_key, neuron_id, 50)?;
println!("Staked 50 tokens to neuron {}", neuron_id);
```

### Governance and Voting
```rust
use crate::core::governance::{proposal_handler::propose, voting::{vote, finalize}};

let governance = core::governance::Governance::new(nervous_system.neurons.clone());

let proposal_id = governance.propose("Increase block size".to_string(), &signing_key, neuron_id)?;
let proposal_id = propose(&governance, "Increase block size".to_string(), &signing_key, neuron_id)?;
println!("Proposal created with id: {}", proposal_id);

match governance.vote(&signing_key, neuron_id, proposal_id, true, 10) {
Ok(_) => println!("Voted on proposal {}", proposal_id),
Err(e) => println!("Voting failed: {}", e),
match vote(&governance, &signing_key, neuron_id, proposal_id, true, 10) {
Ok(_) => println!("Voted on proposal {}", proposal_id),
Err(e) => println!("Voting failed: {}", e),
}

let proposal_result = governance.finalize(proposal_id)?;
let proposal_result = finalize(&governance, proposal_id)?;
println!("Proposal finalized with result: {}", proposal_result);
```

Expand Down
11 changes: 11 additions & 0 deletions src/core/api/v1/blockchain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use crate::core::types::Block;
use crate::core::types::Address;
use crate::core::consensus::ConsensusEngine;

pub fn get_current_validator(consensus: &mut ConsensusEngine) -> Option<Address> {
crate::core::consensus::select_next_validator(consensus)
}

pub fn get_latest_block(consensus: &mut ConsensusEngine) -> Option<Block> {
consensus.chain.last().cloned()
}
7 changes: 7 additions & 0 deletions src/core/api/v1/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub mod wallet;
pub mod transaction;
pub mod blockchain;

pub use wallet::create_wallet;
pub use transaction::{build_transaction, finalize_transaction, submit_transaction};
pub use blockchain::{get_current_validator, get_latest_block};
37 changes: 10 additions & 27 deletions src/core/api/v1.rs → src/core/api/v1/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
use crate::core::types::{Transaction, TransactionType, TransactionStatus, Address, Block};
use crate::core::types::{Transaction, TransactionType, TransactionStatus, Address};
use crate::core::consensus::{ConsensusEngine, compute_transaction_hash};
use chrono::Utc;
use ed25519_dalek::{SigningKey, VerifyingKey};
use rand::rngs::OsRng;
use hex;
use crate::core::crypto::sign_data;

pub fn create_wallet() -> (SigningKey, VerifyingKey, Address) {
let signing_key = SigningKey::generate(&mut OsRng);
let verifying_key = VerifyingKey::from(&signing_key);
let address = hex::encode(verifying_key.to_bytes());

(signing_key, verifying_key, address)
}
use chrono::Utc;
use ed25519_dalek::SigningKey;

pub fn build_transaction(
consensus: &mut ConsensusEngine,
Expand All @@ -24,11 +14,11 @@ pub fn build_transaction(
tx_type: TransactionType,
) -> Transaction {
let fee = amount / 100;
let index = consensus
.chain
.iter()
.map(|chain| chain.transactions.len())
.sum::<usize>() as u32
let index = consensus
.chain
.iter()
.map(|chain| chain.transactions.len())
.sum::<usize>() as u32
+ consensus.mempool.len() as u32;

Transaction {
Expand All @@ -46,6 +36,7 @@ pub fn build_transaction(
signature: vec![],
}
}

pub fn finalize_transaction(tx: &mut Transaction, signing_key: &SigningKey) -> Result<(), String> {
let mut tx_clone = tx.clone();
tx_clone.hash.clear();
Expand All @@ -61,13 +52,5 @@ pub fn finalize_transaction(tx: &mut Transaction, signing_key: &SigningKey) -> R
}

pub fn submit_transaction(consensus: &mut ConsensusEngine, tx: Transaction) -> Result<(), String> {
consensus.add_transaction(tx)
}

pub fn get_current_validator(consensus: &ConsensusEngine) -> Option<Address> {
consensus.select_next_validator()
}

pub fn get_latest_block(consensus: &ConsensusEngine) -> Option<Block> {
consensus.chain.last().cloned()
crate::core::consensus::transaction::add_transaction(consensus, tx)
}
12 changes: 12 additions & 0 deletions src/core/api/v1/wallet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use ed25519_dalek::{SigningKey, VerifyingKey};
use rand::rngs::OsRng;
use hex;
use crate::core::types::Address;

pub fn create_wallet() -> (SigningKey, VerifyingKey, Address) {
let signing_key = SigningKey::generate(&mut OsRng);
let verifying_key = VerifyingKey::from(&signing_key);
let address = hex::encode(verifying_key.to_bytes());

(signing_key, verifying_key, address)
}
Loading

0 comments on commit 8307170

Please sign in to comment.