Module: Ethash

Defined in:
lib/ethash/version.rb,
ext/ethash/ethash.c

Constant Summary collapse

VERSION =
'0.2.0'
REVISION =
LONG2NUM((long) ETHASH_REVISION)
DATASET_BYTES_INIT =
LONG2NUM((long) ETHASH_DATASET_BYTES_INIT)
DATASET_BYTES_GROWTH =
LONG2NUM((long) ETHASH_DATASET_BYTES_GROWTH)
CACHE_BYTES_INIT =
LONG2NUM((long) ETHASH_CACHE_BYTES_INIT)
CACHE_BYTES_GROWTH =
LONG2NUM((long) ETHASH_CACHE_BYTES_GROWTH)
EPOCH_LENGTH =
LONG2NUM((long) ETHASH_EPOCH_LENGTH)
MIX_BYTES =
LONG2NUM((long) ETHASH_MIX_BYTES)
HASH_BYTES =
LONG2NUM((long) ETHASH_HASH_BYTES)
DATASET_PARENTS =
LONG2NUM((long) ETHASH_DATASET_PARENTS)
CACHE_ROUNDS =
LONG2NUM((long) ETHASH_CACHE_ROUNDS)
ACCESSES =
LONG2NUM((long) ETHASH_ACCESSES)

Class Method Summary collapse

Class Method Details

.get_seedhash(blknum) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'ext/ethash/ethash.c', line 52

static VALUE
ethash_get_seedhash_wrapper(VALUE self, VALUE blknum) {
  unsigned long block_number = NUM2ULONG(blknum);

  if (block_number >= ETHASH_EPOCH_LENGTH * 2048) {
    char error_message[1024];
    sprintf(error_message, "Block number must be less than %i (was %lu)", ETHASH_EPOCH_LENGTH*2048, block_number);

    rb_raise(rb_eRuntimeError, error_message);
    return NULL;
  }

  ethash_h256_t seedhash = ethash_get_seedhash(block_number);
  return rb_str_new((char*)&seedhash, 32);
}

.hashimoto_light(blknum, cache, header, _nonce) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'ext/ethash/ethash.c', line 20

static VALUE
ethash_hashimoto_light(VALUE self, VALUE blknum, VALUE cache, VALUE header, VALUE _nonce) {
  char *cache_bytes = RSTRING_PTR(cache);
  unsigned long cache_size = RSTRING_LEN(cache);

  char *header_bytes = RSTRING_PTR(header);
  unsigned long header_size = RSTRING_LEN(header);

  unsigned long block_number = NUM2ULONG(blknum);
  unsigned long nonce = NUM2ULL(_nonce);

  struct ethash_light *s;
  s = calloc(sizeof(*s), 1);
  s->cache = cache_bytes;
  s->cache_size = cache_size;
  s->block_number = block_number;

  struct ethash_h256 *h;
  h = calloc(sizeof(*h), 1);
  for (unsigned int i=0; i<32; i++) h->b[i] = header_bytes[i];

  struct ethash_return_value out = ethash_light_compute(s, *h, nonce);

  VALUE hash = rb_hash_new();
  VALUE key_mixhash = ID2SYM(rb_intern("mixhash"));
  VALUE key_result = ID2SYM(rb_intern("result"));
  rb_hash_aset(hash, key_mixhash, rb_str_new(&out.mix_hash, 32));
  rb_hash_aset(hash, key_result, rb_str_new(&out.result, 32));

  return hash;
}

.mkcache_bytes(blknum) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'ext/ethash/ethash.c', line 9

static VALUE
ethash_mkcache_bytes(VALUE self, VALUE blknum) {
  unsigned long block_number = NUM2ULONG(blknum);

  ethash_light_t L = ethash_light_new(block_number);
  VALUE val = rb_str_new(L->cache, L->cache_size);
  free(L->cache);

  return val;
}