Class: Rubychain::Block

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index, timestamp, data, prev_hash) ⇒ Block

Returns a new instance of Block.



8
9
10
11
12
13
14
# File 'lib/rubychain/block.rb', line 8

def initialize(index, timestamp, data, prev_hash)
  @index = index
  @timestamp = timestamp
  @data = data
  @prev_hash = prev_hash
  @hash = hash_block
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/rubychain/block.rb', line 6

def data
  @data
end

#hashObject (readonly)

Returns the value of attribute hash.



6
7
8
# File 'lib/rubychain/block.rb', line 6

def hash
  @hash
end

#indexObject (readonly)

Returns the value of attribute index.



6
7
8
# File 'lib/rubychain/block.rb', line 6

def index
  @index
end

#prev_hashObject (readonly)

Returns the value of attribute prev_hash.



6
7
8
# File 'lib/rubychain/block.rb', line 6

def prev_hash
  @prev_hash
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



6
7
8
# File 'lib/rubychain/block.rb', line 6

def timestamp
  @timestamp
end

Instance Method Details

#hash_blockObject

Create the blocks hash by encrypting all the blocks data using SHA256



18
19
20
21
22
23
# File 'lib/rubychain/block.rb', line 18

def hash_block
  hash_string = [index,timestamp,data,prev_hash].join
  sha = Digest::SHA256.new
  sha.update(hash_string)
  sha.hexdigest
end