0 votes
1 view
in Blockchain by (7k points)

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;
};

1 Answer

0 votes
by (14.5k points)

Going through your code, I can deduce that you have successfully installed Geth on AWS. The main thing we need to do now is to configure an Ethereum Node. 

Make sure that you are in the home directory of your cloud server. Using pwd command, create a folder that will serve as the genesis block of your Ethereum Blockchain. Now change the directory to the newly created folder and then create a file called genesis.json

Command-line: 

mkdir mlg-ethchain cd mlg-ethchain nano genesis.json

You cannot create a private blockchain without a genesis block. These blocks are mostly embedded within the client but in the case of Ethereum users are allowed to configure a genesis block through a JSON object. 

The JSON object looks like: 

{

"nonce": "0xdeadbeefdeadbeef",

"timestamp": "0x0",

"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",

"extraData": "0x0",

"gasLimit": "0x8000000",

"difficulty": "0x400",

"Mixhash": 

"0x0000000000000000000000000000000000000000000000000000000000000000", "coinbase": "0x3333333333333333333333333333333333333333",

"alloc": {

}

}

You should also note that the coinbase is the default address for mining. And as wallet has not been created yet, the default address can be set to any valid Ethereum address. For testing and development, it is recommended that mining should be started from a lower difficulty level and increased gradually. parentHash is zero and gasLimit is set to the maximum amount os gas that is required to execute any type of transactions. Nonce is set as a random number. In the alloc section, users can allocate a number of pre-mined tokens or ether to certain addresses at the beginning of the blockchain.

For confirming if the configuration is correct or not, use the command:

cat genesis.json

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...