Browse Source

AlethZero fixes.

cl-refactor
Gav Wood 10 years ago
parent
commit
cfd8274097
  1. 45
      eth/main.cpp
  2. 9
      libethcore/BlockInfo.cpp
  3. 1
      libethcore/BlockInfo.h
  4. 29
      libethcore/EthashAux.cpp
  5. 4
      libethcore/EthashAux.h

45
eth/main.cpp

@ -142,12 +142,10 @@ void help()
<< " -R,--rebuild First rebuild the blockchain from the existing database." << endl << " -R,--rebuild First rebuild the blockchain from the existing database." << endl
<< " -r,--remote <host> Connect to remote host (default: none)." << endl << " -r,--remote <host> Connect to remote host (default: none)." << endl
<< " -s,--secret <secretkeyhex> Set the secret key for use with send command (default: auto)." << endl << " -s,--secret <secretkeyhex> Set the secret key for use with send command (default: auto)." << endl
<< " -S,--temporary-secret <secretkeyhex> Set the secret key for use with send command, for this session only." << endl
<< " -v,--verbosity <0 - 9> Set the log verbosity from 0 to 9 (Default: 8)." << endl << " -v,--verbosity <0 - 9> Set the log verbosity from 0 to 9 (Default: 8)." << endl
<< " -x,--peers <number> Attempt to connect to given number of peers (Default: 5)." << endl << " -x,--peers <number> Attempt to connect to given number of peers (Default: 5)." << endl
<< " -V,--version Show the version and exit." << endl << " -V,--version Show the version and exit." << endl
#if ETH_EVMJIT
<< " --jit Use EVM JIT (default: off)." << endl
#endif
; ;
exit(0); exit(0);
} }
@ -274,8 +272,9 @@ int main(int argc, char** argv)
/// Mining params /// Mining params
unsigned mining = ~(unsigned)0; unsigned mining = ~(unsigned)0;
bool forceMining = false; bool forceMining = false;
KeyPair us = KeyPair::create(); KeyPair sigKey = KeyPair::create();
Address coinbase = us.address(); Secret sessionSecret;
Address coinbase = sigKey.address();
/// Structured logging params /// Structured logging params
bool structuredLogging = false; bool structuredLogging = false;
@ -291,7 +290,7 @@ int main(int argc, char** argv)
if (b.size()) if (b.size())
{ {
RLP config(b); RLP config(b);
us = KeyPair(config[0].toHash<Secret>()); sigKey = KeyPair(config[0].toHash<Secret>());
coinbase = config[1].toHash<Address>(); coinbase = config[1].toHash<Address>();
} }
@ -374,7 +373,9 @@ int main(int argc, char** argv)
return -1; return -1;
} }
else if ((arg == "-s" || arg == "--secret") && i + 1 < argc) else if ((arg == "-s" || arg == "--secret") && i + 1 < argc)
us = KeyPair(h256(fromHex(argv[++i]))); sigKey = KeyPair(h256(fromHex(argv[++i])));
else if ((arg == "-S" || arg == "--session-secret") && i + 1 < argc)
sessionSecret = h256(fromHex(argv[++i]));
else if (arg == "--structured-logging-format" && i + 1 < argc) else if (arg == "--structured-logging-format" && i + 1 < argc)
structuredLoggingFormat = string(argv[++i]); structuredLoggingFormat = string(argv[++i]);
else if (arg == "--structured-logging") else if (arg == "--structured-logging")
@ -510,10 +511,13 @@ int main(int argc, char** argv)
{ {
RLPStream config(2); RLPStream config(2);
config << us.secret() << coinbase; config << sigKey.secret() << coinbase;
writeFile(configFile, config.out()); writeFile(configFile, config.out());
} }
if (sessionSecret)
sigKey = KeyPair(sessionSecret);
// Two codepaths is necessary since named block require database, but numbered // Two codepaths is necessary since named block require database, but numbered
// blocks are superuseful to have when database is already open in another process. // blocks are superuseful to have when database is already open in another process.
if (mode == OperationMode::DAGInit && !(initDAG == LatestBlock || initDAG == PendingBlock)) if (mode == OperationMode::DAGInit && !(initDAG == LatestBlock || initDAG == PendingBlock))
@ -626,7 +630,7 @@ int main(int argc, char** argv)
c->setAddress(coinbase); c->setAddress(coinbase);
} }
cout << "Transaction Signer: " << us.address() << endl; cout << "Transaction Signer: " << sigKey.address() << endl;
cout << "Mining Benefactor: " << coinbase << endl; cout << "Mining Benefactor: " << coinbase << endl;
web3.startNetwork(); web3.startNetwork();
@ -641,8 +645,7 @@ int main(int argc, char** argv)
if (jsonrpc > -1) if (jsonrpc > -1)
{ {
jsonrpcConnector = unique_ptr<jsonrpc::AbstractServerConnector>(new jsonrpc::HttpServer(jsonrpc, "", "", SensibleHttpThreads)); jsonrpcConnector = unique_ptr<jsonrpc::AbstractServerConnector>(new jsonrpc::HttpServer(jsonrpc, "", "", SensibleHttpThreads));
jsonrpcServer = shared_ptr<WebThreeStubServer>(new WebThreeStubServer(*jsonrpcConnector.get(), web3, vector<KeyPair>({us}))); jsonrpcServer = shared_ptr<WebThreeStubServer>(new WebThreeStubServer(*jsonrpcConnector.get(), web3, vector<KeyPair>({sigKey})));
jsonrpcServer->setIdentities({us});
jsonrpcServer->StartListening(); jsonrpcServer->StartListening();
} }
#endif #endif
@ -766,8 +769,7 @@ int main(int argc, char** argv)
if (jsonrpc < 0) if (jsonrpc < 0)
jsonrpc = SensibleHttpPort; jsonrpc = SensibleHttpPort;
jsonrpcConnector = unique_ptr<jsonrpc::AbstractServerConnector>(new jsonrpc::HttpServer(jsonrpc, "", "", SensibleHttpThreads)); jsonrpcConnector = unique_ptr<jsonrpc::AbstractServerConnector>(new jsonrpc::HttpServer(jsonrpc, "", "", SensibleHttpThreads));
jsonrpcServer = shared_ptr<WebThreeStubServer>(new WebThreeStubServer(*jsonrpcConnector.get(), web3, vector<KeyPair>({us}))); jsonrpcServer = shared_ptr<WebThreeStubServer>(new WebThreeStubServer(*jsonrpcConnector.get(), web3, vector<KeyPair>({sigKey})));
jsonrpcServer->setIdentities({us});
jsonrpcServer->StartListening(); jsonrpcServer->StartListening();
} }
else if (cmd == "jsonstop") else if (cmd == "jsonstop")
@ -779,12 +781,11 @@ int main(int argc, char** argv)
#endif #endif
else if (cmd == "address") else if (cmd == "address")
{ {
cout << "Current address:" << endl cout << "Current address:" << endl << sigKey.address() << endl;
<< toHex(us.address().asArray()) << endl;
} }
else if (cmd == "secret") else if (cmd == "secret")
{ {
cout << "Secret Key: " << toHex(us.secret().asArray()) << endl; cout << "Secret Key: " << sigKey.secret() << endl;
} }
else if (c && cmd == "block") else if (c && cmd == "block")
{ {
@ -799,7 +800,7 @@ int main(int argc, char** argv)
} }
else if (c && cmd == "balance") else if (c && cmd == "balance")
{ {
cout << "Current balance: " << formatBalance( c->balanceAt(us.address())) << " = " <<c->balanceAt(us.address()) << " wei" << endl; cout << "Current balance: " << formatBalance( c->balanceAt(sigKey.address())) << " = " <<c->balanceAt(sigKey.address()) << " wei" << endl;
} }
else if (c && cmd == "transact") else if (c && cmd == "transact")
{ {
@ -915,7 +916,7 @@ int main(int argc, char** argv)
try try
{ {
Address dest = h160(fromHex(hexAddr, WhenError::Throw)); Address dest = h160(fromHex(hexAddr, WhenError::Throw));
c->submitTransaction(us.secret(), amount, dest, bytes(), minGas); c->submitTransaction(sigKey.secret(), amount, dest, bytes(), minGas);
} }
catch (BadHexCharacter& _e) catch (BadHexCharacter& _e)
{ {
@ -984,7 +985,7 @@ int main(int argc, char** argv)
else if (gas < minGas) else if (gas < minGas)
cwarn << "Minimum gas amount is" << minGas; cwarn << "Minimum gas amount is" << minGas;
else else
c->submitTransaction(us.secret(), endowment, init, gas, gasPrice); c->submitTransaction(sigKey.secret(), endowment, init, gas, gasPrice);
} }
else else
cwarn << "Require parameters: contract ENDOWMENT GASPRICE GAS CODEHEX"; cwarn << "Require parameters: contract ENDOWMENT GASPRICE GAS CODEHEX";
@ -1101,7 +1102,7 @@ int main(int argc, char** argv)
{ {
string hexSec; string hexSec;
iss >> hexSec; iss >> hexSec;
us = KeyPair(h256(fromHex(hexSec))); sigKey = KeyPair(h256(fromHex(hexSec)));
} }
else else
cwarn << "Require parameter: setSecret HEXSECRETKEY"; cwarn << "Require parameter: setSecret HEXSECRETKEY";
@ -1141,7 +1142,7 @@ int main(int argc, char** argv)
string path; string path;
iss >> path; iss >> path;
RLPStream config(2); RLPStream config(2);
config << us.secret() << coinbase; config << sigKey.secret() << coinbase;
writeFile(path, config.out()); writeFile(path, config.out());
} }
else else
@ -1157,7 +1158,7 @@ int main(int argc, char** argv)
if (b.size()) if (b.size())
{ {
RLP config(b); RLP config(b);
us = KeyPair(config[0].toHash<Secret>()); sigKey = KeyPair(config[0].toHash<Secret>());
coinbase = config[1].toHash<Address>(); coinbase = config[1].toHash<Address>();
} }
else else

9
libethcore/BlockInfo.cpp

@ -23,6 +23,7 @@
#include <libdevcore/RLP.h> #include <libdevcore/RLP.h>
#include <libdevcrypto/TrieDB.h> #include <libdevcrypto/TrieDB.h>
#include <libethcore/Common.h> #include <libethcore/Common.h>
#include "EthashAux.h"
#include "ProofOfWork.h" #include "ProofOfWork.h"
#include "Exceptions.h" #include "Exceptions.h"
#include "Params.h" #include "Params.h"
@ -63,8 +64,7 @@ void BlockInfo::clear()
h256 const& BlockInfo::seedHash() const h256 const& BlockInfo::seedHash() const
{ {
if (!m_seedHash) if (!m_seedHash)
for (u256 n = number; n >= c_epochDuration; n -= c_epochDuration) m_seedHash = EthashAux::seedHash((unsigned)number);
m_seedHash = sha3(m_seedHash);
return m_seedHash; return m_seedHash;
} }
@ -145,9 +145,14 @@ void BlockInfo::populateFromHeader(RLP const& _header, Strictness _s, h256 const
throw; throw;
} }
if (number > ~(unsigned)0)
throw InvalidNumber();
// check it hashes according to proof of work or that it's the genesis block. // check it hashes according to proof of work or that it's the genesis block.
if (_s == CheckEverything && parentHash && !ProofOfWork::verify(*this)) if (_s == CheckEverything && parentHash && !ProofOfWork::verify(*this))
BOOST_THROW_EXCEPTION(InvalidBlockNonce() << errinfo_hash256(headerHash(WithoutNonce)) << errinfo_nonce(nonce) << errinfo_difficulty(difficulty)); BOOST_THROW_EXCEPTION(InvalidBlockNonce() << errinfo_hash256(headerHash(WithoutNonce)) << errinfo_nonce(nonce) << errinfo_difficulty(difficulty));
else if (_s == QuickNonce && parentHash && !ProofOfWork::preVerify(*this))
BOOST_THROW_EXCEPTION(InvalidBlockNonce() << errinfo_hash256(headerHash(WithoutNonce)) << errinfo_nonce(nonce) << errinfo_difficulty(difficulty));
if (_s != CheckNothing) if (_s != CheckNothing)
{ {

1
libethcore/BlockInfo.h

@ -39,6 +39,7 @@ enum IncludeNonce
enum Strictness enum Strictness
{ {
CheckEverything, CheckEverything,
QuickNonce,
IgnoreNonce, IgnoreNonce,
CheckNothing CheckNothing
}; };

29
libethcore/EthashAux.cpp

@ -63,24 +63,47 @@ ethash_params EthashAux::params(unsigned _n)
return p; return p;
} }
h256 EthashAux::seedHash(unsigned _number)
{
unsigned epoch = _number / ETHASH_EPOCH_LENGTH;
RecursiveGuard l(get()->x_this);
if (_number < get()->m_seedHashes.size())
return get()->m_seedHashes[_number];
h256 ret;
unsigned n = 0;
if (!get()->m_seedHashes.empty())
{
ret = get()->m_seedHashes.back();
n = get()->m_seedHashes.size() - 1;
}
cdebug << "Searching for seedHash of epoch " << epoch;
for (; n < epoch; ++n, ret = sha3(ret))
cdebug << "Epoch" << n << "is" << ret.abridged();
return ret;
}
ethash_params EthashAux::params(h256 const& _seedHash) ethash_params EthashAux::params(h256 const& _seedHash)
{ {
RecursiveGuard l(get()->x_this); RecursiveGuard l(get()->x_this);
unsigned epoch = 0; unsigned epoch = 0;
try try
{ {
epoch = get()->m_seedHashes.at(_seedHash); epoch = get()->m_epochs.at(_seedHash);
} }
catch (...) catch (...)
{ {
for (h256 h; h != _seedHash && epoch < 2048; ++epoch, h = h256(h)) {} cdebug << "Searching for seedHash " << _seedHash.abridged();
for (h256 h; h != _seedHash && epoch < 2048; ++epoch, h = sha3(h))
{
cdebug << "Epoch" << epoch << "is" << h.abridged();
}
if (epoch == 2048) if (epoch == 2048)
{ {
std::ostringstream error; std::ostringstream error;
error << "apparent block number for " << _seedHash.abridged() << " is too high; max is " << (ETHASH_EPOCH_LENGTH * 2048); error << "apparent block number for " << _seedHash.abridged() << " is too high; max is " << (ETHASH_EPOCH_LENGTH * 2048);
throw std::invalid_argument(error.str()); throw std::invalid_argument(error.str());
} }
get()->m_seedHashes[_seedHash] = epoch; get()->m_epochs[_seedHash] = epoch;
} }
return params(epoch * ETHASH_EPOCH_LENGTH); return params(epoch * ETHASH_EPOCH_LENGTH);
} }

4
libethcore/EthashAux.h

@ -36,6 +36,7 @@ public:
using LightType = void const*; using LightType = void const*;
using FullType = void const*; using FullType = void const*;
static h256 seedHash(unsigned _number);
static ethash_params params(BlockInfo const& _header); static ethash_params params(BlockInfo const& _header);
static ethash_params params(h256 const& _seedHash); static ethash_params params(h256 const& _seedHash);
static ethash_params params(unsigned _n); static ethash_params params(unsigned _n);
@ -58,7 +59,8 @@ private:
std::map<h256, LightType> m_lights; std::map<h256, LightType> m_lights;
std::map<h256, bytesRef> m_fulls; std::map<h256, bytesRef> m_fulls;
std::map<h256, unsigned> m_seedHashes; std::map<h256, unsigned> m_epochs;
h256s m_seedHashes;
}; };
} }

Loading…
Cancel
Save