class YolodiceValidator

Public Class Methods

new(input_file) click to toggle source
# File lib/yolodice_validator.rb, line 73
def initialize input_file
  @input_file = input_file
  @mismatch_count = 0
end

Private Class Methods

parse_opts(argv) click to toggle source
# File lib/yolodice_validator.rb, line 132
def parse_opts argv
  # There is only one option - pass a file containing a CSV dump
  usage = "Usage: yolodice_validator DUMP_FILE"
  unless argv.length == 1
    puts usage
    exit
  end
  return { input_file: argv[0] }
end
run(opts) click to toggle source
# File lib/yolodice_validator.rb, line 142
def run opts
  yv = YolodiceValidator.new opts[:input_file]
  yv.run
end

Public Instance Methods

run() click to toggle source
# File lib/yolodice_validator.rb, line 78
def run
  CSV.open @input_file do |file|
    # parse first line
    seed_id0, secret_hashed_hex0, secret_hex0, client_phrase0, seed_created_at0 = file.shift
    @generator = YolodiceGenerator.new secret_hex0, client_phrase0
    assume 'secret_hashed_hex', secret_hashed_hex0, @generator.server_key_hash_hex
    if @mismatch_count == 0
      puts "Seed seems OK, validating individual bets."
    else
      puts "Seed data is not valid, checking bets anyway."
    end
    print '.'
    @last_dot = true
    # read the rest of lines, verify individual bets
    while bet_data = file.shift do
      bet_id0, nonce0, rolled0, target0, range0, amount0, profit0 = bet_data
      bet_id0 = bet_id0.to_i
      nonce0 = nonce0.to_i
      rolled0 = rolled0.to_i
      target0 = target0.to_i
      amount0 = amount0.to_i
      profit0 = profit0.to_i

      bet = @generator.generate_bet nonce0, amount0, target0, range0
      assume "bet #{bet_id0} result", rolled0.to_i, bet[:result]
      assume "bet #{bet_id0} profit", profit0.to_i, bet[:profit]
      if nonce0 % 1000 == 0
        print '.' 
        @last_dot = true
      end
    end
    print "\n"
    if @mismatch_count == 0
      puts "All bets verified OK"
    else
      puts "#{@mismatch_count} errors found."
    end

  end
end

Private Instance Methods

assume(label, v0, v1) click to toggle source
# File lib/yolodice_validator.rb, line 120
def assume label, v0, v1
  unless v0 == v1
    print "\n" if @last_dot
    puts "MISMATCH: #{label}, in file: #{v0}, calculated: #{v1}"
    @mismatch_count += 1
    @last_dot = false
  end

end