GithubHelp home page GithubHelp logo

teejays / copperchain Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 0.0 2.69 MB

A simple blockchain implementation in Go.

Go 98.50% Makefile 1.50%
go golang golang-package blockchain blockchain-demos restful-api

copperchain's Introduction

CopperChain

CopperChain implements a basic blockchain in Golang. It treats blockchain as a package, which can be imported by other packages and used. It also has the optional functionality of a web server, which provides basic Get and Post endpoints.

Click here for code documentation.

Getting Started

Prerequisites:

  1. Install Golang. You can install it from the official Go website.
  2. Install the following Go packages (if you don't have them already):
    • GoFiledb: A micro, very easy to use, DB client that uses filesystem for storage. To install, run: go get -u github.com/teejays/gofiledb
    • Gorilla Mux: A powerful URL router and dispatcher for golang. To install, run: go get -u github.com/gorilla/mux

Usage:

First, install CopperChain by running in your command line:

go get -u github.com/teejays/copperchain

After this, CopperChain can be included in your package easily. Add the following import statement to the go file which needs to use the package:

import github.com/teejays/copperchain

Before you interact with CopperChain, you need to initialize it. You can initialize it by calling the Init() function, that accepts optional parameters.

// Init Copper Chain
copperchain.Init(copperchain.Options{})

Once initialized, you can get the existing blockchain by calling the GetCopperChain() method.

chain := copperchain.GetCopperChain()

You can add a block to the blockchain by calling the AddToCopperChain() method, which requires a parameter of the form map[string]interface. This allows for flexibility of data that can be kept in the block.

data := make(map[string]interface{})
data["transaction_id"] = "abc123"
err := copperchain.AddToCopperChain(data)

If you want to set up the blockchain as RESTful API, you can call the RunSerever() method, which takes some options around the server address on which to rum the server.

err := copperchain.RunServer(copperchain.ServerOptions{
		Host: "127.0.0.1", //optional
		Port: 8080, // default 8080
	})

Example Code

Here is an example code that demonstrates the use of CopperChain. It can also be found at example.go.

package main

import (
	"fmt"
	"github.com/teejays/copperchain"
	"log"
)

func main() {

	// Init CopperChain
	copperchain.Init(copperchain.Options{
		DataRoot: ".data", 
	})
	
    	// Get CopperChain
	chain := copperchain.GetCopperChain()
	fmt.Printf("Chain: %+v\n", chain)

	// Add block data to chain (data should always be a map[string]interface{})
	var data map[string]interface{} = make(map[string]interface{})
	data["some_key"] = "the_value"
	copperchain.AddToCopperChain(data)

	// Check that the changes are reflected
	chain = copperchain.GetCopperChain()
	fmt.Printf("Chain: %+v\n", chain)
    
	// (optional) Run Server
	err = copperchain.RunServer(copperchain.ServerOptions{
		Host: serverHost,
		Port: serverPort,
	})
	if err != nil {
		log.Fatal(err)
	}

}

To manually run the provided example, download the example code locally, build it and run it:

go build -o example
./example

API Endpoints

If you decide to run the built-in webserver, it will listen on two endpoints:

  1. HTTP GET /
    • provides the entire chain
    • curl example: curl http://localhost:8080/
  2. HTTP POST /
    • accepts a json object as body, which can be parsed into a map[string]interface{}
    • curl example: curl http://localhost:8080/ -H "Content-Type: application/json" -d '{"transaction_id":"abc123"}'

Contact

For any issues, please open a new issue. If you want to contribute, please feel free to submit a merge request or reach out to me at [email protected].

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.