GithubHelp home page GithubHelp logo

blockchain-project4's Introduction

Project #3. Web Service for a Blockchain

This is Project 3, Web Service for a Blockchain, in this project I used the classes to manage my private blockchain in Project 2, and exposed its functionality through a RESTful end points using Express/node js.

Setup project for Review.

To setup the project for review do the following:

  1. Download the project.
  2. Run command npm install to install the project dependencies.
  3. Run command node index.js in the root directory.
  4. Go to http://localhost:8000.

API Documentation

There are 6 endpoints in this project:

  1. Create a new block -> POST: /block/

to test this use the following curl command:

curl -X POST \
  http://localhost:8000/block/ \
  -H 'Content-Type: application/json' \
  -d '{"address": "1PMA5dqwJfy2qX42gYKCkDLK9gnAwV9HF2", "star": {"dec": "68° 52 56.9", "ra": "16h 29m 1.0s", "story": "Found star using https://www.google.com/sky/"}}'
  1. Get a block using its height -> GET: /blockchain/:blockId

to test this use the following curl command:

curl -X GET \
  http://localhost:8000/block/0

i have given the endpoint 0 which is the genesis block.

  1. Add a request validation -> POST: /requestValidation

to test this use the following curl command:

curl -X POST \
	http://localhost:8000/requestValidation \
	-H 'Content-Type: application/json' \
	-d '{"address": "1PMA5dqwJfy2qX42gYKCkDLK9gnAwV9HF2"}'
  1. Validate message signature -> POST: /message-signature/validate

to test this use the following curl command:

curl -X POST \
	http://localhost:8000/message-signature/validate \
	-H 'Content-Type: application/json' \
	-d '{"address": "1PMA5dqwJfy2qX42gYKCkDLK9gnAwV9HF2", "signature": "PLEASE GENERATE THIS USING 5KktgEJW7y1sLoqsys8dBAcWqgm9uih1UaghANX8FnQQxVs4ZGw"}'
  1. Get blocks by hash -> GET: /star/hash::hash

to test this use the following curl command:

curl -X POST \
	http://localhost:8000/star/hash:TYPE_ANY_HASH_HERE
  1. Get blocks by wallet address -> GET: /star/address::address

to test this use the following curl command:

curl -X POST \
	http://localhost:8000/star/address:1PMA5dqwJfy2qX42gYKCkDLK9gnAwV9HF2

Error handeling, there are 7 types of errors:

  1. blockNotFound: this happens when you query a height that is not there yet.
  2. blockNotValid: when added block data causes a problem in the block creation function.
  3. blockHasNoData: when data is not provided
  4. mempoolEntryNotFound: while validating the sent signature for a given wallet. If no entry was found related to such wallet, this error is raised.
  5. mempoolEntryWindowExpired: straight forward.
  6. signatureInvalid: when provided signature is not valid.
  7. addressNotConfirmed: when trying to add a block with given wallet. However, there is no confirmed requrests for it.

Testing the project

The file simpleChain.js in the root directory has all the code to be able to test the project, please review the comments in the file and uncomment the code to be able to test each feature implemented:

  • Uncomment the function:
(function theLoop (i) {
	setTimeout(function () {
		let blockTest = new Block.Block("Test Block - " + (i + 1));
		// Be careful this only will work if your method 'addBlock' in the Blockchain.js file return a Promise
		myBlockChain.addBlock(blockTest).then((result) => {
			console.log(result);
			i++;
			if (i < 10) theLoop(i);
		});
	}, 1000);
  })(0);

This function will create 10 test blocks in the chain.

  • Uncomment the function
myBlockChain.getBlock(0).then((block) => {
	console.log(block);
}).catch((err) => { console.log(err);});

This function get from the Blockchain the block requested.

  • Uncomment the function
myBlockChain.validateBlock(0).then((valid) => {
	console.log(valid);
})
.catch((error) => {
	console.log(error);
})

This function validate and show in the console if the block is valid or not, if you want to modify a block to test this function uncomment this code:

myBlockChain.getBlock(5).then((block) => {
	let blockAux = JSON.parse(block);
	blockAux.body = "Tampered Block";
	myBlockChain._modifyBlock(blockAux.height, JSON.stringify(blockAux)).then((blockModified) => {
		if(blockModified){
			myBlockChain.validateBlock(blockAux.height).then((valid) => {
				console.log(`Block #${blockAux.height}, is valid? = ${valid}`);
			})
			.catch((error) => {
				console.log(error);
			})
		} else {
			console.log("The Block wasn't modified");
		}
	}).catch((err) => { console.log(err);});
}).catch((err) => { console.log(err);});

myBlockChain.getBlock(6).then((block) => {	
	let blockAux = JSON.parse(block);
	blockAux.previousBlockHash = "jndininuud94j9i3j49dij9ijij39idj9oi";
	myBlockChain._modifyBlock(blockAux.height, JSON.stringify(blockAux)).then((blockModified) => {
		if(blockModified){
			console.log("The Block was modified");
		} else {
			console.log("The Block wasn't modified");
		}
	}).catch((err) => { console.log(err);});
}).catch((err) => { console.log(err);});
  • Uncomment this function:
myBlockChain.validateChain().then((errorLog) => {
	if(errorLog.length > 0){
		console.log("The chain is not valid:");
		errorLog.forEach(error => {
			console.log(error);
		});
	} else {
		console.log("No errors found, The chain is Valid!");
	}
})
.catch((error) => {
	console.log(error);
})

This function validates the whole chain and return a list of errors found during the validation.

What do I learned with this Project

  • I was able to identify the basic data model for a Blockchain application.
  • I was able to use LevelDB to persist the Blockchain data.
  • I was able to write algorithms for basic operations in the Blockchain.
  • I was able to write a flow for authorizing wallets to contribute to the block.

blockchain-project4's People

Contributors

smokyabdulrahman avatar

Watchers

 avatar  avatar

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.