for beginner understanding, explained with code how block chain can be implemented in java using arrayListpull/885/head
parent
120204995f
commit
3bbabbcc65
Binary file not shown.
Binary file not shown.
@ -0,0 +1,28 @@
|
|||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class ImplimentChain {
|
||||||
|
/**
|
||||||
|
Hash = digital signature
|
||||||
|
Each Block will have :-
|
||||||
|
List of transactions
|
||||||
|
Previous Hash
|
||||||
|
Hash
|
||||||
|
*/
|
||||||
|
|
||||||
|
ArrayList<MyInfo> blockchain = new ArrayList<>();
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String[] genesisTransactions = {"Ishita sent nini 20 bitcoin","Rajat sent fin 50 bitcoin"};
|
||||||
|
MyInfo genesisInfo = new MyInfo(0, genesisTransactions);
|
||||||
|
|
||||||
|
String[] block2Transaction = {"A sent 10 bitcoin to B", "B sent 5 bitcoin to C"};
|
||||||
|
MyInfo block2 = new MyInfo(genesisInfo.getBlockHash(), block2Transaction);
|
||||||
|
|
||||||
|
System.out.println("Hash of genesis Block :");
|
||||||
|
System.out.println(genesisInfo.getBlockHash());
|
||||||
|
System.out.println("Hash of Block 2:");
|
||||||
|
System.out.print(block2.getBlockHash());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,30 @@
|
|||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class MyInfo {
|
||||||
|
|
||||||
|
private int previousHash;
|
||||||
|
private String[] transactions;
|
||||||
|
|
||||||
|
private int BlockHash;
|
||||||
|
|
||||||
|
public MyInfo(int previousHash, String[] transactions) {
|
||||||
|
this.previousHash = previousHash;
|
||||||
|
this.transactions = transactions;
|
||||||
|
|
||||||
|
Object[] contents = {Arrays.hashCode(transactions), previousHash };
|
||||||
|
this.BlockHash = Arrays.hashCode(contents);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPreviousHash() {
|
||||||
|
return previousHash;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] getTransactions() {
|
||||||
|
return transactions;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getBlockHash() {
|
||||||
|
return BlockHash;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in new issue