This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add get-accounts * add get-account tests * add new test key * update readme * remove comment * fix docstring * Update README.md Co-authored-by: Martín Triay <[email protected]> * add nre msg for get-accounts * fix logging * update test logs * test nre get-accounts msgs * add link to docs in print msg * add link to docs, move internal func * update test logs * formatting * change log to print * update msgs in get-accounts tests Co-authored-by: Martín Triay <[email protected]>
- Loading branch information
1 parent
b51f41a
commit 951cf24
Showing
10 changed files
with
231 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
"""Retrieve and manage deployed accounts.""" | ||
import json | ||
import logging | ||
|
||
from nile.accounts import current_index | ||
from nile.core.account import Account | ||
|
||
|
||
def get_accounts(network): | ||
"""Retrieve deployed accounts.""" | ||
try: | ||
total_accounts = current_index(network) | ||
logging.info(f"\nTotal registered accounts: {total_accounts}\n") | ||
except FileNotFoundError: | ||
print(f"\n❌ No registered accounts detected in {network}.accounts.json") | ||
print("For more info, see https://github.com/OpenZeppelin/nile#get-accounts\n") | ||
return | ||
|
||
with open(f"{network}.accounts.json", "r") as f: | ||
account_data = json.load(f) | ||
|
||
accounts = [] | ||
pubkeys = list(account_data.keys()) | ||
addresses = [i["address"] for i in account_data.values()] | ||
signers = [i["alias"] for i in account_data.values()] | ||
|
||
for i in range(total_accounts): | ||
logging.info(f"{i}: {addresses[i]}") | ||
|
||
_account = _check_and_return_account(signers[i], pubkeys[i], network) | ||
accounts.append(_account) | ||
|
||
logging.info("\n🚀 Successfully retrieved deployed accounts") | ||
return accounts | ||
|
||
|
||
def _check_and_return_account(signer, pubkey, network): | ||
account = Account(signer, network) | ||
assert str(pubkey) == str( | ||
account.signer.public_key | ||
), "Signer pubkey does not match deployed pubkey" | ||
return account |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
"""Tests for get-accounts command.""" | ||
import logging | ||
from unittest.mock import MagicMock, patch | ||
|
||
import pytest | ||
|
||
from nile.core.account import Account | ||
from nile.utils.get_accounts import _check_and_return_account, get_accounts | ||
|
||
NETWORK = "goerli" | ||
PUBKEYS = [ | ||
"883045738439352841478194533192765345509759306772397516907181243450667673002", | ||
"661519931401775515888740911132355225260405929679788917190706536765421826262", | ||
] | ||
ADDRESSES = ["333", "444"] | ||
INDEXES = [0, 1] | ||
ALIASES = ["TEST_KEY", "TEST_KEY_2"] | ||
|
||
MOCK_ACCOUNTS = { | ||
PUBKEYS[0]: { | ||
"address": ADDRESSES[0], | ||
"index": INDEXES[0], | ||
"alias": ALIASES[0], | ||
}, | ||
PUBKEYS[1]: { | ||
"address": ADDRESSES[1], | ||
"index": INDEXES[1], | ||
"alias": ALIASES[1], | ||
}, | ||
} | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def tmp_working_dir(monkeypatch, tmp_path): | ||
monkeypatch.chdir(tmp_path) | ||
return tmp_path | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def mock_subprocess(): | ||
with patch("nile.core.compile.subprocess") as mock_subprocess: | ||
yield mock_subprocess | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"private_keys, public_keys", | ||
[ | ||
([ALIASES[0], PUBKEYS[0]]), | ||
([ALIASES[1], PUBKEYS[1]]), | ||
], | ||
) | ||
def test__check_and_return_account_with_matching_keys(private_keys, public_keys): | ||
# Check matching public/private keys | ||
account = _check_and_return_account(private_keys, public_keys, NETWORK) | ||
|
||
assert type(account) is Account | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"private_keys, public_keys", | ||
[ | ||
([ALIASES[0], PUBKEYS[1]]), | ||
([ALIASES[1], PUBKEYS[0]]), | ||
], | ||
) | ||
def test__check_and_return_account_with_mismatching_keys(private_keys, public_keys): | ||
# Check mismatched public/private keys | ||
with pytest.raises(AssertionError) as err: | ||
_check_and_return_account(private_keys, public_keys, NETWORK) | ||
|
||
assert "Signer pubkey does not match deployed pubkey" in str(err.value) | ||
|
||
|
||
def test_get_accounts_no_activated_accounts_feedback(capsys): | ||
get_accounts(NETWORK) | ||
# This test uses capsys in order to test the print statements (instead of logging) | ||
captured = capsys.readouterr() | ||
|
||
assert ( | ||
f"❌ No registered accounts detected in {NETWORK}.accounts.json" in captured.out | ||
) | ||
assert ( | ||
"For more info, see https://github.com/OpenZeppelin/nile#get-accounts" | ||
in captured.out | ||
) | ||
|
||
|
||
@patch("nile.utils.get_accounts.current_index", MagicMock(return_value=len(PUBKEYS))) | ||
@patch("nile.utils.get_accounts.open", MagicMock()) | ||
@patch("nile.utils.get_accounts.json.load", MagicMock(return_value=MOCK_ACCOUNTS)) | ||
def test_get_accounts_activated_accounts_feedback(caplog): | ||
logging.getLogger().setLevel(logging.INFO) | ||
|
||
# Default argument | ||
get_accounts(NETWORK) | ||
|
||
# Check total accounts log | ||
assert f"\nTotal registered accounts: {len(PUBKEYS)}\n" in caplog.text | ||
|
||
# Check index/address log | ||
for i in range(len(PUBKEYS)): | ||
assert f"{INDEXES[i]}: {ADDRESSES[i]}" in caplog.text | ||
|
||
# Check final success log | ||
assert "\n🚀 Successfully retrieved deployed accounts" in caplog.text | ||
|
||
|
||
@patch("nile.utils.get_accounts.current_index", MagicMock(return_value=len(PUBKEYS))) | ||
@patch("nile.utils.get_accounts.open", MagicMock()) | ||
@patch("nile.utils.get_accounts.json.load", MagicMock(return_value=MOCK_ACCOUNTS)) | ||
def test_get_accounts_with_keys(): | ||
|
||
with patch( | ||
"nile.utils.get_accounts._check_and_return_account" | ||
) as mock_return_account: | ||
result = get_accounts(NETWORK) | ||
|
||
# Check correct args are passed to `_check_and_receive_account` | ||
for i in range(len(PUBKEYS)): | ||
mock_return_account.assert_any_call(ALIASES[i], PUBKEYS[i], NETWORK) | ||
|
||
# Assert call count equals correct number of accounts | ||
assert mock_return_account.call_count == len(PUBKEYS) | ||
|
||
# assert returned accounts array equals correct number of accounts | ||
assert len(result) == len(PUBKEYS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
Only unit tests for now. | ||
""" | ||
|
||
from unittest.mock import patch | ||
|
||
import click | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters