Skip to content

Commit

Permalink
autonity subcommand supporting all view calls
Browse files Browse the repository at this point in the history
  • Loading branch information
dtebbs committed Nov 3, 2022
1 parent 78afbb3 commit 32ed653
Show file tree
Hide file tree
Showing 9 changed files with 296 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ autcli.egg-info
build
notes.org
scripts/examples/*

_test_*
11 changes: 10 additions & 1 deletion autcli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
Autonity RPC Client
"""

from autcli.commands import maketx, signtx, sendtx, waittx, get, list as list_group
from autcli.commands import (
maketx,
signtx,
sendtx,
waittx,
get,
list as list_group,
autonity,
)
from autcli.logging import enable_logging

from click import group, option
Expand All @@ -25,3 +33,4 @@ def aut(verbose: bool) -> None:
aut.add_command(waittx.waittx)
aut.add_command(get.get)
aut.add_command(list_group.list_group)
aut.add_command(autonity.autonity)
167 changes: 167 additions & 0 deletions autcli/commands/autonity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
"""
The `autonity` command group.
"""

from autcli.options import rpc_endpoint_option
from autcli.utils import web3_from_endpoint_arg, to_json

from autonity import Autonity

from web3 import Web3
from click import group, command, argument
from typing import Callable, Sequence, Optional, Any


@group()
def autonity() -> None:
"""
Commands related to the Autonity contract. See the Autonity
contract reference for details.
"""


GetMethod = Callable[[Autonity], Any]


def _show_sequence(value: Sequence[Any]) -> str:
return "\n".join([str(v) for v in value])


def _show_json(value: Any) -> str:
return to_json(value, pretty=True)


def _autonity_getter_command(
name: str,
method: GetMethod,
comment: str,
show: Optional[Callable[[Any], str]] = None,
) -> None:
"""
Create an autonity method and register it.
"""

@command(name=name, help=comment)
@rpc_endpoint_option
def cmd(rpc_endpoint: Optional[str]) -> None:
aut = Autonity(web3_from_endpoint_arg(None, rpc_endpoint))
ret = method(aut)
if show:
print(show(ret))
else:
print(ret)

autonity.add_command(cmd)


_autonity_getter_command(
"config", Autonity.config, "Print the Autonity contract config", _show_json
)
_autonity_getter_command("epoch-id", Autonity.epoch_id, "ID of current epoch")
_autonity_getter_command(
"last-epoch-block", Autonity.last_epoch_block, "block number of the last epoch"
)
_autonity_getter_command(
"epoch-total-bonded-stake",
Autonity.epoch_total_bonded_stake,
"Total stake bonded this epoch",
)
_autonity_getter_command(
"total-redistributed", Autonity.total_redistributed, "Total fees redistributed"
)
_autonity_getter_command("epoch-reward", Autonity.epoch_reward, "Reward for this epoch")
_autonity_getter_command(
"tail-bonding-id", Autonity.tail_bonding_id, "Tail ID of bonding queue"
)
_autonity_getter_command(
"head-bonding-id", Autonity.head_bonding_id, "Head ID of bonding queue"
)
_autonity_getter_command(
"tail-unbonding-id", Autonity.tail_unbonding_id, "Tail ID of unbondign queue "
)
_autonity_getter_command(
"head-unbonding-id", Autonity.head_unbonding_id, "Head ID of unbondign queue "
)
_autonity_getter_command("deployer", Autonity.deployer, "Contract deployer")
_autonity_getter_command(
"get-last-epoch-block", Autonity.get_last_epoch_block, "Block of last epoch"
)
_autonity_getter_command("get-version", Autonity.get_version, "Contract version")
_autonity_getter_command(
"get-committee", Autonity.get_committee, "Get current committee", _show_json
)
_autonity_getter_command(
"get-validators", Autonity.get_validators, "Get current validators", _show_sequence
)


@command()
@rpc_endpoint_option
@argument("validator_address", nargs=1)
def get_validator(rpc_endpoint: Optional[str], validator_address: str) -> None:
"""
Get the information about a specific validator.
"""
addr = Web3.toChecksumAddress(validator_address)
aut = Autonity(web3_from_endpoint_arg(None, rpc_endpoint))
print(_show_json(aut.get_validator(addr)))


autonity.add_command(get_validator)


_autonity_getter_command(
"get-max-committee-size", Autonity.get_max_committee_size, "Maximum committee size"
)
_autonity_getter_command(
"get-committee-enodes", Autonity.get_committee_enodes, "Enodes in current committee"
)
_autonity_getter_command(
"get-minimum-base-fee", Autonity.get_minimum_base_fee, "Minimum base fee"
)
_autonity_getter_command("get-operator", Autonity.get_operator, "Contract operator")


@command()
@rpc_endpoint_option
@argument("height", type=int, nargs=1)
@argument("round_", metavar="ROUND", type=int, nargs=1)
def get_proposer(rpc_endpoint: Optional[str], height: int, round_: int) -> None:
"""
Proposer at the given height and round
"""
aut = Autonity(web3_from_endpoint_arg(None, rpc_endpoint))
print(aut.get_proposer(height, round_))


autonity.add_command(get_proposer)


@command()
@rpc_endpoint_option
@argument("start", type=int, nargs=1)
@argument("end", type=int, nargs=1)
def get_bonding_req(rpc_endpoint: Optional[str], start: int, end: int) -> None:
"""
Get queued bonding information between start and end ids.
"""
aut = Autonity(web3_from_endpoint_arg(None, rpc_endpoint))
print(_show_json(aut.get_bonding_req(start, end)))


autonity.add_command(get_bonding_req)


@command()
@rpc_endpoint_option
@argument("start", type=int, nargs=1)
@argument("end", type=int, nargs=1)
def get_unbonding_req(rpc_endpoint: Optional[str], start: int, end: int) -> None:
"""
Get queued unbonding information between start and end ids.
"""
aut = Autonity(web3_from_endpoint_arg(None, rpc_endpoint))
print(_show_json(aut.get_unbonding_req(start, end)))


autonity.add_command(get_unbonding_req)
6 changes: 3 additions & 3 deletions autcli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def to_checksum_address(address: str) -> ChecksumAddress:
return checksum_address


def to_json(data: Mapping[str, V], pretty=False) -> str:
def to_json(data: Mapping[str, V], pretty: bool = False) -> str:
"""
Take python data structure, return json formatted data.
Expand All @@ -151,8 +151,8 @@ def to_json(data: Mapping[str, V], pretty=False) -> str:
"""
if pretty:
return json.dumps(cast(Dict[Any, Any], data), indent=2)
else:
return Web3.toJSON(cast(Dict[Any, Any], data))

return Web3.toJSON(cast(Dict[Any, Any], data))


def string_is_32byte_hash(hash_str: str) -> bool:
Expand Down
82 changes: 82 additions & 0 deletions scripts/autonity_getters_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env bash

set -x
set -e

mkdir -p _test_autonity_getters
pushd _test_autonity_getters

../scripts/setup_config_file.sh

echo "aut autonity config"
aut autonity config

echo "aut autonity deployer"
aut autonity deployer

echo "aut autonity epoch-id"
aut autonity epoch-id

echo "aut autonity epoch-reward"
aut autonity epoch-reward

echo "aut autonity epoch-total-bonded-stake"
aut autonity epoch-total-bonded-stake

echo "aut autonity get-bonding-req"
aut autonity get-bonding-req 100 110

echo "aut autonity get-committee"
aut autonity get-committee

echo "aut autonity get-committee-enodes"
aut autonity get-committee-enodes

echo "aut autonity get-last-epoch-block"
aut autonity get-last-epoch-block

echo "aut autonity get-max-committee-size"
aut autonity get-max-committee-size

echo "aut autonity get-minimum-base-fee"
aut autonity get-minimum-base-fee

echo "aut autonity get-operator"
aut autonity get-operator

echo "aut autonity get-proposer"
aut autonity get-proposer 100 3

echo "aut autonity get-unbonding-req"
aut autonity get-unbonding-req 200 220

echo "aut autonity get-validators"
aut autonity get-validators > validators
cat validators
v1=$(head -n1 validators)

echo "aut autonity get-validator"
aut autonity get-validator ${v1}

echo "aut autonity get-version"
aut autonity get-version

echo "aut autonity head-bonding-id"
aut autonity head-bonding-id

echo "aut autonity head-unbonding-id"
aut autonity head-unbonding-id

echo "aut autonity last-epoch-block"
aut autonity last-epoch-block

echo "aut autonity tail-bonding-id"
aut autonity tail-bonding-id

echo "aut autonity tail-unbonding-id"
aut autonity tail-unbonding-id

echo "aut autonity total-redistributed"
aut autonity total-redistributed

popd
26 changes: 26 additions & 0 deletions scripts/autonity_getters_tests.sh~
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

# Assumes env vars or .autrc file is available

aut autonity config
aut autonity deployer
aut autonity epoch-id
aut autonity epoch-reward
aut autonity epoch-total-bonded-stake
aut autonity get-bonding-req
aut autonity get-committee
aut autonity get-committee-enodes
aut autonity get-last-epoch-block
aut autonity get-max-committee-size
aut autonity get-minimum-base-fee
aut autonity get-operator
aut autonity get-proposer
aut autonity get-unbonding-req
aut autonity get-validator
aut autonity get-validators
aut autonity get-version
aut autonity head-bonding-id
aut autonity head-unbonding-id
aut autonity last-epoch-block
aut autonity tail-bonding-id
aut autonity tail-unbonding-id
aut autonity total-redistributed
5 changes: 1 addition & 4 deletions scripts/cli_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ pushd _test_data

# Remove WEB3_ENDPOINT env var and set up a configuration file instead
unset WEB3_ENDPOINT

echo '[aut]' > .autrc
echo 'rpc_endpoint = https://rpc1.piccadilly.autonity.org:8545/' >> .autrc
echo 'keyfile = keystore/alice.key' >> .autrc
../scripts/setup_config_file.sh

# Get Alice and Bob's balances
alice_balance_orig=`aut get balance $ALICE`
Expand Down
5 changes: 5 additions & 0 deletions scripts/setup_config_file.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

echo '[aut]' > .autrc
echo 'rpc_endpoint = https://rpc1.piccadilly.autonity.org:8545/' >> .autrc
echo 'keyfile = keystore/alice.key' >> .autrc

0 comments on commit 32ed653

Please sign in to comment.