datachain

package module
v0.0.0-...-8f02f41 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 15, 2019 License: MIT Imports: 12 Imported by: 0

README

datachain

A simple (DAG) blockchain that can hold arbitrary data, uses CryptoNight hash function.

Usage

package main

import (
	"fmt"

	bc "github.com/cauefcr/datachain"
)

func main() {
	var (
		blockchain = bc.Blockchain{}
		blockfile  = "./blockchain.json"
	)
	// load blockchain from file (if it exists)
	blockchain = bc.BlockchainFromFile(blockfile)
	// make a new block
	nb := bc.Block{Data: []byte("The Times 03/Jan/2009 Chancellor on brink of second bailout for banks")}

	// mine the block
	nb.Mine(nb)
	// put it in the blockchain
	blockchain = append(blockchain, nb)

	fmt.Printf("%+v", blockchain)

	// uncomment to have an orphaned block
	// blockchain = append(blockchain, bc.Block{Data: []byte("The Times 03/Jan/2009 Chancellor on brink of second bailout for banks")})
	// check to see if the blockchain data makes sense, it will remove everything after a broken node
	if clean_chain := blockchain.Comb(); len(clean_chain) != len(blockchain) {
		fmt.Println("blocks were orphaned")
		blockchain = clean_chain
	} else {
		fmt.Println("No orphans, the blockchain was clean")
	}
	//view blockchain
	fmt.Printf("%+v", blockchain)

	// save blockchain to file
	blockchain.Tofile(blockfile)
}

Features:

[x]Mine

[x]Saving

[x]Reading

[ ]Synching with other nodes

[x]Dealing with tree-like structures that can arrive

[ ]Querying structural data about the DAG

[ ]Subscribing to sub-tree add events

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Server

func Server(path string, bc Blockchain, blockfile string)

Types

type Block

type Block struct {
	Time     int64  `json:"t"`
	Nonce    int64  `json:"nonce"`
	Data     []byte `json:"data"`
	Prevhash []byte `json:"phash"`
}

func (*Block) Mine

func (b *Block) Mine(prev Block)

func (Block) Prettify

func (b Block) Prettify() PrettyBlock

func (Block) SaveBlock

func (b Block) SaveBlock(blockfile string) error

type Blockchain

type Blockchain []Block

func BlockchainFromFile

func BlockchainFromFile(blockfile string) Blockchain

func LastBlocks

func LastBlocks(blockfile string, n int) Blockchain

func NewBlockchain

func NewBlockchain(blockfile string) Blockchain

func (Blockchain) Comb

func (bc Blockchain) Comb() Blockchain

func (Blockchain) Prettify

func (bc Blockchain) Prettify() PrettyChain

func (Blockchain) Tofile

func (bc Blockchain) Tofile(blockfile string)

to-do: io.reader version for big chains

type PrettyBlock

type PrettyBlock struct {
	Time     time.Time `json:"time"`
	Nonce    int64     `json:"nonce"`
	Data     string    `json:"data"`
	Prevhash string    `json:"prev_hash"`
	Hash     string    `json:"hash"`
}

type PrettyChain

type PrettyChain []PrettyBlock

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL