NodeJs blockchain on private ethereum

0 votes

I have created simple blockchain application using NodeJS. The blockchain data file is getting stored on local File System. There is no mining blocks, no difficulty level involved in this blockchain.

Please suggest, if I can host this application on private ethereum / hyperledge, and what all changes I would need to do for this? Below code I'm using for creating blocks.

Sample Genesis Block

[{"index":0,"previousHash":"0","timestamp":1465154705,"transaction":{"id":"0","transactionHash":"0","type":"","data":{"StudInfo":[{"id":"","studentId":"","parenterId":"","schemeId":"","batchId":"","instructorId":"","trainingId":"","skillId":""}]},"fromAddress":""},"hash":"816534932c2b7154836da6afc367695e6337db8a921823784c14378abed4f7d7"}]

Sample Code(NodeJS)

var generateNextBlock = (blockData) => { var previousBlock = getLatestBlock(); var nextIndex = previousBlock.index + 1; var nextTimestamp = new Date().getTime() / 1000; var nextHash = calculateHash(nextIndex, previousBlock.hash, nextTimestamp, blockData); return new Block(nextIndex, previousBlock.hash, nextTimestamp, blockData, nextHash); }; var calculateHashForBlock = (block) => { return calculateHash(block.index, block.previousHash, block.timestamp, block.transaction); }; var calculateHash = (index, previousHash, timestamp, transaction) => { return CryptoJS.SHA256(index + previousHash + timestamp + transaction).toString(); }; var addBlock = (newBlock) => { if (isValidNewBlock(newBlock, getLatestBlock())) { blockchain.push(newBlock); blocksDb.write(blockchain); } }; var isValidNewBlock = (newBlock, previousBlock) => { if (previousBlock.index + 1 !== newBlock.index) { console.log('invalid index'); return false; } else if (previousBlock.hash !== newBlock.previousHash) { console.log('invalid previoushash'); return false; } else if (calculateHashForBlock(newBlock) !== newBlock.hash) { console.log(typeof (newBlock.hash) + ' ' + typeof calculateHashForBlock(newBlock)); console.log('invalid hash: ' + calculateHashForBlock(newBlock) + ' ' + newBlock.hash); return false; } return true; };

Apr 4, 2022 in Blockchain by Soham
• 9,700 points
117 views

1 answer to this question.

0 votes

From what I understand, your JS code does some sort of low-level block generation. I'm not sure of the purpose of that code, but it's not an app that you could "port to" Ethereum or Hyperledger [Fabric], since the role of those blockchain engines is precise to implement the block generation logic for you.

A JS app designed to work with Ethereum wouldn't do any of the block management. On the contrary, it would perform high-level and client-level interaction with a smart contract, which is basically a class (similar to Java classes) available on the whole network and whose methods are guaranteed by the network to do what they're meant to. Your JS app would essentially call the smart contract's methods as a client without caring about any block production issues.

In other, more vague but maybe more familiar terms:

  • Client: the JS app

  • Server/backend: the smart contract

  • Infrastructure/engine: Ethereum

On Ethereum, the way you get to the smart contract as a client is by sending RPC calls in JSON (hence the name JSON-RPC) to the contract's methods. The communication is done by embedding that JSON over HTTP to an Ethereum node, preferably your own. In Javascript, a few libraries such as web3 give you a higher-level abstraction view so that you don't need to care about JSON-RPC and you can think of your contract's methods as normal Javascript functions.

Also, since you're asking about private Ethereum in another consequence of that distribution of layers is that client code and smart contracts don't need to care about whether the Ethereum network is a public or a private one, i.e. what consensus protocol is in place. To make a bold analogy, it's similar to how SQL queries or schemas stay the same no matter how the database is persisted on disk.

Interaction with Hyperledger Fabric is similar in concept, except that you do plain REST calls to an HTTP endpoint exposed by the network. I'm not sure what client-level abstraction libraries are available.

answered Apr 6, 2022 by Aditya
• 7,660 points

Related Questions In Blockchain

0 votes
1 answer

Mining on private Ethereum blockchain

You control how miners work on your ...READ MORE

answered Jul 25, 2018 in Blockchain by Johnathon
• 9,090 points
713 views
0 votes
0 answers

Attach two nodes on different machine via private blockchain (Ethereum)

I have seen many blogs regarding ways ...READ MORE

Jun 9, 2020 in Blockchain by Ritvik
• 120 points
915 views
0 votes
0 answers

Private ethereum blockchain mining on a Raspberry Pi - is it possible?

I am trying to write a private ...READ MORE

Mar 4, 2022 in Blockchain by Soham
• 9,700 points
434 views
0 votes
1 answer

Private ethereum blockchain mining on a Raspberry Pi - is it possible?

There are some pretty good guides out ...READ MORE

answered Apr 12, 2022 in Blockchain by Soham
• 9,700 points
214 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,202 views
0 votes
1 answer
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured by TLS encryption in hashgraph architecture-hashgraph, ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,450 points
815 views
0 votes
1 answer

On-Chain NFT: How to store a pixel image on the ethereum blockchain?

The images are usually not stored in ...READ MORE

answered Feb 24, 2022 in Blockchain by Aditya
• 7,660 points
525 views
+1 vote
1 answer

Is it possible to store images on the Ethereum blockchain?

This is possible for your information but, ...READ MORE

answered Feb 24, 2022 in Blockchain by Aditya
• 7,660 points
942 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP