mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
commit
2411f5d839
@ -145,6 +145,13 @@ inline std::string toHex(u256 val, HexPrefix prefix = HexPrefix::DontAdd)
|
||||
return (prefix == HexPrefix::Add) ? "0x" + str : str;
|
||||
}
|
||||
|
||||
inline std::string toCompactHexWithPrefix(u256 val)
|
||||
{
|
||||
std::ostringstream ret;
|
||||
ret << std::hex << val;
|
||||
return "0x" + ret.str();
|
||||
}
|
||||
|
||||
// Algorithms for string and string-like collections.
|
||||
|
||||
/// Escapes a string into the C-string representation.
|
||||
|
@ -90,7 +90,7 @@ string Whiskers::replace(
|
||||
string tagName(_match[1]);
|
||||
if (!tagName.empty())
|
||||
{
|
||||
assertThrow(_parameters.count(tagName), WhiskersError, "Tag " + tagName + " not found.");
|
||||
assertThrow(_parameters.count(tagName), WhiskersError, "Value for tag " + tagName + " not provided.");
|
||||
return _parameters.at(tagName);
|
||||
}
|
||||
else
|
||||
|
@ -29,15 +29,19 @@ namespace solidity
|
||||
|
||||
enum class ExperimentalFeature
|
||||
{
|
||||
ABIEncoderV2, // new ABI encoder that makes use of JULIA
|
||||
Test,
|
||||
TestOnlyAnalysis
|
||||
};
|
||||
|
||||
static const std::map<ExperimentalFeature, bool> ExperimentalFeatureOnlyAnalysis = {
|
||||
static const std::map<ExperimentalFeature, bool> ExperimentalFeatureOnlyAnalysis =
|
||||
{
|
||||
{ ExperimentalFeature::TestOnlyAnalysis, true },
|
||||
};
|
||||
|
||||
static const std::map<std::string, ExperimentalFeature> ExperimentalFeatureNames = {
|
||||
static const std::map<std::string, ExperimentalFeature> ExperimentalFeatureNames =
|
||||
{
|
||||
{ "ABIEncoderV2", ExperimentalFeature::ABIEncoderV2 },
|
||||
{ "__test", ExperimentalFeature::Test },
|
||||
{ "__testOnlyAnalysis", ExperimentalFeature::TestOnlyAnalysis },
|
||||
};
|
||||
|
@ -1529,8 +1529,6 @@ TypePointer ArrayType::interfaceType(bool _inLibrary) const
|
||||
TypePointer baseExt = m_baseType->interfaceType(_inLibrary);
|
||||
if (!baseExt)
|
||||
return TypePointer();
|
||||
if (m_baseType->category() == Category::Array && m_baseType->isDynamicallySized())
|
||||
return TypePointer();
|
||||
|
||||
if (isDynamicallySized())
|
||||
return make_shared<ArrayType>(DataLocation::Memory, baseExt);
|
||||
|
1074
libsolidity/codegen/ABIFunctions.cpp
Normal file
1074
libsolidity/codegen/ABIFunctions.cpp
Normal file
File diff suppressed because it is too large
Load Diff
172
libsolidity/codegen/ABIFunctions.h
Normal file
172
libsolidity/codegen/ABIFunctions.h
Normal file
@ -0,0 +1,172 @@
|
||||
/*
|
||||
This file is part of solidity.
|
||||
|
||||
solidity is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
solidity is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/**
|
||||
* @author Christian <chris@ethereum.org>
|
||||
* @date 2017
|
||||
* Routines that generate JULIA code related to ABI encoding, decoding and type conversions.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <libsolidity/ast/ASTForward.h>
|
||||
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
|
||||
namespace dev {
|
||||
namespace solidity {
|
||||
|
||||
class Type;
|
||||
class ArrayType;
|
||||
class StructType;
|
||||
class FunctionType;
|
||||
using TypePointer = std::shared_ptr<Type const>;
|
||||
using TypePointers = std::vector<TypePointer>;
|
||||
|
||||
///
|
||||
/// Class to generate encoding and decoding functions. Also maintains a collection
|
||||
/// of "functions to be generated" in order to avoid generating the same function
|
||||
/// multiple times.
|
||||
///
|
||||
/// Make sure to include the result of ``requestedFunctions()`` to a block that
|
||||
/// is visible from the code that was generated here.
|
||||
class ABIFunctions
|
||||
{
|
||||
public:
|
||||
~ABIFunctions();
|
||||
|
||||
/// @returns assembly code block to ABI-encode values of @a _givenTypes residing on the stack
|
||||
/// into memory, converting the types to @a _targetTypes on the fly.
|
||||
/// Assumed variables to be present: <$value0> <$value1> ... <$value(n-1)> <$headStart>
|
||||
/// Does not allocate memory (does not change the memory head pointer), but writes
|
||||
/// to memory starting at $headStart and an unrestricted amount after that.
|
||||
/// Assigns the end of encoded memory either to $value0 or (if that is not present)
|
||||
/// to $headStart.
|
||||
std::string tupleEncoder(
|
||||
TypePointers const& _givenTypes,
|
||||
TypePointers const& _targetTypes,
|
||||
bool _encodeAsLibraryTypes = false
|
||||
);
|
||||
|
||||
/// @returns auxiliary functions referenced from the block generated in @a tupleEncoder
|
||||
std::string requestedFunctions();
|
||||
|
||||
private:
|
||||
/// @returns the name of the cleanup function for the given type and
|
||||
/// adds its implementation to the requested functions.
|
||||
/// @param _revertOnFailure if true, causes revert on invalid data,
|
||||
/// otherwise an assertion failure.
|
||||
std::string cleanupFunction(Type const& _type, bool _revertOnFailure = false);
|
||||
|
||||
/// @returns the name of the function that converts a value of type @a _from
|
||||
/// to a value of type @a _to. The resulting vale is guaranteed to be in range
|
||||
/// (i.e. "clean"). Asserts on failure.
|
||||
std::string conversionFunction(Type const& _from, Type const& _to);
|
||||
|
||||
std::string cleanupCombinedExternalFunctionIdFunction();
|
||||
|
||||
/// @returns a function that combines the address and selector to a single value
|
||||
/// for use in the ABI.
|
||||
std::string combineExternalFunctionIdFunction();
|
||||
|
||||
/// @returns the name of the ABI encoding function with the given type
|
||||
/// and queues the generation of the function to the requested functions.
|
||||
/// @param _compacted if true, the input value was just loaded from storage
|
||||
/// or memory and thus might be compacted into a single slot (depending on the type).
|
||||
std::string abiEncodingFunction(
|
||||
Type const& _givenType,
|
||||
Type const& _targetType,
|
||||
bool _encodeAsLibraryTypes,
|
||||
bool _compacted
|
||||
);
|
||||
/// Part of @a abiEncodingFunction for array target type and given calldata array.
|
||||
std::string abiEncodingFunctionCalldataArray(
|
||||
Type const& _givenType,
|
||||
Type const& _targetType,
|
||||
bool _encodeAsLibraryTypes
|
||||
);
|
||||
/// Part of @a abiEncodingFunction for array target type and given memory array or
|
||||
/// a given storage array with one item per slot.
|
||||
std::string abiEncodingFunctionSimpleArray(
|
||||
ArrayType const& _givenType,
|
||||
ArrayType const& _targetType,
|
||||
bool _encodeAsLibraryTypes
|
||||
);
|
||||
std::string abiEncodingFunctionMemoryByteArray(
|
||||
ArrayType const& _givenType,
|
||||
ArrayType const& _targetType,
|
||||
bool _encodeAsLibraryTypes
|
||||
);
|
||||
/// Part of @a abiEncodingFunction for array target type and given storage array
|
||||
/// where multiple items are packed into the same storage slot.
|
||||
std::string abiEncodingFunctionCompactStorageArray(
|
||||
ArrayType const& _givenType,
|
||||
ArrayType const& _targetType,
|
||||
bool _encodeAsLibraryTypes
|
||||
);
|
||||
|
||||
// @returns the name of the ABI encoding function with the given type
|
||||
// and queues the generation of the function to the requested functions.
|
||||
// Case for _givenType being a string literal
|
||||
std::string abiEncodingFunctionStringLiteral(
|
||||
Type const& _givenType,
|
||||
Type const& _targetType,
|
||||
bool _encodeAsLibraryTypes
|
||||
);
|
||||
|
||||
std::string abiEncodingFunctionFunctionType(
|
||||
FunctionType const& _from,
|
||||
Type const& _to,
|
||||
bool _encodeAsLibraryTypes,
|
||||
bool _compacted
|
||||
);
|
||||
|
||||
/// @returns a function that copies raw bytes of dynamic length from calldata
|
||||
/// or memory to memory.
|
||||
/// Pads with zeros and might write more than exactly length.
|
||||
std::string copyToMemoryFunction(bool _fromCalldata);
|
||||
|
||||
std::string shiftLeftFunction(size_t _numBits);
|
||||
std::string shiftRightFunction(size_t _numBits, bool _signed);
|
||||
/// @returns the name of a function that rounds its input to the next multiple
|
||||
/// of 32 or the input if it is a multiple of 32.
|
||||
std::string roundUpFunction();
|
||||
|
||||
std::string arrayLengthFunction(ArrayType const& _type);
|
||||
/// @returns the name of a function that converts a storage slot number
|
||||
/// or a memory pointer to the slot number / memory pointer for the data position of an array
|
||||
/// which is stored in that slot / memory area.
|
||||
std::string arrayDataAreaFunction(ArrayType const& _type);
|
||||
/// @returns the name of a function that advances an array data pointer to the next element.
|
||||
/// Only works for memory arrays and storage arrays that store one item per slot.
|
||||
std::string nextArrayElementFunction(ArrayType const& _type);
|
||||
|
||||
/// Helper function that uses @a _creator to create a function and add it to
|
||||
/// @a m_requestedFunctions if it has not been created yet and returns @a _name in both
|
||||
/// cases.
|
||||
std::string createFunction(std::string const& _name, std::function<std::string()> const& _creator);
|
||||
|
||||
/// @returns the size of the static part of the encoding of the given types.
|
||||
size_t headSize(TypePointers const& _targetTypes);
|
||||
|
||||
/// Map from function name to code for a multi-use function.
|
||||
std::map<std::string, std::string> m_requestedFunctions;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
@ -44,11 +44,6 @@ namespace dev
|
||||
namespace solidity
|
||||
{
|
||||
|
||||
void CompilerContext::addMagicGlobal(MagicVariableDeclaration const& _declaration)
|
||||
{
|
||||
m_magicGlobals.insert(&_declaration);
|
||||
}
|
||||
|
||||
void CompilerContext::addStateVariable(
|
||||
VariableDeclaration const& _declaration,
|
||||
u256 const& _storageOffset,
|
||||
|
@ -48,7 +48,7 @@ namespace solidity {
|
||||
class CompilerContext
|
||||
{
|
||||
public:
|
||||
CompilerContext(CompilerContext* _runtimeContext = nullptr):
|
||||
explicit CompilerContext(CompilerContext* _runtimeContext = nullptr):
|
||||
m_asm(std::make_shared<eth::Assembly>()),
|
||||
m_runtimeContext(_runtimeContext)
|
||||
{
|
||||
@ -56,7 +56,9 @@ public:
|
||||
m_runtimeSub = size_t(m_asm->newSub(m_runtimeContext->m_asm).data());
|
||||
}
|
||||
|
||||
void addMagicGlobal(MagicVariableDeclaration const& _declaration);
|
||||
void setExperimentalFeatures(std::set<ExperimentalFeature> const& _features) { m_experimentalFeatures = _features; }
|
||||
bool experimentalFeatureActive(ExperimentalFeature _feature) const { return m_experimentalFeatures.count(_feature); }
|
||||
|
||||
void addStateVariable(VariableDeclaration const& _declaration, u256 const& _storageOffset, unsigned _byteOffset);
|
||||
void addVariable(VariableDeclaration const& _declaration, unsigned _offsetToCurrent = 0);
|
||||
void removeVariable(VariableDeclaration const& _declaration);
|
||||
@ -68,7 +70,6 @@ public:
|
||||
void adjustStackOffset(int _adjustment) { m_asm->adjustDeposit(_adjustment); }
|
||||
unsigned stackHeight() const { solAssert(m_asm->deposit() >= 0, ""); return unsigned(m_asm->deposit()); }
|
||||
|
||||
bool isMagicGlobal(Declaration const* _declaration) const { return m_magicGlobals.count(_declaration) != 0; }
|
||||
bool isLocalVariable(Declaration const* _declaration) const;
|
||||
bool isStateVariable(Declaration const* _declaration) const { return m_stateVariables.count(_declaration) != 0; }
|
||||
|
||||
@ -265,8 +266,8 @@ private:
|
||||
} m_functionCompilationQueue;
|
||||
|
||||
eth::AssemblyPointer m_asm;
|
||||
/// Magic global variables like msg, tx or this, distinguished by type.
|
||||
std::set<Declaration const*> m_magicGlobals;
|
||||
/// Activated experimental features.
|
||||
std::set<ExperimentalFeature> m_experimentalFeatures;
|
||||
/// Other already compiled contracts to be used in contract creation calls.
|
||||
std::map<ContractDefinition const*, eth::Assembly const*> m_compiledContracts;
|
||||
/// Storage offsets of state variables
|
||||
|
@ -25,6 +25,7 @@
|
||||
#include <libevmasm/Instruction.h>
|
||||
#include <libsolidity/codegen/ArrayUtils.h>
|
||||
#include <libsolidity/codegen/LValue.h>
|
||||
#include <libsolidity/codegen/ABIFunctions.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@ -182,6 +183,18 @@ void CompilerUtils::encodeToMemory(
|
||||
|
||||
if (_givenTypes.empty())
|
||||
return;
|
||||
else if (
|
||||
_padToWordBoundaries &&
|
||||
!_copyDynamicDataInPlace &&
|
||||
m_context.experimentalFeatureActive(ExperimentalFeature::ABIEncoderV2)
|
||||
)
|
||||
{
|
||||
// Use the new JULIA-based encoding function
|
||||
auto stackHeightBefore = m_context.stackHeight();
|
||||
abiEncode(_givenTypes, targetTypes, _encodeAsLibraryTypes);
|
||||
solAssert(stackHeightBefore - m_context.stackHeight() == sizeOnStack(_givenTypes), "");
|
||||
return;
|
||||
}
|
||||
|
||||
// Stack during operation:
|
||||
// <v1> <v2> ... <vn> <mem_start> <dyn_head_1> ... <dyn_head_r> <end_of_mem>
|
||||
@ -289,6 +302,28 @@ void CompilerUtils::encodeToMemory(
|
||||
popStackSlots(argSize + dynPointers + 1);
|
||||
}
|
||||
|
||||
void CompilerUtils::abiEncode(
|
||||
TypePointers const& _givenTypes,
|
||||
TypePointers const& _targetTypes,
|
||||
bool _encodeAsLibraryTypes
|
||||
)
|
||||
{
|
||||
// stack: <$value0> <$value1> ... <$value(n-1)> <$headStart>
|
||||
|
||||
vector<string> variables;
|
||||
size_t numValues = sizeOnStack(_givenTypes);
|
||||
for (size_t i = 0; i < numValues; ++i)
|
||||
variables.push_back("$value" + to_string(i));
|
||||
variables.push_back("$headStart");
|
||||
|
||||
ABIFunctions funs;
|
||||
string routine = funs.tupleEncoder(_givenTypes, _targetTypes, _encodeAsLibraryTypes);
|
||||
routine += funs.requestedFunctions();
|
||||
m_context.appendInlineAssembly("{" + routine + "}", variables);
|
||||
// Remove everyhing except for "value0" / the final memory pointer.
|
||||
popStackSlots(numValues);
|
||||
}
|
||||
|
||||
void CompilerUtils::zeroInitialiseMemoryArray(ArrayType const& _type)
|
||||
{
|
||||
auto repeat = m_context.newTag();
|
||||
|
@ -103,6 +103,14 @@ public:
|
||||
bool _encodeAsLibraryTypes = false
|
||||
);
|
||||
|
||||
/// Special case of @a encodeToMemory which assumes that everything is padded to words
|
||||
/// and dynamic data is not copied in place (i.e. a proper ABI encoding).
|
||||
void abiEncode(
|
||||
TypePointers const& _givenTypes,
|
||||
TypePointers const& _targetTypes,
|
||||
bool _encodeAsLibraryTypes = false
|
||||
);
|
||||
|
||||
/// Zero-initialises (the data part of) an already allocated memory array.
|
||||
/// Length has to be nonzero!
|
||||
/// Stack pre: <length> <memptr>
|
||||
|
@ -100,6 +100,7 @@ void ContractCompiler::initializeContext(
|
||||
map<ContractDefinition const*, eth::Assembly const*> const& _compiledContracts
|
||||
)
|
||||
{
|
||||
m_context.setExperimentalFeatures(_contract.sourceUnit().annotation().experimentalFeatures);
|
||||
m_context.setCompiledContracts(_compiledContracts);
|
||||
m_context.setInheritanceHierarchy(_contract.annotation().linearizedBaseContracts);
|
||||
CompilerUtils(m_context).initialiseFreeMemoryPointer();
|
||||
|
@ -42,11 +42,36 @@ namespace test
|
||||
BOOST_CHECK_EQUAL(toHex(m_logs[0].data), toHex(DATA)); \
|
||||
} while (false)
|
||||
|
||||
static string const NewEncoderPragma = "pragma experimental ABIEncoderV2;\n";
|
||||
|
||||
#define NEW_ENCODER(CODE) \
|
||||
{ \
|
||||
sourceCode = NewEncoderPragma + sourceCode; \
|
||||
{ CODE } \
|
||||
}
|
||||
|
||||
#define BOTH_ENCODERS(CODE) \
|
||||
{ \
|
||||
{ CODE } \
|
||||
NEW_ENCODER(CODE) \
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_SUITE(ABIEncoderTest, SolidityExecutionFramework)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(both_encoders_macro)
|
||||
{
|
||||
// This tests that the "both encoders macro" at least runs twice and
|
||||
// modifies the source.
|
||||
string sourceCode;
|
||||
int runs = 0;
|
||||
BOTH_ENCODERS(runs++;)
|
||||
BOOST_CHECK(sourceCode == NewEncoderPragma);
|
||||
BOOST_CHECK_EQUAL(runs, 2);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(value_types)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
string sourceCode = R"(
|
||||
contract C {
|
||||
event E(uint a, uint16 b, uint24 c, int24 d, bytes3 x, bool, C);
|
||||
function f() {
|
||||
@ -59,16 +84,18 @@ BOOST_AUTO_TEST_CASE(value_types)
|
||||
}
|
||||
}
|
||||
)";
|
||||
compileAndRun(sourceCode);
|
||||
callContractFunction("f()");
|
||||
REQUIRE_LOG_DATA(encodeArgs(
|
||||
10, u256(65534), u256(0x121212), u256(-1), string("\x1b\xab\xab"), true, u160(u256(-5))
|
||||
));
|
||||
BOTH_ENCODERS(
|
||||
compileAndRun(sourceCode);
|
||||
callContractFunction("f()");
|
||||
REQUIRE_LOG_DATA(encodeArgs(
|
||||
10, u256(65534), u256(0x121212), u256(-1), string("\x1b\xab\xab"), true, u160(u256(-5))
|
||||
));
|
||||
)
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(string_literal)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
string sourceCode = R"(
|
||||
contract C {
|
||||
event E(string, bytes20, string);
|
||||
function f() {
|
||||
@ -76,19 +103,21 @@ BOOST_AUTO_TEST_CASE(string_literal)
|
||||
}
|
||||
}
|
||||
)";
|
||||
compileAndRun(sourceCode);
|
||||
callContractFunction("f()");
|
||||
REQUIRE_LOG_DATA(encodeArgs(
|
||||
0x60, string("abcde"), 0xa0,
|
||||
6, string("abcdef"),
|
||||
0x8b, string("abcdefabcdefgehabcabcasdfjklabcdefabcedefghabcabcasdfjklabcdefabcdefghabcabcasdfjklabcdeefabcdefghabcabcasdefjklabcdefabcdefghabcabcasdfjkl")
|
||||
));
|
||||
BOTH_ENCODERS(
|
||||
compileAndRun(sourceCode);
|
||||
callContractFunction("f()");
|
||||
REQUIRE_LOG_DATA(encodeArgs(
|
||||
0x60, string("abcde"), 0xa0,
|
||||
6, string("abcdef"),
|
||||
0x8b, string("abcdefabcdefgehabcabcasdfjklabcdefabcedefghabcabcasdfjklabcdefabcdefghabcabcasdfjklabcdeefabcdefghabcabcasdefjklabcdefabcdefghabcabcasdfjkl")
|
||||
));
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(enum_type_cleanup)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
string sourceCode = R"(
|
||||
contract C {
|
||||
enum E { A, B }
|
||||
function f(uint x) returns (E en) {
|
||||
@ -96,15 +125,17 @@ BOOST_AUTO_TEST_CASE(enum_type_cleanup)
|
||||
}
|
||||
}
|
||||
)";
|
||||
compileAndRun(sourceCode);
|
||||
BOOST_CHECK(callContractFunction("f(uint256)", 0) == encodeArgs(0));
|
||||
BOOST_CHECK(callContractFunction("f(uint256)", 1) == encodeArgs(1));
|
||||
BOOST_CHECK(callContractFunction("f(uint256)", 2) == encodeArgs());
|
||||
BOTH_ENCODERS(
|
||||
compileAndRun(sourceCode);
|
||||
BOOST_CHECK(callContractFunction("f(uint256)", 0) == encodeArgs(0));
|
||||
BOOST_CHECK(callContractFunction("f(uint256)", 1) == encodeArgs(1));
|
||||
BOOST_CHECK(callContractFunction("f(uint256)", 2) == encodeArgs());
|
||||
)
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(conversion)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
string sourceCode = R"(
|
||||
contract C {
|
||||
event E(bytes4, bytes4, uint16, uint8, int16, int8);
|
||||
function f() {
|
||||
@ -118,17 +149,96 @@ BOOST_AUTO_TEST_CASE(conversion)
|
||||
}
|
||||
}
|
||||
)";
|
||||
BOTH_ENCODERS(
|
||||
compileAndRun(sourceCode);
|
||||
callContractFunction("f()");
|
||||
REQUIRE_LOG_DATA(encodeArgs(
|
||||
string(3, 0) + string("\x0a"), string("\xf1\xf2"),
|
||||
0xff, 0xff, u256(-1), u256(1)
|
||||
));
|
||||
)
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(memory_array_one_dim)
|
||||
{
|
||||
string sourceCode = R"(
|
||||
contract C {
|
||||
event E(uint a, int16[] b, uint c);
|
||||
function f() {
|
||||
int16[] memory x = new int16[](3);
|
||||
assembly {
|
||||
for { let i := 0 } lt(i, 3) { i := add(i, 1) } {
|
||||
mstore(add(x, mul(add(i, 1), 0x20)), add(0xfffffffe, i))
|
||||
}
|
||||
}
|
||||
E(10, x, 11);
|
||||
}
|
||||
}
|
||||
)";
|
||||
|
||||
compileAndRun(sourceCode);
|
||||
callContractFunction("f()");
|
||||
REQUIRE_LOG_DATA(encodeArgs(
|
||||
string(3, 0) + string("\x0a"), string("\xf1\xf2"),
|
||||
0xff, 0xff, u256(-1), u256(1)
|
||||
));
|
||||
// The old encoder does not clean array elements.
|
||||
REQUIRE_LOG_DATA(encodeArgs(10, 0x60, 11, 3, u256("0xfffffffe"), u256("0xffffffff"), u256("0x100000000")));
|
||||
|
||||
compileAndRun(NewEncoderPragma + sourceCode);
|
||||
callContractFunction("f()");
|
||||
REQUIRE_LOG_DATA(encodeArgs(10, 0x60, 11, 3, u256(-2), u256(-1), u256(0)));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(memory_array_two_dim)
|
||||
{
|
||||
string sourceCode = R"(
|
||||
contract C {
|
||||
event E(uint a, int16[][2] b, uint c);
|
||||
function f() {
|
||||
int16[][2] memory x;
|
||||
x[0] = new int16[](3);
|
||||
x[1] = new int16[](2);
|
||||
x[0][0] = 7;
|
||||
x[0][1] = int16(0x010203040506);
|
||||
x[0][2] = -1;
|
||||
x[1][0] = 4;
|
||||
x[1][1] = 5;
|
||||
E(10, x, 11);
|
||||
}
|
||||
}
|
||||
)";
|
||||
NEW_ENCODER(
|
||||
compileAndRun(sourceCode);
|
||||
callContractFunction("f()");
|
||||
REQUIRE_LOG_DATA(encodeArgs(10, 0x60, 11, 0x40, 0xc0, 3, 7, 0x0506, u256(-1), 2, 4, 5));
|
||||
)
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(memory_byte_array)
|
||||
{
|
||||
string sourceCode = R"(
|
||||
contract C {
|
||||
event E(uint a, bytes[] b, uint c);
|
||||
function f() {
|
||||
bytes[] memory x = new bytes[](2);
|
||||
x[0] = "abcabcdefghjklmnopqrsuvwabcdefgijklmnopqrstuwabcdefgijklmnoprstuvw";
|
||||
x[1] = "abcdefghijklmnopqrtuvwabcfghijklmnopqstuvwabcdeghijklmopqrstuvw";
|
||||
E(10, x, 11);
|
||||
}
|
||||
}
|
||||
)";
|
||||
NEW_ENCODER(
|
||||
compileAndRun(sourceCode);
|
||||
callContractFunction("f()");
|
||||
REQUIRE_LOG_DATA(encodeArgs(
|
||||
10, 0x60, 11,
|
||||
2, 0x40, 0xc0,
|
||||
66, string("abcabcdefghjklmnopqrsuvwabcdefgijklmnopqrstuwabcdefgijklmnoprstuvw"),
|
||||
63, string("abcdefghijklmnopqrtuvwabcfghijklmnopqstuvwabcdeghijklmopqrstuvw")
|
||||
));
|
||||
)
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(storage_byte_array)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
string sourceCode = R"(
|
||||
contract C {
|
||||
bytes short;
|
||||
bytes long;
|
||||
@ -140,18 +250,20 @@ BOOST_AUTO_TEST_CASE(storage_byte_array)
|
||||
}
|
||||
}
|
||||
)";
|
||||
compileAndRun(sourceCode);
|
||||
callContractFunction("f()");
|
||||
REQUIRE_LOG_DATA(encodeArgs(
|
||||
0x40, 0x80,
|
||||
31, string("123456789012345678901234567890a"),
|
||||
75, string("ffff123456789012345678901234567890afffffffff123456789012345678901234567890a")
|
||||
));
|
||||
BOTH_ENCODERS(
|
||||
compileAndRun(sourceCode);
|
||||
callContractFunction("f()");
|
||||
REQUIRE_LOG_DATA(encodeArgs(
|
||||
0x40, 0x80,
|
||||
31, string("123456789012345678901234567890a"),
|
||||
75, string("ffff123456789012345678901234567890afffffffff123456789012345678901234567890a")
|
||||
));
|
||||
)
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(storage_array)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
string sourceCode = R"(
|
||||
contract C {
|
||||
address[3] addr;
|
||||
event E(address[3] a);
|
||||
@ -165,14 +277,16 @@ BOOST_AUTO_TEST_CASE(storage_array)
|
||||
}
|
||||
}
|
||||
)";
|
||||
compileAndRun(sourceCode);
|
||||
callContractFunction("f()");
|
||||
REQUIRE_LOG_DATA(encodeArgs(u160(-1), u160(-2), u160(-3)));
|
||||
BOTH_ENCODERS(
|
||||
compileAndRun(sourceCode);
|
||||
callContractFunction("f()");
|
||||
REQUIRE_LOG_DATA(encodeArgs(u160(-1), u160(-2), u160(-3)));
|
||||
)
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(storage_array_dyn)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
string sourceCode = R"(
|
||||
contract C {
|
||||
address[] addr;
|
||||
event E(address[] a);
|
||||
@ -184,14 +298,16 @@ BOOST_AUTO_TEST_CASE(storage_array_dyn)
|
||||
}
|
||||
}
|
||||
)";
|
||||
compileAndRun(sourceCode);
|
||||
callContractFunction("f()");
|
||||
REQUIRE_LOG_DATA(encodeArgs(0x20, 3, u160(1), u160(2), u160(3)));
|
||||
BOTH_ENCODERS(
|
||||
compileAndRun(sourceCode);
|
||||
callContractFunction("f()");
|
||||
REQUIRE_LOG_DATA(encodeArgs(0x20, 3, u160(1), u160(2), u160(3)));
|
||||
)
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(storage_array_compact)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
string sourceCode = R"(
|
||||
contract C {
|
||||
int72[] x;
|
||||
event E(int72[]);
|
||||
@ -208,16 +324,18 @@ BOOST_AUTO_TEST_CASE(storage_array_compact)
|
||||
}
|
||||
}
|
||||
)";
|
||||
compileAndRun(sourceCode);
|
||||
callContractFunction("f()");
|
||||
REQUIRE_LOG_DATA(encodeArgs(
|
||||
0x20, 8, u256(-1), 2, u256(-3), 4, u256(-5), 6, u256(-7), 8
|
||||
));
|
||||
BOTH_ENCODERS(
|
||||
compileAndRun(sourceCode);
|
||||
callContractFunction("f()");
|
||||
REQUIRE_LOG_DATA(encodeArgs(
|
||||
0x20, 8, u256(-1), 2, u256(-3), 4, u256(-5), 6, u256(-7), 8
|
||||
));
|
||||
)
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(external_function)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
string sourceCode = R"(
|
||||
contract C {
|
||||
event E(function(uint) external returns (uint), function(uint) external returns (uint));
|
||||
function(uint) external returns (uint) g;
|
||||
@ -227,15 +345,17 @@ BOOST_AUTO_TEST_CASE(external_function)
|
||||
}
|
||||
}
|
||||
)";
|
||||
compileAndRun(sourceCode);
|
||||
callContractFunction("f(uint256)");
|
||||
string functionIdF = asString(m_contractAddress.ref()) + asString(FixedHash<4>(dev::keccak256("f(uint256)")).ref());
|
||||
REQUIRE_LOG_DATA(encodeArgs(functionIdF, functionIdF));
|
||||
BOTH_ENCODERS(
|
||||
compileAndRun(sourceCode);
|
||||
callContractFunction("f(uint256)");
|
||||
string functionIdF = asString(m_contractAddress.ref()) + asString(FixedHash<4>(dev::keccak256("f(uint256)")).ref());
|
||||
REQUIRE_LOG_DATA(encodeArgs(functionIdF, functionIdF));
|
||||
)
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(external_function_cleanup)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
string sourceCode = R"(
|
||||
contract C {
|
||||
event E(function(uint) external returns (uint), function(uint) external returns (uint));
|
||||
// This test relies on the fact that g is stored in slot zero.
|
||||
@ -247,9 +367,35 @@ BOOST_AUTO_TEST_CASE(external_function_cleanup)
|
||||
}
|
||||
}
|
||||
)";
|
||||
compileAndRun(sourceCode);
|
||||
callContractFunction("f(uint256)");
|
||||
REQUIRE_LOG_DATA(encodeArgs(string(24, char(-1)), string(24, char(-1))));
|
||||
BOTH_ENCODERS(
|
||||
compileAndRun(sourceCode);
|
||||
callContractFunction("f(uint256)");
|
||||
REQUIRE_LOG_DATA(encodeArgs(string(24, char(-1)), string(24, char(-1))));
|
||||
)
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(calldata)
|
||||
{
|
||||
string sourceCode = R"(
|
||||
contract C {
|
||||
event E(bytes);
|
||||
function f(bytes a) external {
|
||||
E(a);
|
||||
}
|
||||
}
|
||||
)";
|
||||
string s("abcdef");
|
||||
string t("abcdefgggggggggggggggggggggggggggggggggggggggghhheeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeggg");
|
||||
bool newEncoder = false;
|
||||
BOTH_ENCODERS(
|
||||
compileAndRun(sourceCode);
|
||||
callContractFunction("f(bytes)", 0x20, s.size(), s);
|
||||
// The old encoder did not pad to multiples of 32 bytes
|
||||
REQUIRE_LOG_DATA(encodeArgs(0x20, s.size()) + (newEncoder ? encodeArgs(s) : asBytes(s)));
|
||||
callContractFunction("f(bytes)", 0x20, t.size(), t);
|
||||
REQUIRE_LOG_DATA(encodeArgs(0x20, t.size()) + (newEncoder ? encodeArgs(t) : asBytes(t)));
|
||||
newEncoder = true;
|
||||
)
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
@ -3128,7 +3128,7 @@ BOOST_AUTO_TEST_CASE(event_really_lots_of_data)
|
||||
callContractFunction("deposit()");
|
||||
BOOST_REQUIRE_EQUAL(m_logs.size(), 1);
|
||||
BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress);
|
||||
BOOST_CHECK(m_logs[0].data == encodeArgs(10, 0x60, 15, 4) + FixedHash<4>(dev::keccak256("deposit()")).asBytes());
|
||||
BOOST_CHECK_EQUAL(toHex(m_logs[0].data), toHex(encodeArgs(10, 0x60, 15, 4) + FixedHash<4>(dev::keccak256("deposit()")).asBytes()));
|
||||
BOOST_REQUIRE_EQUAL(m_logs[0].topics.size(), 1);
|
||||
BOOST_CHECK_EQUAL(m_logs[0].topics[0], dev::keccak256(string("Deposit(uint256,bytes,uint256)")));
|
||||
}
|
||||
@ -3152,7 +3152,7 @@ BOOST_AUTO_TEST_CASE(event_really_lots_of_data_from_storage)
|
||||
callContractFunction("deposit()");
|
||||
BOOST_REQUIRE_EQUAL(m_logs.size(), 1);
|
||||
BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress);
|
||||
BOOST_CHECK(m_logs[0].data == encodeArgs(10, 0x60, 15, 3, string("ABC")));
|
||||
BOOST_CHECK_EQUAL(toHex(m_logs[0].data), toHex(encodeArgs(10, 0x60, 15, 3, string("ABC"))));
|
||||
BOOST_REQUIRE_EQUAL(m_logs[0].topics.size(), 1);
|
||||
BOOST_CHECK_EQUAL(m_logs[0].topics[0], dev::keccak256(string("Deposit(uint256,bytes,uint256)")));
|
||||
}
|
||||
@ -4432,10 +4432,12 @@ BOOST_AUTO_TEST_CASE(array_copy_storage_abi)
|
||||
// NOTE: This does not really test copying from storage to ABI directly,
|
||||
// because it will always copy to memory first.
|
||||
char const* sourceCode = R"(
|
||||
pragma experimental ABIEncoderV2;
|
||||
contract c {
|
||||
uint8[] x;
|
||||
uint16[] y;
|
||||
uint24[] z;
|
||||
uint24[][] w;
|
||||
function test1() returns (uint8[]) {
|
||||
for (uint i = 0; i < 101; ++i)
|
||||
x.push(uint8(i));
|
||||
@ -4451,6 +4453,13 @@ BOOST_AUTO_TEST_CASE(array_copy_storage_abi)
|
||||
z.push(uint24(i));
|
||||
return z;
|
||||
}
|
||||
function test4() returns (uint24[][]) {
|
||||
w.length = 5;
|
||||
for (uint i = 0; i < 5; ++i)
|
||||
for (uint j = 0; j < 101; ++j)
|
||||
w[i].push(uint24(j));
|
||||
return w;
|
||||
}
|
||||
}
|
||||
)";
|
||||
compileAndRun(sourceCode);
|
||||
@ -4460,6 +4469,14 @@ BOOST_AUTO_TEST_CASE(array_copy_storage_abi)
|
||||
BOOST_CHECK(callContractFunction("test1()") == encodeArgs(0x20, 101) + valueSequence);
|
||||
BOOST_CHECK(callContractFunction("test2()") == encodeArgs(0x20, 101) + valueSequence);
|
||||
BOOST_CHECK(callContractFunction("test3()") == encodeArgs(0x20, 101) + valueSequence);
|
||||
BOOST_CHECK(callContractFunction("test4()") ==
|
||||
encodeArgs(0x20, 5, 0xa0, 0xa0 + 102 * 32 * 1, 0xa0 + 102 * 32 * 2, 0xa0 + 102 * 32 * 3, 0xa0 + 102 * 32 * 4) +
|
||||
encodeArgs(101) + valueSequence +
|
||||
encodeArgs(101) + valueSequence +
|
||||
encodeArgs(101) + valueSequence +
|
||||
encodeArgs(101) + valueSequence +
|
||||
encodeArgs(101) + valueSequence
|
||||
);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(array_copy_storage_abi_signed)
|
||||
|
Loading…
Reference in New Issue
Block a user