diff options
author | Gav Wood <i@gavwood.com> | 2014-04-03 22:14:29 +0800 |
---|---|---|
committer | Gav Wood <i@gavwood.com> | 2014-04-03 22:14:29 +0800 |
commit | cae6132e3547da3e0b59b3ec0d648ea102ae35b8 (patch) | |
tree | 59742978b086099888522f092059cd6260006bf1 | |
parent | 913cdd9fd76f1969aa901ec7aec9210599dc4fea (diff) | |
parent | b3de90e246a53ead7614b09c1a6b0d815e845479 (diff) | |
download | dexon-solidity-cae6132e3547da3e0b59b3ec0d648ea102ae35b8.tar.gz dexon-solidity-cae6132e3547da3e0b59b3ec0d648ea102ae35b8.tar.zst dexon-solidity-cae6132e3547da3e0b59b3ec0d648ea102ae35b8.zip |
Merge branch 'splitcode' into develop
Conflicts:
eth/main.cpp
-rw-r--r-- | crypto.cpp | 1 | ||||
-rw-r--r-- | txTest.cpp | 8 | ||||
-rw-r--r-- | vm.cpp | 136 |
3 files changed, 95 insertions, 50 deletions
@@ -42,7 +42,6 @@ int cryptoTest() t.nonce = 0; t.receiveAddress = h160(fromHex("944400f4b88ac9589a0f17ed4671da26bddb668b")); t.value = 1000; - t.data = u256s(); cnote << RLP(t.rlp(false)); cnote << toHex(t.rlp(false)); cnote << t.sha3(false); @@ -52,15 +52,15 @@ BOOST_AUTO_TEST_CASE(mine_and_send_to_peer) mine(c1, 1); auto c1bal = c1.state().balance(kp1.address()); BOOST_REQUIRE(c1bal > 0); - BOOST_REQUIRE(c1bal > c1.state().fee()); +// BOOST_REQUIRE(c1bal > c1.state().fee()); //send c2 some eth from c1 - auto txAmount = c1bal - c1.state().fee(); - c1.transact(kp1.secret(), c2.address(), txAmount); +// auto txAmount = c1bal - c1.state().fee(); +// c1.transact(kp1.secret(), c2.address(), txAmount); //mine some more to include the transaction on chain mine(c1, 1); auto c2bal = c2.state().balance(kp2.address()); BOOST_REQUIRE(c2bal > 0); - BOOST_REQUIRE(c2bal == txAmount); +// BOOST_REQUIRE(c2bal == txAmount); } @@ -39,8 +39,8 @@ class FakeExtVM: public ExtVMFace public: FakeExtVM() {} - FakeExtVM(FeeStructure const& _fees, BlockInfo const& _previousBlock, BlockInfo const& _currentBlock, uint _currentNumber): - ExtVMFace(Address(), Address(), 0, u256s(), _fees, _previousBlock, _currentBlock, _currentNumber) + FakeExtVM(BlockInfo const& _previousBlock, BlockInfo const& _currentBlock, uint _currentNumber): + ExtVMFace(Address(), Address(), 0, 1, bytesConstRef(), bytesConstRef(), _previousBlock, _currentBlock, _currentNumber) {} u256 store(u256 _n) @@ -51,7 +51,15 @@ public: { get<3>(addresses[myAddress])[_n] = _v; } - void mktx(Transaction& _t) + u256 balance(Address _a) { return get<0>(addresses[_a]); } + void subBalance(u256 _a) { get<0>(addresses[myAddress]) -= _a; } + u256 txCount(Address _a) { return get<1>(addresses[_a]); } + void suicide(Address _a) + { + get<0>(addresses[_a]) += get<0>(addresses[myAddress]); + addresses.erase(myAddress); + } + void transact(Transaction& _t) { if (get<0>(addresses[myAddress]) >= _t.value) { @@ -61,41 +69,57 @@ public: 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) + h160 create(u256 _endowment, u256* _gas, bytesConstRef _code, bytesConstRef _init) { - return get<3>(addresses[_a])[_pos]; + Transaction t; + t.value = _endowment; + t.gasPrice = gasPrice; + t.gas = *_gas; + t.data = _code.toBytes(); + t.init = _init.toBytes(); + txs.push_back(t); + return right160(t.sha3(false)); } - u256 extroPrice(Address _a) { return get<2>(addresses[_a]); } - void suicide(Address _a) + + bool call(Address _receiveAddress, u256 _txValue, bytesConstRef _txData, u256* _gas, bytesRef _txOut) { - for (auto const& i: get<3>(addresses[myAddress])) - if (i.second) - get<0>(addresses[_a]) += fees.m_memoryFee; - get<0>(addresses[_a]) += get<0>(addresses[myAddress]); - addresses.erase(myAddress); + Transaction t; + t.value = _txValue; + t.gasPrice = gasPrice; + t.gas = *_gas; + t.data = _txData.toVector(); + t.receiveAddress = _receiveAddress; + txs.push_back(t); + (void)_txOut; + return true; } - void setTransaction(Address _txSender, u256 _txValue, u256s const& _txData) + void setTransaction(Address _txSender, u256 _txValue, u256 _gasPrice, bytes const& _txData) { txSender = _txSender; txValue = _txValue; - txData = _txData; + txData = &_txData; + gasPrice = _gasPrice; } - void setContract(Address _myAddress, u256 _myBalance, u256 _myNonce, u256s _myData) + void setContract(Address _myAddress, u256 _myBalance, u256 _myNonce, bytes const& _code, map<u256, u256> const& _storage) { myAddress = _myAddress; - set(myAddress, _myBalance, _myNonce, _myData); + set(myAddress, _myBalance, _myNonce, _code, _storage); } - void set(Address _a, u256 _myBalance, u256 _myNonce, u256s _myData) + void set(Address _a, u256 _myBalance, u256 _myNonce, bytes const& _code, map<u256, u256> const& _storage) { get<0>(addresses[_a]) = _myBalance; get<1>(addresses[_a]) = _myNonce; get<2>(addresses[_a]) = 0; - for (unsigned i = 0; i < _myData.size(); ++i) - get<3>(addresses[_a])[i] = _myData[i]; + get<3>(addresses[_a]) = _storage; + get<4>(addresses[_a]) = _code; + } + + void reset(u256 _myBalance, u256 _myNonce, map<u256, u256> const& _storage) + { + txs.clear(); + addresses.clear(); + set(myAddress, _myBalance, _myNonce, get<4>(addresses[myAddress]), _storage); } mObject exportEnv() @@ -106,7 +130,6 @@ public: push(ret, "currentDifficulty", currentBlock.difficulty); push(ret, "currentTimestamp", currentBlock.timestamp); ret["currentCoinbase"] = toString(currentBlock.coinbaseAddress); - push(ret, "feeMultiplier", fees.multiplier()); return ret; } @@ -117,7 +140,6 @@ public: currentBlock.difficulty = toInt(_o["currentDifficulty"]); currentBlock.timestamp = toInt(_o["currentTimestamp"]); currentBlock.coinbaseAddress = Address(_o["currentCoinbase"].get_str()); - fees.setMultiplier(toInt(_o["feeMultiplier"])); } static u256 toInt(mValue const& _v) @@ -133,6 +155,19 @@ public: return 0; } + static byte toByte(mValue const& _v) + { + switch (_v.type()) + { + case str_type: return (byte)stoi(_v.get_str()); + case int_type: return (byte)_v.get_uint64(); + case bool_type: return (byte)_v.get_bool(); + case real_type: return (byte)_v.get_real(); + default: cwarn << "Bad type for scalar: " << _v.type(); + } + return 0; + } + static void push(mObject& o, string const& _n, u256 _v) { if (_v < (u256)1 << 64) @@ -157,7 +192,6 @@ public: mObject o; push(o, "balance", get<0>(a.second)); push(o, "nonce", get<1>(a.second)); - push(o, "extroPrice", get<2>(a.second)); mObject store; string curKey; @@ -197,7 +231,6 @@ public: auto& a = addresses[Address(i.first)]; get<0>(a) = toInt(o["balance"]); get<1>(a) = toInt(o["nonce"]); - get<2>(a) = toInt(o["extroPrice"]); if (o.count("store")) for (auto const& j: o["store"].get_obj()) { @@ -207,9 +240,9 @@ public: } if (o.count("code")) { - u256s d = compileLisp(o["code"].get_str()); - for (unsigned i = 0; i < d.size(); ++i) - get<3>(a)[(u256)i] = d[i]; + bytes e; + bytes d = compileLisp(o["code"].get_str(), false, e); + get<4>(a) = d; } } } @@ -220,6 +253,7 @@ public: ret["address"] = toString(myAddress); ret["sender"] = toString(txSender); push(ret, "value", txValue); + push(ret, "gasPrice", gasPrice); mArray d; for (auto const& i: txData) push(d, i); @@ -232,8 +266,11 @@ public: myAddress = Address(_o["address"].get_str()); txSender = Address(_o["sender"].get_str()); txValue = toInt(_o["value"]); + gasPrice = toInt(_o["gasPrice"]); + thisTxData.clear(); for (auto const& j: _o["data"].get_array()) - txData.push_back(toInt(j)); + thisTxData.push_back(toByte(j)); + txData = &thisTxData; } mArray exportTxs() @@ -262,20 +299,14 @@ public: t.receiveAddress = Address(tx["destination"].get_str()); t.value = toInt(tx["value"]); for (auto const& j: tx["data"].get_array()) - t.data.push_back(toInt(j)); + t.data.push_back(toByte(j)); txs.push_back(t); } } - void reset(u256 _myBalance, u256 _myNonce, u256s _myData) - { - txs.clear(); - addresses.clear(); - set(myAddress, _myBalance, _myNonce, _myData); - } - - map<Address, tuple<u256, u256, u256, map<u256, u256>>> addresses; + map<Address, tuple<u256, u256, u256, map<u256, u256>, bytes>> addresses; Transactions txs; + bytes thisTxData; }; #define CREATE_TESTS 0 @@ -315,21 +346,37 @@ public: if (_fillin) o["pre"] = mValue(fev.exportState()); + bytes output; for (auto i: o["exec"].get_array()) { fev.importExec(i.get_obj()); - vm.go(fev); + output = vm.go(fev).toBytes(); } if (_fillin) { o["post"] = mValue(fev.exportState()); o["txs"] = fev.exportTxs(); + mArray df; + for (auto const& i: output) + FakeExtVM::push(df, i); + o["out"] = df; } else { FakeExtVM test; test.importState(o["post"].get_obj()); test.importTxs(o["txs"].get_array()); + int i = 0; + for (auto const& d: o["out"].get_array()) + { + if (output[i] != FakeExtVM::toInt(d)) + { + cwarn << "Test failed: output byte" << i << "different."; + passed = false; + } + ++i; + } + if (test.addresses != fev.addresses) { cwarn << "Test failed: state different."; @@ -359,13 +406,12 @@ public: cb.difficulty = 256; cb.timestamp = 1; cb.coinbaseAddress = toAddress(sha3("coinbase")); - FeeStructure fees; - fees.setMultiplier(1); - FakeExtVM fev(fees, pb, cb, 0); - fev.setContract(toAddress(sha3("contract")), ether, 0, compileLisp("(suicide (txsender))")); + FakeExtVM fev(pb, cb, 0); + bytes init; + fev.setContract(toAddress(sha3("contract")), ether, 0, compileLisp("(suicide (txsender))", false, init), map<u256, u256>()); o["env"] = fev.exportEnv(); o["pre"] = fev.exportState(); - fev.setTransaction(toAddress(sha3("sender")), ether, u256s()); + fev.setTransaction(toAddress(sha3("sender")), ether, finney, bytes()); mArray execs; execs.push_back(fev.exportExec()); o["exec"] = execs; |