Luyao

Fully Decentralized Forum With 60 Lines of Code

Try it here: Bodhi Space

Last year, I built a 100-line protocol called Bodhi, designed to store content and incentivize high-value content at a basic protocol level. If you’re intrigued about why this might work, you can check out the details here.

Theoretically, it can be used to build any kind of application (like Reddit, Twitter, or YouTube) with very few lines of code (less than 100 lines).

When I launched it in the Chinese community four months ago, it became popular among speculators, but many developers overlooked it, assuming it was just another speculative token project and not delving into the concept behind it.

So, I decided to build the first application on Bodhi myself, to demonstrate what can be achieved with it.

I created a fully decentralized forum with just 60 lines of code, called Bodhi Space. The code is extremely simple; you can check it out here on Etherscan.


For those not familiar with blockchain, let me explain step-by-step how it works.

Storage

At a very basic level, content is stored on a blockchain storage platform called Arweave.

Arweave’s mechanism is elegantly simple. You pay once, and your content is stored forever, like on a decentralized hard disk. I’m surprised no one has tried to build a forum on it before.

Every stored file has an Arweave ID, like pyA8pudekCgoIKpL8EqxvseMDtxvtDeUTiaVa0Gl2FA. You can access the content through gateways like:

You can also run your own nodes, they are open-source and permissionless.

Program Environment

The logic runs on Ethereum/Optimism.

Ethereum is like a big computer that operates on everyone’s machines, but once you deploy your code, no one can change it, and it cannot be stopped.

Optimism batches transactions to lower the fees on Ethereum. This technique is known as ‘Layer 2’. They are also open-source and permissionless.

Programs running on the blockchain are termed contracts. Essentially, they are just bytecode stored at a specific address. When a user interacts with a contract, the Ethereum Virtual Machine (EVM) reads the bytecode, and run it with user’s call. This architecture allows any program call any other program in the same computer. (Personally, I find this revolutionary for program collabration, which, in my view, is the core value of blockchain.)

Both Bodhi’s 100-line code and Space’s 60-line code run on this system.

Bodhi

What Bodhi does is turn an Arweave ID into an asset (similar to a startup where people invest and the share price increases, but the economic details will not be covered in this article).

The structure is straightforward:

struct Asset {
    uint256 id;
    string arTxId; // Arweave transaction ID
    address creator;
}

App: Space

The app Bodhi Space has two contracts, Space and Space Factory.

The core of the Space contract is a tree structure. Each node is an asset, and each asset can have children assets (replies). The root is the Space itself.

mapping(uint256 => uint256) public assetToParent;

When user creating a post, the Space contract will call Bodhi to create that asset, and insert that asset as a node into the tree.

function create(string calldata arTxId, uint256 parentId) external {
    // ...
    assetToParent[assetId] = parentId;
    bodhi.create(arTxId);
}

There are three additional functions mainly used for managing the space, but we won’t go into detail here.

Additionally, we have a contract called Space Factory. Users can call Space Factory to deploy a brand-new Space with their settings. This is similar to Reddit users creating a subreddit, except that it’s permanent and cannot be shut down.

This is all the code for Space Factory:

mapping(uint256 => address) public spaces;
uint256 public spaceIndex = 0;

function create(uint256 assetId, string calldata spaceName) public {
    Space newSpace = new Space(assetId, spaceName, msg.sender);
    spaces[spaceIndex] = address(newSpace);
    spaceIndex++;
}

When you see these code, you might think

“WTF, this is not a forum, it’s just a tree structure of some IDs”.

But the final product may surprise you. It’s a fully functional and smooth-running product with no database, operating entirely on the blockchain.

What’s even more fantastic is that

  1. Anyone can build a frontend for it, as all the data is public and transparent on the blockchain.

  2. Anyone can build anything on top of it without needing to worry about me interfering. Since you can read the code, you know I cannot do anything to stop anyone from using it.

  3. Because it’s hosted on the blockchain, anyone can add new features or even transform it into a completely different product.

I hope this article helps people who have never been interested in blockchain to understand it better.

Building these projects has often been a lonely journey for me – mostly because everyone else in crypto is focused on financial applications, and I truly wish more people could see the broader potential of this field.


Further Reading