Browse Source

First unit testing harness for VM.

NOT fix.
cl-refactor
Gav Wood 11 years ago
parent
commit
9fdf7446db
  1. 2
      libethereum/Common.h
  2. 2
      libethereum/ExtVMFace.h
  3. 2
      libethereum/State.h
  4. 1
      libethereum/VM.h
  5. 110
      test/vm.cpp

2
libethereum/Common.h

@ -90,6 +90,8 @@ std::string asHex(_T const& _data, int _w = 2)
return ret.str();
}
template <unsigned T> class UnitTest {};
template <unsigned N>
class FixedHash
{

2
libethereum/ExtVMFace.h

@ -46,8 +46,6 @@ public:
u256 store(u256 _n) { return 0; }
void setStore(u256 _n, u256 _v) {}
u256 temp(u256 _n) { return 0; }
void setTemp(u256 _n, u256 _v) {}
void mktx(Transaction& _t) {}
u256 balance(Address _a) { return 0; }
void payFee(bigint _fee) {}

2
libethereum/State.h

@ -46,8 +46,6 @@ std::map<Address, AddressState> const& genesisState();
#define ETH_SENDER_PAYS_SETUP 1
template <unsigned T> class UnitTest {};
static const std::map<u256, u256> EmptyMapU256U256;
class ExtVM;

1
libethereum/VM.h

@ -224,7 +224,6 @@ template <class Ext> void eth::VM::go(Ext& _ext, uint64_t _steps)
case Instruction::NOT:
require(1);
m_stack.back() = m_stack.back() ? 0 : 1;
m_stack.pop_back();
break;
case Instruction::MYADDRESS:
m_stack.push_back(fromAddress(_ext.myAddress));

110
test/vm.cpp

@ -20,81 +20,79 @@
* State test functions.
*/
#include <boost/algorithm/string.hpp>
#include <boost/filesystem/operations.hpp>
#include <secp256k1.h>
#include <BlockChain.h>
#include <State.h>
#include <Defaults.h>
#include <ExtVMFace.h>
#include <Transaction.h>
#include <VM.h>
#include <Instruction.h>
using namespace std;
using namespace eth;
namespace eth
{
template <> class UnitTest<1>
class FakeExtVM: public ExtVMFace
{
public:
int operator()()
FakeExtVM(Address _myAddress, u256 _myBalance, u256 _myNonce, u256s _myData, Address _txSender, u256 _txValue, u256s const& _txData, FeeStructure const& _fees, BlockInfo const& _previousBlock, BlockInfo const& _currentBlock, uint _currentNumber):
ExtVMFace(_myAddress, _txSender, _txValue, _txData, _fees, _previousBlock, _currentBlock, _currentNumber)
{
c_genesisDifficulty = (u256)1;
string tmpDir = (boost::filesystem::temp_directory_path() / "vmTest").string();
KeyPair p = KeyPair::create();
Overlay o(State::openDB(tmpDir, true));
State s(p.address(), o);
BlockChain bc(tmpDir, true);
cout << s;
s.commitToMine(bc);
s.mine(1000000);
bc.attemptImport(s.blockData(), o);
s.sync(bc);
cout << s;
Transaction c;
c.receiveAddress = Address();
c.nonce = 0;
c.data = assemble("txsender sload txvalue add txsender sstore stop");
// (sstore (add (txvalue (sload txsender))))
c.value = ether;
c.sign(p.secret());
s.execute(c.rlp());
Address ca = right160(c.sha3());
cout << s;
s.commitToMine(bc);
s.mine(1000000);
bc.attemptImport(s.blockData(), o);
s.sync(bc);
reset(_myBalance, _myNonce, _myData);
}
cout << s;
u256 store(u256 _n) { return get<3>(addresses[myAddress])[_n]; }
void setStore(u256 _n, u256 _v) { get<3>(addresses[myAddress])[_n] = _v; }
void mktx(Transaction& _t) { txs.push_back(_t); }
u256 balance(Address _a) { return get<0>(addresses[_a]); }
void payFee(bigint _fee) { get<0>(addresses[myAddress]) = (u256)(get<0>(addresses[myAddress]) - _fee); }
u256 txCount(Address _a) { return get<1>(addresses[_a]); }
u256 extro(Address _a, u256 _pos) { return get<3>(addresses[_a])[_pos]; }
u256 extroPrice(Address _a) { return get<2>(addresses[_a]); }
void suicide(Address _a) { dead = _a; }
void reset(u256 _myBalance, u256 _myNonce, u256s _myData)
{
txs.clear();
addresses.clear();
get<0>(addresses[myAddress]) = _myBalance;
get<1>(addresses[myAddress]) = _myNonce;
get<2>(addresses[myAddress]) = 0;
for (unsigned i = 0; i < _myData.size(); ++i)
get<3>(addresses[myAddress])[i] = _myData[i];
dead = Address();
}
// cout << s.m_db;
map<Address, tuple<u256, u256, u256, map<u256, u256>>> addresses;
Transactions txs;
Address dead;
};
c.receiveAddress = ca;
c.nonce = 1;
c.data = {};
c.value = 69 * wei;
c.sign(p.secret());
s.execute(c.rlp());
template <> class UnitTest<1>
{
public:
int operator()()
{
VM vm;
BlockInfo pb;
pb.hash = sha3("previousHash");
pb.nonce = sha3("previousNonce");
BlockInfo cb = pb;
cb.difficulty = 256;
cb.timestamp = 1;
cb.coinbaseAddress = toAddress(sha3("coinbase"));
FeeStructure fees;
fees.setMultiplier(1);
cout << s;
string code = "(suicide (txsender))";
s.commitToMine(bc);
s.mine();
bc.attemptImport(s.blockData(), o);
s.sync(bc);
FakeExtVM fev(toAddress(sha3("contract")), ether, 0, compileLisp(code), toAddress(sha3("sender")), ether, u256s(), fees, pb, cb, 0);
cout << s;
vm.go(fev);
cnote << fev.dead << formatBalance(fev.balance(toAddress(sha3("contract"))));
return 0;
}
};
}
int vmTest()

Loading…
Cancel
Save