|
| 1 | +# Ruby Blockchain Example |
| 2 | +[](https://github.com/AnotherRegularDude/ruby_blockchain_example/actions/workflows/ci.yml) |
| 3 | +[](https://coveralls.io/github/AnotherRegularDude/ruby_blockchain_example?branch=main) |
| 4 | + |
| 5 | +Educational implementation of core blockchain primitives in Ruby: blocks, transactions, proof-of-work difficulty, chain validation, and service objects. |
| 6 | + |
| 7 | +## Features |
| 8 | +- Create the genesis block and initialize the chain via `Case::Blockchain::Create` and `Case::Block::CreateGenesis`. |
| 9 | +- Add new blocks with transaction data through `Case::Blockchain::Add`. |
| 10 | +- Validate chain integrity and difficulty with `Case::Blockchain::Validate`. |
| 11 | +- Typed entities and value objects on dry-struct/dry-types with Zeitwerk autoloading. |
| 12 | + |
| 13 | +## Tech stack |
| 14 | +- Ruby 3.4.7 |
| 15 | +- dry-rb (`dry-struct`, `dry-types`, `dry-initializer`) |
| 16 | +- `resol` (service layer) |
| 17 | +- `zeitwerk` (autoload) |
| 18 | +- `rspec`, `simplecov`/`simplecov-lcov`, `rubocop`, `pry` |
| 19 | + |
| 20 | +## Project structure |
| 21 | +``` |
| 22 | +app/ |
| 23 | + case/ # service objects |
| 24 | + entity/ # block, transaction, blockchain entities |
| 25 | + value/ # transaction input/output value objects |
| 26 | +config.rb # dependencies and Zeitwerk setup |
| 27 | +spec/ # RSpec tests |
| 28 | +``` |
| 29 | + |
| 30 | +## Getting started |
| 31 | +1. Install Ruby 3.4.7 (rbenv/rvm) and Bundler. |
| 32 | +2. Install dependencies: |
| 33 | + ```bash |
| 34 | + bundle install |
| 35 | + ``` |
| 36 | +3. Run tests: |
| 37 | + ```bash |
| 38 | + bundle exec rspec |
| 39 | + ``` |
| 40 | + |
| 41 | +## Usage examples |
| 42 | +Create a chain, add a block, and validate: |
| 43 | +```ruby |
| 44 | +require_relative "config" |
| 45 | + |
| 46 | +input = Value::Transaction::Input.new(transaction_id: "0" * 64, output_index: 0) |
| 47 | +output = Value::Transaction::Output.new(address: "demo_wallet", amount: BigDecimal("1.0")) |
| 48 | +transaction = Entity::Transaction.build([input], [output]) |
| 49 | + |
| 50 | +blockchain = Case::Blockchain::Create.call!.value! |
| 51 | + |
| 52 | +Case::Blockchain::Add.call!(blockchain, [transaction]) |
| 53 | + |
| 54 | +valid = Case::Blockchain::Validate.call(blockchain).value! |
| 55 | +puts "Chain valid? #{valid}" |
| 56 | +``` |
| 57 | + |
| 58 | +Create a chain with custom difficulty and multiple transactions: |
| 59 | +```ruby |
| 60 | +require_relative "config" |
| 61 | + |
| 62 | +input1 = Value::Transaction::Input.new(transaction_id: "a" * 64, output_index: 0) |
| 63 | +output1 = Value::Transaction::Output.new(address: "alice", amount: BigDecimal("0.5")) |
| 64 | +input2 = Value::Transaction::Input.new(transaction_id: "b" * 64, output_index: 1) |
| 65 | +output2 = Value::Transaction::Output.new(address: "bob", amount: BigDecimal("0.25")) |
| 66 | + |
| 67 | +transactions = [ |
| 68 | + Entity::Transaction.build([input1], [output1]), |
| 69 | + Entity::Transaction.build([input2], [output2]), |
| 70 | +] |
| 71 | + |
| 72 | +blockchain = Case::Blockchain::Create.call!(difficulty: 2).value! |
| 73 | +Case::Blockchain::Add.call!(blockchain, transactions) |
| 74 | + |
| 75 | +puts "Blocks in chain: #{blockchain.blocks.size}" |
| 76 | +``` |
| 77 | + |
| 78 | +## Commands |
| 79 | +- Run tests: `bundle exec rspec` |
| 80 | +- Coverage report (HTML and LCOV): `COVER=1 bundle exec rspec` (outputs to `coverage/`) |
| 81 | +- Lint: `bundle exec rubocop` |
| 82 | + |
| 83 | +## Configuration |
| 84 | +- Default mining difficulty is set in `SimpleBlockchain.default_difficulty` (see `config.rb`). |
| 85 | +- SimpleCov is enabled when `COVER=1` or `COVER=true` is set in the environment. |
0 commit comments