Skip to content

Commit

Permalink
Merge pull request #131 from Chia-Network/develop
Browse files Browse the repository at this point in the history
Release 1.0.35
  • Loading branch information
TheLastCicada authored Nov 2, 2023
2 parents 0542b04 + 24c902b commit 7df6468
Show file tree
Hide file tree
Showing 38 changed files with 2,146 additions and 1,664 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
8 changes: 6 additions & 2 deletions .github/workflows/build-installers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ concurrency:
cancel-in-progress: true

permissions:
id-token: write
contents: write

jobs:
Expand Down Expand Up @@ -313,8 +314,11 @@ jobs:
echo "Array being passed is ${APPS_ARRAY}"
echo "APPLICATIONS=${APPS_ARRAY}" >> $GITHUB_OUTPUT
- name: Gets JWT Token from GitHub
uses: Chia-Network/actions/github/jwt@main

- name: Trigger apt repo update
run: |
curl -s -XPOST -H "Authorization: Bearer ${{ secrets.GLUE_ACCESS_TOKEN }}" --data '{"climate_tokenization_repo":"${{ steps.repo-name.outputs.REPO_NAME }}","application_name":"${{ steps.apt-metadata.outputs.APPLICATIONS }}","release_version":"${{ steps.tag-name.outputs.TAGNAME }}","add_debian_version":"true","arm64":"available"}' ${{ secrets.GLUE_API_URL }}/api/v1/climate-tokenization/${{ github.sha }}/start
curl -s -XPOST -H "Authorization: Bearer ${{ secrets.GLUE_ACCESS_TOKEN }}" --data '{"climate_tokenization_repo":"${{ steps.repo-name.outputs.REPO_NAME }}","application_name":"${{ steps.apt-metadata.outputs.APPLICATIONS }}","release_version":"${{ steps.tag-name.outputs.TAGNAME }}","add_debian_version":"true","arm64":"available"}' ${{ secrets.GLUE_API_URL }}/api/v1/climate-tokenization/${{ github.sha }}/success/deploy
curl -s -XPOST -H "Authorization: Bearer ${{ env.JWT_TOKEN }}" --data '{"climate_tokenization_repo":"${{ steps.repo-name.outputs.REPO_NAME }}","application_name":"${{ steps.apt-metadata.outputs.APPLICATIONS }}","release_version":"${{ steps.tag-name.outputs.TAGNAME }}","add_debian_version":"true","arm64":"available"}' ${{ secrets.GLUE_API_URL }}/api/v1/climate-tokenization/${{ github.sha }}/start
curl -s -XPOST -H "Authorization: Bearer ${{ env.JWT_TOKEN }}" --data '{"climate_tokenization_repo":"${{ steps.repo-name.outputs.REPO_NAME }}","application_name":"${{ steps.apt-metadata.outputs.APPLICATIONS }}","release_version":"${{ steps.tag-name.outputs.TAGNAME }}","add_debian_version":"true","arm64":"available"}' ${{ secrets.GLUE_API_URL }}/api/v1/climate-tokenization/${{ github.sha }}/success/deploy
if: startsWith(github.ref, 'refs/tags/')
76 changes: 76 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: Tests

on:
push:
branches:
- main
pull_request:
branches:
- '**'

jobs:
test:
name: Python tests
runs-on: ubuntu-latest

env:
CHIA_ROOT: ${{ github.workspace }}/.chia/mainnet
CHIA_SIMULATOR_ROOT: ${{ github.workspace }}/.chia/simulator
BLOCKS_AND_PLOTS_VERSION: 0.35.0

steps:
- name: Clean workspace
uses: Chia-Network/actions/clean-workspace@main

- name: Checkout Code
uses: actions/checkout@v3
with:
submodules: recursive

- name: Setup Python
uses: Chia-Network/actions/setup-python@main
with:
python-version: '3.10'

- name: Create virtual environment
uses: Chia-Network/actions/create-venv@main
id: create-venv

- name: Activate virtual environment
uses: Chia-Network/actions/activate-venv@main
with:
directories: ${{ steps.create-venv.outputs.activate-venv-directories }}

- name: Run poetry install
uses: Chia-Network/actions/poetry@main
with:
poetry-command: "install --with dev"

- name: Cache test blocks and plots
uses: actions/cache@v3
env:
SEGMENT_DOWNLOAD_TIMEOUT_MIN: 1
id: test-blocks-plots
with:
path: |
${{ github.workspace }}/.chia/blocks
${{ github.workspace }}/.chia/test-plots
key: ${{ env.BLOCKS_AND_PLOTS_VERSION }}

- name: Checkout test blocks and plots
if: steps.test-blocks-plots.outputs.cache-hit != 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh release download ${{ env.BLOCKS_AND_PLOTS_VERSION }} -R Chia-Network/test-cache --archive=tar.gz -O - | tar xzf -
mkdir "${GITHUB_WORKSPACE}/.chia"
mv "${GITHUB_WORKSPACE}/test-cache-${{ env.BLOCKS_AND_PLOTS_VERSION }}/"* "${GITHUB_WORKSPACE}/.chia"
- name: Run Python tests
env:
MODE: dev
PYTHONPATH: ".:./chia-blockchain"
run: |
chia init
chia version
python -m pytest tests
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
10 changes: 0 additions & 10 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
## 1.0.34 (2023-10-24)

### Feat

- dont use meta_ prefix for cadt metadata

### Fix

- meta_ prefix in tests

## 1.0.33 (2023-10-12)

### Fix
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Climate Token Driver Suite

![Tested Up to Chia Version](https://raw.githubusercontent.com/Chia-Network/core-registry-api/main/testedChiaVersion.svg)

This application can run in 4 modes, each providing a separate application with a distinct use case:

* **Chia Climate Tokenization**:
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
29 changes: 21 additions & 8 deletions app/api/v1/activities.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from sqlalchemy import desc, asc
from typing import Dict, List, Optional

from pydantic import ValidationError
from fastapi import APIRouter, Depends
from fastapi.encoders import jsonable_encoder
from sqlalchemy.orm import Session
Expand All @@ -12,7 +11,7 @@
from app.errors import ErrorCode
from app.logger import logger
from app.utils import disallow

import pprint
router = APIRouter()


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 All @@ -97,15 +96,29 @@ async def get_activity(
)
return schemas.ActivitiesResponse()

pp = pprint.PrettyPrinter(indent=4)

pp.pprint(f"Got {len(activities)} activities from activities table.")
activities_with_cw: List[schemas.ActivityWithCW] = []
for activity in activities:
unit: Dict = units.get(activity.asset_id).copy()
pp.pprint(f"Checking activity: {activity}")
unit = units.get(activity.asset_id)
if unit is None:
continue
unit = unit.copy()
token = unit.pop("token", None)
org = unit.pop("organization", None)
project = unit.pop("project", None)

try:
token_on_chain = schemas.TokenOnChain.parse_obj(token)
print("instantiated TokenOnChain with parse_obj", flush=True)
except ValidationError:
print("failed to instantiate TokenOnChain with parse_obj", flush=True)
raise

activity_with_cw = schemas.ActivityWithCW(
token=token,
token=token_on_chain,
cw_unit=unit,
cw_org=org,
cw_project=project,
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
Loading

0 comments on commit 7df6468

Please sign in to comment.