Diving deeper into Cellframe: Python SDK & Plugins pt. 1

Diving deeper into Cellframe: Python SDK and Plugins pt. 1

Category: Guide

Title image, read title

This article was originally written by Mika Hyttinen. Medium link: https://medium.com/@mika.hyttinen


As Cellframe is gaining some attraction from investors and also from developers (it definitely deserves that!), I wanted to write something about it’s powerful Python SDK.

If some of my readers don’t know this yet, Cellframe is a third generation blockchain platform which allows you to create blockchain services/solutions with it’s SDK. Cellframe also packs some interesting core features:

  • 2-level sharding
  • post-quantum cryptography (from NIST PQC Round 3 submissions)
  • P2P cross-chain operations

And much much more.

Cellframe and it’s Python SDK allows you to write dApps, or actually t-dApps (truly decentralized apps) for the Cellframe ecosystem. It also allows you to write just simple plugins for the Cellframe node.

Why Python?

  Tiobe index: Popularity of programming languages, March 2022. <a href=https://tiobe.com/tiobe-index">

There are probably many reasons why Cellframe chose Python as the default language for their SDK, but i’ll give here few reasons why I personally think they made this choice:

  1. Keeping it simple, Python is the world’s most popular programming language.
  2. Python is easy to learn, easy to use and it has tons of ready to use libraries available to install with Python package installer.
  3. And as Cellframe is written in C, Integrating Python to C based software is solid choice.

Building a simple plugin with Cellframe Python SDK

Welcome to the Matrix.

Even though my background isn’t exactly in programming field, I decided to make a example of Cellframe Python SDK usage anyway as I still have some experience on languages like Bash, PHP, C++, Javascript (and HTML 😆).

First of all, you should have Cellframe Python SDK installed. Easiest way to get things going is to probably just install Cellframe node. If you have Raspberry Pi 3/4 laying around, you can use my Raspberry Pi tutorial for your node installation. In that particular tutorial I also explained how to correctly make Cellframe node to load plugins as you need to edit configuration file before loading the plugins is possible.

If you are running Windows, you can install Cellframe Dashboard as it has Cellframe node built-in. The path for configuration file in Windows is %SystemDrive%\Users\Public\Documents\cellframe-node\etc

Understanding the structure of plugin files and folders

If you read my earlier tutorial, the default path for plugins is /opt/cellframe-node/var/lib/plugins. By default, that particular directory is missing from that path so feel free to create the directory plugins by yourself.

When you have created the plugins directory, it’s time to create another directory inside plugins. In this example, we’re using tutorial .

Now inside tutorial, you need to have two files. One file is the plugin itself (in this example we’ll use tutorial.py ) and the second file is manifest.json . This is the current directory and file structure after making all the necessary files:

Now, using manifest.json, you specify basic metadata about your plugin such as the name, version and description. You can also declare if this current plugin has dependencies of other plugins. Typical manifest.json looks something like this:

Typical manifest.json file

The name, version, author and description keys must have a string values. The name value must match the name of the plugin and the name of the directory. The version and author values specifies the plugin version and plugin author. The dependencys ² key must have a array value which contains strings with plugin names. If the specified plugins are not installed, then this plugin will not be launched. The description value specifies a short description of the plugin.¹

¹ Source: https://wiki.cellframe.net/03.+Develop/Cellframe+Node+Python+Extentions/Python+Plugins

² Note: Dependencys is misspelled. It should be fixed in later versions.

The tutorial.py (or whatever you may want to call it) is the plugin which runs when you start Cellframe node. A skeleton for Cellframe node plugin could look something like this:

Typical Python plugin skeleton file.

When starting the node service, the init functions of the python script of each plugin will be called. When stopping the node service, the deinit functions will be called.

Let’s create a simple plugin!

Let’s start this series of articles with a simple “Hello World!” plugin tutorial.

Cellframe Python SDK allows you to add your own commands, which you can execute with the cellframe-node-cli tool.

First we need to study how all this works and look at their Wiki for AppCliServer.cmdItemCreate() and AppCliServer.setReplyText() methods

cmdItemCreate() method takes 4 arguments:

name: Name of the command
callbackFunction: Runs a function when command is called
doc: Short description of the command
docEx: Long description of the command

setReplyText() method takes 2 arguments:

replyText: String that is returned when command is called
idStrReply: must be a parameter idStrReply passed to the callback function.

So after we add this to our plugin, it should look something like this:

Adding cmdItemCreate()

As the second argument is the handler function, we need to add a function to our plugin which is called when you run helloworld -command:

Added the function helloWorld.

So after adding that, the finished plugin looks something like this:

Finished plugin!

If you don’t have the plugin placed in your plugins folder yet, you may move it there now. After that, just restart your node and fire up your terminal/Powershell etc. and type in cellframe-node-cli help , you should then be able to see our helloworld command in the list of commands:

Second from the bottom, our helloworld command!

So to use that command, just type in cellframe-node-cli helloworld and:

Voilà!

So there you have it. First simple plugin for your Cellframe node!

What’s next?

In the next article we will get back to our node installation series, this time we will be covering Windows (different type of installations, recommendations etc.).

When I’ll get back to the SDK, we will be building something more advanced and serious 😉.

Questions? Recommendations?

If you want to build something on Cellframe, join their development Telegram channel!

You can also contact me on Twitter or with Telegram.Sources of this tutorial are also available on my github.

Thank you for reading!