Skip to content

Commit

Permalink
config file support
Browse files Browse the repository at this point in the history
  • Loading branch information
D1sk1n committed Jul 1, 2019
1 parent fc0473f commit 81b4e11
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 24 deletions.
39 changes: 26 additions & 13 deletions Browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
###########
# Imports #
###########
from time import sleep
import re
import sys
from multiprocessing import Process
import os
import traceback
import json

from time import sleep
from multiprocessing import Process

from rpc_client import get_acct_raw, get_acct_info, start_rpc_client_instance
from client import start_client_instance, do_cmd
Expand All @@ -28,8 +31,6 @@
# Definitions #
###############
ctr = 0 # counter of requests since last init
DB_PATH = './tx_cache.db' # path to the db we should load
CLIENT_PATH = '~/libra/' # root directory of Libra client
c2 = None # placeholder for connection object

header = '''<html><head><title>Libra Testnet Experimental Browser</title></head>
Expand Down Expand Up @@ -109,7 +110,7 @@ def add_br_every64(s):
@app.route('/')
def index():
update_counters()
c2, conn = connect_to_db(DB_PATH)
c2, conn = connect_to_db(config['DB_PATH'])

bver = str(get_latest_version(c2))

Expand All @@ -120,7 +121,7 @@ def index():
@app.route('/version/<ver>')
def version(ver):
update_counters()
c2, conn = connect_to_db(DB_PATH)
c2, conn = connect_to_db(config['DB_PATH'])

bver = str(get_latest_version(c2))

Expand Down Expand Up @@ -158,7 +159,7 @@ def acct_details(acct):
if not is_valid_account(acct):
return invalid_account_template

c2, conn = connect_to_db(DB_PATH)
c2, conn = connect_to_db(config['DB_PATH'])
bver = str(get_latest_version(c2))

acct_state_raw = get_acct_raw(acct)
Expand Down Expand Up @@ -195,7 +196,7 @@ def search_redir():
@app.route('/stats')
def stats():
update_counters()
c2, conn = connect_to_db(DB_PATH)
c2, conn = connect_to_db(config['DB_PATH'])
try:
# get stats
stats_all_time = calc_stats(c2)
Expand All @@ -217,7 +218,7 @@ def stats():
def faucet():
update_counters()

c2, conn = connect_to_db(DB_PATH)
c2, conn = connect_to_db(config['DB_PATH'])
bver = str(get_latest_version(c2))

message = ''
Expand Down Expand Up @@ -255,13 +256,25 @@ def send_asset(path):
# Main #
########
if __name__ == '__main__':
tx_p = Process(target=tx_db_worker, args=(DB_PATH, ))
with open('config.json', 'r') as f:
config = json.load(f)

try:
config = config[os.getenv("BROWSER")]
except:
config = config["PRODUCTION"]

print("system configuration:")
print(json.dumps(config, indent=4))

tx_p = Process(target=tx_db_worker, args=(config['DB_PATH'], config['RPC_SERVER'], config['MINT_ACCOUNT']))
tx_p.start()

start_rpc_client_instance()
start_rpc_client_instance(config['RPC_SERVER'], config['MINT_ACCOUNT'])

p = start_client_instance(CLIENT_PATH)
p = start_client_instance(config['CLIENT_PATH'], config['ACCOUNT_FILE'])

sleep(1)

app.run(port=5000, threaded=False, host='0.0.0.0', debug=False)
app.run(port=config['FLASK_PORT'], threaded=config['FLASK_THREADED'],
host=config['FLASK_HOST'], debug=config['FLASK_DEBUG'])
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ A browser for the Libra Blockchain TestNet. See: https://librabrowser.io

## Installation
1. Install Libra per official instructions
2. make sure the CLIENT_PATH variable is correct
3. pip3 install grpcio grpcio-tools hexdump
2. Run: pip3 install grpcio grpcio-tools hexdump
3. Open the official client, create an account and save the account to disk (should be set in ACCOUNT_FILE setting)
4. Edit config.json and make sure that settings match your environment (in particular CLIENT_PATH)

## Running the project
At the root project folder execute the command:
> python3 Browser.py
Or to execute and leave it to run with output redirected to a file execute:
> nohup python3 Browser.py &> browser.log < /dev/null &
> tail -f browser.log #if you want to see the logs
To use "DEVELOPMENT" mode settings set the environment variable "BROWSER=DEVELOPMENT"

## Credits
rpc support is based on: https://github.com/egorsmkv/libra-grpc-py
4 changes: 2 additions & 2 deletions client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#########
# Funcs #
#########
def start_client_instance(client_path = ''):
def start_client_instance(client_path = '', account_file = ''):
c_path = os.path.expanduser(client_path + "target/debug/client")
p = Popen([c_path, "--host", "ac.testnet.libra.org", "--port", "80",
"-s", "./scripts/cli/trusted_peers.config.toml"], cwd=os.path.expanduser(client_path),
Expand All @@ -25,7 +25,7 @@ def start_client_instance(client_path = ''):
print(os.read(p.stdout.fileno(), 10000))

print('loading account')
print(do_cmd("a r ./test_acct", p = p))
print(do_cmd("a r " + account_file, p = p))
sys.stdout.flush()

return p
Expand Down
6 changes: 3 additions & 3 deletions db_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,17 @@ def init_db(c):
sys.exit()


def tx_db_worker(db_path='./tx_cache.db'):
def tx_db_worker(db_path, rpc_server, mint_addr):
while True:
try:
print('transactions db worker starting')

# create rpc connection
try:
start_rpc_client_instance()
start_rpc_client_instance(rpc_server, mint_addr)
except:
sleep(10)
start_rpc_client_instance()
start_rpc_client_instance(rpc_server, mint_addr)

# connect to DB
c, conn = connect_to_db(db_path) # returns cursor object
Expand Down
13 changes: 9 additions & 4 deletions rpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,25 @@
###########
# Globals #
###########
SERVER_ADDRESS = 'ac.testnet.libra.org:8000'
MINT_ACCOUNT = '0000000000000000000000000000000000000000000000000000000000000000'
SERVER_ADDRESS = ''
MINT_ACCOUNT = ''
stub = None
last_version_seen = 0


#########
# Funcs #
#########
def start_rpc_client_instance(server_address = SERVER_ADDRESS):
def start_rpc_client_instance(rpc_server, mint_addr):
global last_version_seen
global stub
global SERVER_ADDRESS
global MINT_ACCOUNT

channel = grpc.insecure_channel(server_address)
SERVER_ADDRESS = rpc_server
MINT_ACCOUNT = mint_addr

channel = grpc.insecure_channel(SERVER_ADDRESS)
stub = AdmissionControlStub(channel)

last_version_seen = get_latest_version_from_ledger()
Expand Down

0 comments on commit 81b4e11

Please sign in to comment.