Skip to content

Commit

Permalink
Merge pull request #75 from pipermerriam/piper/refactor-to-remove-global
Browse files Browse the repository at this point in the history
Piper/refactor to remove global
  • Loading branch information
pipermerriam committed Nov 30, 2016
2 parents 942d78f + 1eee71f commit 54bca15
Show file tree
Hide file tree
Showing 10 changed files with 327 additions and 381 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ env:
- TOX_ENV=py34
- TOX_ENV=py35
- TOX_ENV=flake8
cache: pip
cache:
pip: true
install:
- "travis_retry pip install setuptools --upgrade"
- "travis_retry pip install setuptools pip --upgrade"
- "travis_retry pip install tox"
script:
- tox -e $TOX_ENV
Expand Down
5 changes: 2 additions & 3 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ def hex_accounts(accounts):

@pytest.yield_fixture()
def rpc_server():
from testrpc.server import application
from testrpc.testrpc import full_reset
from testrpc.server import get_application

full_reset()
application = get_application()

port = get_open_port()

Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pytest==2.9.2
requests==2.10.0
gevent==1.1.2
rlp==0.4.6
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
],
entry_points={
'console_scripts': [
'testrpc=testrpc.__main__:main',
'testrpc-py=testrpc.cli:runserver',
],
}
)
13 changes: 4 additions & 9 deletions testrpc/__main__.py → testrpc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@
accounts,
)

from .server import application
from .testrpc import (
web3_clientVersion,
evm_reset,
)
from .server import get_application


@click.command()
Expand All @@ -33,11 +29,10 @@
default=8545,
type=int,
)
def main(host, port):

print(web3_clientVersion())
def runserver(host, port):
application = get_application()

evm_reset()
print(application.rpc_methods.web3_clientVersion())

print("\nAvailable Accounts\n==================")
for account in accounts:
Expand Down
225 changes: 225 additions & 0 deletions testrpc/rpc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
import sys
import logging

from sha3 import sha3_256

from ethereum.tester import (
languages,
)

from .client import EthTesterClient
from .client.utils import (
force_bytes,
decode_hex,
encode_number,
encode_32bytes,
)

from .utils import (
input_transaction_formatter,
input_filter_params_formatter,
normalize_block_number,
)


logger = logging.getLogger(__name__)


class RPCMethods(object):
def __init__(self):
self.full_reset()

def full_reset(self):
self.client = EthTesterClient()
self.reset_rpc_meta()

def reset_rpc_meta(self):
self.RPC_META = {
'eth_protocolVersion': 63,
'eth_syncing': False,
'eth_mining': True,
'net_version': 1,
'net_listening': False,
'net_peerCount': 0,
'homestead_block_number': self.client.evm.block.config['HOMESTEAD_FORK_BLKNUM'],
'dao_fork_block_number': self.client.evm.block.config['DAO_FORK_BLKNUM'],
}

def rpc_configure(self, key, value):
self.RPC_META[key] = value

if key == 'homestead_block_number':
self.client.evm.block.config['HOMESTEAD_FORK_BLKNUM'] = value
elif key == 'dao_fork_block_number':
self.client.evm.block.config['DAO_FORK_BLKNUM'] = value

#
# Snapshot and Reset
#
def evm_reset(self):
self.client.reset_evm()
return True

def evm_snapshot(self):
snapshot_idx = self.client.snapshot_evm()
return encode_number(snapshot_idx)

def evm_revert(self, snapshot_idx=None):
try:
self.client.revert_evm(snapshot_idx)
except ValueError:
return False
else:
return True

def evm_mine(self):
self.client.mine_block()

#
# eth_ Functions
#
def eth_coinbase(self):
logger.info('eth_coinbase')
return self.client.get_coinbase()

def eth_gasPrice(self):
logger.info('eth_gasPrice')
return encode_number(self.client.get_gas_price())

def eth_blockNumber(self):
logger.info('eth_blockNumber')
return encode_number(self.client.get_block_number())

def eth_sendTransaction(self, transaction):
formatted_transaction = input_transaction_formatter(transaction)
return self.client.send_transaction(**formatted_transaction)

def eth_estimateGas(self, transaction, block_number="latest"):
formatted_transaction = input_transaction_formatter(transaction)
return self.client.estimate_gas(**formatted_transaction)

def eth_sendRawTransaction(self, raw_tx):
return self.client.send_raw_transaction(raw_tx)

def eth_call(self, transaction, block_number="latest"):
formatted_transaction = input_transaction_formatter(transaction)
return self.client.call(**formatted_transaction)

def eth_accounts(self):
return self.client.get_accounts()

def eth_getCompilers(self):
return languages.keys()

def eth_compileSolidity(self, code):
# TODO: convert to python-solidity lib once it exists
raise NotImplementedError("This has not yet been implemented")

def eth_getCode(self, address, block_number="latest"):
return self.client.get_code(address, normalize_block_number(block_number))

def eth_getBalance(self, address, block_number="latest"):
return self.client.get_balance(address, normalize_block_number(block_number))

def eth_getTransactionCount(self, address, block_number="latest"):
return encode_number(self.client.get_transaction_count(
address,
normalize_block_number(block_number),
))

def eth_getTransactionByHash(self, tx_hash):
try:
return self.client.get_transaction_by_hash(tx_hash)
except ValueError:
return None

def eth_getBlockByHash(self, block_hash, full_tx=True):
return self.client.get_block_by_hash(block_hash, full_tx)

def eth_getBlockByNumber(self, block_number, full_tx=True):
return self.client.get_block_by_number(normalize_block_number(block_number), full_tx)

def eth_getTransactionReceipt(self, tx_hash):
try:
return self.client.get_transaction_receipt(tx_hash)
except ValueError:
return None

def eth_newBlockFilter(self, *args, **kwargs):
# TODO: convert to tester_client once filters implemented
raise NotImplementedError("This has not yet been implemented")

def eth_newPendingTransactionFilter(self, *args, **kwargs):
# TODO: convert to tester_client once filters implemented
raise NotImplementedError("This has not yet been implemented")

def eth_newFilter(self, filter_params):
formatted_filter_params = input_filter_params_formatter(filter_params)
return self.client.new_filter(**formatted_filter_params)

def eth_getFilterChanges(self, filter_id):
return self.client.get_filter_changes(filter_id)

def eth_getFilterLogs(self, filter_id):
return self.client.get_filter_logs(filter_id)

def eth_uninstallFilter(self, filter_id):
return self.client.uninstall_filter(filter_id)

def eth_protocolVersion(self):
return self.RPC_META['eth_protocolVersion']

def eth_syncing(self):
return self.RPC_META['eth_syncing']

def eth_mining(self):
return self.RPC_META['eth_mining']

def web3_sha3(self, value, encoding='hex'):
logger.info('web3_sha3')
if encoding == 'hex':
value = decode_hex(value)
else:
value = force_bytes(value)
return encode_32bytes(sha3_256(value).digest())

def web3_clientVersion(self):
from testrpc import __version__
return "TestRPC/" + __version__ + "/{platform}/python{v.major}.{v.minor}.{v.micro}".format(
v=sys.version_info,
platform=sys.platform,
)

#
# net_ API
#
def net_version(self):
return self.RPC_META['net_version']

def net_listening(self):
return self.RPC_META['net_listening']

def net_peerCount(self):
return self.RPC_META['net_peerCount']

#
# personal_ API
#
def personal_listAccounts(self):
return self.eth_accounts()

def personal_importRawKey(self, private_key, passphrase):
return self.client.import_raw_key(private_key, passphrase)

def personal_lockAccount(self, address):
return self.client.lock_account(address)

def personal_newAccount(self, passphrase=None):
return self.client.new_account(passphrase)

def personal_unlockAccount(self, address, passphrase, duration=None):
return self.client.unlock_account(address, passphrase, duration)

def personal_signAndSendTransaction(self, transaction, passphrase):
formatted_transaction = input_transaction_formatter(transaction)
return self.client.send_and_sign_transaction(passphrase, **formatted_transaction)
Loading

0 comments on commit 54bca15

Please sign in to comment.