plugeth-utils/docs/hooks.rst

122 lines
5.3 KiB
ReStructuredText
Raw Normal View History

.. _hooks:
=====================
Selected Plugin Hooks
=====================
Plugin Hooks
************
2023-06-02 14:20:24 +00:00
Plugeth provides several hooks from which the plugin can capture data from Geth. Additionally in the case of **subcommands** the provided hooks are designed to change the behavior of Geth.
2021-09-24 21:01:16 +00:00
Hooks are called from functions within the plugin. For example, if we wanted to bring in data from the StateUpdate hook. We would impliment it like so:
(from `blockupdates`_)
2021-09-24 21:01:16 +00:00
.. code-block:: Go
2021-09-24 21:01:16 +00:00
func StateUpdate(blockRoot core.Hash, parentRoot core.Hash, destructs map[core.Hash]struct{}, accounts map[core.Hash][]byte, storage map[core.Hash]map[core.Hash][]byte, codeUpdates map[core.Hash][]byte) {
su := &stateUpdate{
Destructs: destructs,
Accounts: accounts,
Storage: storage,
Code: codeUpdates,
}
cache.Add(blockRoot, su)
data, _ := rlp.EncodeToBytes(su)
backend.ChainDb().Put(append([]byte("su"), blockRoot.Bytes()...), data)
}
2021-09-24 21:01:16 +00:00
2023-06-02 14:20:24 +00:00
Many hooks can be deployed in one plugin as is the case with the **BlockUpdater** plugin.
2021-09-24 21:01:16 +00:00
.. contents:: :local:
2021-09-24 21:01:16 +00:00
2023-06-02 14:20:24 +00:00
StateUpdate
***********
2021-09-24 21:01:16 +00:00
**Function Signature**:``func(root common.Hash, parentRoot common.Hash, destructs map[common.Hash]struct{}, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte)``
2021-09-24 21:01:16 +00:00
2023-06-02 14:20:24 +00:00
The state update plugin provides a snapshot of the state subsystem in the form of a a stateUpdate object. The stateUpdate object contains all information transformed by a transaction but not the transaction itself.
2021-09-24 21:01:16 +00:00
Invoked for each new block, StateUpdate provides the changes to the blockchain state. root corresponds to the state root of the new block. parentRoot corresponds to the state root of the parent block. destructs serves as a set of accounts that self-destructed in this block. accounts maps the hash of each account address to the SlimRLP encoding of the account data. storage maps the hash of each account to a map of that account's stored data.
2023-06-02 14:20:24 +00:00
.. warning:: StateUpdate is only called if Geth is running with
``-snapshots=true``. This is the default behavior for Geth, but if you are explicitly running with ``--snapshot=false`` this function will not be invoked.
2021-09-24 21:01:16 +00:00
AppendAncient
2021-09-24 21:01:16 +00:00
*************
**Function Signature**:``func(number uint64, hash, header, body, receipts, td []byte)``
2021-09-24 21:01:16 +00:00
Invoked when the freezer moves a block from LevelDB to the ancients database. ``number`` is the number of the block. ``hash`` is the 32 byte hash of the block as a raw ``[]byte``. ``header``, ``body``, and ``receipts`` are the RLP encoded versions of their respective block elements. ``td`` is the byte encoded total difficulty of the block.
GetRPCCalls
***********
**Function Signature**:``func(string, string, string)``
2023-06-02 14:20:24 +00:00
Invoked when the RPC handler registers a method call. Returns the call ``id``, method ``name``, and any ``params`` that may have been passed in.
.. todo:: missing a couple of hooks
PreProcessBlock
***************
**Function Signature**:``func(*types.Block)``
Invoked before the transactions of a block are processed. Returns a block object.
2021-09-24 21:01:16 +00:00
PreProcessTransaction
*********************
2021-09-24 21:01:16 +00:00
**Function Signature**:``func(*types.Transaction, *types.Block, int)``
Invoked before each individual transaction of a block is processed. Returns a transaction, block, and index number.
BlockProcessingError
********************
**Function Signature**:``func(*types.Transaction, *types.Block, error)``
2023-06-02 14:20:24 +00:00
Invoked if an error occurs while processing a transaction. This only applies to errors that would unvalidate the block were this transaction is included not errors such as reverts or opcode errors. Returns a transaction, block, and error.
NewHead
*******
**Function Signature**:``func(*types.Block, common.Hash, []*types.Log)``
2023-06-02 14:20:24 +00:00
Invoked when a new block becomes the canonical latest block. Returns a block, hash, and logs.
2023-06-02 14:20:24 +00:00
.. note:: If several blocks are processed in a group (such as
during a reorg) this may not be called for each block. You should track the prior latest head if you need to process intermediate blocks.
NewSideBlock
2021-09-24 21:01:16 +00:00
************
**Function Signature**:``func(*types.Block, common.Hash, []*types.Log)``
2023-06-02 14:20:24 +00:00
Invoked when a block is side-chained. Returns a block, hash, and logs.
2021-09-24 21:01:16 +00:00
.. note:: Blocks passed to this method are non-canonical blocks.
2021-09-24 21:01:16 +00:00
Reorg
*****
2021-09-24 21:01:16 +00:00
**Function Signature**:``func(common *types.Block, oldChain, newChain types.Blocks)``
2021-09-24 21:01:16 +00:00
2023-06-02 14:20:24 +00:00
Invoked when a chain reorg occurs, that is; at least one block is removed and one block is added. (``oldChain`` is a list of removed blocks, ``newChain`` is a list of newliy added blocks, and ``common`` is the latest block that is an ancestor to both oldChain and newChain.) Returns a block, a list of old blocks, and a list of new blocks.
2021-09-24 21:01:16 +00:00
.. _blockupdates: https://github.com/openrelayxyz/plugeth-plugins/tree/master/packages/blockupdates
2021-09-24 21:01:16 +00:00
.. _StateUpdate: https://github.com/openrelayxyz/plugeth/blob/develop/core/state/plugin_hooks.go
.. _Invocation: https://github.com/openrelayxyz/plugeth/blob/develop/core/state/statedb.go#L955
.. _AppendAncient: https://github.com/openrelayxyz/plugeth/blob/develop/core/rawdb/plugin_hooks.go
.. _GetRPCCalls: https://github.com/openrelayxyz/plugeth/blob/develop/rpc/plugin_hooks.go
.. _NewHead: https://github.com/openrelayxyz/plugeth/blob/develop/core/plugin_hooks.go#L108