gochain

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2019 License: MIT Imports: 8 Imported by: 0

README

gochain

Build Status Coverage Status

This is a fast and simple implementation which uses a basic SHA256 problem to solve for mining based on a padding of zeros for the difficulty.

Usage

package main

import (
  gc "github.com/ohmybrew/gochain"
  "fmt"
)

// See tests for more examples...

// New chain.
c := new(gc.Chain)

// Add two blocks, mine them with a difficulty level of "2".
dif := 2
blk1 := c.BuildBlock(dif, "One")
blk2 := c.BuildBlock(dif, "Two")

blk1.Mine()
blk1.GenerateHash(true)
blk2.Mine()
blk2.GenerateHash(true)

fmt.Println("Block valid?", blk.IsValid())
fmt.Println("Block valid?", blk2.IsValid())
fmt.Println("Chain is valid?", c.IsValid())
fmt.Println("Same block?", c.IsSameBlock(blk, blk))

// Block to JSON
j := blk1.Encode()
fmt.Println(string(j)) // {"previous_hash": ..., "hash": ..., "index": ..., "nonce": ..., "timestamp": ..., "difficulty": ..., "data": ...}

// Chain to JSON
cj := c.Encode()
fmt.Println(string(j)) // {"blocks":[{"previous_hash": ..., "hash": ..., "index": ..., "nonce": ..., "timestamp": ..., "difficulty": ..., "data": ...}, {...}]}

// Get first, last, previous blocks
fb, _ := c.FirstBlock()     // equals blk1, if no first block, error will be second return
lb, _ := c.LastBlock()      // equals blk2, if no last block, error will be second return
pb, _ := c.PreviousBlock(1) // by index, 1 - 1 = 0, so this will equal blk1, if no previous block, error will be second return

Testing

bin/test for test suite.

bin/cover for test suite with coverage output.

Documentation

Available through godoc.org.

Important files:

  • block.go contains the struct for a block and its methods.
  • chain.go contains the struct for the chain and its methods.
  • gochain.go is empty, simply the package index.

LICENSE

This project is released under the MIT license.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Block

type Block struct {
	Previous   *Block    `json:"-"`
	Hash       []byte    `json:"hash"`
	Index      int       `json:"index"`
	Nonce      int       `json:"nonce"`
	Difficulty int       `json:"difficulty"`
	Data       string    `json:"data"`
	Timestamp  time.Time `json:"timestamp"`
}

Reprecents a block.

func (Block) Encode

func (blk Block) Encode() (j []byte)

Encodes the struct to JSON format.

func (*Block) GenerateHash

func (blk *Block) GenerateHash(save bool) (hs []byte)

Generate a hash for the block based on the block's struct data in JSON format. Option to save or simply generate.

func (Block) IsMined

func (blk Block) IsMined() bool

Check if the block is minded. Simply checks it has an nonce value.

func (Block) IsValid

func (blk Block) IsValid() bool

Confirms the block validity.

func (Block) IsValidNonce

func (blk Block) IsValidNonce() bool

Checks if this block's nonce is valid.

func (Block) MarshalJSON

func (blk Block) MarshalJSON() ([]byte, error)

Marshal for JSON encode. Used to add "previous_hash" to the JSON output.

func (*Block) Mine

func (blk *Block) Mine() (n int)

Mines the block. Will keep running until the nonce is valid and solved for the difficulty.

func (Block) ValidateNonce

func (blk Block) ValidateNonce(n int) bool

Validates the nonce by combining previous block's nonce with input n. Adding both together and hashing, should equal the padding of the difficulty.

type Chain

type Chain struct {
	Blocks []*Block `json:"blocks"`
}

Reprecents a block chain.

func (*Chain) AddBlock

func (c *Chain) AddBlock(blk *Block) *Chain

Adds a block to the chain and returns the chain.

func (*Chain) BuildBlock

func (c *Chain) BuildBlock(dif int, dat string) (blk *Block)

Builds a block easily. Difficult and data are the only two items required. The newly created block is returned and added to the chain.

func (Chain) Encode

func (c Chain) Encode() (j []byte)

Encodes the struct to JSON format.

func (Chain) FirstBlock

func (c Chain) FirstBlock() (*Block, error)

Get the first block in the chain.

func (Chain) IsSameBlock

func (c Chain) IsSameBlock(b1 *Block, b2 *Block) bool

Checks if the blocks are the same (simple hash check).

func (Chain) IsValid

func (c Chain) IsValid() bool

Walks the chain to ensure all blocks are valid.

func (Chain) LastBlock

func (c Chain) LastBlock() (*Block, error)

Get the last block in the chain.

func (Chain) Length

func (c Chain) Length() int

Return the chain length.

func (Chain) PreviousBlock

func (c Chain) PreviousBlock(i int) (*Block, error)

Gets the previous block relative to the provided index. If no available previous block is found, nil is returned

Jump to

Keyboard shortcuts

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