Browse Source

Cleanups and preparation.

cl-refactor
Gav Wood 11 years ago
parent
commit
b5eb3b7a9d
  1. 10
      eth/main.cpp
  2. 19
      libethereum/Common.cpp
  3. 5
      libethereum/Common.h
  4. 16
      libethereum/State.cpp

10
eth/main.cpp

@ -29,14 +29,16 @@ using namespace eth;
int main() int main()
{ {
// Our address. // Our address.
Address us; // TODO: should be loaded from config file/set at command-line. h256 privkey = sha3("123");
Address us = toPublic(privkey); // TODO: should be loaded from config file/set at command-line.
BlockChain bc; // Maintains block database. BlockChain bc; // Maintains block database.
TransactionQueue tq; // Maintains list of incoming transactions not yet on the block chain. TransactionQueue tq; // Maintains list of incoming transactions not yet on the block chain.
State s(us); // TODO: Switch to disk-backed state (leveldb? could do with a diffing DB) State s(us);
// s.restore(); // TODO: Implement - key optimisation.
// Synchronise the state according to the block chain - i.e. replay all transactions, in order. Will take a while if the state isn't restored. // Synchronise the state according to the block chain - i.e. replay all transactions in block chain, in order.
// In practise this won't need to be done since the State DB will contain the keys for the tries for most recent (and many old) blocks.
// TODO: currently it contains keys for *all* blocks. Make it remove old ones.
s.sync(bc); s.sync(bc);
s.sync(tq); s.sync(tq);

19
libethereum/Common.cpp

@ -24,11 +24,11 @@
#else #else
#pragma GCC diagnostic ignored "-Wunused-function" #pragma GCC diagnostic ignored "-Wunused-function"
#endif #endif
#include <secp256k1.h>
#include <sha3.h> #include <sha3.h>
#if WIN32 #if WIN32
#pragma warning(pop) #pragma warning(pop)
#else #else
#pragma GCC diagnostic warning "-Wunused-function"
#endif #endif
#include <random> #include <random>
#include "Common.h" #include "Common.h"
@ -143,3 +143,20 @@ h256 eth::sha3(bytesConstRef _input)
sha3(_input, bytesRef(&ret[0], 32)); sha3(_input, bytesRef(&ret[0], 32));
return ret; return ret;
} }
Address eth::toPublic(h256 _private)
{
bytes pubkey(65);
int pubkeylen = 65;
int ret = secp256k1_ecdsa_seckey_verify(_private.data());
if (!ret)
return Address();
ret = secp256k1_ecdsa_pubkey_create(pubkey.data(), &pubkeylen, _private.data(), 1);
if (!ret)
return Address();
pubkey.resize(pubkeylen);
ret = secp256k1_ecdsa_pubkey_verify(pubkey.data(), pubkey.size());
if (!ret)
return Address();
return left160(eth::sha3(bytesConstRef(&pubkey).cropped(1)));
}

5
libethereum/Common.h

@ -313,5 +313,10 @@ inline bytes sha3Bytes(std::string const& _input) { return sha3Bytes((std::strin
inline bytes sha3Bytes(bytes const& _input) { return sha3Bytes((bytes*)&_input); } inline bytes sha3Bytes(bytes const& _input) { return sha3Bytes((bytes*)&_input); }
h256 sha3(bytesConstRef _input); h256 sha3(bytesConstRef _input);
inline h256 sha3(bytes const& _input) { return sha3(bytesConstRef((bytes*)&_input)); } inline h256 sha3(bytes const& _input) { return sha3(bytesConstRef((bytes*)&_input)); }
inline h256 sha3(std::string const& _input) { return sha3(bytesConstRef(_input)); }
/// Convert a private key into the public key equivalent.
/// @returns 0 if it's not a valid private key.
Address toPublic(h256 _private);
} }

16
libethereum/State.cpp

@ -447,6 +447,14 @@ void State::execute(Transaction const& _t, Address _sender)
} }
} }
// Convert from a 256-bit integer stack/memory entry into a 160-bit Address hash.
// Currently we just pull out the left (high-order in BE) 160-bits.
// TODO: check that this is correct.
inline Address asAddress(u256 _item)
{
return left160(h256(_item));
}
void State::execute(Address _myAddress, Address _txSender, u256 _txValue, u256 _txFee, u256s const& _txData, u256* _totalFee) void State::execute(Address _myAddress, Address _txSender, u256 _txValue, u256 _txFee, u256s const& _txData, u256* _totalFee)
{ {
std::vector<u256> stack; std::vector<u256> stack;
@ -892,14 +900,14 @@ void State::execute(Address _myAddress, Address _txSender, u256 _txValue, u256 _
require(2); require(2);
auto memoryAddress = stack.back(); auto memoryAddress = stack.back();
stack.pop_back(); stack.pop_back();
Address contractAddress = left160(stack.back()); Address contractAddress = asAddress(stack.back());
stack.back() = contractMemory(contractAddress, memoryAddress); stack.back() = contractMemory(contractAddress, memoryAddress);
break; break;
} }
case Instruction::BALANCE: case Instruction::BALANCE:
{ {
require(1); require(1);
stack.back() = balance(low160(stack.back())); stack.back() = balance(asAddress(stack.back()));
break; break;
} }
case Instruction::MKTX: case Instruction::MKTX:
@ -907,7 +915,7 @@ void State::execute(Address _myAddress, Address _txSender, u256 _txValue, u256 _
require(4); require(4);
Transaction t; Transaction t;
t.receiveAddress = left160(stack.back()); t.receiveAddress = asAddress(stack.back());
stack.pop_back(); stack.pop_back();
t.value = stack.back(); t.value = stack.back();
stack.pop_back(); stack.pop_back();
@ -933,7 +941,7 @@ void State::execute(Address _myAddress, Address _txSender, u256 _txValue, u256 _
case Instruction::SUICIDE: case Instruction::SUICIDE:
{ {
require(1); require(1);
Address dest = left160(stack.back()); Address dest = asAddress(stack.back());
// TODO: easy once we have the local cache of memory in place. // TODO: easy once we have the local cache of memory in place.
u256 minusVoidFee = 0;//m_current[_myAddress].memory().size() * c_memoryFee; u256 minusVoidFee = 0;//m_current[_myAddress].memory().size() * c_memoryFee;
addBalance(dest, balance(_myAddress) + minusVoidFee); addBalance(dest, balance(_myAddress) + minusVoidFee);

Loading…
Cancel
Save