Class: KairosMcp::KairosChain::Block

Inherits:
Object
  • Object
show all
Defined in:
lib/kairos_mcp/kairos_chain/block.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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 = timestamp
  @data = data
  @previous_hash = previous_hash
  @merkle_root = merkle_root
  @hash = calculate_hash
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/kairos_mcp/kairos_chain/block.rb', line 8

def data
  @data
end

#hashObject (readonly)

Returns the value of attribute hash.



8
9
10
# File 'lib/kairos_mcp/kairos_chain/block.rb', line 8

def hash
  @hash
end

#indexObject (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_rootObject (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_hashObject (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

#timestampObject (readonly)

Returns the value of attribute timestamp.



8
9
10
# File 'lib/kairos_mcp/kairos_chain/block.rb', line 8

def timestamp
  @timestamp
end

Class Method Details

.genesisObject



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_hashObject



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_hObject



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