區(qū)塊:區(qū)塊頭+區(qū)塊體
區(qū)塊頭:每個區(qū)塊的前80個字節(jié)(640bits)乳蛾。其中有6部分信息
version? 版本號? 4個字節(jié)
前一個區(qū)塊的hash? ? 32個字節(jié)
本區(qū)塊所有交易產(chǎn)生的MerkleRoot? 32個字節(jié)
時間戳? 4個字節(jié)
新時間戳要大于11個區(qū)塊平均時間戳陷遮;不超過當(dāng)前網(wǎng)絡(luò)時間2個小時姑荷。
難度目標(biāo)bits? ? 4個字節(jié)
隨機(jī)數(shù) Nonce? 4個字節(jié)
區(qū)塊體記載了交易詳情萌焰、交易計(jì)數(shù)器颤霎、區(qū)塊大小猪钮。
代碼:
/**
* 〈UnixTimeStamp〉
* @author Michael
* @create 2017/7/25
* @since 1.0.0
*/
public class UnixTimeStamp {
? ? public static void main(String[] args) {
? ? ? ? long timestamp = System.currentTimeMillis();
? ? ? ? System.out.println(timestamp);
? ? }
}
//merkletrees
import java.util.List;
/**
* 〈merkleTrees〉
* @author Michael
* @create 2018/7/25
* @since 1.0.0
*/
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
public class MerkleTrees {?
? ? ?// transaction List List txList;
? ? // Merkle Root
? ? String root;
? ? //構(gòu)造函數(shù)
? ? public MerkleTrees(List txList) {
? ? ? ? ? ? this.txList = txList;
? ? ? ? ? ? root = "";
? ? ?}
? ? public void merkle_tree() {
? ? ? ? ? ? ListtempTxList = new ArrayList();
? ? ? ? ? ? for (int i = 0; i < this.txList.size(); i++) {
? ? ? ? ? ? ? ? tempTxList.add(this.txList.get(i));
? ? ? ? ? ? }
? ? ? ? ? ?List newTxList = getNewTxList(tempTxList);
? ? ? ? ? ? while (newTxList.size() != 1) {
? ? ? ? ? ? ? ? newTxList = getNewTxList(newTxList);
? ? ? ? ? ? }
? ? ? ? ? ?this.root = newTxList.get(0);
? ? }
? ? private ListgetNewTxList(ListtempTxList) {?
? ? ? ? ? ? ListnewTxList = new ArrayList();
? ? ? ? ? ? int index = 0;
? ? ? ? ? ? while (index < tempTxList.size()) {
? ? ? ? ? ? ? ? // left
? ? ? ? ? ? ? ? String left = tempTxList.get(index);
? ? ? ? ? ? ? ? index++;
? ? ? ? ? ? ? ? // right
? ? ? ? ? ? ? ? String right = "";
? ? ? ? ? ? ? ? if (index != tempTxList.size()) {
? ? ? ? ? ? ? ? ? ? right = tempTxList.get(index);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? // sha2 hex value
? ? ? ? ? ? ? ? String sha2HexValue = getSHA2HexValue(left + right);
? ? ? ? ? ? ? ? newTxList.add(sha2HexValue);
? ? ? ? ? ? ? ? index++;
? ? ? ? ? ? }
? ? ? ? ? ? return newTxList;
? ? }
? ? public String getSHA2HexValue(String str) {
? ? ? ? ? ? byte[] cipher_byte;
? ? ? ? ? ? try{
? ? ? ? ? ? ? ? MessageDigest md = MessageDigest.getInstance("SHA-256");
? ? ? ? ? ? ? ? md.update(str.getBytes());
? ? ? ? ? ? ? ? cipher_byte = md.digest();
? ? ? ? ? ? ? ? StringBuilder sb = new StringBuilder(2 * cipher_byte.length);
? ? ? ? ? ? ? ? for(byte b: cipher_byte) {
? ? ? ? ? ? ? ? ? ? sb.append(String.format("%02x", b&0xff) );
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return sb.toString();
? ? ? ? ? ? } catch (Exception e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? ? ? return "";
? ? }
? ? public String getRoot() {
? ? ? ? ? ? return this.root;
? ? ? ? }
}
import java.util.ArrayList;import java.util.List;
/**?
參考地址: https://blog.csdn.net/xiangzhihong8/article/details/53931213?
* *〈測試函數(shù)〉
?* * @author Michael?
* * @create 2018/7/25?
** @since 1.0.0?
*/
public class MerkleTreesTest {?
? ? ?public static void main(String [] args) {?
? ? ? ?ListtempTxList = new ArrayList();
? ? ? ? tempTxList.add("a");
? ? ? ? tempTxList.add("b");
? ? ? ? tempTxList.add("c");
? ? ? ? tempTxList.add("d");
? ? ? ? MerkleTrees merkleTrees = new MerkleTrees(tempTxList);
? ? ? ? merkleTrees.merkle_tree();
? ? ? ? System.out.println("root : " + merkleTrees.getRoot());
? ? }
}