hiveMerkle Directed Acyclic Graphs (DAGs)

A Merkle Directed Acyclic Graph (DAG) is built into the template to enable the network to have a chain of events similar to a blockchain.

This is a more advanced feature of the subnet template and should be used if peers require historical data for features, such as to score other peers in the network based on multiple epochs of data, or if newly joined peers require syncing historical data, similar to a blockchain archive node.

This will be a high-level overview of setting up the DAG, syncing peers, and publishing nodes to the DAG.

This can be used in place of using GossipSub directly. The DAG uses the GossipSub itself, but the data will instead form a DAG chain, similar to a blockchain.

To set up a DAG, there are two main concepts: the DAG receiver and the DAG publisher.

DAG Receiver

There are only 4 classes that must set up the Merkle DAG receiver logic that can handle multiple payloads:

MerkleDagSyncProtocol

The MerkleDagSyncProtocol handles syncing the Merkle DAG between peers. For example, when a peer joins the subnet, it will handle the communication between peers to get the joining peer up to date on the latest DAG node.

SyncProtocolPeerRequestClient

The SyncProtocolPeerRequestClient handles calling the MerkleDagSyncProtocol.

DagPeerSetProvider

This handles fetching peers to request syncing the Merkle DAG to in the MerkleDagSyncProtocol.

DagGossipSystem

The DagGossipSystem is the base layer for all Merkle DAG logic, including receiving gossip messages from other peers to form a DAG.


DAG Publisher

The DAG publishing logic comes with a template called the DagPublisherTemplate. Here, you use this template to set up

When creating a Merkle DAG specific to your subnets' use cases, you need to create a payload that will be announced to the DAG. You can also create multiple payloads, and each payload will, by default, have its own line in the Merkle DAG.

Publishing a node to the DAG can be as simple as a function that has an interval where it will publish to the DagGossipSystem.


Usage

Start with the data that will be published to the DAG.

Payload & Schema

First, we create the payload, which is a Pydantic dataclass template:

DagPayloadTemplate

The DAG payload template is the payload template that will be inhreited by your custom payload class.

Custom Payload

Create the custom payload. In this example, each peer will publish a peer state payload including its role and state.

Initializing Merkle DAG Example

We now initialize the DagGossipSystem which handles all DAG related tasks

The following logic will be handled by the ServerBase template, so all of the P2P arguments, such as dht, pubsub, and gossipsub, are automatically generated.

Initializing Merkle DAG Publisher

Now we create the class that handles configuring the payload for the DAG.

run

In this example, the peer publishes a peer state payload once every 20 seconds to the DAG.

build_payload

Build the payload to be published to the DAG

Last updated