Added RPC plugin scaffolding

This commit is contained in:
Kevin F 2022-08-11 14:25:07 -05:00
parent bc71d12b77
commit b2ebc56419
5 changed files with 39 additions and 9 deletions

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 @@
plugin_apis = {}

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
@dispatcher.add_method
def foobar(**kwargs):
return kwargs["foo"] + kwargs["bar"]
class OnionrRPC(object):
@cherrypy.expose
@ -50,16 +53,14 @@ 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_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)
@ -67,3 +68,4 @@ def on_init(api, data=None):
add_onionr_thread(
cherrypy.quickstart, 5, 'OnionrRPCServer',
OnionrRPC(), initial_sleep=0)

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