plugeth-utils/docs/custom.rst

86 lines
2.2 KiB
ReStructuredText
Raw Normal View History

.. _custom:
2022-02-10 23:18:33 +00:00
=======================
Writing a Custom Plugin
=======================
.. toctree::
:hidden:
RPC_method
subscription
tracer
2022-02-14 18:02:35 +00:00
Before setting out to write a plugin it will be helpful to be familiar with the :ref:`types`. Different plugins will require different implementation.
Basic Implementation
====================
2022-02-10 23:18:33 +00:00
All plugins will share some common aspects.
Package
-------
2022-02-10 23:18:33 +00:00
Any plugin will need its own package located in ``plugeth-plugins/packages``. The package will need to include a ``main.go`` from which the ``.so`` file will be built. The package and main file should share the same name and the name should be a word that describes the basic functionality of the plugin.
Initialize
----------
2022-02-10 23:18:33 +00:00
Most plugins will need to be initialized with a ``func Initialize``. The initialize function will need to be passed at least three arguments: ``cli.Context``, ``core.PluginLoader``, ``core.Logger``.
2022-02-10 23:18:33 +00:00
All plugins could have an intial template that looks something like this:
.. code-block:: Go
package main
import (
"github.com/openrelayxyz/plugeth-utils/core"
"gopkg.in/urfave/cli.v1"
)
var log core.Logger
func Initialize(ctx *cli.Context, loader core.PluginLoader, logger core.Logger) {
log = logger
log.Info("loaded New Custom Plugin")
}
InitializeNode
--------------
2022-02-10 23:18:33 +00:00
Many plugins will make use of ``func InitializeNode``.
.. code-block:: Go
func InitializeNode(stack core.Node, b core.Backend) {
backend = b
log.Info("Initialized node and backend")
}
2022-02-10 23:18:33 +00:00
``InitializeNode`` is called as soon as the Geth node is initialized. The ``core.Node`` object represents the running node with P2P and RPC capabilities, while the ``core.Backend`` gives you access to blocks and other data you may need to access.
Specialization
==============
From this point implimentation becomes more specialized to the particular plugin type. Continue from here for specific instructions for the following plugins:
* :ref:`RPC_method`
* :ref:`subscription`
* :ref:`tracer`
2022-02-10 23:18:33 +00:00
.. _blockupdates: https://github.com/openrelayxyz/plugeth-plugins/blob/master/packages/blockupdates/main.go
.. _hello: https://github.com/openrelayxyz/plugeth-plugins/blob/master/packages/hello/main.go