Skip to content

Commit

Permalink
Merge pull request #41 from pipermerriam/piper/fix-clientVersion-to-b…
Browse files Browse the repository at this point in the history
…e-correct

implement and test the personal api
  • Loading branch information
pipermerriam authored Jun 30, 2016
2 parents 358a9ac + d219067 commit ce734e2
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 7 deletions.
8 changes: 4 additions & 4 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ def get_open_port():
@pytest.fixture(scope="session")
def accounts():
from ethereum import tester
from ethereum import utils
return [utils.encode_hex(acct) for acct in tester.accounts]
from eth_tester_client.utils import normalize_address
return [normalize_address(acct) for acct in tester.accounts]


@pytest.yield_fixture()
def rpc_server():
from wsgiref.simple_server import make_server
from testrpc.server import application
from testrpc.testrpc import evm_reset
from testrpc.testrpc import full_reset

evm_reset()
full_reset()

port = get_open_port()

Expand Down
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
Werkzeug>=0.11.10
json-rpc>=1.10.3
# TODO: bump this once the next version of pyethereum is released.
ethereum>=1.3.6
rlp>=0.4.4
ethereum-tester-client>=0.3.0
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
# TODO: bump this once the next version of pyethereum is released.
'ethereum>=1.3.6',
'rlp>=0.4.4',
'ethereum-tester-client>=0.2.2',
'ethereum-tester-client>=0.3.0',
],
entry_points={
'console_scripts': [
Expand Down
6 changes: 6 additions & 0 deletions testrpc/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ def add_method_with_lock(rpc_fn, *args, **kwargs):
add_method_with_lock(testrpc.evm_revert, 'evm_revert')
add_method_with_lock(testrpc.evm_mine, 'evm_mine')
add_method_with_lock(testrpc.rpc_configure, 'rpc_configure')
add_method_with_lock(testrpc.personal_importRawKey, 'personal_importRawKey')
add_method_with_lock(testrpc.personal_listAccounts, 'personal_listAccounts')
add_method_with_lock(testrpc.personal_lockAccount, 'personal_lockAccount')
add_method_with_lock(testrpc.personal_newAccount, 'personal_newAccount')
add_method_with_lock(testrpc.personal_unlockAccount, 'personal_unlockAccount')
add_method_with_lock(testrpc.personal_sendTransaction, 'personal_sendTransaction')


@Request.application
Expand Down
37 changes: 35 additions & 2 deletions testrpc/testrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
tester_client = EthTesterClient()


def full_reset():
global tester_client
tester_client = EthTesterClient()


#
# Snapshot and Reset
#
Expand Down Expand Up @@ -202,8 +207,9 @@ def web3_sha3(value):


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


Expand All @@ -217,3 +223,30 @@ def net_listening():

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


def personal_listAccounts():
return eth_accounts()


def personal_importRawKey(private_key, passphrase):
return tester_client.import_raw_key(private_key, passphrase)


def personal_lockAccount(address):
return tester_client.lock_account(address)


def personal_newAccount(passphrase=None):
return tester_client.new_account(passphrase)


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


def personal_sendTransaction(transaction, passphrase):
txn_kwargs = {
TXN_KWARGS_MAP.get(k, k): v for k, v in transaction.items()
}
return tester_client.send_and_sign_transaction(passphrase, **txn_kwargs)
41 changes: 41 additions & 0 deletions tests/endpoints/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import pytest


@pytest.fixture()
def account_password():
return "a-password"


@pytest.fixture()
def account_private_key():
from eth_tester_client.utils import mk_random_privkey
return mk_random_privkey()


@pytest.fixture()
def account_public_key(account_private_key):
from ethereum.utils import privtoaddr
from eth_tester_client.utils import encode_address
return encode_address(privtoaddr(account_private_key))


@pytest.fixture()
def password_account(rpc_client, accounts, account_password,
account_private_key, account_public_key):
from eth_tester_client.utils import normalize_address
address = rpc_client(
'personal_importRawKey',
[account_private_key, account_password],
)
assert normalize_address(address) == normalize_address(account_public_key)

initial_balance = 1000000000000000000000 # 1,000 ether

rpc_client('eth_sendTransaction', [{
'from': accounts[0],
'to': address,
'value': initial_balance,
}])

assert rpc_client('eth_getBalance', [address]) == initial_balance
return address
16 changes: 16 additions & 0 deletions tests/endpoints/test_personal_importRawKey.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from eth_tester_client.utils import (
normalize_address,
mk_random_privkey,
)


def test_personal_importRawKey(accounts, rpc_client):
initial_accounts = rpc_client('personal_listAccounts')

private_key = mk_random_privkey()

new_account = rpc_client('personal_importRawKey', [private_key, 'a-password'])
n_new_account = normalize_address(new_account)

assert n_new_account in {normalize_address(acct) for acct in rpc_client('personal_listAccounts')}
assert rpc_client('personal_unlockAccount', [new_account, 'a-password']) is True
11 changes: 11 additions & 0 deletions tests/endpoints/test_personal_listAccounts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from eth_tester_client.utils import (
normalize_address,
)


def test_personal_listAccounts(accounts, rpc_client):
actual = rpc_client('personal_listAccounts')
n_actual = {normalize_address(acct) for acct in actual}
n_expected = {normalize_address(acct) for acct in accounts}

assert n_actual == n_expected
38 changes: 38 additions & 0 deletions tests/endpoints/test_personal_lockAccount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import pytest

from eth_tester_client.utils import (
normalize_address,
mk_random_privkey,
)


def test_personal_lockAccount(accounts, rpc_client, password_account, account_password):
assert rpc_client('personal_unlockAccount', [password_account, account_password])

initial_balance = rpc_client('eth_getBalance', [accounts[1]])

# confirm it's unlocked
rpc_client('eth_sendTransaction', [{
'from': password_account,
'to': accounts[1],
'value': 1234,
}])
after_balance = rpc_client('eth_getBalance', [accounts[1]])

assert after_balance - initial_balance == 1234

assert rpc_client('personal_lockAccount', [password_account])

# sanity check
before_balance = rpc_client('eth_getBalance', [accounts[2]])

with pytest.raises(AssertionError):
# confirm it's now locked
rpc_client('eth_sendTransaction', [{
'from': password_account,
'to': accounts[2],
'value': 1234,
}])

# sanity check
assert rpc_client('eth_getBalance', [accounts[2]]) == before_balance
7 changes: 7 additions & 0 deletions tests/endpoints/test_personal_newAccount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def test_personal_newAccount(rpc_client):
initial_accounts = rpc_client('personal_listAccounts')

new_account = rpc_client('personal_newAccount', ['some-password'])

assert new_account not in initial_accounts
assert new_account in rpc_client('personal_listAccounts')
23 changes: 23 additions & 0 deletions tests/endpoints/test_personal_sendTransaction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest


def test_personal_sendTransaction(accounts, rpc_client, password_account, account_password):
initial_balance = rpc_client('eth_getBalance', [accounts[1]])

# confirm it fails with a bad password
with pytest.raises(AssertionError):
rpc_client('personal_sendTransaction', [{
'from': password_account,
'to': accounts[1],
'value': 1234,
}, "incorrect-password"])
assert rpc_client('eth_getBalance', [accounts[1]]) == initial_balance

rpc_client('personal_sendTransaction', [{
'from': password_account,
'to': accounts[1],
'value': 1234,
}, account_password])
after_balance = rpc_client('eth_getBalance', [accounts[1]])

assert after_balance - initial_balance == 1234
42 changes: 42 additions & 0 deletions tests/endpoints/test_personal_unlockAccount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import pytest

from eth_tester_client.utils import (
normalize_address,
mk_random_privkey,
)


def test_personal_unlockAccount(accounts, rpc_client, password_account, account_password):
initial_balance = rpc_client('eth_getBalance', [accounts[1]])

# confirm it didn't start unlocked
with pytest.raises(AssertionError):
rpc_client('eth_sendTransaction', [{
'from': password_account,
'to': accounts[2],
'value': 1234,
}])
assert rpc_client('eth_getBalance', [accounts[1]]) == initial_balance

assert not rpc_client('personal_unlockAccount', [password_account, 'not-correct-password'])

# confirm it didn't get unlocked.
with pytest.raises(AssertionError):
rpc_client('eth_sendTransaction', [{
'from': password_account,
'to': accounts[2],
'value': 1234,
}])
assert rpc_client('eth_getBalance', [accounts[1]]) == initial_balance

assert rpc_client('personal_unlockAccount', [password_account, account_password])

# confirm it's unlocked
rpc_client('eth_sendTransaction', [{
'from': password_account,
'to': accounts[1],
'value': 1234,
}])
after_balance = rpc_client('eth_getBalance', [accounts[1]])

assert after_balance - initial_balance == 1234

0 comments on commit ce734e2

Please sign in to comment.