1.0.2 • Published 10 months ago

blockgraph v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
10 months ago

BlockTree

BlockTree is a novel implementation of a blockchain that, rather than forming a linear chain of blocks, forms a tree of blocks, with each block having an arbitrary number of child blocks. This enables more flexible, creative, and participatory processes, as any block can be extended with new content.

Overview

Unlike traditional blockchain implementations, which require a consensus mechanism to determine the order of blocks, BlockTree uses a locally validated and passively synchronized model. Each block is identified by a unique UUID, ensuring that blocks can be correctly and uniquely identified, regardless of the order in which they are created or received.

Getting Started

To install BlockTree, clone the repository and install the dependencies:

git clone https://github.com/username/BlockTree.git
cd BlockTree
npm install

how to use

// Create a new BlockTree instance
const blockTree = new BlockTree([]);

// Create a new transaction for the first block
const newBlockData1Transaction = new Transaction(
  TransactionType.ADD,
  new TransactionPayload(0, undefined, "ok")
);

// Create data for the first block
const newBlockData1 = new BlockData(
  "add first block",
  [newBlockData1Transaction],
  "Laodeus"
);

// Add the first block to the BlockTree
blockTree.addBlock("", newBlockData1);

// Log the BlockTree
console.log(JSON.stringify(blockTree));

Interfaces

IBlock

PropertyTypeDescription
idstringThe UUID of the block in the tree.
hashstringThe hash of the block.
previousHashstringThe hash of the previous block in the chain.
dataIBlockDataThe data associated with the block.

Methods

MethodReturnsDescription
calculateHash()stringCalculates the hash for the block.
validate()booleanValidates the block.

IBlockData

PropertyTypeDescription
titlestringThe title of the block.
transactionITransaction[]An array of transactions associated with the block.
authorstringThe author of the block.
timestampstringThe timestamp when the block was created.

IBlockTree

PropertyTypeDescription
genesisBlockIBlockThe first block in the blocktree.
blockListIBlock[]A list of all blocks in the blocktree.

Methods

MethodParametersReturnsDescription
addBlock(parentId: string, blockData: IBlockData)parentId: string, blockData: IBlockDataIBlockAdds a block to the blocktree.
getBlockById(id: string)id: stringIBlock | nullGets a specific block by its ID.
verifyIntegrity()nonebooleanVerifies the integrity of the blocktree.
getBlockChainToGenesis(blockId: string)blockId: stringIBlock[]Gets the chain of blocks from a specified block to the genesis block.
getAllKnownChildren(blockId: string)blockId: stringIBlock[] | nullGets all known children of a specified block.
synchronize(otherBlockTrees: IBlockTree[])otherBlockTrees: IBlockTree[]voidSynchronizes the blocktree with other blocktrees.
getGenesis()noneIBlockReturns the genesis block.

ITransaction

PropertyTypeDescription
typeTransactionTypeThe type of the transaction.
payloadITransactionPayloadThe details of the transaction.

ITransactionPayload

PropertyTypeDescription
startIndexnumberThe start index for the transaction operation.
lengthnumber (optional)The length of the section to be affected by the transaction.
contentstring (optional)The content to be added by the transaction.root

File Structure

├── models
│   ├── Block.ts
│   ├── BlockData.ts
│   ├── BlockTree.ts
│   ├── Transaction.ts
│   ├── TransactionPayload.ts
│   ├── Interfaces
│   │   ├── IBlock.ts
│   │   ├── IBlockData.ts
│   │   ├── IBlockTree.ts
│   │   ├── ITransaction.ts
│   │   ├── ITransactionPayload.ts
│   │   └── index.ts
│   └── index.ts
└── index.ts