Commit 28ddc16a authored by Felix Lange's avatar Felix Lange

Merge remote-tracking branch 'ethereum/conversion' into conversion

parents c161d73d 01ff0b31
......@@ -22,8 +22,8 @@
},
{
"ImportPath": "github.com/ethereum/ethash",
"Comment": "v23-12-g149261a",
"Rev": "149261a5d7cafc3943cbcf1d370082ec70d81e8b"
"Comment": "v23.1-26-g934bb4f",
"Rev": "934bb4f5060ab69d96fb6eba4b9a57facc4e160b"
},
{
"ImportPath": "github.com/ethereum/serpent-go",
......
......@@ -31,8 +31,8 @@ import (
"time"
"unsafe"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/pow"
)
......@@ -85,7 +85,7 @@ func makeParamsAndCache(chainManager pow.ChainManager, blockNum uint64) (*Params
Epoch: blockNum / epochLength,
}
C.ethash_params_init(paramsAndCache.params, C.uint32_t(uint32(blockNum)))
paramsAndCache.cache.mem = C.malloc(paramsAndCache.params.cache_size)
paramsAndCache.cache.mem = C.malloc(C.size_t(paramsAndCache.params.cache_size))
seedHash, err := GetSeedHash(blockNum)
if err != nil {
......@@ -118,7 +118,7 @@ func (pow *Ethash) UpdateCache(force bool) error {
func makeDAG(p *ParamsAndCache) *DAG {
d := &DAG{
dag: C.malloc(p.params.full_size),
dag: C.malloc(C.size_t(p.params.full_size)),
file: false,
paramsAndCache: p,
}
......@@ -360,8 +360,7 @@ func (pow *Ethash) Search(block pow.Block, stop <-chan struct{}) (uint64, []byte
}
func (pow *Ethash) Verify(block pow.Block) bool {
return pow.verify(block.HashNoNonce(), block.MixDigest(), block.Difficulty(), block.NumberU64(), block.Nonce())
return pow.verify(block.HashNoNonce().Bytes(), block.MixDigest().Bytes(), block.Difficulty(), block.NumberU64(), block.Nonce())
}
func (pow *Ethash) verify(hash []byte, mixDigest []byte, difficulty *big.Int, blockNum uint64, nonce uint64) bool {
......
......@@ -7,184 +7,192 @@
var Keccak = require('./keccak');
var util = require('./util');
var ethUtil = require('ethereumjs-util');
// 32-bit unsigned modulo
function mod32(x, n)
{
return (x>>>0) % (n>>>0);
function mod32(x, n) {
return (x >>> 0) % (n >>> 0);
}
function fnv(x, y)
{
// js integer multiply by 0x01000193 will lose precision
return ((x*0x01000000 | 0) + (x*0x193 | 0)) ^ y;
function fnv(x, y) {
// js integer multiply by 0x01000193 will lose precision
return ((x * 0x01000000 | 0) + (x * 0x193 | 0)) ^ y;
}
function computeCache(params, seedWords)
{
var cache = new Uint32Array(params.cacheSize >> 2);
var cacheNodeCount = params.cacheSize >> 6;
// Initialize cache
var keccak = new Keccak();
keccak.digestWords(cache, 0, 16, seedWords, 0, seedWords.length);
for (var n = 1; n < cacheNodeCount; ++n)
{
keccak.digestWords(cache, n<<4, 16, cache, (n-1)<<4, 16);
}
var tmp = new Uint32Array(16);
// Do randmemohash passes
for (var r = 0; r < params.cacheRounds; ++r)
{
for (var n = 0; n < cacheNodeCount; ++n)
{
var p0 = mod32(n + cacheNodeCount - 1, cacheNodeCount) << 4;
var p1 = mod32(cache[n<<4|0], cacheNodeCount) << 4;
for (var w = 0; w < 16; w=(w+1)|0)
{
tmp[w] = cache[p0 | w] ^ cache[p1 | w];
}
keccak.digestWords(cache, n<<4, 16, tmp, 0, tmp.length);
}
}
return cache;
function computeCache(params, seedWords) {
var cache = new Uint32Array(params.cacheSize >> 2);
var cacheNodeCount = params.cacheSize >> 6;
// Initialize cache
var keccak = new Keccak();
keccak.digestWords(cache, 0, 16, seedWords, 0, seedWords.length);
for (var n = 1; n < cacheNodeCount; ++n) {
keccak.digestWords(cache, n << 4, 16, cache, (n - 1) << 4, 16);
}
var tmp = new Uint32Array(16);
// Do randmemohash passes
for (var r = 0; r < params.cacheRounds; ++r) {
for (var n = 0; n < cacheNodeCount; ++n) {
var p0 = mod32(n + cacheNodeCount - 1, cacheNodeCount) << 4;
var p1 = mod32(cache[n << 4 | 0], cacheNodeCount) << 4;
for (var w = 0; w < 16; w = (w + 1) | 0) {
tmp[w] = cache[p0 | w] ^ cache[p1 | w];
}
keccak.digestWords(cache, n << 4, 16, tmp, 0, tmp.length);
}
}
return cache;
}
function computeDagNode(o_node, params, cache, keccak, nodeIndex)
{
var cacheNodeCount = params.cacheSize >> 6;
var dagParents = params.dagParents;
var c = (nodeIndex % cacheNodeCount) << 4;
var mix = o_node;
for (var w = 0; w < 16; ++w)
{
mix[w] = cache[c|w];
}
mix[0] ^= nodeIndex;
keccak.digestWords(mix, 0, 16, mix, 0, 16);
for (var p = 0; p < dagParents; ++p)
{
// compute cache node (word) index
c = mod32(fnv(nodeIndex ^ p, mix[p&15]), cacheNodeCount) << 4;
for (var w = 0; w < 16; ++w)
{
mix[w] = fnv(mix[w], cache[c|w]);
}
}
keccak.digestWords(mix, 0, 16, mix, 0, 16);
function computeDagNode(o_node, params, cache, keccak, nodeIndex) {
var cacheNodeCount = params.cacheSize >> 6;
var dagParents = params.dagParents;
var c = (nodeIndex % cacheNodeCount) << 4;
var mix = o_node;
for (var w = 0; w < 16; ++w) {
mix[w] = cache[c | w];
}
mix[0] ^= nodeIndex;
keccak.digestWords(mix, 0, 16, mix, 0, 16);
for (var p = 0; p < dagParents; ++p) {
// compute cache node (word) index
c = mod32(fnv(nodeIndex ^ p, mix[p & 15]), cacheNodeCount) << 4;
for (var w = 0; w < 16; ++w) {
mix[w] = fnv(mix[w], cache[c | w]);
}
}
keccak.digestWords(mix, 0, 16, mix, 0, 16);
}
function computeHashInner(mix, params, cache, keccak, tempNode)
{
var mixParents = params.mixParents|0;
var mixWordCount = params.mixSize >> 2;
var mixNodeCount = mixWordCount >> 4;
var dagPageCount = (params.dagSize / params.mixSize) >> 0;
// grab initial first word
var s0 = mix[0];
// initialise mix from initial 64 bytes
for (var w = 16; w < mixWordCount; ++w)
{
mix[w] = mix[w & 15];
}
for (var a = 0; a < mixParents; ++a)
{
var p = mod32(fnv(s0 ^ a, mix[a & (mixWordCount-1)]), dagPageCount);
var d = (p * mixNodeCount)|0;
for (var n = 0, w = 0; n < mixNodeCount; ++n, w += 16)
{
computeDagNode(tempNode, params, cache, keccak, (d + n)|0);
for (var v = 0; v < 16; ++v)
{
mix[w|v] = fnv(mix[w|v], tempNode[v]);
}
}
}
function computeHashInner(mix, params, cache, keccak, tempNode) {
var mixParents = params.mixParents | 0;
var mixWordCount = params.mixSize >> 2;
var mixNodeCount = mixWordCount >> 4;
var dagPageCount = (params.dagSize / params.mixSize) >> 0;
// grab initial first word
var s0 = mix[0];
// initialise mix from initial 64 bytes
for (var w = 16; w < mixWordCount; ++w) {
mix[w] = mix[w & 15];
}
for (var a = 0; a < mixParents; ++a) {
var p = mod32(fnv(s0 ^ a, mix[a & (mixWordCount - 1)]), dagPageCount);
var d = (p * mixNodeCount) | 0;
for (var n = 0, w = 0; n < mixNodeCount; ++n, w += 16) {
computeDagNode(tempNode, params, cache, keccak, (d + n) | 0);
for (var v = 0; v < 16; ++v) {
mix[w | v] = fnv(mix[w | v], tempNode[v]);
}
}
}
}
function convertSeed(seed)
{
// todo, reconcile with spec, byte ordering?
// todo, big-endian conversion
var newSeed = util.toWords(seed);
if (newSeed === null)
throw Error("Invalid seed '" + seed + "'");
return newSeed;
function convertSeed(seed) {
// todo, reconcile with spec, byte ordering?
// todo, big-endian conversion
var newSeed = util.toWords(seed);
if (newSeed === null)
throw Error("Invalid seed '" + seed + "'");
return newSeed;
}
exports.defaultParams = function()
{
return {
cacheSize: 1048384,
cacheRounds: 3,
dagSize: 1073739904,
dagParents: 256,
mixSize: 128,
mixParents: 64,
};
var params = exports.params = {
REVISION: 23,
DATASET_BYTES_INIT: 1073741824,
DATASET_BYTES_GROWTH: 8388608,
CACHE_BYTES_INIT: 1073741824,
CACHE_BYTES_GROWTH: 131072,
EPOCH_LENGTH: 30000,
MIX_BYTES: 128,
HASH_BYTES: 64,
DATASET_PARENTS: 256,
CACHE_ROUNDS: 3,
ACCESSES: 64
};
var cache_sizes = require('./cache_sizes');
var dag_sizes = require('./dag_sizes');
exports.calcSeed = function(blockNum) {
var epoch;
var seed = new Uint8Array(32);
if (blockNum > cache_sizes.length * params.EPOCH_LENGTH) {
return new Error('Time to upgrade to POS!!!');
} else {
epoch = Math.floor(blockNum / params.EPOCH_LENGTH);
for (var i = 0; i < epoch; i++) {
seed = ethUtil.sha3(new Buffer(seed));
}
return seed;
}
};
exports.Ethash = function(params, seed)
{
// precompute cache and related values
seed = convertSeed(seed);
var cache = computeCache(params, seed);
// preallocate buffers/etc
var initBuf = new ArrayBuffer(96);
var initBytes = new Uint8Array(initBuf);
var initWords = new Uint32Array(initBuf);
var mixWords = new Uint32Array(params.mixSize / 4);
var tempNode = new Uint32Array(16);
var keccak = new Keccak();
var retWords = new Uint32Array(8);
var retBytes = new Uint8Array(retWords.buffer); // supposedly read-only
this.hash = function(header, nonce)
{
// compute initial hash
initBytes.set(header, 0);
initBytes.set(nonce, 32);
keccak.digestWords(initWords, 0, 16, initWords, 0, 8 + nonce.length/4);
// compute mix
for (var i = 0; i != 16; ++i)
{
mixWords[i] = initWords[i];
}
computeHashInner(mixWords, params, cache, keccak, tempNode);
// compress mix and append to initWords
for (var i = 0; i != mixWords.length; i += 4)
{
initWords[16 + i/4] = fnv(fnv(fnv(mixWords[i], mixWords[i+1]), mixWords[i+2]), mixWords[i+3]);
}
// final Keccak hashes
keccak.digestWords(retWords, 0, 8, initWords, 0, 24); // Keccak-256(s + cmix)
return retBytes;
};
this.cacheDigest = function()
{
return keccak.digest(32, new Uint8Array(cache.buffer));
};
exports.defaultParams = function() {
return {
cacheSize: 1048384,
cacheRounds: 3,
dagSize: 1073739904,
dagParents: 256,
mixSize: 128,
mixParents: 64
};
};
exports.Ethash = function(params, seed) {
// precompute cache and related values
// seed = convertSeed(seed);
var cache = computeCache(params, seed);
// preallocate buffers/etc
var initBuf = new ArrayBuffer(96);
var initBytes = new Uint8Array(initBuf);
var initWords = new Uint32Array(initBuf);
var mixWords = new Uint32Array(params.mixSize / 4);
var tempNode = new Uint32Array(16);
var keccak = new Keccak();
var retWords = new Uint32Array(8);
var retBytes = new Uint8Array(retWords.buffer); // supposedly read-only
this.hash = function(header, nonce) {
// compute initial hash
initBytes.set(header, 0);
initBytes.set(nonce, 32);
keccak.digestWords(initWords, 0, 16, initWords, 0, 8 + nonce.length / 4);
// compute mix
for (var i = 0; i !== 16; ++i) {
mixWords[i] = initWords[i];
}
computeHashInner(mixWords, params, cache, keccak, tempNode);
// compress mix and append to initWords
for (var i = 0; i !== mixWords.length; i += 4) {
initWords[16 + i / 4] = fnv(fnv(fnv(mixWords[i], mixWords[i + 1]), mixWords[i + 2]), mixWords[i + 3]);
}
// final Keccak hashes
keccak.digestWords(retWords, 0, 8, initWords, 0, 24); // Keccak-256(s + cmix)
return retBytes;
};
this.cacheDigest = function() {
return keccak.digest(32, new Uint8Array(cache.buffer));
};
};
{
"name": "ethash.js",
"version": "0.0.1",
"description": "",
"main": "ethash.js",
"scripts": {
"test": "node ./test/test.js"
},
"repository": {
"type": "git",
"url": "https://github.com/ethereum/ethash/tree/master/js"
},
"keywords": [
"ethereum"
],
"author": "",
"license": "mit",
"devDependencies": {
"ethereum-tests": "0.0.5"
}
}
var tape = require('tape');
const ethash = require('../ethash.js');
tape('seed hash', function(t) {
t.test('seed should match TRUTH', function(st) {
const seed = '290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563';
const blockNum = 30000;
var r = new Buffer(ethash.calcSeed(blockNum));
st.equal(r.toString('hex'), seed);
st.end();
});
t.test('seed should match TRUTH2', function(st) {
const seed = '510e4e770828ddbf7f7b00ab00a9f6adaf81c0dc9cc85f1f8249c256942d61d9';
const blockNum = 60000;
var r = new Buffer(ethash.calcSeed(blockNum));
st.equal(r.toString('hex'), seed);
st.end();
});
t.test('seed should match TRUTH3', function(st) {
const seed = '510e4e770828ddbf7f7b00ab00a9f6adaf81c0dc9cc85f1f8249c256942d61d9';
const blockNum = 60700;
var r = new Buffer(ethash.calcSeed(blockNum));
st.equal(r.toString('hex'), seed);
st.end();
});
t.test('randomized tests', function(st) {
for (var i = 0; i < 100; i++) {
var x = Math.floor(ethash.params.EPOCH_LENGTH * 2048 * Math.random());
st.equal(ethash.calcSeed(x).toString('hex'), ethash.calcSeed(Math.floor(x / ethash.params.EPOCH_LENGTH) * ethash.params.EPOCH_LENGTH ).toString('hex'));
}
st.end();
});
// '510e4e770828ddbf7f7b00ab00a9f6adaf81c0dc9cc85f1f8249c256942d61d9'
// [7:13:32 PM] Matthew Wampler-Doty: >>> x = randint(0,700000)
//
// >>> pyethash.get_seedhash(x).encode('hex') == pyethash.get_seedhash((x // pyethash.EPOCH_LENGTH) * pyethash.EPOCH_LENGTH).encode('hex')
});
......@@ -4,9 +4,9 @@
/*jslint node: true, shadow:true */
"use strict";
var ethash = require('./ethash');
var util = require('./util');
var Keccak = require('./keccak');
var ethash = require('../ethash');
var util = require('../util');
var Keccak = require('../keccak');
// sanity check hash functions
var src = util.stringToBytes("");
......@@ -31,23 +31,22 @@ var ethashParams = ethash.defaultParams();
var seed = util.hexStringToBytes("9410b944535a83d9adf6bbdcc80e051f30676173c16ca0d32d6f1263fc246466")
var startTime = new Date().getTime();
var hasher = new ethash.Ethash(ethashParams, seed);
console.log('Ethash startup took: '+(new Date().getTime() - startTime) + "ms");
console.log('Ethash startup took: ' + (new Date().getTime() - startTime) + "ms");
console.log('Ethash cache hash: ' + util.bytesToHexString(hasher.cacheDigest()));
var testHexString = "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470";
if (testHexString != util.bytesToHexString(util.hexStringToBytes(testHexString)))
throw Error("bytesToHexString or hexStringToBytes broken");
throw Error("bytesToHexString or hexStringToBytes broken");
var header = util.hexStringToBytes("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470");
var nonce = util.hexStringToBytes("0000000000000000");
var hash;
startTime = new Date().getTime();
var trials = 10;
for (var i = 0; i < trials; ++i)
{
hash = hasher.hash(header, nonce);
for (var i = 0; i < trials; ++i) {
hash = hasher.hash(header, nonce);
}
console.log("Light client hashes averaged: " + (new Date().getTime() - startTime)/trials + "ms");
console.log("Light client hashes averaged: " + (new Date().getTime() - startTime) / trials + "ms");
console.log("Hash = " + util.bytesToHexString(hash));
......@@ -25,9 +25,9 @@ setup (
author = "Matthew Wampler-Doty",
author_email = "matthew.wampler.doty@gmail.com",
license = 'GPL',
version = '23',
version = '23.1',
url = 'https://github.com/ethereum/ethash',
download_url = 'https://github.com/ethereum/ethash/tarball/v23',
download_url = 'https://github.com/ethereum/ethash/tarball/v23.1',
description = 'Python wrappers for ethash, the ethereum proof of work hashing function',
ext_modules = [pyethash],
)
......@@ -48,7 +48,7 @@ extern "C" {
// Sow[i*HashBytes]; j++]]]][[2]][[1]]
static const size_t dag_sizes[2048] = {
static const uint64_t dag_sizes[2048] = {
1073739904U, 1082130304U, 1090514816U, 1098906752U, 1107293056U,
1115684224U, 1124070016U, 1132461952U, 1140849536U, 1149232768U,
1157627776U, 1166013824U, 1174404736U, 1182786944U, 1191180416U,
......@@ -477,7 +477,7 @@ static const size_t dag_sizes[2048] = {
// While[! PrimeQ[i], i--];
// Sow[i*HashBytes]; j++]]]][[2]][[1]]
const size_t cache_sizes[2048] = {
const uint64_t cache_sizes[2048] = {
16776896U, 16907456U, 17039296U, 17170112U, 17301056U, 17432512U, 17563072U,
17693888U, 17824192U, 17955904U, 18087488U, 18218176U, 18349504U, 18481088U,
18611392U, 18742336U, 18874304U, 19004224U, 19135936U, 19267264U, 19398208U,
......
......@@ -43,8 +43,8 @@ extern "C" {
#endif
typedef struct ethash_params {
size_t full_size; // Size of full data set (in bytes, multiple of mix size (128)).
size_t cache_size; // Size of compute cache (in bytes, multiple of node size (64)).
uint64_t full_size; // Size of full data set (in bytes, multiple of mix size (128)).
uint64_t cache_size; // Size of compute cache (in bytes, multiple of node size (64)).
} ethash_params;
typedef struct ethash_return_value {
......@@ -52,45 +52,52 @@ typedef struct ethash_return_value {
uint8_t mix_hash[32];
} ethash_return_value;
size_t ethash_get_datasize(const uint32_t block_number);
size_t ethash_get_cachesize(const uint32_t block_number);
uint64_t ethash_get_datasize(const uint32_t block_number);
uint64_t ethash_get_cachesize(const uint32_t block_number);
// initialize the parameters
static inline void ethash_params_init(ethash_params *params, const uint32_t block_number) {
// Initialize the Parameters
static inline int ethash_params_init(ethash_params *params, const uint32_t block_number) {
params->full_size = ethash_get_datasize(block_number);
if (params->full_size == 0)
return 0;
params->cache_size = ethash_get_cachesize(block_number);
if (params->cache_size == 0)
return 0;
return 1;
}
typedef struct ethash_cache {
void *mem;
} ethash_cache;
void ethash_mkcache(ethash_cache *cache, ethash_params const *params, const uint8_t seed[32]);
void ethash_compute_full_data(void *mem, ethash_params const *params, ethash_cache const *cache);
void ethash_full(ethash_return_value *ret, void const *full_mem, ethash_params const *params, const uint8_t header_hash[32], const uint64_t nonce);
void ethash_light(ethash_return_value *ret, ethash_cache const *cache, ethash_params const *params, const uint8_t header_hash[32], const uint64_t nonce);
int ethash_mkcache(ethash_cache *cache, ethash_params const *params, const uint8_t seed[32]);
int ethash_compute_full_data(void *mem, ethash_params const *params, ethash_cache const *cache);
int ethash_full(ethash_return_value *ret, void const *full_mem, ethash_params const *params, const uint8_t header_hash[32], const uint64_t nonce);
int ethash_light(ethash_return_value *ret, ethash_cache const *cache, ethash_params const *params, const uint8_t header_hash[32], const uint64_t nonce);
void ethash_get_seedhash(uint8_t seedhash[32], const uint32_t block_number);
static inline void ethash_prep_light(void *cache, ethash_params const *params, const uint8_t seed[32]) {
static inline int ethash_prep_light(void *cache, ethash_params const *params, const uint8_t seed[32]) {
ethash_cache c;
c.mem = cache;
ethash_mkcache(&c, params, seed);
return ethash_mkcache(&c, params, seed);
}
static inline void ethash_compute_light(ethash_return_value *ret, void const *cache, ethash_params const *params, const uint8_t header_hash[32], const uint64_t nonce) {
static inline int ethash_compute_light(ethash_return_value *ret, void const *cache, ethash_params const *params, const uint8_t header_hash[32], const uint64_t nonce) {
ethash_cache c;
c.mem = (void *) cache;
ethash_light(ret, &c, params, header_hash, nonce);
return ethash_light(ret, &c, params, header_hash, nonce);
}
static inline void ethash_prep_full(void *full, ethash_params const *params, void const *cache) {
static inline int ethash_prep_full(void *full, ethash_params const *params, void const *cache) {
ethash_cache c;
c.mem = (void *) cache;
ethash_compute_full_data(full, params, &c);
return ethash_compute_full_data(full, params, &c);
}
static inline void ethash_compute_full(ethash_return_value *ret, void const *full, ethash_params const *params, const uint8_t header_hash[32], const uint64_t nonce) {
ethash_full(ret, full, params, header_hash, nonce);
static inline int ethash_compute_full(ethash_return_value *ret, void const *full, ethash_params const *params, const uint8_t header_hash[32], const uint64_t nonce) {
return ethash_full(ret, full, params, header_hash, nonce);
}
// Returns if hash is less than or equal to difficulty
......
......@@ -20,7 +20,6 @@
* @date 2015
*/
#include <assert.h>
#include <inttypes.h>
#include <stddef.h>
#include "ethash.h"
......@@ -29,6 +28,9 @@
#include "internal.h"
#include "data_sizes.h"
// Inline assembly doesn't work
#define ENABLE_SSE 0
#ifdef WITH_CRYPTOPP
#include "sha3_cryptopp.h"
......@@ -37,24 +39,29 @@
#include "sha3.h"
#endif // WITH_CRYPTOPP
size_t ethash_get_datasize(const uint32_t block_number) {
assert(block_number / EPOCH_LENGTH < 2048);
uint64_t ethash_get_datasize(const uint32_t block_number) {
if (block_number / EPOCH_LENGTH >= 2048)
return 0;
return dag_sizes[block_number / EPOCH_LENGTH];
}
size_t ethash_get_cachesize(const uint32_t block_number) {
assert(block_number / EPOCH_LENGTH < 2048);
uint64_t ethash_get_cachesize(const uint32_t block_number) {
if (block_number / EPOCH_LENGTH >= 2048)
return 0;
return cache_sizes[block_number / EPOCH_LENGTH];
}
// Follows Sergio's "STRICT MEMORY HARD HASHING FUNCTIONS" (2014)
// https://bitslog.files.wordpress.com/2013/12/memohash-v0-3.pdf
// SeqMemoHash(s, R, N)
void static ethash_compute_cache_nodes(
int static ethash_compute_cache_nodes(
node *const nodes,
ethash_params const *params,
const uint8_t seed[32]) {
assert((params->cache_size % sizeof(node)) == 0);
if ((params->cache_size % sizeof(node)) != 0)
return 0;
uint32_t const num_nodes = (uint32_t) (params->cache_size / sizeof(node));
SHA3_512(nodes[0].bytes, seed, 32);
......@@ -82,22 +89,27 @@ void static ethash_compute_cache_nodes(
nodes->words[w] = fix_endian32(nodes->words[w]);
}
#endif
return 1;
}
void ethash_mkcache(
int ethash_mkcache(
ethash_cache *cache,
ethash_params const *params,
const uint8_t seed[32]) {
node *nodes = (node *) cache->mem;
ethash_compute_cache_nodes(nodes, params, seed);
return ethash_compute_cache_nodes(nodes, params, seed);
}
void ethash_calculate_dag_item(
int ethash_calculate_dag_item(
node *const ret,
const unsigned node_index,
const uint64_t node_index,
const struct ethash_params *params,
const struct ethash_cache *cache) {
if (params->cache_size % sizeof(node) != 0)
return 0;
uint32_t num_parent_nodes = (uint32_t) (params->cache_size / sizeof(node));
node const *cache_nodes = (node const *) cache->mem;
node const *init = &cache_nodes[node_index % num_parent_nodes];
......@@ -145,23 +157,58 @@ void ethash_calculate_dag_item(
}
SHA3_512(ret->bytes, ret->bytes, sizeof(node));
return 1;
}
void ethash_compute_full_data(
int ethash_compute_full_data(
void *mem,
ethash_params const *params,
ethash_cache const *cache) {
assert((params->full_size % (sizeof(uint32_t) * MIX_WORDS)) == 0);
assert((params->full_size % sizeof(node)) == 0);
if ((params->full_size % (sizeof(uint32_t) * MIX_WORDS)) != 0)
return 0;
if ((params->full_size % sizeof(node)) != 0)
return 0;
node *full_nodes = mem;
// now compute full nodes
for (uint64_t n = 0; n != (params->full_size / sizeof(node)); ++n) {
ethash_calculate_dag_item(&(full_nodes[n]), n, params, cache);
}
return 1;
}
int ethash_compute_full_data_section(
void *mem,
ethash_params const *params,
ethash_cache const *cache,
uint64_t const start,
uint64_t const end) {
if ((params->full_size % (sizeof(uint32_t) * MIX_WORDS)) != 0)
return 0;
if ((params->full_size % sizeof(node)) != 0)
return 0;
if (end >= params->full_size)
return 0;
if (start >= end)
return 0;
node *full_nodes = mem;
// now compute full nodes
for (unsigned n = 0; n != (params->full_size / sizeof(node)); ++n) {
for (uint64_t n = start; n != end; ++n) {
ethash_calculate_dag_item(&(full_nodes[n]), n, params, cache);
}
return 1;
}
static void ethash_hash(
static int ethash_hash(
ethash_return_value *ret,
node const *full_nodes,
ethash_cache const *cache,
......@@ -169,10 +216,10 @@ static void ethash_hash(
const uint8_t header_hash[32],
const uint64_t nonce) {
assert((params->full_size % MIX_WORDS) == 0);
if ((params->full_size % MIX_WORDS) != 0)
return 0;
// pack hash and nonce together into first 40 bytes of s_mix
assert(sizeof(node) * 8 == 512);
node s_mix[MIX_NODES + 1];
memcpy(s_mix[0].bytes, header_hash, 32);
......@@ -254,6 +301,7 @@ static void ethash_hash(
memcpy(ret->mix_hash, mix->bytes, 32);
// final Keccak hash
SHA3_256(ret->result, s_mix->bytes, 64 + 32); // Keccak-256(s + compressed_mix)
return 1;
}
void ethash_quick_hash(
......@@ -291,10 +339,10 @@ int ethash_quick_check_difficulty(
return ethash_check_difficulty(return_hash, difficulty);
}
void ethash_full(ethash_return_value *ret, void const *full_mem, ethash_params const *params, const uint8_t previous_hash[32], const uint64_t nonce) {
ethash_hash(ret, (node const *) full_mem, NULL, params, previous_hash, nonce);
int ethash_full(ethash_return_value *ret, void const *full_mem, ethash_params const *params, const uint8_t previous_hash[32], const uint64_t nonce) {
return ethash_hash(ret, (node const *) full_mem, NULL, params, previous_hash, nonce);
}
void ethash_light(ethash_return_value *ret, ethash_cache const *cache, ethash_params const *params, const uint8_t previous_hash[32], const uint64_t nonce) {
ethash_hash(ret, NULL, cache, params, previous_hash, nonce);
int ethash_light(ethash_return_value *ret, ethash_cache const *cache, ethash_params const *params, const uint8_t previous_hash[32], const uint64_t nonce) {
return ethash_hash(ret, NULL, cache, params, previous_hash, nonce);
}
\ No newline at end of file
......@@ -30,9 +30,9 @@ typedef union node {
} node;
void ethash_calculate_dag_item(
int ethash_calculate_dag_item(
node *const ret,
const unsigned node_index,
const uint64_t node_index,
ethash_params const *params,
ethash_cache const *cache
);
......
......@@ -58,7 +58,7 @@ mkcache_bytes(PyObject *self, PyObject *args) {
}
ethash_params params;
params.cache_size = (size_t) cache_size;
params.cache_size = (uint64_t) cache_size;
ethash_cache cache;
cache.mem = malloc(cache_size);
ethash_mkcache(&cache, &params, (uint8_t *) seed);
......@@ -92,8 +92,8 @@ calc_dataset_bytes(PyObject *self, PyObject *args) {
}
ethash_params params;
params.cache_size = (size_t) cache_size;
params.full_size = (size_t) full_size;
params.cache_size = (uint64_t) cache_size;
params.full_size = (uint64_t) full_size;
ethash_cache cache;
cache.mem = (void *) cache_bytes;
void *mem = malloc(params.full_size);
......@@ -138,8 +138,8 @@ hashimoto_light(PyObject *self, PyObject *args) {
ethash_return_value out;
ethash_params params;
params.cache_size = (size_t) cache_size;
params.full_size = (size_t) full_size;
params.cache_size = (uint64_t) cache_size;
params.full_size = (uint64_t) full_size;
ethash_cache cache;
cache.mem = (void *) cache_bytes;
ethash_light(&out, &cache, &params, (uint8_t *) header, nonce);
......@@ -175,7 +175,7 @@ hashimoto_full(PyObject *self, PyObject *args) {
ethash_return_value out;
ethash_params params;
params.full_size = (size_t) full_size;
params.full_size = (uint64_t) full_size;
ethash_full(&out, (void *) full_bytes, &params, (uint8_t *) header, nonce);
return Py_BuildValue("{s:s#, s:s#}",
"mix digest", out.mix_hash, 32,
......@@ -216,7 +216,7 @@ mine(PyObject *self, PyObject *args) {
ethash_return_value out;
ethash_params params;
params.full_size = (size_t) full_size;
params.full_size = (uint64_t) full_size;
// TODO: Multi threading?
do {
......
......@@ -17,7 +17,7 @@
#include <boost/test/unit_test.hpp>
#include <iostream>
std::string bytesToHexString(const uint8_t *str, const size_t s) {
std::string bytesToHexString(const uint8_t *str, const uint32_t s) {
std::ostringstream ret;
for (int i = 0; i < s; ++i)
......@@ -80,9 +80,11 @@ BOOST_AUTO_TEST_CASE(ethash_params_init_genesis_check) {
BOOST_AUTO_TEST_CASE(ethash_params_init_genesis_calcifide_check) {
ethash_params params;
ethash_params_init(&params, 0);
const uint32_t expected_full_size = 1073739904;
const uint32_t expected_cache_size = 16776896;
BOOST_REQUIRE_MESSAGE(ethash_params_init(&params, 0),
"Params could not be initialized");
const uint32_t
expected_full_size = 1073739904,
expected_cache_size = 16776896;
BOOST_REQUIRE_MESSAGE(params.full_size == expected_full_size,
"\nexpected: " << expected_cache_size << "\n"
<< "actual: " << params.full_size << "\n");
......
......@@ -14,11 +14,8 @@ TEST_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
echo -e "\n################# Testing JS ##################"
# TODO: Use mocha and real testing tools instead of rolling our own
cd $TEST_DIR/../js
if [ -x "$(which nodejs)" ] ; then
nodejs test.js
fi
if [ -x "$(which node)" ] ; then
node test.js
if [ -x "$(which npm)" ] ; then
npm test
fi
echo -e "\n################# Testing C ##################"
......
......@@ -42,7 +42,7 @@ import (
const (
ClientIdentifier = "Ethereum(G)"
Version = "Frontier - 0.9.1"
Version = "0.9.1"
)
var (
......
......@@ -24,7 +24,6 @@ package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
......@@ -210,7 +209,7 @@ func RunVmTest(r io.Reader) (failed int) {
}
if failed == 1 {
fmt.Println(string(statedb.Dump()))
helper.Log.Infoln(string(statedb.Dump()))
}
logger.Flush()
......
......@@ -104,7 +104,7 @@ func BigCopy(src *big.Int) *big.Int {
//
// Returns the maximum size big integer
func BigMax(x, y *big.Int) *big.Int {
if x.Cmp(y) <= 0 {
if x.Cmp(y) < 0 {
return y
}
......@@ -115,7 +115,7 @@ func BigMax(x, y *big.Int) *big.Int {
//
// Returns the minimum size big integer
func BigMin(x, y *big.Int) *big.Int {
if x.Cmp(y) >= 0 {
if x.Cmp(y) > 0 {
return y
}
......
......@@ -35,10 +35,7 @@ func (h *Hash) SetBytes(b []byte) {
b = b[len(b)-hashLength:]
}
// reverse loop
for i := len(b) - 1; i >= 0; i-- {
h[hashLength-len(b)+i] = b[i]
}
copy(h[hashLength-len(b):], b)
}
// Set string `s` to h. If s is larger than len(h) it will panic
......@@ -73,11 +70,7 @@ func (a *Address) SetBytes(b []byte) {
if len(b) > len(a) {
b = b[len(b)-addressLength:]
}
// reverse loop
for i := len(b) - 1; i >= 0; i-- {
a[addressLength-len(b)+i] = b[i]
}
copy(a[addressLength-len(b):], b)
}
// Set string `s` to a. If s is larger than len(a) it will panic
......
......@@ -16,10 +16,6 @@ import (
"gopkg.in/fatih/set.v0"
)
type PendingBlockEvent struct {
Block *types.Block
}
var statelogger = logger.NewLogger("BLOCK")
type BlockProcessor struct {
......@@ -137,7 +133,7 @@ func (self *BlockProcessor) ApplyTransactions(coinbase *state.StateObject, state
block.Header().GasUsed = totalUsedGas
if transientProcess {
go self.eventMux.Post(PendingBlockEvent{block})
go self.eventMux.Post(PendingBlockEvent{block, statedb.Logs()})
}
return receipts, handled, unhandled, erroneous, err
......@@ -146,25 +142,25 @@ func (self *BlockProcessor) ApplyTransactions(coinbase *state.StateObject, state
// Process block will attempt to process the given block's transactions and applies them
// on top of the block's parent state (given it exists) and will return wether it was
// successful or not.
func (sm *BlockProcessor) Process(block *types.Block) (td *big.Int, err error) {
func (sm *BlockProcessor) Process(block *types.Block) (td *big.Int, logs state.Logs, err error) {
// Processing a blocks may never happen simultaneously
sm.mutex.Lock()
defer sm.mutex.Unlock()
header := block.Header()
if sm.bc.HasBlock(header.Hash()) {
return nil, &KnownBlockError{header.Number, header.Hash()}
return nil, nil, &KnownBlockError{header.Number, header.Hash()}
}
if !sm.bc.HasBlock(header.ParentHash) {
return nil, ParentError(header.ParentHash)
return nil, nil, ParentError(header.ParentHash)
}
parent := sm.bc.GetBlock(header.ParentHash)
return sm.processWithParent(block, parent)
}
func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big.Int, err error) {
func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big.Int, logs state.Logs, err error) {
sm.lastAttemptedBlock = block
// Create a new state based on the parent's root (e.g., create copy)
......@@ -177,7 +173,7 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big
// There can be at most two uncles
if len(block.Uncles()) > 2 {
return nil, ValidationError("Block can only contain one uncle (contained %v)", len(block.Uncles()))
return nil, nil, ValidationError("Block can only contain one uncle (contained %v)", len(block.Uncles()))
}
receipts, err := sm.TransitionState(state, parent, block, false)
......@@ -236,7 +232,7 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big
chainlogger.Infof("processed block #%d (%x...)\n", header.Number, block.Hash().Bytes()[0:4])
return td, nil
return td, state.Logs(), nil
}
// Validates the current block. Returns an error if the block was invalid,
......
......@@ -93,7 +93,7 @@ func makeChain(bman *BlockProcessor, parent *types.Block, max int, db common.Dat
blocks := make(types.Blocks, max)
for i := 0; i < max; i++ {
block := makeBlock(bman, parent, i, db, seed)
td, err := bman.processWithParent(block, parent)
td, _, err := bman.processWithParent(block, parent)
if err != nil {
fmt.Println("process with parent failed", err)
panic(err)
......
......@@ -410,7 +410,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error {
for i, block := range chain {
// Call in to the block processor and check for errors. It's likely that if one block fails
// all others will fail too (unless a known block is returned).
td, err := self.processor.Process(block)
td, logs, err := self.processor.Process(block)
if err != nil {
if IsKnownBlockErr(err) {
continue
......@@ -438,29 +438,27 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error {
hash := block.Hash()
chainlogger.Infof("Split detected. New head #%v (%x) TD=%v, was #%v (%x) TD=%v\n", block.Header().Number, hash[:4], td, cblock.Header().Number, chash[:4], self.td)
queue[i] = ChainSplitEvent{block}
queue[i] = ChainSplitEvent{block, logs}
queueEvent.splitCount++
}
self.setTotalDifficulty(td)
self.insert(block)
/* XXX crashes
jsonlogger.LogJson(&logger.EthChainNewHead{
BlockHash: common.Bytes2Hex(block.Hash()),
BlockHash: block.Hash().Hex(),
BlockNumber: block.Number(),
ChainHeadHash: common.Bytes2Hex(cblock.Hash()),
BlockPrevHash: common.Bytes2Hex(block.ParentHash()),
ChainHeadHash: cblock.Hash().Hex(),
BlockPrevHash: block.ParentHash().Hex(),
})
*/
self.setTransState(state.New(block.Root(), self.stateDb))
self.setTxState(state.New(block.Root(), self.stateDb))
queue[i] = ChainEvent{block}
queue[i] = ChainEvent{block, logs}
queueEvent.canonicalCount++
} else {
queue[i] = ChainSideEvent{block}
queue[i] = ChainSideEvent{block, logs}
queueEvent.sideCount++
}
}
......@@ -468,7 +466,6 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error {
}
// XXX put this in a goroutine?
go self.eventMux.Post(queueEvent)
return nil
......
package core
import "github.com/ethereum/go-ethereum/core/types"
import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/state"
)
// TxPreEvent is posted when a transaction enters the transaction pool.
type TxPreEvent struct{ Tx *types.Transaction }
......@@ -15,11 +18,25 @@ type NewBlockEvent struct{ Block *types.Block }
type NewMinedBlockEvent struct{ Block *types.Block }
// ChainSplit is posted when a new head is detected
type ChainSplitEvent struct{ Block *types.Block }
type ChainEvent struct{ Block *types.Block }
type ChainSideEvent struct{ Block *types.Block }
type ChainSplitEvent struct {
Block *types.Block
Logs state.Logs
}
type ChainEvent struct {
Block *types.Block
Logs state.Logs
}
type ChainSideEvent struct {
Block *types.Block
Logs state.Logs
}
type PendingBlockEvent struct {
Block *types.Block
Logs state.Logs
}
type ChainHeadEvent struct{ Block *types.Block }
......
......@@ -33,8 +33,8 @@ type Filter struct {
max int
topics [][]common.Hash
BlockCallback func(*types.Block)
PendingCallback func(*types.Block)
BlockCallback func(*types.Block, state.Logs)
PendingCallback func(*types.Block, state.Logs)
LogsCallback func(state.Logs)
}
......
package types
import (
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/state"
"fmt"
)
type BlockProcessor interface {
Process(*Block) (*big.Int, error)
Process(*Block) (*big.Int, state.Logs, error)
}
const bloomLength = 256
......@@ -26,10 +28,7 @@ func (b *Bloom) SetBytes(d []byte) {
panic(fmt.Sprintf("bloom bytes too big %d %d", len(b), len(d)))
}
// reverse loop
for i := len(d) - 1; i >= 0; i-- {
b[bloomLength-len(d)+i] = b[i]
}
copy(b[bloomLength-len(d):], d)
}
func (b Bloom) Big() *big.Int {
......
......@@ -63,7 +63,7 @@ func (self *FilterManager) filterLoop() {
// Subscribe to events
events := self.eventMux.Subscribe(
core.PendingBlockEvent{},
//core.ChainEvent{},
core.ChainEvent{},
state.Logs(nil))
out:
......@@ -77,7 +77,7 @@ out:
self.filterMu.RLock()
for _, filter := range self.filters {
if filter.BlockCallback != nil {
filter.BlockCallback(event.Block)
filter.BlockCallback(event.Block, event.Logs)
}
}
self.filterMu.RUnlock()
......@@ -86,7 +86,7 @@ out:
self.filterMu.RLock()
for _, filter := range self.filters {
if filter.PendingCallback != nil {
filter.PendingCallback(event.Block)
filter.PendingCallback(event.Block, event.Logs)
}
}
self.filterMu.RUnlock()
......
......@@ -86,7 +86,7 @@ func (self *EthereumApi) getStateWithNum(num int64) *xeth.State {
}
func (self *EthereumApi) start() {
timer := time.NewTicker(filterTickerTime)
timer := time.NewTicker(2 * time.Second)
done:
for {
select {
......@@ -94,20 +94,20 @@ done:
self.logMut.Lock()
self.messagesMut.Lock()
for id, filter := range self.logs {
if time.Since(filter.timeout) > 20*time.Second {
if time.Since(filter.timeout) > filterTickerTime {
self.filterManager.UninstallFilter(id)
delete(self.logs, id)
}
}
for id, filter := range self.messages {
if time.Since(filter.timeout) > 20*time.Second {
if time.Since(filter.timeout) > filterTickerTime {
self.xeth().Whisper().Unwatch(id)
delete(self.messages, id)
}
}
self.logMut.Unlock()
self.messagesMut.Unlock()
self.logMut.Unlock()
case <-self.quit:
break done
}
......@@ -161,7 +161,7 @@ func (self *EthereumApi) NewFilter(args *FilterOptions, reply *interface{}) erro
id = self.filterManager.InstallFilter(filter)
self.logs[id] = &logFilter{timeout: time.Now()}
*reply = i2hex(id)
*reply = common.ToHex(big.NewInt(int64(id)).Bytes())
return nil
}
......@@ -180,10 +180,13 @@ func (self *EthereumApi) NewFilterString(args *FilterStringArgs, reply *interfac
var id int
filter := core.NewFilter(self.xeth().Backend())
callback := func(block *types.Block) {
callback := func(block *types.Block, logs state.Logs) {
self.logMut.Lock()
defer self.logMut.Unlock()
for _, log := range logs {
self.logs[id].add(log)
}
self.logs[id].add(&state.StateLog{})
}
......@@ -198,7 +201,7 @@ func (self *EthereumApi) NewFilterString(args *FilterStringArgs, reply *interfac
id = self.filterManager.InstallFilter(filter)
self.logs[id] = &logFilter{timeout: time.Now()}
*reply = i2hex(id)
*reply = common.ToHex(big.NewInt(int64(id)).Bytes())
return nil
}
......@@ -257,6 +260,11 @@ func (p *EthereumApi) Transact(args *NewTxArgs, reply *interface{}) (err error)
p.register[ags.From] = append(p.register[args.From], args)
}
*/
if err := args.requirements(); err != nil {
return err
}
// TODO: align default values to have the same type, e.g. not depend on
// common.Value conversions later on
if args.Gas.Cmp(big.NewInt(0)) == 0 {
......
......@@ -8,10 +8,18 @@ import (
"github.com/ethereum/go-ethereum/common"
)
func blockNumber(raw json.RawMessage, number *int64) (err error) {
var str string
if err = json.Unmarshal(raw, &str); err != nil {
return NewDecodeParamError(err.Error())
func blockAge(raw interface{}, number *int64) (err error) {
// Parse as integer
num, ok := raw.(float64)
if ok {
*number = int64(num)
return nil
}
// Parse as string/hexstring
str, ok := raw.(string)
if !ok {
return NewDecodeParamError("BlockNumber is not a string")
}
switch str {
......@@ -22,6 +30,7 @@ func blockNumber(raw json.RawMessage, number *int64) (err error) {
default:
*number = common.String2Big(str).Int64()
}
return nil
}
......@@ -95,18 +104,51 @@ type NewTxArgs struct {
}
func (args *NewTxArgs) UnmarshalJSON(b []byte) (err error) {
var obj struct{ From, To, Value, Gas, GasPrice, Data string }
if err = UnmarshalRawMessages(b, &obj, &args.BlockNumber); err != nil {
return err
var obj []json.RawMessage
var ext struct{ From, To, Value, Gas, GasPrice, Data string }
// Decode byte slice to array of RawMessages
if err := json.Unmarshal(b, &obj); err != nil {
return NewDecodeParamError(err.Error())
}
// Check for sufficient params
if len(obj) < 1 {
return NewInsufficientParamsError(len(obj), 1)
}
// Decode 0th RawMessage to temporary struct
if err := json.Unmarshal(obj[0], &ext); err != nil {
return NewDecodeParamError(err.Error())
}
// var ok bool
args.From = ext.From
args.To = ext.To
args.Value = common.String2Big(ext.Value)
args.Gas = common.String2Big(ext.Gas)
args.GasPrice = common.String2Big(ext.GasPrice)
args.Data = ext.Data
// Check for optional BlockNumber param
if len(obj) > 1 {
var raw interface{}
if err = json.Unmarshal(obj[1], &raw); err != nil {
return NewDecodeParamError(err.Error())
}
if err := blockAge(raw, &args.BlockNumber); err != nil {
return err
}
}
args.From = obj.From
args.To = obj.To
args.Value = common.Big(obj.Value)
args.Gas = common.Big(obj.Gas)
args.GasPrice = common.Big(obj.GasPrice)
args.Data = obj.Data
return nil
}
func (args *NewTxArgs) requirements() error {
if len(args.From) == 0 {
return NewValidationError("From", "Is required")
}
return nil
}
......@@ -116,10 +158,27 @@ type GetStorageArgs struct {
}
func (args *GetStorageArgs) UnmarshalJSON(b []byte) (err error) {
if err = UnmarshalRawMessages(b, &args.Address, &args.BlockNumber); err != nil {
var obj []interface{}
if err := json.Unmarshal(b, &obj); err != nil {
return NewDecodeParamError(err.Error())
}
if len(obj) < 1 {
return NewInsufficientParamsError(len(obj), 1)
}
addstr, ok := obj[0].(string)
if !ok {
return NewDecodeParamError("Address is not a string")
}
args.Address = addstr
if len(obj) > 1 {
if err := blockAge(obj[1], &args.BlockNumber); err != nil {
return err
}
}
return nil
}
......@@ -137,16 +196,32 @@ type GetStorageAtArgs struct {
}
func (args *GetStorageAtArgs) UnmarshalJSON(b []byte) (err error) {
var obj []string
if err = UnmarshalRawMessages(b, &obj, &args.BlockNumber); err != nil {
var obj []interface{}
if err := json.Unmarshal(b, &obj); err != nil {
return NewDecodeParamError(err.Error())
}
if len(obj) < 2 {
return NewInsufficientParamsError(len(obj), 2)
}
args.Address = obj[0]
args.Key = obj[1]
addstr, ok := obj[0].(string)
if !ok {
return NewDecodeParamError("Address is not a string")
}
args.Address = addstr
keystr, ok := obj[1].(string)
if !ok {
return NewDecodeParamError("Key is not a string")
}
args.Key = keystr
if len(obj) > 2 {
if err := blockAge(obj[2], &args.BlockNumber); err != nil {
return err
}
}
return nil
}
......@@ -168,10 +243,27 @@ type GetTxCountArgs struct {
}
func (args *GetTxCountArgs) UnmarshalJSON(b []byte) (err error) {
if err = UnmarshalRawMessages(b, &args.Address, &args.BlockNumber); err != nil {
var obj []interface{}
if err := json.Unmarshal(b, &obj); err != nil {
return NewDecodeParamError(err.Error())
}
if len(obj) < 1 {
return NewInsufficientParamsError(len(obj), 1)
}
addstr, ok := obj[0].(string)
if !ok {
return NewDecodeParamError("Address is not a string")
}
args.Address = addstr
if len(obj) > 1 {
if err := blockAge(obj[1], &args.BlockNumber); err != nil {
return err
}
}
return nil
}
......@@ -189,8 +281,7 @@ type GetBalanceArgs struct {
func (args *GetBalanceArgs) UnmarshalJSON(b []byte) (err error) {
var obj []interface{}
r := bytes.NewReader(b)
if err := json.NewDecoder(r).Decode(&obj); err != nil {
if err := json.Unmarshal(b, &obj); err != nil {
return NewDecodeParamError(err.Error())
}
......@@ -205,17 +296,11 @@ func (args *GetBalanceArgs) UnmarshalJSON(b []byte) (err error) {
args.Address = addstr
if len(obj) > 1 {
if obj[1].(string) == "latest" {
args.BlockNumber = -1
} else {
args.BlockNumber = common.Big(obj[1].(string)).Int64()
if err := blockAge(obj[1], &args.BlockNumber); err != nil {
return err
}
}
// if err = UnmarshalRawMessages(b, &args.Address, &args.BlockNumber); err != nil {
// return NewDecodeParamError(err.Error())
// }
return nil
}
......@@ -232,10 +317,27 @@ type GetDataArgs struct {
}
func (args *GetDataArgs) UnmarshalJSON(b []byte) (err error) {
if err = UnmarshalRawMessages(b, &args.Address, &args.BlockNumber); err != nil {
var obj []interface{}
if err := json.Unmarshal(b, &obj); err != nil {
return NewDecodeParamError(err.Error())
}
if len(obj) < 1 {
return NewInsufficientParamsError(len(obj), 1)
}
addstr, ok := obj[0].(string)
if !ok {
return NewDecodeParamError("Address is not a string")
}
args.Address = addstr
if len(obj) > 1 {
if err := blockAge(obj[1], &args.BlockNumber); err != nil {
return err
}
}
return nil
}
......@@ -392,10 +494,6 @@ func (args *FilterOptions) UnmarshalJSON(b []byte) (err error) {
return nil
}
// type FilterChangedArgs struct {
// n int
// }
type DbArgs struct {
Database string
Key string
......@@ -578,31 +676,3 @@ func (args *WhisperFilterArgs) UnmarshalJSON(b []byte) (err error) {
return nil
}
// func (req *RpcRequest) ToRegisterArgs() (string, error) {
// if len(req.Params) < 1 {
// return "", errArguments
// }
// var args string
// err := json.Unmarshal(req.Params, &args)
// if err != nil {
// return "", err
// }
// return args, nil
// }
// func (req *RpcRequest) ToWatchTxArgs() (string, error) {
// if len(req.Params) < 1 {
// return "", errArguments
// }
// var args string
// err := json.Unmarshal(req.Params, &args)
// if err != nil {
// return "", err
// }
// return args, nil
// }
......@@ -43,6 +43,30 @@ func TestGetBalanceArgs(t *testing.T) {
}
}
func TestGetBalanceArgsLatest(t *testing.T) {
input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "latest"]`
expected := new(GetBalanceArgs)
expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1"
expected.BlockNumber = -1
args := new(GetBalanceArgs)
if err := json.Unmarshal([]byte(input), &args); err != nil {
t.Error(err)
}
if err := args.requirements(); err != nil {
t.Error(err)
}
if args.Address != expected.Address {
t.Errorf("Address should be %v but is %v", expected.Address, args.Address)
}
if args.BlockNumber != expected.BlockNumber {
t.Errorf("BlockNumber should be %v but is %v", expected.BlockNumber, args.BlockNumber)
}
}
func TestGetBalanceEmptyArgs(t *testing.T) {
input := `[]`
......@@ -120,7 +144,8 @@ func TestNewTxArgs(t *testing.T) {
"gas": "0x76c0",
"gasPrice": "0x9184e72a000",
"value": "0x9184e72a000",
"data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"}]`
"data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"},
"0x10"]`
expected := new(NewTxArgs)
expected.From = "0xb60e8dd61c5d32be8058bb8eb970870f07233155"
expected.To = "0xd46e8dd67c5d32be8058bb8eb970870f072445675"
......@@ -128,6 +153,7 @@ func TestNewTxArgs(t *testing.T) {
expected.GasPrice = big.NewInt(10000000000000)
expected.Value = big.NewInt(10000000000000)
expected.Data = "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
expected.BlockNumber = big.NewInt(16).Int64()
args := new(NewTxArgs)
if err := json.Unmarshal([]byte(input), &args); err != nil {
......@@ -157,6 +183,30 @@ func TestNewTxArgs(t *testing.T) {
if expected.Data != args.Data {
t.Errorf("Data shoud be %#v but is %#v", expected.Data, args.Data)
}
if expected.BlockNumber != args.BlockNumber {
t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber)
}
}
func TestNewTxArgsBlockInt(t *testing.T) {
input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155"}, 5]`
expected := new(NewTxArgs)
expected.From = "0xb60e8dd61c5d32be8058bb8eb970870f07233155"
expected.BlockNumber = big.NewInt(5).Int64()
args := new(NewTxArgs)
if err := json.Unmarshal([]byte(input), &args); err != nil {
t.Error(err)
}
if expected.From != args.From {
t.Errorf("From shoud be %#v but is %#v", expected.From, args.From)
}
if expected.BlockNumber != args.BlockNumber {
t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber)
}
}
func TestNewTxArgsEmpty(t *testing.T) {
......@@ -169,6 +219,34 @@ func TestNewTxArgsEmpty(t *testing.T) {
}
}
func TestNewTxArgsReqs(t *testing.T) {
args := new(NewTxArgs)
args.From = "0xb60e8dd61c5d32be8058bb8eb970870f07233155"
err := args.requirements()
switch err.(type) {
case nil:
break
default:
t.Errorf("Get %T", err)
}
}
func TestNewTxArgsReqsFromBlank(t *testing.T) {
args := new(NewTxArgs)
args.From = ""
err := args.requirements()
switch err.(type) {
case nil:
t.Error("Expected error but didn't get one")
case *ValidationError:
break
default:
t.Error("Wrong type of error")
}
}
func TestGetStorageArgs(t *testing.T) {
input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "latest"]`
expected := new(GetStorageArgs)
......
......@@ -17,10 +17,6 @@
package rpc
import (
"encoding/json"
"fmt"
"math/big"
"reflect"
"time"
"github.com/ethereum/go-ethereum/common"
......@@ -31,74 +27,6 @@ import (
var rpclogger = logger.NewLogger("RPC")
// Unmarshal state is a helper method which has the ability to decode messsages
// that use the `defaultBlock` (https://github.com/ethereum/wiki/wiki/JSON-RPC#the-default-block-parameter)
// For example a `call`: [{to: "0x....", data:"0x..."}, "latest"]. The first argument is the transaction
// message and the second one refers to the block height (or state) to which to apply this `call`.
func UnmarshalRawMessages(b []byte, iface interface{}, number *int64) (err error) {
var data []json.RawMessage
if err = json.Unmarshal(b, &data); err != nil && len(data) == 0 {
return NewDecodeParamError(err.Error())
}
// Hrm... Occurs when no params
if len(data) == 0 {
return NewDecodeParamError("No data")
}
// Number index determines the index in the array for a possible block number
numberIndex := 0
value := reflect.ValueOf(iface)
rvalue := reflect.Indirect(value)
switch rvalue.Kind() {
case reflect.Slice:
// This is a bit of a cheat, but `data` is expected to be larger than 2 if iface is a slice
if number != nil {
numberIndex = len(data) - 1
} else {
numberIndex = len(data)
}
slice := reflect.MakeSlice(rvalue.Type(), numberIndex, numberIndex)
for i, raw := range data[0:numberIndex] {
v := slice.Index(i).Interface()
if err = json.Unmarshal(raw, &v); err != nil {
fmt.Println(err, v)
return err
}
slice.Index(i).Set(reflect.ValueOf(v))
}
reflect.Indirect(rvalue).Set(slice) //value.Set(slice)
case reflect.Struct:
fallthrough
default:
if err = json.Unmarshal(data[0], iface); err != nil {
return NewDecodeParamError(err.Error())
}
numberIndex = 1
}
// <0 index means out of bound for block number
if numberIndex >= 0 && len(data) > numberIndex {
if err = blockNumber(data[numberIndex], number); err != nil {
return NewDecodeParamError(err.Error())
}
}
return nil
}
func i2hex(n int) string {
return common.ToHex(big.NewInt(int64(n)).Bytes())
}
type RpcServer interface {
Start()
Stop()
}
type Log struct {
Address string `json:"address"`
Topic []string `json:"topic"`
......
{
"first": {
"nonce": "000000000000002a",
"mixhash": "86d46c9a313b096d66609ef84a334d6c3376d3a5e13b18aa5c666094ae04a1eb",
"header": "f90213a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940000000000000000000000000000000000000000a09178d0f23c965d81f0834a4c72c6253ce6830f4022b1359aaebfc1ecba442d4ea056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000082080080830f4240808080a00000000000000000000000000000000000000000000000000000000000000000a086d46c9a313b096d66609ef84a334d6c3376d3a5e13b18aa5c666094ae04a1eb88000000000000002a",
"nonce": "4242424242424242",
"mixhash": "58f759ede17a706c93f13030328bcea40c1d1341fb26f2facd21ceb0dae57017",
"header": "f901f3a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940000000000000000000000000000000000000000a09178d0f23c965d81f0834a4c72c6253ce6830f4022b1359aaebfc1ecba442d4ea056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000080830f4240808080a058f759ede17a706c93f13030328bcea40c1d1341fb26f2facd21ceb0dae57017884242424242424242",
"seed": "0000000000000000000000000000000000000000000000000000000000000000",
"result": "a91eb868c6262d5456a696bd59cf67191ca7578c04dc72f40c5ece41fb1c3f9f",
"cache_size": 1048384,
"result": "dd47fd2d98db51078356852d7c4014e6a5d6c387c35f40e2875b74a256ed7906",
"cache_size": 16776896,
"full_size": 1073739904,
"header_hash": "f71b596d43b462f63552a6d73a525dc777f172de3e9a023c8a85d3271144038b",
"cache_hash": "86a62f39bc1def6c35b54babdca953425392827c1992538c145bad931c546494"
"header_hash": "2a8de2adf89af77358250bf908bf04ba94a6e8c3ba87775564a41d269a05e4ce",
"cache_hash": "35ded12eecf2ce2e8da2e15c06d463aae9b84cb2530a00b932e4bbc484cde353"
}
}
{
"randomStatetest" : {
"env" : {
"currentCoinbase" : "945304eb96065b2a98b57a48a06ae28d285a71b5",
"currentDifficulty" : "5623894562375",
"currentGasLimit" : "115792089237316195423570985008687907853269984665640564039457584007913129639935",
"currentNumber" : "0",
"currentTimestamp" : "1",
"previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6"
},
"logs" : [
],
"out" : "0x",
"post" : {
"095e7baea6a6c7c4c2dfeb977efac326af552d87" : {
"balance" : "372067975",
"code" : "0x7f00000000000000000000000000000000000000000000000000000000000000007f00000000000000000000000100000000000000000000000000000000000000007f000000000000000000000000000000000000000000000000000000000000c3507f000000000000000000000000ffffffffffffffffffffffffffffffffffffffff7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe457fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f000000000000000000000000945304eb96065b2a98b57a48a06ae28d285a71b50753933760005155",
"nonce" : "0",
"storage" : {
"0x" : "0x010000000000000000000000000000000000000000"
}
},
"945304eb96065b2a98b57a48a06ae28d285a71b5" : {
"balance" : "63798",
"code" : "0x6000355415600957005b60203560003555",
"nonce" : "0",
"storage" : {
}
},
"a94f5374fce5edbc8e2a8697c15331677e6ebf0b" : {
"balance" : "999999999627868273",
"code" : "0x",
"nonce" : "1",
"storage" : {
}
}
},
"postStateRoot" : "3b744e6037075a1c3b6b18ccdf377d3815369ad8b476da92bc6db4b37ae4aa29",
"pre" : {
"095e7baea6a6c7c4c2dfeb977efac326af552d87" : {
"balance" : "0",
"code" : "0x7f00000000000000000000000000000000000000000000000000000000000000007f00000000000000000000000100000000000000000000000000000000000000007f000000000000000000000000000000000000000000000000000000000000c3507f000000000000000000000000ffffffffffffffffffffffffffffffffffffffff7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe457fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f000000000000000000000000945304eb96065b2a98b57a48a06ae28d285a71b50753933760005155",
"nonce" : "0",
"storage" : {
}
},
"945304eb96065b2a98b57a48a06ae28d285a71b5" : {
"balance" : "46",
"code" : "0x6000355415600957005b60203560003555",
"nonce" : "0",
"storage" : {
}
},
"a94f5374fce5edbc8e2a8697c15331677e6ebf0b" : {
"balance" : "1000000000000000000",
"code" : "0x",
"nonce" : "0",
"storage" : {
}
}
},
"transaction" : {
"data" : "0x7f00000000000000000000000000000000000000000000000000000000000000007f00000000000000000000000100000000000000000000000000000000000000007f000000000000000000000000000000000000000000000000000000000000c3507f000000000000000000000000ffffffffffffffffffffffffffffffffffffffff7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe457fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f000000000000000000000000945304eb96065b2a98b57a48a06ae28d285a71b507539337",
"gasLimit" : "0x500ddd76",
"gasPrice" : "1",
"nonce" : "0",
"secretKey" : "45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8",
"to" : "095e7baea6a6c7c4c2dfeb977efac326af552d87",
"value" : "372067975"
}
}
}
......@@ -73,9 +73,10 @@ func toValue(val *big.Int) interface{} {
return val
}
func getData(data []byte, start, size uint64) []byte {
x := uint64(math.Min(float64(start), float64(len(data))))
y := uint64(math.Min(float64(x+size), float64(len(data))))
func getData(data []byte, start, size *big.Int) []byte {
dlen := big.NewInt(int64(len(data)))
return common.RightPadBytes(data[x:y], int(size))
s := common.BigMin(start, dlen)
e := common.BigMin(new(big.Int).Add(s, size), dlen)
return common.RightPadBytes(data[s.Uint64():e.Uint64()], int(size.Uint64()))
}
......@@ -64,10 +64,6 @@ func (c *Context) GetRangeValue(x, size uint64) []byte {
return common.RightPadBytes(c.Code[x:y], int(size))
}
func (c *Context) GetCode(x, size uint64) []byte {
return getData(c.Code, x, size)
}
func (c *Context) Return(ret []byte) []byte {
// Return the remaining gas to the caller
c.caller.ReturnGas(c.Gas, c.Price)
......
......@@ -445,14 +445,11 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) {
cOff = stack.pop()
l = stack.pop()
)
var data []byte
if cOff.Cmp(big.NewInt(int64(len(callData)))) <= 0 {
data = getData(callData, cOff.Uint64(), l.Uint64())
}
data := getData(callData, cOff, l)
mem.Set(mOff.Uint64(), l.Uint64(), data)
self.Printf(" => [%v, %v, %v] %x", mOff, cOff, l, data)
self.Printf(" => [%v, %v, %v]", mOff, cOff, l)
case CODESIZE, EXTCODESIZE:
var code []byte
if op == EXTCODESIZE {
......@@ -482,10 +479,7 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) {
l = stack.pop()
)
var codeCopy []byte
if cOff.Cmp(big.NewInt(int64(len(code)))) <= 0 {
codeCopy = getData(code, cOff.Uint64(), l.Uint64())
}
codeCopy := getData(code, cOff, l)
mem.Set(mOff.Uint64(), l.Uint64(), codeCopy)
......@@ -585,11 +579,11 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) {
self.Printf(" => 0x%x", val)
case MSTORE8:
off, val := stack.pop(), stack.pop()
off, val := stack.pop().Int64(), stack.pop().Int64()
mem.store[off.Int64()] = byte(val.Int64() & 0xff)
mem.store[off] = byte(val & 0xff)
self.Printf(" => [%v] 0x%x", off, val)
self.Printf(" => [%v] 0x%x", off, mem.store[off])
case SLOAD:
loc := common.BigToHash(stack.pop())
val := common.Bytes2Big(statedb.GetState(context.Address(), loc))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment