<commit_before>#!/usr/bin/env python

from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer
from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCRequestHandler
from ethereum import tester as t
from rlp.utils import encode_hex
from ethereum import blocks
from ethereum import utils
from ethereum import transactions
from ethereum import processblock
from ethereum.utils import sha3
from ethereum.utils import rlp
from ethereum.tester import keys
from ethereum.tester import accounts
from ethereum.tester import languages
from collections import namedtuple
from ethereum import slogging

# Override the SimpleJSONRPCRequestHandler to support access control (*)
class SimpleJSONRPCRequestHandlerWithCORS(SimpleJSONRPCRequestHandler):
    def do_OPTIONS(self):
        self.send_response(200)
        self.end_headers()

    # Add these headers to all responses
    def end_headers(self):
        self.send_header("Access-Control-Allow-Headers",
                         "Origin, X-Requested-With, Content-Type, Accept")
        self.send_header("Access-Control-Allow-Origin", "*")
        SimpleJSONRPCRequestHandler.end_headers(self)



# Ensure tester.py uses the "official" gas limit.
t.gas_limit = 3141592

Snapshot = namedtuple("Snapshot", ["block_number", "data"])

evm = None
snapshots = None

def strip_0x(s):
    if s[0] == "0" and s[1] == "x":
        s = s[2:]
    return s

# Special non-standard function to reset the evm
# state. Useful for automated testing.
def evm_reset():
    global evm
    global snapshots
    print "Resetting EVM state..."
    evm = t.state()
    snapshots = []
    return True

def evm_snapshot():
    global evm
    global snapshots
    print "Creating snapshot #" + str(len(snapshots))
    snapshots.append(Snapshot(block_number=evm.block.number, data=evm.snapshot()))
    return "0x" + int_to_hex(len(snapshots) - 1)

def evm_revert(index=None):
    global evm
    global snapshots

    if len(snapshots) == 0:
        return False

    if index != None:
        index = int(strip_0x(index), 16)
    else:
        index = len(snapshots) - 1

    print "Reverting snapshot #" + str(index)

    if index >= len(snapshots):
        return False

    snapshot = snapshots[index]

    # print str(snapshot.block_number + 1)
    # print str(len(evm.blocks))

    # Remove all blocks after our saved block number.
    del evm.blocks[snapshot.block_number + 1:len(evm.blocks)]

    # Revert the evm
    evm.revert(snapshot.data)

    # Remove all snapshots after and including this one.
    del snapshots[index:len(snapshots)]
    return True

# init state
evm_reset()

t.set_logging_level(2)
#slogging.configure(':info,eth.pb:debug,eth.vm.exit:trace')
#slogging.configure(':info,eth.vm.exit:debug,eth.pb.tx:info')

print "Ready!"


def isContract(transaction):
    if "to" not in transaction and "data" in transaction:
        return True
    else:
        return False


def int_to_hex(int_value):
    encoded = format(int_value, 'x')
    return encoded.zfill(len(encoded)*2 % 2)


def eth_coinbase():
    print 'eth_coinbase'
    return '0x' + encode_hex(evm.block.coinbase)


def eth_gasPrice():
    print 'eth_gasPrice'
    return '0x' + int_to_hex(1)


def eth_blockNumber():
    print 'eth_blockNumber'
    return '0x' + int_to_hex(evm.block.number)


def send(transaction):
    if "from" in transaction:
        addr = strip_0x(transaction['from']).decode("hex")
        sender = keys[accounts.index(addr)]
    else:
        sender = keys[0]

    if "value" in transaction:
        value = transaction['value']
    else:
        value = 0

    if "to" in transaction:
        to = strip_0x(transaction['to'])
    else:<commit_msg>fix value to be int (eg it was being passed to pyethereum as 0x0)<commit_after>#!/usr/bin/env python

from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer
from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCRequestHandler
from ethereum import tester as t
from rlp.utils import encode_hex
from ethereum import blocks
from ethereum import utils
from ethereum import transactions
from ethereum import processblock
from ethereum.utils import sha3
from ethereum.utils import rlp
from ethereum.tester import keys
from ethereum.tester import accounts
from ethereum.tester import languages
from collections import namedtuple
from ethereum import slogging

# Override the SimpleJSONRPCRequestHandler to support access control (*)
class SimpleJSONRPCRequestHandlerWithCORS(SimpleJSONRPCRequestHandler):
    def do_OPTIONS(self):
        self.send_response(200)
        self.end_headers()

    # Add these headers to all responses
    def end_headers(self):
        self.send_header("Access-Control-Allow-Headers",
                         "Origin, X-Requested-With, Content-Type, Accept")
        self.send_header("Access-Control-Allow-Origin", "*")
        SimpleJSONRPCRequestHandler.end_headers(self)



# Ensure tester.py uses the "official" gas limit.
t.gas_limit = 3141592

Snapshot = namedtuple("Snapshot", ["block_number", "data"])

evm = None
snapshots = None

def strip_0x(s):
    if s[0] == "0" and s[1] == "x":
        s = s[2:]
    return s

# Special non-standard function to reset the evm
# state. Useful for automated testing.
def evm_reset():
    global evm
    global snapshots
    print "Resetting EVM state..."
    evm = t.state()
    snapshots = []
    return True

def evm_snapshot():
    global evm
    global snapshots
    print "Creating snapshot #" + str(len(snapshots))
    snapshots.append(Snapshot(block_number=evm.block.number, data=evm.snapshot()))
    return "0x" + int_to_hex(len(snapshots) - 1)

def evm_revert(index=None):
    global evm
    global snapshots

    if len(snapshots) == 0:
        return False

    if index != None:
        index = int(strip_0x(index), 16)
    else:
        index = len(snapshots) - 1

    print "Reverting snapshot #" + str(index)

    if index >= len(snapshots):
        return False

    snapshot = snapshots[index]

    # print str(snapshot.block_number + 1)
    # print str(len(evm.blocks))

    # Remove all blocks after our saved block number.
    del evm.blocks[snapshot.block_number + 1:len(evm.blocks)]

    # Revert the evm
    evm.revert(snapshot.data)

    # Remove all snapshots after and including this one.
    del snapshots[index:len(snapshots)]
    return True

# init state
evm_reset()

t.set_logging_level(2)
#slogging.configure(':info,eth.pb:debug,eth.vm.exit:trace')
#slogging.configure(':info,eth.vm.exit:debug,eth.pb.tx:info')

print "Ready!"


def isContract(transaction):
    if "to" not in transaction and "data" in transaction:
        return True
    else:
        return False


def int_to_hex(int_value):
    encoded = format(int_value, 'x')
    return encoded.zfill(len(encoded)*2 % 2)


def eth_coinbase():
    print 'eth_coinbase'
    return '0x' + encode_hex(evm.block.coinbase)


def eth_gasPrice():
    print 'eth_gasPrice'
    return '0x' + int_to_hex(1)


def eth_blockNumber():
    print 'eth_blockNumber'
    return '0x' + int_to_hex(evm.block.number)


def send(transaction):
    if "from" in transaction:
        addr = strip_0x(transaction['from']).decode("hex")
        sender = keys[accounts.index(addr)]
    else:
        sender = keys[0]

    if "value" in transaction:
        value = int(strip_0x(transaction['value']), 16)
    else:
        value = 0

    if "to" in transaction:
        to = strip_0x(transaction['to'])
    else:
{'fraction_non_alphanumeric': 0.10289172831203766, 'fraction_numerical': 0.01035642232683255, 'mean_word_length': 3.450029922202274, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2787258', 'n_tokens_mistral': 2434, 'n_tokens_neox': 2375, 'n_words': 633}