Skip to content

Commit

Permalink
Add sidecar for evm
Browse files Browse the repository at this point in the history
  • Loading branch information
puppetninja committed Jul 30, 2024
1 parent 9ea867d commit 78303d4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions utils/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ RUN PIP_INST="pip --no-cache install" \
&& $PIP_INST base58 pynacl \
&& $PIP_INST mnemonic pytezos requests \
&& $PIP_INST pyblake2 pysodium flask \
&& $PIP_INST web3 \
&& apk del .build-deps \
&& $APK_ADD jq netcat-openbsd curl binutils \
&& $APK_ADD lz4
Expand All @@ -29,6 +30,7 @@ COPY config-generator.sh /
COPY entrypoint.sh /
COPY logger.sh /
COPY sidecar.py /
COPY sidecar-evm.py /
COPY snapshot-downloader.sh /
COPY wait-for-dns.sh /
ENTRYPOINT ["/entrypoint.sh"]
Expand Down
1 change: 1 addition & 0 deletions utils/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ case "$CMD" in
config-generator) exec /config-generator.sh "$@" ;;
logger) exec /logger.sh "$@" ;;
sidecar) exec /sidecar.py "$@" ;;
sidecar-evm) exec /sidecar-evm.py "$@" ;;
snapshot-downloader) exec /snapshot-downloader.sh "$@" ;;
wait-for-dns) exec /wait-for-dns.sh "$@" ;;
esac
Expand Down
43 changes: 43 additions & 0 deletions utils/sidecar-evm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#! /usr/bin/env python
from flask import Flask
from web3 import Web3
import requests
import time
import logging

log = logging.getLogger("werkzeug")
log.setLevel(logging.ERROR)

application = Flask(__name__)
web3 = Web3(Web3.HTTPProvider('http://localhost:8545'))

AGE_LIMIT_IN_SECS = 600
# https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/
# Default readiness probe timeoutSeconds is 1s, timeout sync request before that and return a
# connect timeout error if necessary

@application.route("/health")
def health():
if not web3.is_connected():
err = ("Error: Failed to connect evm node. ", 500)
application.logger.error(err)
return err

latest_block = web3.eth.get_block('latest')

latest_block_timestamp = latest_block['timestamp']
current_timestamp = int(time.time())
time_diff = current_timestamp - latest_block_timestamp
if time_diff > AGE_LIMIT_IN_SECS:
err = (
"Error: Chain head is %s secs old, older than %s"
% (time_diff, AGE_LIMIT_IN_SECS),
500,
)
application.logger.error(err)
return err
return "evm rpc is healthy"


if __name__ == "__main__":
application.run(host="0.0.0.0", port=32767, debug=False)

0 comments on commit 78303d4

Please sign in to comment.