2021-09-23 23:01:00 +00:00
.. _api:
===
API
===
2021-10-05 22:43:09 +00:00
Anatomy of a Plugin
===================
2021-10-04 18:51:19 +00:00
2021-09-23 23:01:00 +00:00
Plugins for Plugeth use Golang's `Native Plugin System`_ . Plugin modules must export variables using specific names and types. These will be processed by the plugin loader, and invoked at certain points during Geth's operations.
Flags
-----
* **Name:** Flags
* **Type:** `flag.FlagSet`_
* **Behavior:** This FlagSet will be parsed and your plugin will be able to access the resulting flags. Flags will be passed to Geth from the command line and are intended to of the plugin. Note that if any flags are provided, certain checks are disabled within Geth to avoid failing due to unexpected flags.
Subcommands
-----------
* **Name:** Subcommands
* **Type:** map[string]func(ctx `*cli.Context`_ , args []string) error
* **Behavior:** If Geth is invoked with `` ./geth YOUR_COMMAND `` , the plugin loader will look for `` YOUR_COMMAND `` within this map, and invoke the corresponding function. This can be useful for certain behaviors like manipulating Geth's database without having to build a separate binary.
Initialize
----------
* **Name:** Initialize
* **Type:** func(*cli.Context, core.PluginLoader, core.logs )
2021-10-05 22:43:09 +00:00
* **Behavior:** Called as soon as the plugin is loaded, with the cli context and a reference to the plugin loader. This is your plugin's opportunity to initialize required variables as needed. Note that using the context object you can check arguments, and optionally can manipulate arguments if needed for your plugin.
2021-09-23 23:01:00 +00:00
InitializeNode
--------------
* **Name:** InitializeNode
* **Type:** func(core.Node, core.Backend)
* **Behavior:** This 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 Backend gives you access to a wide array of data you may need to access.
2021-10-18 23:25:33 +00:00
.. note :: If a particular plugin requires access to the node.Node object it can be obtained using the restricted package located in `PluGeth-Utils`_ .
2021-09-23 23:01:00 +00:00
GetAPIs
-------
* **Name:** GetAPIs
* **Type:** func(core.Node, core.Backend) []rpc.API
* **Behavior:** This allows you to register new RPC methods to run within Geth.
The GetAPIs function itself will generally be fairly brief, and will looks something like this:
.. code-block :: go
2021-10-05 22:43:09 +00:00
`` func GetAPIs(stack *node.Node, backend core.Backend) []core.API {
return []rpc.API{
{
Namespace: "mynamespace",
Version: "1.0",
Service: &MyService{backend},
Public: true,
},
}
}``
2021-09-23 23:01:00 +00:00
The bulk of the implementation will be in the `` MyService `` struct. MyService should be a struct with public functions. These functions can have two different types of signatures:
* RPC Calls: For straight RPC calls, a function should have a `` context.Context `` object as the first argument, followed by an arbitrary number of JSON marshallable arguments, and return either a single JSON marshal object, or a JSON marshallable object and an error. The RPC framework will take care of decoding inputs to this function and encoding outputs, and if the error is non-nil it will serve an error response.
* Subscriptions: For subscriptions (supported on IPC and websockets), a function should have a `` context.Context `` object as the first argument followed by an arbitrary number of JSON marshallable arguments, and should return an `` *rpc.Subscription `` object. The subscription object can be created with `` rpcSub := notifier.CreateSubscription() `` , and JSON marshallable data can be sent to the subscriber with `` notifier.Notify(rpcSub.ID, b) `` .
A very simple MyService might look like:
.. code-block :: go
2021-10-05 22:43:09 +00:00
`` type MyService struct{}
2021-10-04 18:51:19 +00:00
2021-10-05 22:43:09 +00:00
func (h MyService) HelloWorld(ctx context.Context) string {
return "Hello World"
}``
2021-10-04 18:51:19 +00:00
2021-10-05 22:43:09 +00:00
And the client could access this with an rpc call to
`` mynamespace_helloworld ``
2021-10-04 18:51:19 +00:00
Injected APIs
2021-10-05 22:43:09 +00:00
=============
2021-10-04 18:51:19 +00:00
In addition to hooks that get invoked by Geth, several objects are injected that give you access to additional information.
Backend Object
--------------
The `` core.Backend `` object is injected by the `` InitializeNode() `` and `` GetAPI() `` functions. It offers the following functions:
Downloader
2021-10-05 22:43:09 +00:00
***** *****
2021-10-04 18:51:19 +00:00
`` Downloader() Downloader ``
Returns a Downloader objects, which can provide Syncing status
SuggestGasTipCap
2021-10-05 22:43:09 +00:00
***** ***** ***** *
2021-10-04 18:51:19 +00:00
`` SuggestGasTipCap(ctx context.Context) (*big.Int, error) ``
Suggests a Gas tip for the current block.
ExtRPCEnabled
2021-10-05 22:43:09 +00:00
***** ***** ***
2021-10-04 18:51:19 +00:00
`` ExtRPCEnabled() bool ``
Returns whether RPC external RPC calls are enabled.
RPCGasCap
2021-10-05 22:43:09 +00:00
***** *** *
2021-10-04 18:51:19 +00:00
`` RPCGasCap() uint64 ``
Returns the maximum Gas available to RPC Calls.
RPCTxFeeCap
2021-10-05 22:43:09 +00:00
***** ***** *
2021-10-04 18:51:19 +00:00
`` RPCTxFeeCap() float64 ``
Returns the maximum transaction fee for a transaction submitted via RPC.
UnprotectedAllowed
2021-10-05 22:43:09 +00:00
***** ***** ***** ***
2021-10-04 18:51:19 +00:00
`` UnprotectedAllowed() bool ``
Returns whether or not unprotected transactions can be transmitted through this
node via RPC.
SetHead
2021-10-05 22:43:09 +00:00
***** **
2021-10-04 18:51:19 +00:00
`` SetHead(number uint64) ``
Resets the head to the specified block number.
HeaderByNumber
2021-10-05 22:43:09 +00:00
***** ***** *** *
2021-10-04 18:51:19 +00:00
`` HeaderByNumber(ctx context.Context, number int64) ([]byte, error) ``
Returns an RLP encoded block header for the specified block number.
The RLP encoded response can be decoded into a `plugeth-utils/restricted/types.Header` object.
HeaderByHash
2021-10-05 22:43:09 +00:00
***** ***** **
2021-10-04 18:51:19 +00:00
`` HeaderByHash(ctx context.Context, hash Hash) ([]byte, error) ``
Returns an RLP encoded block header for the specified block hash.
The RLP encoded response can be decoded into a `plugeth-utils/restricted/types.Header` object.
CurrentHeader
2021-10-05 22:43:09 +00:00
***** ***** ***
2021-10-04 18:51:19 +00:00
`` CurrentHeader() []byte ``
Returns an RLP encoded block header for the current block.
The RLP encoded response can be decoded into a `plugeth-utils/restricted/types.Header` object.
CurrentBlock
2021-10-05 22:43:09 +00:00
***** ***** **
2021-10-04 18:51:19 +00:00
`` CurrentBlock() []byte ``
Returns an RLP encoded full block for the current block.
The RLP encoded response can be decoded into a `plugeth-utils/restricted/types.Block` object.
BlockByNumber
2021-10-05 22:43:09 +00:00
***** ***** ***
2021-10-04 18:51:19 +00:00
`` BlockByNumber(ctx context.Context, number int64) ([]byte, error) ``
2021-09-23 23:01:00 +00:00
2021-10-04 18:51:19 +00:00
Returns an RLP encoded full block for the specified block number.
2021-09-23 23:01:00 +00:00
2021-10-04 18:51:19 +00:00
The RLP encoded response can be decoded into a `plugeth-utils/restricted/types.Block` object.
2021-10-05 22:43:09 +00:00
2021-10-04 18:51:19 +00:00
BlockByHash
2021-10-05 22:43:09 +00:00
***** ***** *
2021-10-04 18:51:19 +00:00
`` BlockByHash(ctx context.Context, hash Hash) ([]byte, error) ``
2021-09-23 23:01:00 +00:00
2021-10-04 18:51:19 +00:00
Returns an RLP encoded full block for the specified block hash.
2021-09-23 23:01:00 +00:00
2021-10-04 18:51:19 +00:00
The RLP encoded response can be decoded into a `plugeth-utils/restricted/types.Block` object.
GetReceipts
2021-10-05 22:43:09 +00:00
***** ***** *
2021-10-04 18:51:19 +00:00
`` GetReceipts(ctx context.Context, hash Hash) ([]byte, error) ``
Returns an JSON encoded list of receipts for the specified block hash.
The JSON encoded response can be decoded into a `plugeth-utils/restricted/types.Receipts` object.
GetTd
2021-10-05 22:43:09 +00:00
*****
2021-10-04 18:51:19 +00:00
`` GetTd(ctx context.Context, hash Hash) *big.Int ``
Returns the total difficulty for the specified block hash.
SubscribeChainEvent
2021-10-05 22:43:09 +00:00
***** ***** ***** *** *
2021-10-04 18:51:19 +00:00
`` SubscribeChainEvent(ch chan<- ChainEvent) Subscription ``
Subscribes the provided channel to new chain events.
SubscribeChainHeadEvent
2021-10-05 22:43:09 +00:00
***** ***** ***** ***** ***
2021-10-04 18:51:19 +00:00
`` SubscribeChainHeadEvent(ch chan<- ChainHeadEvent) Subscription ``
Subscribes the provided channel to new chain head events.
SubscribeChainSideEvent
2021-10-05 22:43:09 +00:00
***** ***** ***** ***** ***
2021-10-04 18:51:19 +00:00
`` SubscribeChainSideEvent(ch chan<- ChainSideEvent) Subscription ``
Subscribes the provided channel to new chain side events.
SendTx
2021-10-05 22:43:09 +00:00
***** *
2021-10-04 18:51:19 +00:00
`` SendTx(ctx context.Context, signedTx []byte) error ``
Sends an RLP encoded, signed transaction to the network.
GetTransaction
2021-10-05 22:43:09 +00:00
***** ***** *** *
2021-10-04 18:51:19 +00:00
`` GetTransaction(ctx context.Context, txHash Hash) ([]byte, Hash, uint64, uint64, error) ``
Returns an RLP encoded transaction at the specified hash, along with the hash and number of the included block, and the transaction's position within that block.
GetPoolTransactions
^^^^^^^^^^^^^^^^^^^
`` GetPoolTransactions() ([][]byte, error) ``
Returns a list of RLP encoded transactions found in the mempool
GetPoolTransaction
2021-10-05 22:43:09 +00:00
***** ***** ***** ***
2021-10-04 18:51:19 +00:00
`` GetPoolTransaction(txHash Hash) []byte ``
Returns the RLP encoded transaction from the mempool at the specified hash.
GetPoolNonce
2021-10-05 22:43:09 +00:00
***** ***** **
2021-10-04 18:51:19 +00:00
`` GetPoolNonce(ctx context.Context, addr Address) (uint64, error) ``
Returns the nonce of the last transaction for a given address, including
transactions found in the mempool.
Stats
2021-10-05 22:43:09 +00:00
*****
2021-10-04 18:51:19 +00:00
`` Stats() (pending int, queued int) ``
Returns the number of pending and queued transactions in the mempool.
TxPoolContent
2021-10-05 22:43:09 +00:00
***** ***** ***
2021-10-04 18:51:19 +00:00
`` TxPoolContent() (map[Address][][]byte, map[Address][][]byte) ``
Returns a map of addresses to the list of RLP encoded transactions pending in
the mempool, and queued in the mempool.
SubscribeNewTxsEvent
2021-10-05 22:43:09 +00:00
***** ***** ***** *****
2021-10-04 18:51:19 +00:00
`` SubscribeNewTxsEvent(chan<- NewTxsEvent) Subscription ``
Subscribe to a feed of new transactions added to the mempool.
GetLogs
2021-10-05 22:43:09 +00:00
***** **
2021-10-04 18:51:19 +00:00
`` GetLogs(ctx context.Context, blockHash Hash) ([][]byte, error) ``
Returns a list of RLP encoded logs found in the specified block.
SubscribeLogsEvent
2021-10-05 22:43:09 +00:00
***** ***** ***** ***
2021-10-04 18:51:19 +00:00
`` SubscribeLogsEvent(ch chan<- [][]byte) Subscription ``
Subscribe to logs included in a confirmed block.
SubscribePendingLogsEvent
2021-10-05 22:43:09 +00:00
***** ***** ***** ***** *****
2021-10-04 18:51:19 +00:00
`` SubscribePendingLogsEvent(ch chan<- [][]byte) Subscription ``
Subscribe to logs from pending transactions.
SubscribeRemovedLogsEvent
2021-10-05 22:43:09 +00:00
***** ***** ***** ***** *****
2021-10-04 18:51:19 +00:00
`` SubscribeRemovedLogsEvent(ch chan<- []byte) Subscription ``
Subscribe to logs removed from the canonical chain in reorged blocks.
Node Object
-----------
The `` core.Node `` object is injected by the `` InitializeNode() `` and `` GetAPI() `` functions. It offers the following functions:
Server
2021-10-05 22:43:09 +00:00
***** *
2021-10-04 18:51:19 +00:00
`` Server() Server ``
The Server object provides access to `` server.PeerCount() `` , the number of peers connected to the node.
DataDir
2021-10-05 22:43:09 +00:00
***** **
2021-10-04 18:51:19 +00:00
`` DataDir() string ``
Returns the Ethereuem datadir.
InstanceDir
2021-10-05 22:43:09 +00:00
***** ***** *
2021-10-04 18:51:19 +00:00
`` InstanceDir() string ``
Returns the instancedir used by the protocol stack.
IPCEndpoint
2021-10-05 22:43:09 +00:00
***** ***** *
2021-10-04 18:51:19 +00:00
`` IPCEndpoint() string ``
The path of the IPC Endpoint for this node.
HTTPEndpoint
2021-10-05 22:43:09 +00:00
***** ***** **
2021-10-04 18:51:19 +00:00
`` HTTPEndpoint() string ``
The url of the HTTP Endpoint for this node.
WSEndpoint
2021-10-05 22:43:09 +00:00
***** *****
2021-10-04 18:51:19 +00:00
`` WSEndpoint() string ``
The url of the websockets Endpoint for this node.
ResolvePath
2021-10-05 22:43:09 +00:00
***** ***** *
2021-10-04 18:51:19 +00:00
`` ResolvePath(x string) string ``
Resolves a path within the DataDir.
2021-09-23 23:01:00 +00:00
.. _*cli.Context: https://pkg.go.dev/github.com/urfave/cli#Context
.. _flag.FlagSet: https://pkg.go.dev/flag#FlagSet
2021-10-04 18:51:19 +00:00
.. _Native Plugin System: https://pkg.go.dev/plugin
Logger
------
The Logger object is injected by the `` Initialize() `` function. It implements
logging based on the interfaces of `Log15 <https://github.com/inconshreveable/log15> `_ .
2021-10-05 22:43:09 +00:00
2021-10-18 23:25:33 +00:00
.. _PluGeth-Utils: https://github.com/openrelayxyz/plugeth-utils
2021-10-05 22:43:09 +00:00
.. _*cli.Context: https://pkg.go.dev/github.com/urfave/cli#Context
.. _flag.FlagSet: https://pkg.go.dev/flag#FlagSet
.. _Native Plugin System: https://pkg.go.dev/plugin