gasgasgas/gas3/__init__.py
2023-09-01 04:06:05 +01:00

109 lines
3.2 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# standard imports
import os
import urllib.parse
import logging
import re
# external imports
from chainlib.eth.gas import balance
from chainlib.eth.cli.config import Config
from chainlib.eth.cli.config import process_config
from chainlib.eth.cli.arg import ArgFlag
from chainlib.eth.cli.arg import Arg
from chainlib.eth.cli.arg import process_args
from chainlib.settings import ChainSettings
from chainlib.eth.settings import process_settings
from eth_contract_registry import Registry
import chainlib.eth.cli
from leveldir.uuid import UUIDDir
# local imports
from gas3.base import do_yield
from gas3.register import do_register
import gas3.error
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
data_path = os.environ.get('GAS3_DATA_DIR', 'data')
registry = os.environ.get('CIC_REGISTRY_ADDRESS')
data_dir = UUIDDir(data_path)
re_yield = re.compile('^/yield')
re_register = re.compile('^/register')
def process_settings_local(settings, config):
conn = settings.get('CONN')
o = balance(settings.get('SENDER_ADDRESS'))
r = conn.do(o)
logg.info('signing with address {} balance start {}'.format(settings.get('SENDER_ADDRESS'), int(r, 16)))
c = Registry(settings.get('CHAIN_SPEC'), gas_oracle=settings.get('GAS_ORACLE'))
o = c.address_of(registry, 'accounts', sender_address=settings.get('SENDER_ADDRESS'))
r = conn.do(o)
accounts_index = c.parse_address_of(r)
settings.set('ACCOUNTS_CONTRACT', accounts_index)
logg.info('using accounts_index {}'.format(accounts_index))
o = c.address_of(registry, 'faucet', sender_address=settings.get('SENDER_ADDRESS'))
r = conn.do(o)
faucet = c.parse_address_of(r)
settings.set('FAUCET_CONTRACT', faucet)
logg.info('using faucet {}'.format(faucet))
return settings
arg_flags = ArgFlag()
arg = Arg(arg_flags)
flags = arg_flags.STD_WRITE | arg_flags.WALLET | arg_flags.EXEC
argparser = chainlib.eth.cli.ArgumentParser()
argparser = process_args(argparser, arg, flags)
args = argparser.parse_args()
config = Config()
config = process_config(config, arg, args, flags)
logg.debug('config loaded:\n{}'.format(config))
settings = ChainSettings()
settings = process_settings(settings, config)
settings = process_settings_local(settings, config)
logg.debug('settings loaded:\n{}'.format(settings))
def application(env, start_response):
req = urllib.parse.urlparse(env['REQUEST_URI'])
scode = 200
stext = 'OK'
r = ''
if re.match(re_yield, req.path):
r = do_yield(req, env, data_dir, settings)
elif re.match(re_register, req.path):
try:
r = do_register(req, env, data_dir, settings)
except gas3.error.Used:
scode = 410
stext = 'Code already used'
except gas3.error.InvalidInput as e:
scode = 400
stext = 'Malformed request url'
r = str(e)
except FileNotFoundError:
scode = 403
stext = 'No such code'
except Exception as e:
scode = 500
stext = 'Server error'
r = str(e)
else:
r = ''
l = len(r)
status = str(scode) + ' ' + stext
start_response(status, [('Content-Length', str(l))])
return [r.encode('utf-8')]