Class: KairosMcp::KairosChain::Block
- Inherits:
-
Object
- Object
- KairosMcp::KairosChain::Block
- Defined in:
- lib/kairos_mcp/kairos_chain/block.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#merkle_root ⇒ Object
readonly
Returns the value of attribute merkle_root.
-
#previous_hash ⇒ Object
readonly
Returns the value of attribute previous_hash.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
Class Method Summary collapse
Instance Method Summary collapse
- #calculate_hash ⇒ Object
-
#initialize(index:, timestamp: Time.now.utc, data:, previous_hash:, merkle_root:) ⇒ Block
constructor
A new instance of Block.
- #to_h ⇒ Object
- #to_json(*args) ⇒ Object
Constructor Details
#initialize(index:, timestamp: Time.now.utc, data:, previous_hash:, merkle_root:) ⇒ Block
Returns a new instance of Block.
10 11 12 13 14 15 16 17 |
# File 'lib/kairos_mcp/kairos_chain/block.rb', line 10 def initialize(index:, timestamp: Time.now.utc, data:, previous_hash:, merkle_root:) @index = index @timestamp = @data = data @previous_hash = previous_hash @merkle_root = merkle_root @hash = calculate_hash end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
8 9 10 |
# File 'lib/kairos_mcp/kairos_chain/block.rb', line 8 def data @data end |
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
8 9 10 |
# File 'lib/kairos_mcp/kairos_chain/block.rb', line 8 def hash @hash end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
8 9 10 |
# File 'lib/kairos_mcp/kairos_chain/block.rb', line 8 def index @index end |
#merkle_root ⇒ Object (readonly)
Returns the value of attribute merkle_root.
8 9 10 |
# File 'lib/kairos_mcp/kairos_chain/block.rb', line 8 def merkle_root @merkle_root end |
#previous_hash ⇒ Object (readonly)
Returns the value of attribute previous_hash.
8 9 10 |
# File 'lib/kairos_mcp/kairos_chain/block.rb', line 8 def previous_hash @previous_hash end |
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
8 9 10 |
# File 'lib/kairos_mcp/kairos_chain/block.rb', line 8 def @timestamp end |
Class Method Details
.genesis ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/kairos_mcp/kairos_chain/block.rb', line 32 def self.genesis new( index: 0, timestamp: Time.at(0).utc, # Fixed timestamp for genesis data: ["Genesis Block"], previous_hash: "0" * 64, merkle_root: "0" * 64 ) end |
Instance Method Details
#calculate_hash ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/kairos_mcp/kairos_chain/block.rb', line 19 def calculate_hash # Combine all attributes to generate a unique hash payload = [ @index, @timestamp.iso8601(6), @previous_hash, @merkle_root, @data.to_json ].join Digest::SHA256.hexdigest(payload) end |
#to_h ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/kairos_mcp/kairos_chain/block.rb', line 42 def to_h { index: @index, timestamp: @timestamp.iso8601(6), data: @data, previous_hash: @previous_hash, merkle_root: @merkle_root, hash: @hash } end |
#to_json(*args) ⇒ Object
53 54 55 |
# File 'lib/kairos_mcp/kairos_chain/block.rb', line 53 def to_json(*args) to_h.to_json(*args) end |