-1

I wanted to develop a web DApplication using Ethereum, Infura, web3J.. can you share your inputs to quickStart, I see more theoretical stuff & don't found any practical examples to step forward using web3j.. I want to store a user earned points on solving some puzzle with some metadata like points creation date, expiry date, puzzle ID & etc in blockchain.

How to model & store above information..

TylerH
  • 20,799
  • 66
  • 75
  • 101

2 Answers2

0

You could be a little more specific in your question. Are you sure you need an interaction with a Java project, or maybe a pure Ethereum implementations would suffice?

For example: If the user interacts with this puzzle in a Java application but you want to store the user's data in the blockchain, then you need Web3j to interact with Ethereum. But maybe the puzzle can be implemented directly into the blockchain. Then you don't need Web3j at all.

FPapi
  • 1
  • Yes I want to interact with BlockChain from Java. Yes in that case I have to use Web3J as I stated in my question. Infura is used to avoid the end user need not to run Geth or Metamask plugin in their local to perform this operation. – Rohith Varala Dec 06 '17 at 22:20
  • 1
    Moreover I want to understand how & where the data gets store, I mean in traditional databases like SQL & NoSQL, data gets stored in tables & collections respectively. How about in BlockChain ( Ethereum). – Rohith Varala Dec 06 '17 at 22:23
  • 1
    Ok, in that case, your data will be stored as you design it. Think of it as if you have a program that runs forever, and you can access any data you store in it at anytime. For example, you can create a struct to store all of your user data: struct User { address adr; string name; uint age; uint score; } and then use this struct to create the data structure you need, for example, a linked list. You can always check this structure to retrieve the user data. check [cryptozombies](https://cryptozombies.io/) . It's very good for learning the basics of solidity. – FPapi Dec 18 '17 at 13:44
  • a basic question.. if we are developing a multi user game, should the data (basic user info, game stats etc) of all users be stored in all users machines? – Venkata Dorisala Dec 19 '17 at 22:43
  • I think that would be a poor design choice as it would require them to have a copy of ethereum's blockchain locally. The whole idea is that end users do not need to hold the data, the blockchain network will do it for them. So the game user just needs an implementation of a client that accesses the blockchain, for example, using web3j – FPapi Dec 20 '17 at 12:52
  • Its not a game, i just mentioned its has Game. i cant reveal the concept.. I just want to store Points and user details.. let me know how to proceed further with above mentioned technologies.. – Rohith Varala Dec 30 '17 at 15:43
0

block-chain

Bitcoin = = Blockchain ??

First of all, let’s clear one thing Blockchain is not a Bitcoin and Bitcoin is not a Blockchain. Blockchain is a concept which has been used in cryptocurrency or digital currency.

The first work on a cryptographically secured chain of blocks was described in 1991 by Stuart Haber and W. Scott Stornetta and became famous in 2008 when an unknown person Satoshi Nakamoto used it in one of most famous cryptocurrency today ie Bitcoin.

Satoshi used Blochchain as the public ledger for all transactions of Bitcoin on the network. The invention of the Blockchain for Bitcoin made it the first digital currency to solve the double-spending problem without the need of a trusted authority or central server. The Bitcoin design has been the inspiration for other applications

each of the Big Four accounting firms is testing Blockchain technologies in various formats. Ernst & Young has provided cryptocurrency wallets to all (Swiss) employees, has installed a Bitcoin ATM in their office in Switzerland, and accepts Bitcoin as payment for all its consulting services.

A Blockchain, originally block chain,is a continuously growing list of records, called blocks, which are linked and secured using cryptography. The words block and chain were used separately in Satoshi Nakamoto's original paper, but were eventually popularized as a single word, Blockchain, by 2016.

Blockchain is a Linked List

If we compare blockchain with Linkedlist, yes it’s a linked list but with the assurance of secured data.it acts like a Linkedlist with where each node has three major component as:

  1. Current Hash is a String element which is calculated (Hashed) from Data and Previous Hash.
  2. Previous Hash holds the Current Hash of the previous block (node)
  3. Data, can be any object, which represents the node

enter image description here enter image description here

As we can see in above diagram its very much similar with Linkedlist but there is no linking between nodes using node address instead it has previous hash which is the current hash of previous node and current hash which is the combination of previous hash and data.

Unless linked list it has fully secured data because if we try to alter the data the current hash would also get changed because onece we create the hash of certain data we get the same hash again and again if our data is same. Hence no one can cheat by changing the data in any of the node because changing data will cause different hash and different hash will cause invailid block hence it ensured the integrity of the BlockChain and reduces chances of tampering with old blocks.

Blockchain provides four important features:

  1. Decentralization (No individual administrator)
  2. Integrity of data (No tampering)
  3. Smart Contracts

Blockchain can be used in a public peer to peer network where participants are not aware about each other or in a private business where participants know each other and trust each other.

Creating Blockchain

Class Block

java public class Block { public int index; public String timestamp; public Data data; public String hash; public String previousHash; }

Block class is a class which contains index,timestamp,data,hash and previousHash.

Index: is the index of a block in a linkedlist
Timestamp: timestamp is used to keep the track of block was created.
Data: data which has to be stored in block.
Hash: unique hash code generated by data + previoushash .
Previoushash: hash of previous block.

Constructor of Block

java public Block(Data data) { this.timeStamp = ""+new Date().getTime(); this.data = data; }

Class Data

``` java package com.piyush.app.blockchain; public class Data {

String name;
int balance;

public Data(String name,int balance){
    this.name=name;
    this.balance=balance;
}
@Override
public
String toString() {
    return this.name+" "+this.balance;
}

} ```

Data class is the class holds our data like name and the balance of a customer. This is the data which should be secured from tempering and blockchain gives us security that there would be no data tempering.

Class BlockChain

java public class BlockChain { public Block generateBlock(Block block,List list) throws Exception{ try { block.previousHash=Utils.getPreviousHash(list); } catch (Exception e) { throw new Exception("previous hash null or empty"); } String hashCode=Utils.generateHash(block); block.setHash(hashCode); return block; } }

Class Utils

``` java public class Utils {

public static String generateHash(Block block) {

    String sha256hex = org.apache.commons.codec.digest.DigestUtils
            .sha256Hex(block.previousHash + "" + block.index + "" + block.timeStamp + "" + block.data);

    return sha256hex;
}

public static <E> String getPreviousHash(List<E> list) throws Exception {

    if (list.size() != 0) {
        Block block = (Block) list.get(list.size() - 1);
        String previousHash = block.getHash();
        if (previousHash != null && !previousHash.equals("")) {
            return previousHash;
        }
        else {
            throw new Exception("previous hash null or empty");
        }
    }
    else {
        return "firsthash";
    }
}

public static Map isBlockChainValid(List<Block> blockChainList) throws Exception {
    if (!blockChainList.isEmpty()) {
        if (blockChainList.size() > 1) {
            return validate(blockChainList);
        }
        else {
            throw new Exception("block chain is empty");
        }
    }
    else {
        throw new Exception("block chain is empty");
    }

}

private static Map validate(List<Block> blockChainList) {
    Block current;
    Block previous;
    Map<String, Object> result = new HashMap();
    for (int i = 1; i < blockChainList.size(); i++) {
        current = blockChainList.get(i);
        previous = blockChainList.get(i - 1);

        if (!previous.getHash().equals(current.getPreviousHash())) {
            result.put("block", blockChainList.get(i));
            result.put("index", i);
        }
    }

    return result;
}

}

```

generateHash: this is the main and most import part of the blockchain where we generate the hash. We have lots of implementation available to generate hash but I have used sha256Hex.

Hash is the combination of data+previoushash.

We have to add one below dependency to get this implementation though there are lots of api and other methods available on internet we can use any one of them.

Gradle: json compile group: 'commons-codec', name: 'commons-codec', version: '1.11'

Maven: xml <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.11</version> </dependency>

getPreviousHash: previousHash also plays very important role in Blockchain because without previous hash we can not create current hash and we can not link our blocks together.

isBlockChainValid: whenever we add any block in our blockchain here we check if the Blochchain is valid or not by checking the previous and current hash of each block.

validate: we validate the each block by generating currenthash of block by previousblock and data and match it with already present currenthash if both are same then data id not altered else data hash been altered and block chain is invalid.

When Data is altered

``` Block Chain is altered at index2

hash 3cd627b352c68a2d4a3664806355a6f3fcf3d378b336380b907fcb71e41edf5f previous hash firsthash data piyush1 28

hash 1dd182aa0d8466541a97bd780571728b3169ec7a8df97c9bb526a35d2fffa8a0 previous hash 80430d2efe27badd4e73dd52e65893f5d8aaba87fb1f186643c12c5d7f830074 data piyush2 29

hash 043dfeb5ea9cd0337a1d386b5b58bcf850248f84644a0b73bbfc1f762e5fe1ef previous hash 3cd627b352c68a2d4a3664806355a6f3fcf3d378b336380b907fcb71e41edf5f data piyush2 28

hash 80430d2efe27badd4e73dd52e65893f5d8aaba87fb1f186643c12c5d7f830074 previous hash 043dfeb5ea9cd0337a1d386b5b58bcf850248f84644a0b73bbfc1f762e5fe1ef data piyush3 28

```

No data altered

```` hash 33bd892bad70ff4fa7ab0f3ec648df8a40608f872337e8a8cfc8b5c8e87e1c49 previous hash firsthash data piyush1 28

hash 41169e01eb6160f0947640340540c246e5654c2db02223a6a44d24c016337d3a previous hash 33bd892bad70ff4fa7ab0f3ec648df8a40608f872337e8a8cfc8b5c8e87e1c49 data piyush2 28

hash 48441e40a3cd6384a2ce09edf3f3acefa4f18d852608fd65bc51ead861411c96 previous hash 41169e01eb6160f0947640340540c246e5654c2db02223a6a44d24c016337d3a data piyush3 28 ````

Please go through DataAuthorisation blog to know more about how to prevent your data to get tempered.

find sample project at blockchain

Piyush Mittal
  • 1,860
  • 1
  • 21
  • 39