feat: Add Proof of Work implementation for v2#4
Conversation
- Add nonce and difficulty fields to Block model - Implement mine_block method with PoW algorithm - Update calculate_hash to include nonce - Add valid_hash? method to verify difficulty requirement - Update Blockchain.add_block to mine blocks - Update integrity_valid? to check PoW validity - Add GET endpoint for block details with mining info - Add optional difficulty parameter to block creation 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
WalkthroughSe introduce Proof-of-Work: Block ahora tiene campos Cambios
Diagramas de SecuenciasequenceDiagram
participant Cliente
participant App as main.rb
participant Blockchain
participant Block
Cliente->>App: POST /chain/:id/block (data, difficulty)
App->>App: validate_difficulty(difficulty)
App->>Blockchain: add_block(data, difficulty)
Blockchain->>Block: build new block(data, difficulty)
Block-->Block: nonce = 0
Blockchain->>Block: mine_block()
loop Minado (PoW)
Block->>Block: incrementar nonce
Block->>Block: calcular hash
alt hash cumple dificultad
Block-->>Blockchain: hash minado
end
end
Blockchain->>Blockchain: save! block
Blockchain-->>App: bloque creado
App-->>Cliente: { block_hash, nonce, difficulty }
Cliente->>App: GET /chain/:id/block/:block_id
App->>Blockchain: find_block(block_id)
Blockchain-->>App: bloque
App->>Block: valid_hash?
Block-->>App: true/false
App-->>Cliente: { id, index, data, hash, previous_hash, nonce, difficulty, timestamp, valid_hash }
Esfuerzo estimado de revisión de código🎯 4 (Complejo) | ⏱️ ~45 minutos
Poema
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Resolved conflict in main.rb by keeping PoW implementation with difficulty parameter
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
main.rb(2 hunks)src/block.rb(2 hunks)src/blockchain.rb(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
src/blockchain.rb (1)
src/block.rb (3)
mine_block(60-69)calculate_hash(43-46)valid_hash?(74-77)
main.rb (2)
src/blockchain.rb (1)
add_block(22-34)src/block.rb (1)
valid_hash?(74-77)
- Add validate_difficulty helper method - Convert difficulty to integer and default to 2 if nil - Reject negative values with 422 status - Reject values > 10 to prevent excessive mining time - Prevents TypeError from string/float inputs Addresses CodeRabbit security review comment
Summary
nonceanddifficultyfields to Block modelmine_blockmethod that finds valid hash with required leading zerosTechnical Details
valid_hash?method checks if hash meets difficulty requirementAPI Changes
difficultyparameter and returnsnonceanddifficultyin responseTest plan
🤖 Generated with Claude Code
Summary by CodeRabbit
Nuevas funcionalidades
Mejoras