Crates.io | blockchain-cli |
lib.rs | blockchain-cli |
version | 1.0.26 |
source | src |
created_at | 2023-09-02 20:19:06.028656 |
updated_at | 2024-10-21 08:31:39.53098 |
description | A Rust crate provides an interface for interacting with a blockchain. |
homepage | |
repository | https://github.com/slavik-pastushenko/blockchain-rs |
max_upload_size | |
id | 961902 |
size | 35,100 |
A Rust crate provides an interface for interacting with a blockchain.
new(difficulty, reward, fee)
: Initialize a new blockchain with the specified parameters.get_transactions(page, size)
: Get a list of current transactions in the blockchain using pagination details.get_transaction(hash)
: Get a transaction by its hash.add_transaction(from, to, amount)
: Add a new transaction to the blockchain.validate_transaction(from, amount)
: Validate a new transaction to the blockchain.create_wallet(email)
: Create a new wallet with a unique email and an initial balance.get_wallet_balance(address)
: Get a wallet's balance based on its address.get_wallet_transactions(address, page, size)
: Get a wallet's transaction history based on its address and using pagination details.get_last_hash()
: Get the hash of the last block in the blockchain.update_difficulty(difficulty)
: Update the mining difficulty of the blockchain.update_reward(reward)
: Update the block reward.update_fee(fee)
: Update the transaction fee.generate_new_block()
: Generate a new block and append it to the blockchain.get_merkle(transactions)
: Calculate the Merkle root hash for a list of transactions.proof_of_work(header)
: Perform the proof-of-work process to mine a block.hash(item)
: Calculate the SHA-256 hash of a serializable item.Option | Data type | Description |
---|---|---|
difficulty |
f64 |
The initial mining difficulty level of the network. |
reward |
f64 |
The initial block reward for miners. |
fee |
f64 |
The transaction fee. |
This crate uses #![forbid(unsafe_code)]
to ensure everything is implemented in 100% safe Rust.
For more in-depth details, please refer to the full documentation.
If you encounter any issues or have questions that are not addressed in the documentation, feel free to submit an issue.
Explore the capabilities of this blockchain implementation through a set of examples:
use blockchain::Chain;
fn main() {
// Initialise a new blockchain
let mut chain = Chain::new(2, 100.0, 0.01);
// Create a wallet for a sender
let sender = chain.create_wallet(String::from("sender@mail.com"));
// Create a wallet for a receiver
let receiver = chain.create_wallet(String::from("receiver@mail.com"));
// Add a transaction
chain.add_transaction(sender, receiver, 1.25);
// Get a transaction
let transaction = chain.get_transaction(
String::from("6e8c5dc01145016e5a979683ba7e13bafaf85e765490aa33c0bba1f41cf581ed")
);
match transaction {
Some(trx) => println!("📦 Transaction: {:?}", trx),
None => println!("❌ Transaction was not found"),
}
// Get all transactions
let transactions = chain.get_transactions();
println!("📦 Transactions: {:?}", transactions);
// Others
}
Build the application:
cargo build
Test the application:
cargo test
Run the application:
cargo run
Run clippy:
cargo clippy --all-targets --all-features --no-deps -- -D warnings
Run lint:
cargo fmt
Generate documentation in HTML format:
cargo doc --open