如何为Android开发实现区块链?

时间:2018-01-28 04:07:36

标签: android-studio blockchain

我已经看过许多关于区块链的教程,但它们都是一些假设学习者是网络开发者或相关领域的人。是否有一些东西供Android开发人员学习,以便我们可以使用android studio与区块链技术。?

1 个答案:

答案 0 :(得分:0)

基本上你需要了解什么是区块链以及它是如何工作的?区块链是分布式系统,其中没有中央节点来处理所有事务。所有交易均由矿工处理。区块链是区块的组合。每个块包含前一个块和当前块的哈希值。如果先前的块哈希码改变,则当前块哈希码也必须改变。请参阅下面的代码,以了解简单的块如何互连以及哈希码的重要性。

public class Block {

private int previoushash;
private String [] transactions;

private int blockhash;

public Block(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;
}

}

Main.java

public class Main {

ArrayList<Block> blockchain = new ArrayList<>();

public static void main(String [] args) {
    /*
     hash= digital signature

        Each block contains:

            -list of transactions
            -previous hash

            -hash
     * */

    System.out.println("Blochchain test initialized:\n");

    String [] genesisTransactions = {"a send 10 bitcoin to elnur", "b send 3 bitcoin to elnur" , "c send 5 bitcoin to elnur"};
    Block genesisBlock = new Block(0, genesisTransactions);
    System.out.println("Hash of genesis block:");
    System.out.println(genesisBlock.getBlockhash());


    String [] block2transactions = {"elnur send 2 bitcoin to ibm","elnur send 6 bitcoin to sap","elnur send 12 bitcoin to amazon"};
    Block block2 = new Block(genesisBlock.getBlockhash(),block2transactions);
    System.out.println("Hash of block 2:");
    System.out.println(block2.getBlockhash());


    String [] block3transactions = {"elnur send 2 bitcoin to satoshi","satoshi send 1 bitcoin to starbuck"};
    Block block3 = new Block(block2.getBlockhash(),block3transactions);
    System.out.println("Hash of block 3:");
    System.out.println(block3.getBlockhash());

}

}