Compare commits
4 Commits
lash/store
...
sohail/doc
| Author | SHA1 | Date | |
|---|---|---|---|
|
05224a9dd6
|
|||
| 2f65aa37ff | |||
|
|
be7dea24ac
|
||
| 65b3d4d409 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -2,3 +2,5 @@ __pycache__
|
||||
*.egg-info
|
||||
build/
|
||||
*.pyc
|
||||
.venv
|
||||
.clicada
|
||||
69
README.md
Normal file
69
README.md
Normal file
@@ -0,0 +1,69 @@
|
||||
## Clicada
|
||||
|
||||
> Admin Command Line Interface to interact with cic-meta and cic-cache
|
||||
|
||||
### Pre-requisites
|
||||
|
||||
- Public key uploaded to `cic-auth-helper`
|
||||
- PGP Keyring for your key
|
||||
|
||||
### Installation
|
||||
|
||||
Use either of the following installation methods:
|
||||
|
||||
1. Install from git release (recommended)
|
||||
|
||||
```bash
|
||||
wget https://git.grassecon.net/grassrootseconomics/clicada/archive/v0.0.6.zip
|
||||
unzip clicada-v0.0.6.zip
|
||||
cd clicada
|
||||
python3 -m venv .venv
|
||||
source .venv/bin/activate
|
||||
pip install -r requirements.txt --extra-index-url=https://pip.grassrootseconomics.net
|
||||
```
|
||||
|
||||
2. Install from pip to path (non sudo)
|
||||
|
||||
```bash
|
||||
pip3 install -UI --extra-index-url=https://pip.grassrootseconomics.net clicada
|
||||
```
|
||||
|
||||
### Usage
|
||||
|
||||
```bash
|
||||
usage: clicada [...optional arguments] [...positional arguments]
|
||||
|
||||
positional arguments:
|
||||
{user,u,tag,t}
|
||||
user (u) retrieve transactions for a user
|
||||
tag (t) locally assign a display value to an identifier
|
||||
|
||||
optional arguments:
|
||||
-h, --help show this help message and exit
|
||||
--no-logs Turn off all logging
|
||||
-v Be verbose
|
||||
-vv Be very verbose
|
||||
-c CONFIG, --config CONFIG
|
||||
Configuration directory
|
||||
-n NAMESPACE, --namespace NAMESPACE
|
||||
Configuration namespace
|
||||
--dumpconfig {env,ini}
|
||||
Output configuration and quit. Use with --raw to omit values and output schema only.
|
||||
--env-prefix ENV_PREFIX
|
||||
environment prefix for variables to overwrite configuration
|
||||
-p P, --rpc-provider P
|
||||
RPC HTTP(S) provider url
|
||||
--rpc-dialect RPC_DIALECT
|
||||
RPC HTTP(S) backend dialect
|
||||
--height HEIGHT Block height to execute against
|
||||
-i I, --chain-spec I Chain specification string
|
||||
-u, --unsafe Do not verify address checksums
|
||||
--seq Use sequential rpc ids
|
||||
-y Y, --key-file Y Keystore file to use for signing or address
|
||||
--raw Do not decode output
|
||||
--fee-price FEE_PRICE
|
||||
override fee price
|
||||
--fee-limit FEE_LIMIT
|
||||
override fee limit
|
||||
|
||||
```
|
||||
@@ -158,8 +158,6 @@ class CmdCtrl:
|
||||
r = self.config.get(k, default)
|
||||
if k in [
|
||||
'_FORCE',
|
||||
'_FORCE_ALL',
|
||||
'_RAW_TX',
|
||||
]:
|
||||
if r == None:
|
||||
return False
|
||||
|
||||
@@ -9,10 +9,7 @@ from cic_eth_registry.lookup.tokenindex import TokenIndexLookup
|
||||
from cic_types.models.person import Person
|
||||
from chainlib.eth.address import to_checksum_address
|
||||
from chainlib.encode import TxHexNormalizer
|
||||
from hexathon import (
|
||||
add_0x,
|
||||
strip_0x,
|
||||
)
|
||||
from hexathon import add_0x
|
||||
|
||||
# local imports
|
||||
from clicada.tx import TxGetter
|
||||
@@ -22,7 +19,6 @@ from clicada.token import (
|
||||
token_balance,
|
||||
)
|
||||
from clicada.tx import ResolvedTokenTx
|
||||
from clicada.tx.file import FileTxStore
|
||||
from clicada.error import MetadataNotFoundError
|
||||
|
||||
logg = logging.getLogger(__name__)
|
||||
@@ -34,16 +30,12 @@ def process_args(argparser):
|
||||
argparser.add_argument('-m', '--method', type=str, help='lookup method')
|
||||
argparser.add_argument('--meta-url', dest='meta_url', type=str, help='Url to retrieve metadata from')
|
||||
argparser.add_argument('-f', '--force-update', dest='force_update', action='store_true', help='Update records of mutable entries')
|
||||
argparser.add_argument('-ff', '--force-update-all', dest='force_update_all', action='store_true', help='Update records of mutable entries and immutable entries')
|
||||
argparser.add_argument('--raw-tx', dest='raw_tx', action='store_true', help='Also cache raw transaction data')
|
||||
argparser.add_argument('identifier', type=str, help='user identifier')
|
||||
|
||||
|
||||
def extra_args():
|
||||
return {
|
||||
'raw_tx': '_RAW_TX',
|
||||
'force_update': '_FORCE',
|
||||
'force_update_all': '_FORCE_ALL',
|
||||
'method': 'META_LOOKUP_METHOD',
|
||||
'meta_url': 'META_URL',
|
||||
'identifier': '_IDENTIFIER',
|
||||
@@ -64,7 +56,7 @@ def execute(ctrl):
|
||||
|
||||
store_path = '.clicada'
|
||||
user_phone_file_label = 'phone'
|
||||
user_phone_store = FileUserStore(ctrl.opener('meta'), ctrl.chain(), user_phone_file_label, store_path, int(ctrl.get('FILESTORE_TTL')), encrypter=ctrl.encrypter, notifier=ctrl)
|
||||
user_phone_store = FileUserStore(ctrl.opener('meta'), ctrl.chain(), user_phone_file_label, store_path, int(ctrl.get('FILESTORE_TTL')), encrypter=ctrl.encrypter)
|
||||
|
||||
ctrl.notify('resolving identifier {} to wallet address'.format(ctrl.get('_IDENTIFIER')))
|
||||
user_address = user_phone_store.by_phone(ctrl.get('_IDENTIFIER'), update=ctrl.get('_FORCE'))
|
||||
@@ -86,7 +78,7 @@ def execute(ctrl):
|
||||
token_store = FileTokenStore(ctrl.chain(), ctrl.conn(), 'token', store_path)
|
||||
|
||||
user_address_file_label = 'address'
|
||||
user_address_store = FileUserStore(ctrl.opener('meta'), ctrl.chain(), user_address_file_label, store_path, int(ctrl.get('FILESTORE_TTL')), encrypter=ctrl.encrypter, notifier=ctrl)
|
||||
user_address_store = FileUserStore(ctrl.opener('meta'), ctrl.chain(), user_address_file_label, store_path, int(ctrl.get('FILESTORE_TTL')), encrypter=ctrl.encrypter)
|
||||
|
||||
ctrl.notify('resolving metadata for address {}'.format(user_address_normal))
|
||||
try:
|
||||
@@ -116,24 +108,16 @@ Tags: {}""".format(
|
||||
)
|
||||
)
|
||||
|
||||
raw_rpc = None
|
||||
if ctrl.get('_RAW_TX'):
|
||||
raw_rpc = ctrl.rpc
|
||||
|
||||
tx_store = FileTxStore(store_path, rpc=raw_rpc, notifier=ctrl)
|
||||
tx_lines = []
|
||||
seen_tokens = {}
|
||||
for tx_src in txs['data']:
|
||||
tx_hash = strip_0x(tx_src['tx_hash'])
|
||||
ctrl.notify('resolve details for tx {}'.format(tx_hash))
|
||||
ctrl.notify('resolve details for tx {}'.format(tx_src['tx_hash']))
|
||||
tx = ResolvedTokenTx.from_dict(tx_src)
|
||||
tx.resolve(token_store, user_address_store, show_decimals=True, update=ctrl.get('_FORCE'))
|
||||
tx_lines.append(tx)
|
||||
seen_tokens[tx.source_token_label] = tx.source_token
|
||||
seen_tokens[tx.destination_token_label] = tx.destination_token
|
||||
tx_store.put(tx_hash, str(tx_src), overwrite=ctrl.get('_FORCE_ALL'))
|
||||
|
||||
ctrl.write("Balances:")
|
||||
for k in seen_tokens.keys():
|
||||
ctrl.notify('resolve token {}'.format(seen_tokens[k]))
|
||||
(token_symbol, token_decimals) = token_store.by_address(seen_tokens[k])
|
||||
@@ -141,7 +125,7 @@ Tags: {}""".format(
|
||||
balance = token_balance(ctrl.chain(), ctrl.conn(), seen_tokens[k], user_address)
|
||||
fmt = '{:.' + str(token_decimals) + 'f}'
|
||||
decimal_balance = fmt.format(balance / (10 ** token_decimals))
|
||||
ctrl.write(" {} {}".format(token_symbol, decimal_balance))
|
||||
ctrl.write("Balances:\n {} {}".format(token_symbol, decimal_balance))
|
||||
|
||||
print()
|
||||
for l in tx_lines:
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
# standard imports
|
||||
import os
|
||||
import logging
|
||||
|
||||
# external imports
|
||||
from chainlib.eth.tx import transaction
|
||||
from leveldir.numeric import NumDir
|
||||
from leveldir.hex import HexDir
|
||||
from hexathon import strip_0x
|
||||
|
||||
logg = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class FileTxStore:
|
||||
|
||||
subdivision = 100000
|
||||
|
||||
def __init__(self, store_base_path, rpc=None, notifier=None):
|
||||
tx_base_path = os.path.join(store_base_path, 'tx')
|
||||
num_base_path = os.path.join(tx_base_path, 'blocks')
|
||||
hash_base_path = os.path.join(tx_base_path, 'hash')
|
||||
raw_base_path = os.path.join(tx_base_path, 'raw')
|
||||
self.block_index_dir = NumDir(num_base_path)
|
||||
self.hash_index_dir = HexDir(hash_base_path, 32)
|
||||
self.raw_index_dir = HexDir(raw_base_path, 32)
|
||||
self.rpc = rpc
|
||||
self.notifier = notifier
|
||||
|
||||
|
||||
def put(self, k, v, overwrite=False):
|
||||
if self.notifier != None:
|
||||
self.notifier.notify('caching tx data for {}'.format(k))
|
||||
hsh = bytes.fromhex(k)
|
||||
if not overwrite and self.hash_index_dir.have(k):
|
||||
logg.debug('tx store already has {}'.format(k))
|
||||
return
|
||||
|
||||
self.hash_index_dir.add(hsh, v.encode('utf-8'))
|
||||
|
||||
if self.rpc != None:
|
||||
self.notifier.notify('retrieve and cache raw tx data for {}'.format(k))
|
||||
o = transaction(k)
|
||||
r = self.rpc.conn.do(o)
|
||||
raw = bytes.fromhex(strip_0x(r['raw']))
|
||||
self.raw_index_dir.add(hsh, raw)
|
||||
@@ -65,7 +65,7 @@ class Account(Person):
|
||||
|
||||
class FileUserStore:
|
||||
|
||||
def __init__(self, metadata_opener, chain_spec, label, store_base_path, ttl, encrypter=None, notifier=None):
|
||||
def __init__(self, metadata_opener, chain_spec, label, store_base_path, ttl, encrypter=None):
|
||||
invalidate_before = datetime.datetime.now() - datetime.timedelta(seconds=ttl)
|
||||
self.invalidate_before = int(invalidate_before.timestamp())
|
||||
self.have_xattr = False
|
||||
@@ -83,7 +83,6 @@ class FileUserStore:
|
||||
self.metadata_opener = metadata_opener
|
||||
self.failed_entities = {}
|
||||
self.encrypter = encrypter
|
||||
self.notifier = notifier
|
||||
|
||||
|
||||
def __validate_dir(self):
|
||||
@@ -192,6 +191,7 @@ class FileUserStore:
|
||||
|
||||
if self.encrypter != None:
|
||||
v = self.encrypter.decrypt(k, v)
|
||||
logg.debug('>>>>>>>>>>>>< v decoded {}'.format(v))
|
||||
v = v.decode('utf-8')
|
||||
|
||||
logg.debug('retrieved {} from {}'.format(k, p))
|
||||
@@ -215,8 +215,6 @@ class FileUserStore:
|
||||
logg.info(e)
|
||||
pass
|
||||
|
||||
self.notifier.notify('wallet address for phone {} not found locally, retrieve from metadata service'.format(phone))
|
||||
|
||||
getter = self.metadata_opener
|
||||
ptr = generate_metadata_pointer(phone.encode('utf-8'), MetadataPointer.PHONE)
|
||||
r = None
|
||||
@@ -262,8 +260,6 @@ class FileUserStore:
|
||||
logg.info(e)
|
||||
pass
|
||||
|
||||
self.notifier.notify('metadata for wallet {} not found locally, retrieve from metadata service'.format(address))
|
||||
|
||||
getter = self.metadata_opener
|
||||
|
||||
ptr = generate_metadata_pointer(bytes.fromhex(address), MetadataPointer.PERSON)
|
||||
@@ -281,7 +277,6 @@ class FileUserStore:
|
||||
person = Account()
|
||||
person_data = person.deserialize(person_data=data)
|
||||
|
||||
self.notifier.notify('wallet {} resolved to {}, retrieve extended metadata from metadata service'.format(address, str(person)))
|
||||
ptr = generate_metadata_pointer(bytes.fromhex(address), MetadataPointer.CUSTOM)
|
||||
r = None
|
||||
try:
|
||||
|
||||
@@ -8,4 +8,3 @@ hexathon~=0.1.0
|
||||
pycryptodome~=3.10.1
|
||||
chainlib-eth~=0.0.21
|
||||
chainlib~=0.0.17
|
||||
leveldir~=0.1.0
|
||||
|
||||
Reference in New Issue
Block a user