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.
To setup the project for review do the following:
- Download the project.
- Run command npm install to install the project dependencies.
- Run command node index.js in the root directory.
- Go to http://localhost:8000.
There are 6 endpoints in this project:
- 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/"}}'
- 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.
- 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"}'
- 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"}'
- 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
- 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
- blockNotFound: this happens when you query a height that is not there yet.
- blockNotValid: when added block data causes a problem in the block creation function.
- blockHasNoData: when data is not provided
- mempoolEntryNotFound: while validating the sent signature for a given wallet. If no entry was found related to such wallet, this error is raised.
- mempoolEntryWindowExpired: straight forward.
- signatureInvalid: when provided signature is not valid.
- addressNotConfirmed: when trying to add a block with given wallet. However, there is no confirmed requrests for it.
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.
- 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.