Compare commits

...

4 Commits

Author SHA1 Message Date
Kevin F b59e79a21f Init wot plugin 2022-08-16 09:55:01 -05:00
Kevin F 3b8644fa8f Init wot plugin 2022-08-16 09:54:54 -05:00
Kevin F b2ebc56419 Added RPC plugin scaffolding 2022-08-11 14:25:07 -05:00
Kevin F bc71d12b77 Removed notifications requirements 2022-08-11 14:24:40 -05:00
13 changed files with 117 additions and 21 deletions

View File

@ -1 +0,0 @@
simplenotifications==0.2.18

View File

@ -1,8 +0,0 @@
#
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile --generate-hashes --output-file=requirements-notifications.txt requirements-notifications.in
#
simplenotifications==0.2.18 \
--hash=sha256:b7efd3d834b1922a3279287913d87fe4ef6fad181ca7edf54bfb8f1d973941e0

View File

@ -62,7 +62,7 @@ def daemon():
def _handle_sig_term(signum, frame):
sys.exit(0)
with open(filepaths.pid_file, 'w') as f:
f.write(str(os.getpid()))
@ -77,14 +77,15 @@ def daemon():
logger.info(
f"Onionr daemon is running under pid {os.getpid()}", terminal=True)
events.event('init', threaded=False)
events.event('afterinit', threaded=False)
events.event('daemon_start')
add_onionr_thread(
clean_block_database, 60, 'clean_block_database', initial_sleep=0)
Thread(
target=gossip.start_gossip_threads,
daemon=True,
target=gossip.start_gossip_threads,
daemon=True,
name='start_gossip_threads').start()
try:

View File

@ -21,6 +21,7 @@ import os, re, importlib
import traceback
from . import onionrevents as events
from .pluginapis import plugin_apis
import config, logger
from utils import identifyhome

View File

@ -0,0 +1,7 @@
#
from typing import Callable, Dict
# plugin apis are methods intended to be available to the rpc
# plugin, this is so plugins can provide apis to other plugins
# plugins add their methods during or before afterinit event
plugin_apis: Dict[str, Callable] = {}

View File

@ -28,11 +28,12 @@ def identify_home() -> str:
"""
path = os.environ.get('ONIONR_HOME', None)
if path is None:
system = platform.system()
if system == 'Linux':
path = os.path.expanduser('~') + '/.local/share/onionr/'
path = os.environ.get(
'XDG_DATA_HOME',
os.path.expanduser('~') + '/.local/share/onionr/')
elif system == 'Darwin':
path = os.path.expanduser('~' +
'/Library/Application Support/onionr/')

View File

@ -6,6 +6,7 @@ import sys
import os
import locale
from threading import Thread
from time import sleep
import cherrypy
@ -37,11 +38,13 @@ PLUGIN_VERSION = '0.0.0'
socket_file_path = identifyhome.identify_home() + 'rpc.sock'
from jsonrpc import JSONRPCResponseManager, dispatcher
import jsonrpc
import ujson
jsonrpc.manager.json = ujson
# RPC modules map Onionr APIs to the RPC dispacher
from rpc import blocks, pluginrpcmethods
@dispatcher.add_method
def foobar(**kwargs):
return kwargs["foo"] + kwargs["bar"]
class OnionrRPC(object):
@cherrypy.expose
@ -50,16 +53,18 @@ class OnionrRPC(object):
# Dispatcher is dictionary {<method_name>: callable}
data = cherrypy.request.body.read().decode('utf-8')
dispatcher["echo"] = lambda s: s
dispatcher["add"] = lambda a, b: a + b
response = JSONRPCResponseManager.handle(data, dispatcher)
return response.json
def on_afterinit(api, data=None):
pluginrpcmethods.add_plugin_rpc_methods()
def on_init(api, data=None):
config = {
'server.socket_file': socket_file_path,
#'server.socket_file': socket_file_path,
'server.socket_port': 0,
'engine.autoreload.on': False
}
cherrypy.config.update(config)

View File

@ -0,0 +1,25 @@
from secrets import randbits
from onionrblocks import Block
from jsonrpc import dispatcher
import ujson
from base64 import b85decode
from gossip.blockqueues import gossip_block_queues
from blockdb import get_blocks_after_timestamp
@dispatcher.add_method
def get_blocks(timestamp):
return [block.raw for block in get_blocks_after_timestamp(timestamp)]
queue_to_use = randbits(1)
@dispatcher.add_method
def insert_block(block):
block = Block(
block['id'], b85decode(block['raw']), auto_verify=False)
gossip_block_queues[queue_to_use].put_nowait(block)
return "ok"
#dispatcher['get_blocks_after_timestamp'] = get_blocks_after_timestamp

View File

@ -0,0 +1,8 @@
from jsonrpc import dispatcher
from onionrplugins.pluginapis import plugin_apis
def add_plugin_rpc_methods():
for method in plugin_apis:
dispatcher[method] = plugin_apis[method]

View File

@ -0,0 +1 @@
PYTHONPATH=./venv/bin/python310:../../src/:./

View File

@ -0,0 +1,4 @@
{ "name": "example",
"version": "0.0.0",
"author": "onionr"
}

View File

@ -0,0 +1,52 @@
"""Onionr - Private P2P Communication.
Default example plugin for devs or to test blocks
"""
import sys
import os
import locale
from time import sleep
import traceback
from typing import Set, TYPE_CHECKING
from threading import Thread, local
import blockdb
from gossip.peerset import gossip_peer_set
import logger
import onionrplugins
locale.setlocale(locale.LC_ALL, '')
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
# import after path insert
"""
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
plugin_name = 'wot'
PLUGIN_VERSION = '0.0.0'
def wot_test(arg: int):
return arg + 1
def on_init(api, data=None):
logger.info(
f"Web of Trust Plugin v{PLUGIN_VERSION} enabled", terminal=True)
onionrplugins.plugin_apis['wot'] = wot_test