23 #include <boost/random.hpp> 24 #include <boost/filesystem/path.hpp> 47 bool genValidRlp =
true;
49 if (bugProbability < 80)
56 for (
auto i = 0; i <
size; i++)
59 std::string blockDebug;
66 int length = _result.size() / 2;
81 header =
toCompactHex(183 + hexlength.size() / 2) + hexlength;
96 header =
toCompactHex(247 + hexlength.size() / 2) + hexlength;
100 _result = header + _result;
101 _debug =
"[" + header +
"(" +
toString(length) +
"){" +
toString(rtype) +
"}]" + _debug;
102 return _result.size() / 2;
107 bool genbug2 =
false;
109 if (bugProbability < 50 && !genValidRlp)
112 if (bugProbability < 50 && !genValidRlp)
115 std::string emptyZeros = genValidRlp ?
"" : genbug ?
"00" :
"";
116 std::string emptyZeros2 = genValidRlp ?
"" : genbug2 ?
"00" :
"";
125 _result.insert(0, rlp);
126 _debug.insert(0,
"[" + rlp +
"]");
135 if (genValidRlp &&
fromHex(hex)[0] < 128)
138 _result.insert(0,
toCompactHex(128 + len) + emptyZeros + hex);
139 _debug.insert(0,
"[" +
toCompactHex(128 + len) +
"(" +
toString(len) +
")]" + emptyZeros + hex);
146 if (len < 56 && genValidRlp)
151 std::string rlpblock =
toCompactHex(183 + hexlen.size() / 2) + hexlen + emptyZeros + hex;
152 _debug.insert(0,
"[" +
toCompactHex(183 + hexlen.size() / 2) + hexlen +
"(" +
toString(len) +
"){2}]" + emptyZeros + hex);
153 _result.insert(0, rlpblock);
154 return rlpblock.size() / 2;
169 if (len < 56 && genValidRlp)
174 _result.insert(0, rlpblock);
175 return rlpblock.size() / 2;
194 std::string hash =
"";
196 for (
auto i = 0; i < _length; i++)
213 int size = (int)rndSizeGen();
218 for (
auto i = 0; i <
size; i++)
220 uint8_t opcode = weightsDefined ? randOpCodeWeight() :
randOpCodeGen();
223 if (info.
name.find(
"INVALID_INSTRUCTION") != std::string::npos)
234 if (info.
name.find(
"PUSH") != std::string::npos)
239 if (info.
name.find(
"PUSH") == std::string::npos)
242 code += (byte ==
"") ?
"00" : byte;
269 auto now = std::chrono::steady_clock::now().time_since_epoch();
270 auto timeSinceEpoch = std::chrono::duration_cast<std::chrono::nanoseconds>(now).
count();
271 gen.seed(static_cast<unsigned int>(timeSinceEpoch));
276 int length = _hex.length() / 2;
277 int pushCode = 96 + length - 1;
293 unsigned num = info.
args;
320 for (
int i = 0; i < times; i ++)
359 for (
unsigned i = 0; i < num; i++)
373 for (
auto i = 0; i < 255; i++)
374 mapWeights.insert(std::pair<int, int>(i, 50));
401 std::vector<int> weights;
403 weights.push_back(
element.second);
412 cnote <<
"Testing Random Code: ";
419 BOOST_ERROR(
"Exception thrown when generating random code!");
static boostIntGenerator randUniIntGen
Generate random UniformInt from uniIntDist.
std::string toCompactHex(u256 val, HexPrefix prefix=HexPrefix::DontAdd, unsigned _min=0)
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
static boostIntDistrib opCodeDist
0..255 opcodes
static boostIntGenerator randOpCodeGen
Generate random value from opCodeDist.
int args
Number of items required on the stack for this instruction (and, for the purposes of ret...
place 1 byte item on stack
static boostUInt64Generator randUInt64Gen
Generate random uInt64.
Fixture class for boost output when running testeth.
bytes rlp(_T _t)
Export a single item in RLP format, returning a byte array.
h160 Address
An Ethereum address: 20 bytes.
static std::string getPushCode(int _value)
swaps the highest and 17th highest value on the stack
boost::random::variate_generator< boost::mt19937 &, boostDescreteDistrib > boostWeightGenerator
std::string toString(string32 const &_s)
Make normal string from fixed-length string.
halt execution and register account for later deletion
copies the 16th highest item in the stack to the top of the stack
InstructionInfo instructionInfo(Instruction _inst)
Information on all the instructions.
halt execution returning output data
void addAddress(dev::Address const &_address)
bytes fromHex(std::string const &_s, WhenError _throw=WhenError::DontThrow)
static std::string rndRLPSequence(int _depth, std::string &_debug)
Generate random rlp byte sequence of a given depth (e.g [[[]],[]]).
std::string name
The name of the instruction.
void setWeight(dev::eth::Instruction _opCode, int _weight)
copies the highest item in the stack to the top of the stack
boost::random::discrete_distribution boostDescreteDistrib
static boost::random::mt19937 gen
Random generator.
message-call with another account's code only
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
static void refreshSeed()
Instruction
Virtual machine bytecode instruction.
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 64, 64, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u64
static std::string fillArguments(dev::eth::Instruction _opcode, RandomCodeOptions const &_options)
Information structure for a particular instruction.
place 32 byte item on stack
BOOST_AUTO_TEST_CASE(rndCode)
swaps the highest and second highest value on the stack
#define BOOST_FIXTURE_TEST_SUITE(a, b)
static u256 randomUniInt(u256 _maxVal=0)
static std::string rndByteSequence(int _length=1, SizeStrictness _sizeType=SizeStrictness::Strict)
Generate random byte string of a given length.
uint8_t const size_t const size
static std::string randomUniIntHex(u256 _maxVal=0)
Generate random int64.
dev::Address getRandomAddress() const
#define BOOST_AUTO_TEST_SUITE_END()
static std::string generate(int _maxOpNumber=1, RandomCodeOptions _options=RandomCodeOptions())
Generate random vm code.
message-call into an account
boostDescreteDistrib opCodeProbability
dev::WithExisting max(dev::WithExisting _a, dev::WithExisting _b)
static boostUint64Distrib uInt64Dist
0..2**64
std::vector< dev::Address > addressList
static boostIntGenerator randOpLengGen
Generate random length from opLengDist.
boost::random::uniform_int_distribution boostIntDistrib
static boostIntDistrib opLengDist
1..32 byte string
boost::random::variate_generator< boost::mt19937 &, boostUint64Distrib > boostUInt64Generator
static int recursiveRLP(std::string &_result, int _depth, std::string &_debug)
boost::uniform_int< uint64_t > boostUint64Distrib
boost::random::variate_generator< boost::mt19937 &, boostIntDistrib > boostIntGenerator
std::map< int, int > mapWeights
static boostIntDistrib uniIntDist
0..0x7fffffff
create a new account with associated code