Skip to content

Commit

Permalink
Update chia-blockchain to 2.1.0 and major updates to precommit and my…
Browse files Browse the repository at this point in the history
…py cleanup
  • Loading branch information
emlowe committed Oct 17, 2023
1 parent 0f3e4db commit 3a1c49d
Show file tree
Hide file tree
Showing 33 changed files with 1,803 additions and 1,616 deletions.
10 changes: 10 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[flake8]
max-line-length = 120
exclude = ./typings/**/*
ignore = E203,W503
per-file-ignores =
# line too long
tests/test_activities_api.py: E501
tests/test_crud_chia.py: E501
# imported but unused
app/api/v1/__init__.py: F401
21 changes: 7 additions & 14 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,20 @@ repos:
repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
- hooks:
- args:
- --in-place
- --remove-all-unused-imports
- --expand-star-imports
- --remove-duplicate-keys
- --remove-unused-variables
exclude: .*/__init__.py
id: autoflake
repo: https://github.com/myint/autoflake
rev: v1.4
- id: flake8
repo: https://github.com/pycqa/flake8
rev: 6.1.0
- hooks:
- args:
- --profile
- black
id: isort
repo: https://github.com/pre-commit/mirrors-isort
rev: v5.10.1
repo: https://github.com/pycqa/isort
rev: 5.12.0
- hooks:
- id: black
repo: https://github.com/psf/black
rev: 22.3.0
rev: 23.7.0
- hooks:
- entry: mypy
id: mypy
Expand All @@ -35,7 +28,7 @@ repos:
- python
- pyi
repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.961
rev: v1.4.1
- hooks:
- id: commitizen
stages:
Expand Down
6 changes: 3 additions & 3 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.



Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
Expand Down
3 changes: 0 additions & 3 deletions app/api/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ async def _get_rpc_client(
rpc_port: int,
root_path: Path = DEFAULT_ROOT_PATH,
) -> Iterator[RpcClient]:

rpc_client_cls = {
NodeType.FULL_NODE: FullNodeRpcClient,
NodeType.WALLET: WalletRpcClient,
Expand All @@ -63,7 +62,6 @@ async def _get_rpc_client(


async def get_wallet_rpc_client() -> Iterator[WalletRpcClient]:

async for _ in _get_rpc_client(
node_type=NodeType.WALLET,
self_hostname=settings.CHIA_HOSTNAME,
Expand All @@ -74,7 +72,6 @@ async def get_wallet_rpc_client() -> Iterator[WalletRpcClient]:


async def get_full_node_rpc_client() -> Iterator[FullNodeRpcClient]:

async for _ in _get_rpc_client(
node_type=NodeType.FULL_NODE,
self_hostname=settings.CHIA_HOSTNAME,
Expand Down
7 changes: 3 additions & 4 deletions app/api/v1/activities.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from sqlalchemy import desc, asc
from typing import Dict, List, Optional

from fastapi import APIRouter, Depends
Expand All @@ -25,7 +24,7 @@ async def get_activity(
mode: Optional[GatewayMode] = None,
page: int = 1,
limit: int = 10,
sort: str = 'desc',
sort: str = "desc",
db: Session = Depends(deps.get_db_session),
):
"""Get activity.
Expand Down Expand Up @@ -54,7 +53,7 @@ async def get_activity(
pass
case _:
raise ErrorCode().bad_request_error(message="search_by is invalid")

if minHeight is not None:
activity_filters["and"].append(models.Activity.height >= minHeight)

Expand All @@ -77,7 +76,7 @@ async def get_activity(
total: int

order_by_clause = []
if sort.lower() == 'desc':
if sort.lower() == "desc":
order_by_clause.append(models.Activity.height.desc())
order_by_clause.append(models.Activity.coin_id.desc())
else:
Expand Down
11 changes: 4 additions & 7 deletions app/api/v1/cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ async def _scan_token_activity(
climate_warehouse: crud.ClimateWareHouseCrud,
blockchain: crud.BlockChainCrud,
) -> bool:

state = db_crud.select_block_state_first()
if state.peak_height is None:
logger.warning("Full node state has not been retrieved.")
Expand Down Expand Up @@ -100,9 +99,7 @@ async def _scan_token_activity(

db_crud.batch_insert_ignore_activity(activities)

db_crud.update_block_state(
current_height=target_start_height
)
db_crud.update_block_state(current_height=target_start_height)
return True


Expand All @@ -118,9 +115,10 @@ async def scan_token_activity() -> None:
as_async_contextmanager(deps.get_db_session) as db,
as_async_contextmanager(deps.get_full_node_rpc_client) as full_node_client,
):

db_crud = crud.DBCrud(db=db)
climate_warehouse = crud.ClimateWareHouseCrud(url=settings.CADT_API_SERVER_HOST, api_key=settings.CADT_API_KEY)
climate_warehouse = crud.ClimateWareHouseCrud(
url=settings.CADT_API_SERVER_HOST, api_key=settings.CADT_API_KEY
)
blockchain = crud.BlockChainCrud(full_node_client=full_node_client)

try:
Expand Down Expand Up @@ -167,7 +165,6 @@ async def scan_blockchain_state() -> None:
as_async_contextmanager(deps.get_db_session) as db,
as_async_contextmanager(deps.get_full_node_rpc_client) as full_node_client,
):

db_crud = crud.DBCrud(db=db)

try:
Expand Down
7 changes: 3 additions & 4 deletions app/api/v1/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.util.bech32m import decode_puzzle_hash, encode_puzzle_hash
from chia.util.byte_types import hexstr_to_bytes
from chia.util.ints import uint32
from chia.wallet.derive_keys import (
master_sk_to_wallet_sk,
master_sk_to_wallet_sk_unhardened,
Expand All @@ -31,7 +32,6 @@ async def get_key(
prefix: str = "bls1238",
wallet_rpc_client: WalletRpcClient = Depends(deps.get_wallet_rpc_client),
):

fingerprint: int = await wallet_rpc_client.get_logged_in_fingerprint()

result: Dict = await wallet_rpc_client.get_private_key(fingerprint)
Expand All @@ -40,10 +40,10 @@ async def get_key(

wallet_secret_key: PrivateKey
if hardened:
wallet_secret_key = master_sk_to_wallet_sk(secret_key, derivation_index)
wallet_secret_key = master_sk_to_wallet_sk(secret_key, uint32(derivation_index))
else:
wallet_secret_key = master_sk_to_wallet_sk_unhardened(
secret_key, derivation_index
secret_key, uint32(derivation_index)
)

wallet_public_key: G1Element = wallet_secret_key.get_g1()
Expand All @@ -63,7 +63,6 @@ async def get_key(
async def parse_key(
address: str,
):

try:
puzzle_hash: bytes = decode_puzzle_hash(address)
except ValueError:
Expand Down
39 changes: 23 additions & 16 deletions app/api/v1/tokens.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import json;

import json
from typing import Any, Dict, Tuple

from blspy import G1Element, G2Element
from chia.rpc.wallet_rpc_client import WalletRpcClient
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.types.coin_spend import CoinSpend
from chia.types.spend_bundle import SpendBundle
from chia.util.byte_types import hexstr_to_bytes
Expand All @@ -15,8 +15,8 @@
from app.core import utils
from app.core.climate_wallet.wallet import ClimateWallet
from app.core.types import ClimateTokenIndex, GatewayMode
from app.utils import disallow
from app.logger import logger
from app.utils import disallow

router = APIRouter()

Expand All @@ -39,8 +39,8 @@ async def create_tokenization_tx(
wallet_client=wallet_rpc_client
)

token: schemas.Token = request.token
payment: schemas.Payment = request.payment
token = request.token
payment = request.payment

token_index = ClimateTokenIndex(
org_uid=token.org_uid,
Expand Down Expand Up @@ -68,6 +68,9 @@ async def create_tokenization_tx(
}

for mode in GatewayMode:
if wallet.mode_to_public_key is None:
raise ValueError("Wallet mode to public key is None!")

public_key: G1Element = wallet.mode_to_public_key[mode]

mod_hash: bytes
Expand Down Expand Up @@ -163,19 +166,19 @@ async def create_detokenization_file(
This endpoint is to be called by the client.
"""

token: schemas.TokenOnChain = request.token
payment: schemas.Payment = request.payment
token = request.token
payment = request.payment

token_index = ClimateTokenIndex(
org_uid=token.org_uid,
warehouse_project_id=token.warehouse_project_id,
vintage_year=token.vintage_year,
sequence_num=token.sequence_num,
)
tail_metadata: schemas.TailMetadataBase = token.detokenization
tail_metadata = token.detokenization

mode_to_public_key: Dict[GatewayMode, G1Element] = {
GatewayMode.DETOKENIZATION: tail_metadata.public_key,
GatewayMode.DETOKENIZATION: G1Element.from_bytes(tail_metadata.public_key),
}
mode_to_message_and_signature: Dict[GatewayMode, Tuple[bytes, G2Element]] = {
GatewayMode.DETOKENIZATION: (
Expand All @@ -187,7 +190,7 @@ async def create_detokenization_file(

wallet = ClimateWallet(
token_index=token_index,
root_public_key=token.public_key,
root_public_key=G1Element.from_bytes(token.public_key),
mode_to_public_key=mode_to_public_key,
mode_to_message_and_signature=mode_to_message_and_signature,
wallet_client=wallet_rpc_client,
Expand All @@ -198,9 +201,11 @@ async def create_detokenization_file(
raise ValueError(f"Asset id {asset_id} inconsistent with request body!")

cat_wallet_info = await utils.get_cat_wallet_info_by_asset_id(
asset_id=hexstr_to_bytes(asset_id),
asset_id=bytes32.from_hexstr(asset_id),
wallet_client=wallet_rpc_client,
)
if cat_wallet_info is None:
raise ValueError(f"Asset id {asset_id} not found in wallet!")

result: Dict = await wallet.create_detokenization_request(
amount=payment.amount,
Expand Down Expand Up @@ -270,7 +275,7 @@ async def create_permissionless_retirement_tx(
This endpoint is to be called by the client.
"""

token: schemas.TokenOnChain = request.token
token = request.token
payment: schemas.RetirementPaymentWithPayer = request.payment

token_index = ClimateTokenIndex(
Expand All @@ -279,7 +284,7 @@ async def create_permissionless_retirement_tx(
vintage_year=token.vintage_year,
sequence_num=token.sequence_num,
)
tail_metadata: schemas.TailMetadataBase = token.permissionless_retirement
tail_metadata = token.permissionless_retirement

mode_to_message_and_signature: Dict[GatewayMode, Tuple[bytes, G2Element]] = {
GatewayMode.PERMISSIONLESS_RETIREMENT: (
Expand All @@ -291,7 +296,7 @@ async def create_permissionless_retirement_tx(

wallet = ClimateWallet(
token_index=token_index,
root_public_key=token.public_key,
root_public_key=G1Element.from_bytes(token.public_key),
mode_to_message_and_signature=mode_to_message_and_signature,
wallet_client=wallet_rpc_client,
constants=constants,
Expand All @@ -301,15 +306,17 @@ async def create_permissionless_retirement_tx(
raise ValueError(f"Asset id {asset_id} inconsistent with request body!")

cat_wallet_info = await utils.get_cat_wallet_info_by_asset_id(
asset_id=hexstr_to_bytes(asset_id),
asset_id=bytes32.from_hexstr(asset_id),
wallet_client=wallet_rpc_client,
)
if cat_wallet_info is None:
raise ValueError(f"Asset id {asset_id} not found in wallet!")

# Create a dictionary with the variables you want to log
log_data = {
"amount": payment.amount,
"fee": payment.fee,
"wallet_id": cat_wallet_info.id
"wallet_id": cat_wallet_info.id,
}

# Convert the dictionary to a JSON-formatted string
Expand Down
14 changes: 6 additions & 8 deletions app/api/v1/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
from chia.types.blockchain_format.program import Program
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.types.coin_spend import CoinSpend
from chia.types.spend_bundle import SpendBundle
from chia.util.byte_types import hexstr_to_bytes
from chia.wallet.cat_wallet.cat_info import CATInfo
from chia.wallet.cat_wallet.cat_utils import construct_cat_puzzle
from chia.wallet.puzzles.cat_loader import CAT_MOD
from chia.wallet.cat_wallet.cat_utils import CAT_MOD, construct_cat_puzzle
from chia.wallet.transaction_record import TransactionRecord
from chia.wallet.transaction_sorting import SortKey
from chia.wallet.util.wallet_types import WalletType
Expand Down Expand Up @@ -42,8 +40,8 @@ async def get_transaction(
"""

transaction_record: TransactionRecord = await wallet_rpc_client.get_transaction(
wallet_id=None,
transaction_id=hexstr_to_bytes(transaction_id),
wallet_id=0,
transaction_id=bytes32.from_hexstr(transaction_id),
)

return schemas.Transaction(
Expand All @@ -65,7 +63,6 @@ async def get_transactions(
to_address: Optional[str] = None,
wallet_rpc_client: WalletRpcClient = Depends(deps.get_wallet_rpc_client),
):

"""Get transactions.
This endpoint is to be called by the client.
Expand Down Expand Up @@ -111,7 +108,8 @@ async def get_transactions(
for transaction_record in transaction_records
],
)

if cat_info is None:
raise ValueError(f"Wallet {wallet_id} is not a CAT wallet")
gateway_puzzle: Program = create_gateway_puzzle()
gateway_puzzle_hash: bytes32 = gateway_puzzle.get_tree_hash()
gateway_cat_puzzle: Program = construct_cat_puzzle(
Expand All @@ -126,7 +124,7 @@ async def get_transactions(
if transaction_record.to_puzzle_hash != gateway_puzzle_hash:
continue

spend_bundle: SpendBundle = transaction_record.spend_bundle
spend_bundle = transaction_record.spend_bundle
if spend_bundle is None:
continue

Expand Down
Loading

0 comments on commit 3a1c49d

Please sign in to comment.