Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from covalenthq/develop
Browse files Browse the repository at this point in the history
Refactoring finaliser agent
  • Loading branch information
noslav authored Apr 26, 2022
2 parents d2b6205 + 08490d7 commit d8182ff
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 56 deletions.
109 changes: 68 additions & 41 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
# Editors
.vscode/
.idea/

# Vagrant
.vagrant/

# Mac/OSX
.DS_Store

# Windows
Thumbs.db

# Source for the following rules: https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore
# Created by .ignore support plugin (hsz.mobi)
### Python template
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand All @@ -22,6 +10,7 @@ __pycache__/

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
Expand All @@ -33,11 +22,9 @@ lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
Expand All @@ -52,15 +39,13 @@ pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
Expand All @@ -69,7 +54,6 @@ coverage.xml
# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
Expand All @@ -84,43 +68,86 @@ docs/_build/
# PyBuilder
target/

# Jupyter Notebook
# IPython Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
# dotenv
.env
.venv
env/

# virtualenv
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject
### VirtualEnv template
# Virtualenv
# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/
.Python
[Bb]in
[Ii]nclude
[Ll]ib
[Ll]ib64
[Ll]ocal
[Ss]cripts
pyvenv.cfg
.venv
pip-selfcheck.json
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff:
.idea/workspace.xml
.idea/tasks.xml
.idea/dictionaries
.idea/vcs.xml
.idea/jsLibraryMappings.xml

# Sensitive or high-churn files:
.idea/dataSources.ids
.idea/dataSources.xml
.idea/dataSources.local.xml
.idea/sqlDataSources.xml
.idea/dynamic.xml
.idea/uiDesigner.xml

# Gradle:
.idea/gradle.xml
.idea/libraries

# Mongo Explorer plugin:
.idea/mongoSettings.xml

.idea/

## File-based project format:
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# mkdocs documentation
/site
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# mypy
.mypy_cache/
.dmypy.json
dmypy.json
rewards
logs/
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,42 @@
# bsp-finalizer
# bsp-finalizer

# Running

Put appropriate values on the `.env` file:

```
RPC_ENDPOINT=""
BLOCK_ID_START="1606150927003422917" # The block ID from which DB thread starts reading off
PROOFCHAIN_ADDRESS="0x48bb0d9653D30b977439c71B3F6C4557137dD0ad"
FINALIZER_PRIVATE_KEY=""
FINALIZER_ADDRESS=""
DB_USER=""
DB_PASSWORD=""
DB_HOST="master.datamodel.db.covalenthq.com"
DB_DATABASE="blockchains"
```

Load environment variables:
```
direnv allow
```

Create directories for logging:

```
mkdir -p logs/{Contract,Finalizer,DB}
```

Install python packages (venv):

```
python3 -m venv ./.venv
source .venv/bin/activate
brew install openssl # In case you don't already installed it
export LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib"; pip install -r requirements.txt
```

Run the script:
```
python3 src/main.py
```
2 changes: 1 addition & 1 deletion abi/ProofChainContractABI
Original file line number Diff line number Diff line change
Expand Up @@ -1020,4 +1020,4 @@
"stateMutability": "view",
"type": "function"
}
]
]
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ frozenlist==1.3.0
hexbytes==0.2.2
idna==3.3
importlib-resources==5.7.0
ipfshttpclient==0.8.0a2
jsonschema==4.4.0
lru-dict==1.1.7
multiaddr==0.0.9
Expand Down
21 changes: 21 additions & 0 deletions src/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Covalent

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
5 changes: 1 addition & 4 deletions contract.py → src/contract.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import asyncio
import logging
import pprint
import statistics
import traceback

Expand Down Expand Up @@ -29,7 +26,7 @@ def __init__(self, rpc_endpoint, finalizer_address, finalizer_prvkey, proofchain
abi=f.read()
)

self.logger = logformat.LogFormat.init_logger("Contract")
self.logger = logformat.LogFormat.init_logger("Contract", console_mode=True)

# asynchronous defined function to loop
# this loop sets up an event filter and is looking for new entires for the "PairCreated" event
Expand Down
File renamed without changes.
File renamed without changes.
3 changes: 1 addition & 2 deletions finalizer.py → src/finalizer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import threading
import time
import logging
import traceback

import logformat
Expand All @@ -12,7 +11,7 @@ class Finalizer(threading.Thread):
def __init__(self, cn: ProofChainContract):
super().__init__()
self.contract = cn
self.logger = logformat.LogFormat.init_logger("Finalizer")
self.logger = logformat.LogFormat.init_logger("Finalizer", console_mode=True)

def __main_loop(self):
# self.contract.estimate_gas_price()
Expand Down
11 changes: 8 additions & 3 deletions logformat.py → src/logformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def format(self, record):
return formatter.format(record)

@staticmethod
def init_logger(class_name):
def init_logger(class_name, console_mode=False):
logger = logging.getLogger(class_name)
logger.setLevel(logging.DEBUG)

Expand All @@ -34,10 +34,15 @@ def init_logger(class_name):
ch.suffix = "%Y-%m-%d_%H-%M-%S.log"
# ch = logging.FileHandler("logs.txt")
ch.setLevel(logging.DEBUG)
format = LogFormat()

ch.setFormatter(LogFormat())
ch.setFormatter(format)

logger.addHandler(ch)
if console_mode:
console_handler = logging.StreamHandler()
console_handler.setFormatter(format)
logger.addHandler(console_handler)
logger.propagate = False

return logger
return logger
16 changes: 13 additions & 3 deletions main.py → src/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import logging
import time

from contract import ProofChainContract
from dbmanager import DBManager
from finalizer import Finalizer
import os
from dotenv import load_dotenv


def is_any_thread_alive(threads):
return True in [t.is_alive() for t in threads]


if __name__ == "__main__":

load_dotenv()
Expand Down Expand Up @@ -34,13 +40,17 @@
database=DB_DATABASE,
host=DB_HOST
)
dbm.daemon = True

dbm.start()
finalizer = Finalizer(contract)
finalizer.daemon = True
dbm.start()

finalizer.start()

dbm.join()
finalizer.join()
while is_any_thread_alive([finalizer, dbm]):
time.sleep(0.3)

#
# contract.send_finalize(4, 10430382)
# contract.subscribe_on_event(handle_event)
Expand Down

0 comments on commit d8182ff

Please sign in to comment.