public void setIndex(int index) {
this.index = index;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public List getTransactions() {
return transactions;
}
public void setTransactions(List transactions) {
this.transactions = transactions;
}
public int getNonce() {
return nonce;
}
public void setNonce(int nonce) {
this.nonce = nonce;
}
public String getPreviousHash() {
return previousHash;
}
public void setPreviousHash(String previousHash) {
this.previousHash = previousHash;
}
public String getHash() {
return hash;
}
public void setHash(String hash) {
this.hash = hash;
}
/**
-
挖矿
-
@param blockchain 整个区块链
-
@param txs 需记账交易记录
-
@param address 矿工钱包地址
-
@return
*/
public static void mineBlock(List blockchain, List txs, String address) {
//加入系统奖励的交易
Transaction sysTx = new Transaction(CryptoUtil.UUID(), “”, address, 10);
txs.add(sysTx);
//获取当前区块链里的最后一个区块
Block latestBlock = blockchain.get(blockchain.size() - 1);
//随机数
int nonce = 1;
String hash = “”;
while(true){
hash = CryptoUtil.SHA256(latestBlock.getHash() + JSON.toJSONString(txs) + nonce);
if (hash.startsWith(“0000”)) {
System.out.println(“=====计算结果正确,计算次数为:” +nonce+ “,hash:” + hash);
break;
}
nonce++;
System.out.println(“计算错误,hash:” + hash);
}
//解出难题,可以构造新区块并加入进区块链里
Block newBlock = new Block(latestBlock.getIndex() + 1, System.currentTimeMillis(), txs, nonce, latestBlock.getHash(), hash);
blockchain.add(newBlock);
System.out.println(“挖矿后的区块链:” + JSON.toJSONString(blockchain));
}
}
Transaction
package com.blockchain.model;
/**
-
交易
-
@author zhiwei liao
*/
public class Transaction {
/**
- 交易唯一标识
*/
private String id;
/**
- 交易发送方
*/
private String sender;
/**
- 交易接收方
*/
private String recipient;
/**
- 交易金额
*/
private int amount;
public Transaction() {
super();
}
public Transaction(String id, String sender, String recipient, int amount) {
super();
this.id = id;
this.sender = sender;
this.recipient = recipient;
this.amount = amount;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public String getRecipient() {
return recipient;
}
public void setRecipient(String recipient) {
this.recipient = recipient;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Transaction other = (Transaction) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
加密工具类
package com.blockchain.security;
import java.security.MessageDigest;
import java.util.UUID;
/**
-
加密工具类
-
@author zhiwei liao
*/
public class CryptoUtil {
private CryptoUtil() {
}
public static String SHA256(String str) {
MessageDigest messageDigest;
String encodeStr = “”;
try {
messageDigest = MessageDigest.getInstance(“SHA-256”);
messageDigest.update(str.getBytes(“UTF-8”));
encodeStr = byte2Hex(messageDigest.digest());
} catch (Exception e) {
System.out.println(“getSHA256 is error” + e.getMessage());
}
return encodeStr;
}
public static String UUID() {
return UUID.randomUUID().toString().replaceAll(“\-”, “”);
}
private static String byte2Hex(byte[] bytes) {
StringBuilder builder = new StringBuilder();
String temp;
for (int i = 0; i < bytes.length; i++) {
temp = Integer.toHexString(bytes[i] & 0xFF);
if (temp.length() == 1) {
builder.append(“0”);
}
builder.append(temp);
}
return builder.toString();
}
}
Main方法执行:
package com.blockchain;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.blockchain.model.Block;
import com.blockchain.model.Transaction;
import com.blockchain.security.CryptoUtil;
/**
-
交易
-
@author zhiwei liao
*/
public class Main {
public static void main(String[] args) {
//创建一个空的区块链
List blockchain = new ArrayList<>();
//生成创世区块
Block block = new Block(1, System.currentTimeMillis(), new ArrayList(), 1, “1”, “1”);
//加入创世区块到区块链里
blockchain.add(block);
System.out.println(JSON.toJSONString(blockchain));
// 发送方钱包地址
String sender = “sender_wallet”;
//接收方钱包地址
String recipient = “recipient_wallet”;
//创建一个空的交易集合
List txs = new ArrayList<>();
//挖矿
Block.mineBlock(blockchain, txs, sender);
System.out.println(sender + “钱包的余额为:” + getWalletBalance(blockchain, sender));
//创建一个空的交易集合
List txs1 = new ArrayList<>();
//已发生但未记账的交易记录,发送者给接收者转账3个比特币
Transaction tx1 = new Transaction(CryptoUtil.UUID(), sender, recipient, 3);
//已发生但未记账的交易记录,发送者给接收者转账1个比特币
Transaction tx2 = new Transaction(CryptoUtil.UUID(), sender, recipient, 1);
txs1.add(tx1);
txs1.add(tx2);
//挖矿
Block.mineBlock(blockchain, txs1, sender);
System.out.println(sender + “钱包的余额为:” + getWalletBalance(blockchain, sender));
System.out.println(recipient + “钱包的余额为:” + getWalletBalance(blockchain, recipient));
}
/**
-
查询余额
-
@param blockchain
-
@param address
-
@return
*/
public static int getWalletBalance(List blockchain, String address) {
int balance = 0;
for (Block block : blockchain) {
List transactions = block.getTransactions();
for (Transaction transaction : transactions) {
if (address.equals(transaction.getRecipient())) {
balance += transaction.getAmount();
}
if (address.equals(transaction.getSender())) {
balance -= transaction.getAmount();
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Java)
最后
一次偶然,从朋友那里得到一份“java高分面试指南”,里面涵盖了25个分类的面试题以及详细的解析:JavaOOP、Java集合/泛型、Java中的IO与NIO、Java反射、Java序列化、Java注解、多线程&并发、JVM、Mysql、Redis、Memcached、MongoDB、Spring、Spring Boot、Spring Cloud、RabbitMQ、Dubbo 、MyBatis 、ZooKeeper 、数据结构、算法、Elasticsearch 、Kafka 、微服务、Linux。
这不,马上就要到招聘季了,很多朋友又开始准备“金三银四”的春招啦,那我想这份“java高分面试指南”应该起到不小的作用,所以今天想给大家分享一下。
请注意:关于这份“java高分面试指南”,每一个方向专题(25个)的题目这里几乎都会列举,在不看答案的情况下,大家可以自行测试一下水平 且由于篇幅原因,这边无法展示所有完整的答案解析
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
…(img-mkDa8TTn-1713822427245)]
最后
一次偶然,从朋友那里得到一份“java高分面试指南”,里面涵盖了25个分类的面试题以及详细的解析:JavaOOP、Java集合/泛型、Java中的IO与NIO、Java反射、Java序列化、Java注解、多线程&并发、JVM、Mysql、Redis、Memcached、MongoDB、Spring、Spring Boot、Spring Cloud、RabbitMQ、Dubbo 、MyBatis 、ZooKeeper 、数据结构、算法、Elasticsearch 、Kafka 、微服务、Linux。
这不,马上就要到招聘季了,很多朋友又开始准备“金三银四”的春招啦,那我想这份“java高分面试指南”应该起到不小的作用,所以今天想给大家分享一下。
[外链图片转存中…(img-iW5yLfaj-1713822427245)]
请注意:关于这份“java高分面试指南”,每一个方向专题(25个)的题目这里几乎都会列举,在不看答案的情况下,大家可以自行测试一下水平 且由于篇幅原因,这边无法展示所有完整的答案解析
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!