Understanding L1 and L2 Levels in Cellframe Node Plugin Development

Cellframe Node allows developers to write plugins using Python at two distinct levels of abstraction: L1 (Low-Level Cellframe-SDK Calls) and L2 (High-Level Python Wrappers). These levels provide different approaches and tools for interacting with the core functionalities of the Cellframe-SDK, each suited to different development needs and expertise.

L1: Low-Level Cellframe-SDK Calls

Overview: The L1 level represents the most direct way to interact with the core functionalities of the Cellframe Node. It provides pure Python bindings to the methods and functions defined in the Cellframe-SDK, which are originally written in C.

Key Features:

  1. Direct SDK Access: L1 allows developers to call functions and methods directly from the Cellframe-SDK, providing detailed control over the node’s operations.
  2. Performance: As these bindings are direct wrappers around C functions, they can offer high performance and low memory load.
  3. Flexibility: Developers have the flexibility to utilize the full range of SDK functionalities, enabling them to implement highly customized and optimized solutions.
  4. Steep Learning Curve: Working at this level requires a good understanding of the Cellframe-SDK and its C-based API, making it more suitable for developers with experience in C or those who need to perform low-level operations.

Use Cases:

  • Implementing performance-critical components.
  • Accessing advanced or specialized features of the Cellframe-SDK.

Example Use:

from CellFrame.Network import Net
from DAP.Core import logIt
 
def init():
    net = Net.byName("riemann")
    logIt.notice(f"Network: {net.getCurAddr()}")
    return 0
 

Enhancing Development with Type Hinting

To improve code development and readability, type hinting is supported through .pyi files for DSP and Cellframe modules. These files provide type annotations that enhance code completion and static analysis in development environments like Visual Studio Code.

Integration with Visual Studio Code: To enable proper highlighting and type suggestions in Visual Studio Code, you need to add these .pyi files to your extraPaths.

Configuration Example:

  1. Open your settings.json file in Visual Studio Code.
  2. Add the path to your .pyi files under python.analysis.extraPaths
{"python.analysis.extraPaths": ["./path/to/your/pyi/files" ]}

This configuration ensures that Visual Studio Code recognizes the type annotations and provides better code suggestions and error checking, enhancing the overall development experience on the L1 level.

L2: High-Level Python Wrappers

Overview: The L2 level offers a more user-friendly and Pythonic approach to plugin development. It consists of high-level wrappers written in Python that simplify the complexity of the underlying L1 calls.

Key Features:

  1. Ease of Use: The high-level Python wrappers are designed to be intuitive and easy to use, following Python’s conventions and idioms.
  2. Rapid Development: By providing a simplified interface, L2 enables quicker development and prototyping of plugins.
  3. Readability and Maintainability: Code written at the L2 level is generally more readable and maintainable, making it accessible to a broader range of developers, including those less familiar with the Cellframe-SDK or C programming.
  4. Reduced Complexity: L2 hides the lower-level details, allowing developers to focus on the functionality of their plugins without worrying about the intricacies of the SDK.

Use Cases:

  • Developing general-purpose plugins quickly and efficiently.
  • Creating plugins that utilize standard blockchain operations without the need for extensive customization.

Example Use:

from pycfhelpers.node.net import CFNet
from pycfhelpers.node.logging import CFLog
 
log = CFLog()
 
def init():
    net = CFNet("riemann")
    log.notice(f"Network: {net.address}")
    return 0

Choosing Between L1 and L2

By offering both L1 and L2 levels, Cellframe Node ensures that developers have the flexibility to choose the right tool for their needs, whether they require the raw power and control of low-level SDK calls or the simplicity and ease of high-level Python wrappers.