In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to realize digital currency in Java language". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Introduction
Blockchain technology is a more revolutionary technology than artificial intelligence, which only improves human productivity, while blockchain will change the production relations of human society, and it will subvert the existing way of cooperation in our human society. Understanding and mastering the knowledge and technology related to blockchain is something that every developer must do, so that we can grasp the dividend of this trend of the times.
This paper will build a simplified version of blockchain based on Java language to realize digital currency.
Create a block
A blockchain is a data structure that is sequentially linked from back to front by blocks containing transaction information. Blocks are linked sequentially from back to front in this chain, with each block pointing to the previous block. Take Bitcoin as an example, each block mainly contains the following information fields:
Chunk size: the size of chunk data in bytes
Block head: the fields that make up the block head
Block hash value
Parent block hash value
Timestamp: approximate time of block generation
Merkle root: the hash value of the merkle tree root traded in this block
Difficulty goal: the difficulty goal of the block workload proof algorithm
Nonce: counter for workload proof algorithm
Transaction counter: number of transactions
Transaction: transaction information recorded in a block
For details, see Chapter 9-Block chain of "mastering Bitcoin" (second Edition).
Block data structure
Here, we are mainly to implement the simplest blockchain structure, which contains only the following information fields:
/ * * Block * * @ author wangwei * @ date 2018-02-02 * / @ Datapublic class Block {/ * * Block hash value * / private String hash; / * the hash value of the previous block * / private String previousHash; / * Block data * / private String data / * Block creation time (in seconds) * / private long timeSt public Block () {} public Block (String hash, String previousHash, String data, long timeStamp) {this (); this.hash = hash; this.previousHash = previousHash; this.data = data; this.timeStamp = timeSt}} Block Hash value calculation
Encrypted hash value, a digital fingerprint obtained by secondary hashing of the block head by the SHA256 algorithm. The Hash value is used to ensure the security of blockchain. Hash computing is computationally sensitive and takes a while to complete even on high-performance computers (which is why people buy high-performance GPU for bitcoin mining). The blockchain architecture is designed to make Hash computing difficult, in order to make it more difficult to add a new block, thereby preventing the block from being modified at will.
/ *
Create a new block
* * @ param previousHash * @ param data * @ return * / public static Block newBlock (String previousHash, String data) {Block block = newBlock ("", previousHash, data.getBytes (), Instant.now (). GetEpochSecond ()); block.setHash (); return block;} / * * calculate block Hash *
* Note: when preparing chunk data, be sure to convert from the original data type to byte [], not directly from the string * * @ return * / private void setHash () {byte [] prevBlockHashBytes = {}; if (StringUtils.isNoneBlank (this.getPrevBlockHash () {prevBlockHashBytes = new BigInteger (this.getPrevBlockHash (), 16) .toByteArray () } byte [] headers = ByteUtils.merge (prevBlockHashBytes, this.getData (). GetBytes (), ByteUtils.toBytes (this.getTimeStamp (); this.setHash (DigestUtils.sha256Hex (headers));} create block chain
Block chain is essentially a data structure of ordered and reverse linked lists. This means that the block is stored in the order in which it is inserted, while each block holds a link to the previous block. This structure ensures that the newly inserted block can be quickly obtained as well as its hash value. This structure ensures that you can quickly get the newly inserted block and (efficiently) get its hash value.
Block chain data structure / *
Block chain
* * @ author wangwei * @ date 2018-02-02 * / public class Blockchain {@ Getter private List blockList; public Blockchain (List blockList) {this.blockList = blockList;}} add blocks
Add a method to add a block chain
/ *
Add Block
* * @ param data data * / public void addBlock (String data) {Block previousBlock = blockList.get (blockList.size ()-1); this.addBlock (Block.newBlock (previousBlock.getHash (), data));} / * *
Add Block
* * @ param block Block * / public void addBlock (Block block) {this.blockList.add (block);} Genesis Block
Before adding a block, there must be a Genesis Block in the blockchain. Add the Genesis Block method in Block:
/ *
Create the Genesis Block
* * @ return * / public static Block newGenesisBlock () {return Block.newBlock ("", "GenesisBlock");} create a block chain
Then add a method to create a blockchain in Blockchain:
/ *
Create a blockchain
* * @ return * / public static Blockchain newBlockchain () {List blocks = new LinkedList (); blocks.add (Block.newGenesisBlock ()); return newBlockchain (blocks);} Test run / * * author wangwei * @ date 2018-02-05 * / public class BlockchainTest {public static void main (String [] args) {Blockchain blockchain = Blockchain.newBlockchain (); blockchain.addBlock ("Send 1 BTC to Ivan") Blockchain.addBlock ("Send 2 more BTC to Ivan"); for (Block block: blockchain.getBlockList ()) {System.out.println ("Prev. Hash: "+ block.getPreviousHash (); System.out.println (" Data: "+ block.getData ()); System.out.println (" Hash: "+ block.getHash ()); System.out.println ();} / * output the following information: * / Prev. Hash: Data: Genesis BlockHash: 4492cb9d396a9a52e7ff17ef3782f022ddcdc7b2c276bc6dd3d448b0655eb3d4Prev. Hash: 4492cb9d396a9a52e7ff17ef3782f022ddcdc7b2c276bc6dd3d448b0655eb3d4Data: Send 1 BTC to IvanHash: cd716d59d98ad673035ab7035ece751718ea9842944a4743c298bebc0fe24c04Prev. Hash: cd716d59d98ad673035ab7035ece751718ea9842944a4743c298bebc0fe24c04Data: Send 2 more BTC to IvanHash: 42f78d6a86f88aa9b5b10e468494dfd1b3f558a9fb74a01eb348c2cbfc5d000a, "how to realize digital currency in Java language" is introduced here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.