module Ethereum::Ethash

Constants

CACHE_BY_SEED_MAX
EPOCH_LENGTH

Public Class Methods

cache_by_file(block_number, data=nil) click to toggle source
# File lib/ethereum/ethash.rb, line 30
def cache_by_file(block_number, data=nil)
  path = "/tmp/ruby_ethereum_hashimoto_cache_#{block_number}"
  if data
    File.open(path, 'wb') {|f| f.write Marshal.dump(data) }
  else
    if File.exist?(path)
      File.open(path, 'rb') {|f| Marshal.load f.read }
    else
      nil
    end
  end
end
cache_by_seed() click to toggle source
# File lib/ethereum/ethash.rb, line 26
def cache_by_seed
  @cache_by_seed ||= {} # ordered hash
end
get_cache(blknum) click to toggle source
# File lib/ethereum/ethash.rb, line 43
def get_cache(blknum)
  seed = get_seed blknum

  if cache_by_seed.has_key?(seed)
    c = cache_by_seed.delete seed # pop
    cache_by_seed[seed] = c # and append at end
    return c
  end

  if c = cache_by_file(Utils.encode_hex(seed))
    cache_by_seed[seed] = c
    return c
  end

  # Use c++ implementation or ethash_ruby
  c = ::Ethash.mkcache_bytes blknum
  #c = EthashRuby::Cache.new(blknum).to_a

  cache_by_seed[seed] = c
  cache_by_file Utils.encode_hex(seed), c
  if cache_by_seed.size > CACHE_BY_SEED_MAX
    cache_by_seed.delete cache_by_seed.keys.first # remove last recently accessed
  end

  c
end
get_seed(block_number) click to toggle source
# File lib/ethereum/ethash.rb, line 17
def get_seed(block_number)
  epoch_no = block_number / EPOCH_LENGTH
  while seeds.size <= epoch_no
    seeds.push Ethereum::Utils.keccak256(seeds.last)
  end

  seeds[epoch_no]
end
hashimoto_light(blknum, cache, mining_hash, bin_nonce) click to toggle source
# File lib/ethereum/ethash.rb, line 71
def hashimoto_light(blknum, cache, mining_hash, bin_nonce)
  nonce = Utils.big_endian_to_int(bin_nonce)
  ::Ethash.hashimoto_light blknum, cache, mining_hash, nonce
end
seeds() click to toggle source
# File lib/ethereum/ethash.rb, line 13
def seeds
  @seeds ||= ["\x00"*32]
end