aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGav Wood <g@ethdev.com>2015-02-12 23:33:15 +0800
committerGav Wood <g@ethdev.com>2015-02-12 23:33:15 +0800
commit1f6198a9ae6c67061d897d67a0d40a1c1806fdec (patch)
treeb00572ed1930114c12663594cda980afe517dfa1
parent0fea318c9b1c6275c7f601335f1aef559b2ecad3 (diff)
parentd089703254d9d8243522f94b6585eb2c4e99bd57 (diff)
downloaddexon-solidity-1f6198a9ae6c67061d897d67a0d40a1c1806fdec.tar.gz
dexon-solidity-1f6198a9ae6c67061d897d67a0d40a1c1806fdec.tar.zst
dexon-solidity-1f6198a9ae6c67061d897d67a0d40a1c1806fdec.zip
Merge pull request #1019 from chriseth/sol_bytes
Basic implementation of byte arrays.
-rw-r--r--SolidityEndToEndTest.cpp133
-rw-r--r--solidityExecutionFramework.h2
2 files changed, 133 insertions, 2 deletions
diff --git a/SolidityEndToEndTest.cpp b/SolidityEndToEndTest.cpp
index 1bfb55f6..6c90ca62 100644
--- a/SolidityEndToEndTest.cpp
+++ b/SolidityEndToEndTest.cpp
@@ -668,7 +668,7 @@ BOOST_AUTO_TEST_CASE(mapping_state)
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0));
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1));
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(2));
- // voting without vote right shourd be rejected
+ // voting without vote right should be rejected
testSolidityAgainstCpp("vote(address,address)", vote, u160(0), u160(2));
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0));
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1));
@@ -2254,6 +2254,137 @@ BOOST_AUTO_TEST_CASE(generic_call)
BOOST_CHECK_EQUAL(m_state.balance(m_contractAddress), 50 - 2);
}
+BOOST_AUTO_TEST_CASE(store_bytes)
+{
+ // this test just checks that the copy loop does not mess up the stack
+ char const* sourceCode = R"(
+ contract C {
+ function save() returns (uint r) {
+ r = 23;
+ savedData = msg.data;
+ r = 24;
+ }
+ bytes savedData;
+ }
+ )";
+ compileAndRun(sourceCode);
+ // empty copy loop
+ BOOST_CHECK(callContractFunction("save()") == encodeArgs(24));
+ BOOST_CHECK(callContractFunction("save()", "abcdefg") == encodeArgs(24));
+}
+
+BOOST_AUTO_TEST_CASE(call_forward_bytes)
+{
+ char const* sourceCode = R"(
+ contract receiver {
+ uint public received;
+ function receive(uint x) { received += x + 1; }
+ function() { received = 0x80; }
+ }
+ contract sender {
+ function sender() { rec = new receiver(); }
+ function() { savedData = msg.data; }
+ function forward() returns (bool) { rec.call(savedData); return true; }
+ function clear() returns (bool) { delete savedData; return true; }
+ function val() returns (uint) { return rec.received(); }
+ receiver rec;
+ bytes savedData;
+ }
+ )";
+ compileAndRun(sourceCode, 0, "sender");
+ BOOST_CHECK(callContractFunction("receive(uint256)", 7) == bytes());
+ BOOST_CHECK(callContractFunction("val()") == encodeArgs(0));
+ BOOST_CHECK(callContractFunction("forward()") == encodeArgs(true));
+ BOOST_CHECK(callContractFunction("val()") == encodeArgs(8));
+ BOOST_CHECK(callContractFunction("clear()") == encodeArgs(true));
+ BOOST_CHECK(callContractFunction("val()") == encodeArgs(8));
+ BOOST_CHECK(callContractFunction("forward()") == encodeArgs(true));
+ BOOST_CHECK(callContractFunction("val()") == encodeArgs(0x80));
+}
+
+BOOST_AUTO_TEST_CASE(copying_bytes_multiassign)
+{
+ char const* sourceCode = R"(
+ contract receiver {
+ uint public received;
+ function receive(uint x) { received += x + 1; }
+ function() { received = 0x80; }
+ }
+ contract sender {
+ function sender() { rec = new receiver(); }
+ function() { savedData1 = savedData2 = msg.data; }
+ function forward(bool selector) returns (bool) {
+ if (selector) { rec.call(savedData1); delete savedData1; }
+ else { rec.call(savedData2); delete savedData2; }
+ return true;
+ }
+ function val() returns (uint) { return rec.received(); }
+ receiver rec;
+ bytes savedData1;
+ bytes savedData2;
+ }
+ )";
+ compileAndRun(sourceCode, 0, "sender");
+ BOOST_CHECK(callContractFunction("receive(uint256)", 7) == bytes());
+ BOOST_CHECK(callContractFunction("val()") == encodeArgs(0));
+ BOOST_CHECK(callContractFunction("forward(bool)", true) == encodeArgs(true));
+ BOOST_CHECK(callContractFunction("val()") == encodeArgs(8));
+ BOOST_CHECK(callContractFunction("forward(bool)", false) == encodeArgs(true));
+ BOOST_CHECK(callContractFunction("val()") == encodeArgs(16));
+ BOOST_CHECK(callContractFunction("forward(bool)", true) == encodeArgs(true));
+ BOOST_CHECK(callContractFunction("val()") == encodeArgs(0x80));
+}
+
+BOOST_AUTO_TEST_CASE(delete_removes_bytes_data)
+{
+ char const* sourceCode = R"(
+ contract c {
+ function() { data = msg.data; }
+ function del() returns (bool) { delete data; return true; }
+ bytes data;
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("---", 7) == bytes());
+ BOOST_CHECK(!m_state.storage(m_contractAddress).empty());
+ BOOST_CHECK(callContractFunction("del()", 7) == encodeArgs(true));
+ BOOST_CHECK(m_state.storage(m_contractAddress).empty());
+}
+
+BOOST_AUTO_TEST_CASE(copy_from_calldata_removes_bytes_data)
+{
+ char const* sourceCode = R"(
+ contract c {
+ function set() returns (bool) { data = msg.data; return true; }
+ function() { data = msg.data; }
+ bytes data;
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("set()", 1, 2, 3, 4, 5) == encodeArgs(true));
+ BOOST_CHECK(!m_state.storage(m_contractAddress).empty());
+ sendMessage(bytes(), false);
+ BOOST_CHECK(m_output == bytes());
+ BOOST_CHECK(m_state.storage(m_contractAddress).empty());
+}
+
+BOOST_AUTO_TEST_CASE(copy_removes_bytes_data)
+{
+ char const* sourceCode = R"(
+ contract c {
+ function set() returns (bool) { data1 = msg.data; return true; }
+ function reset() returns (bool) { data1 = data2; return true; }
+ bytes data1;
+ bytes data2;
+ }
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callContractFunction("set()", 1, 2, 3, 4, 5) == encodeArgs(true));
+ BOOST_CHECK(!m_state.storage(m_contractAddress).empty());
+ BOOST_CHECK(callContractFunction("reset()") == encodeArgs(true));
+ BOOST_CHECK(m_state.storage(m_contractAddress).empty());
+}
+
BOOST_AUTO_TEST_SUITE_END()
}
diff --git a/solidityExecutionFramework.h b/solidityExecutionFramework.h
index 5a693536..4ef9bfdc 100644
--- a/solidityExecutionFramework.h
+++ b/solidityExecutionFramework.h
@@ -139,6 +139,7 @@ private:
return encode(_cppFunction(_arguments...));
}
+protected:
void sendMessage(bytes const& _data, bool _isCreation, u256 const& _value = 0)
{
m_state.addBalance(m_sender, _value); // just in case
@@ -171,7 +172,6 @@ private:
m_logs = executive.logs();
}
-protected:
bool m_optimize = false;
bool m_addStandardSources = false;
Address m_sender;