小皮博客 | Xiaopi's Blog

33-区块链里面的HelloWorld

直接上代码了

python版

  • 源代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import hashlib as hasher
import datetime as date

class Block:
def __init__(self, index, timestamp, data, previous_hash):
self.index = index
self.timestamp = timestamp
self.data = data
self.previous_hash = previous_hash
self.hash = self.hash_block()

def hash_block(self):
sha = hasher.sha256()
sha.update(str(self.index) +
str(self.timestamp) +
str(self.data) +
str(self.previous_hash))
return sha.hexdigest()

def __str__(self):
output = str(self.index) + " / " + \
str(self.timestamp) + " / " + \
str(self.data) + " / " + \
str(self.previous_hash[:10])
return output

def create_genesis_block():
# Manually construct a block with
# index zero and arbitrary previous hash
return Block(0, date.datetime.now(), "Awesome Neo", "0")

def next_block(last_block):
this_index = last_block.index + 1
this_timestamp = date.datetime.now()
this_data = "Hello, I am the Neo Coin! Block " + str(this_index)
previous_hash = last_block.hash
return Block(this_index, this_timestamp, this_data, previous_hash)

# Create the blockchain and add the genesis block
blockchain = [create_genesis_block()]
previous_block = blockchain[0]

# How many blocks should we add to the chain
# after the genesis block
num_of_blocks_to_add = 10

# Add blocks to the chain
for i in range(0, num_of_blocks_to_add):
block_to_add = next_block(previous_block)
blockchain.append(block_to_add)
previous_block = block_to_add

# Tell everyone about it
print "Neo Coin! Block #{} has been added to the blockchain! ".format(block_to_add.index)
print block_to_add
print "Hash: {}\n".format(block_to_add.hash)
  • 执行结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Neo Coin! Block #1 has been added to the blockchain! 
1 / 2018-06-05 14:05:22.938457 / Hello, I am the Neo Coin! Block 1 / a23ae1da4a
Hash: 919166349a83da484959e63a481edbeb7bd9b7b729b095f5f34e2a66c0a300b4

Neo Coin! Block #2 has been added to the blockchain!
2 / 2018-06-05 14:05:22.938564 / Hello, I am the Neo Coin! Block 2 / 919166349a
Hash: ca8234e03378206ca75ecdeb9684565b1dbe8b1543cbab8769045e987033e06d

Neo Coin! Block #3 has been added to the blockchain!
3 / 2018-06-05 14:05:22.938602 / Hello, I am the Neo Coin! Block 3 / ca8234e033
Hash: 95d5b5232a9e62ad7586aac312c812ea50f2e5f93d2304ef5775ceeebfef40e6

Neo Coin! Block #4 has been added to the blockchain!
4 / 2018-06-05 14:05:22.938636 / Hello, I am the Neo Coin! Block 4 / 95d5b5232a
Hash: b49fb1ec2541f115911668484e390b542b38b1fa404bd1c7cc0acffb2206f204

Neo Coin! Block #5 has been added to the blockchain!
5 / 2018-06-05 14:05:22.938667 / Hello, I am the Neo Coin! Block 5 / b49fb1ec25
Hash: 403d1ff497d5f0a125a79a39027ccdf0a88059a9595fe577313f2014d635288a

Neo Coin! Block #6 has been added to the blockchain!
6 / 2018-06-05 14:05:22.938698 / Hello, I am the Neo Coin! Block 6 / 403d1ff497
Hash: 830ae497ab5333c9fe2b728a71d65e7080a622e4caac23b7ad5c26d22751eb7c

Neo Coin! Block #7 has been added to the blockchain!
7 / 2018-06-05 14:05:22.938731 / Hello, I am the Neo Coin! Block 7 / 830ae497ab
Hash: 438646e0460004080ee9e82ce0341d1cb83a32c5b39bc1818e43cbddf9d00e37

Neo Coin! Block #8 has been added to the blockchain!
8 / 2018-06-05 14:05:22.938765 / Hello, I am the Neo Coin! Block 8 / 438646e046
Hash: 44e4c9bad637a9d588b291a8d6b60c0d8cd32f682b14e8db6488b8c7ccbb5133

Neo Coin! Block #9 has been added to the blockchain!
9 / 2018-06-05 14:05:22.938798 / Hello, I am the Neo Coin! Block 9 / 44e4c9bad6
Hash: 8cc2ed10b36346218b3c52a4ed6feb0c292ef3b30f3c46ba58400c7916f03b87

Neo Coin! Block #10 has been added to the blockchain!
10 / 2018-06-05 14:05:22.938831 / Hello, I am the Neo Coin! Block 10 / 8cc2ed10b3
Hash: ab64e62c74822b41a2e975f5441787be0d67e309ccc6be44052053418ad08fc4

Java版

  • 源代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.sunrise22.scoin;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

public class ScoinApp {

private static final int NUM_OF_BLOCKS_TO_ADD = 10;

private static ScoinInnerBlock[] ScoinInnerBlockChain = new ScoinInnerBlock[NUM_OF_BLOCKS_TO_ADD];

public static void main(String[] args) {
ScoinInnerBlock genesisBlock = createGenesisBlock();
ScoinInnerBlockChain[0] = genesisBlock;
ScoinInnerBlock lastBlock = genesisBlock;

for (int i = 1; i < NUM_OF_BLOCKS_TO_ADD; i++) {
ScoinInnerBlock current = nextBlock(lastBlock);
ScoinInnerBlockChain[i] = current;

System.out.println("S Coin! Block #{" + current.getIndex() + "} has been added to the chain!");
System.out.println(current);
System.out.println("Hash: " + current.getHash());
System.out.println("========================================================================");
lastBlock = current;
}
}

public static ScoinInnerBlock createGenesisBlock() {
Date date = new Date();
return new ScoinInnerBlock(0, date.toString(), "Slience is gold!", ".notatall.");
}

public static ScoinInnerBlock nextBlock(ScoinInnerBlock lastBlock) {
int thisIndex = lastBlock.getIndex() + 1;
String thisData = "Hello, I am the S coin! Block " + thisIndex;
String prevHash = lastBlock.getHash();
Date date = new Date();
return new ScoinInnerBlock(thisIndex, date.toString(), thisData, prevHash);
}
}

class ScoinInnerBlock {

public int getIndex() {
return index;
}

public String getTimestamp() {
return timestamp;
}

public String getData() {
return data;
}

public String getPreviousHash() {
return previousHash;
}

public String getHash() {
return hash;
}

private int index;
private String timestamp;
private String data;
private String previousHash;
private String hash;

public ScoinInnerBlock(int index, String timestamp, String data, String previousHash) {
super();
this.index = index;
this.timestamp = timestamp;
this.data = data;
this.previousHash = previousHash;
this.hash = hashBlock();
}

public String hashBlock() {
String src = this.index + this.timestamp + this.data + this.previousHash;
String sha = getSHA256StrJava(src);
return sha;
}

@Override
public String toString() {
return "ScoinInnerBlock [index=" + index + ", timestamp=" + timestamp + ", data=" + data + ", previousHash="
+ previousHash + ", hash=" + hash + "]";
}

/**
* 利用java原生的摘要实现SHA256加密 <br >
*
* @param str
* 加密后的报文<br>
* @return
*/
public static String getSHA256StrJava(String str) {
MessageDigest messageDigest;
String encodeStr = "";
try {
messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.update(str.getBytes("UTF-8"));
encodeStr = byte2Hex(messageDigest.digest());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return encodeStr;
}

/**
* 将byte转为16进制 @param bytes @return
*/

private static String byte2Hex(byte[] bytes) {
StringBuffer stringBuffer = new StringBuffer();
String temp = null;
for (int i = 0; i < bytes.length; i++) {
temp = Integer.toHexString(bytes[i] & 0xFF);
if (temp.length() == 1) {
// 1得到一位的进行补0操作
stringBuffer.append("0");
}
stringBuffer.append(temp);
}
return stringBuffer.toString();
}
}
  • 执行结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
S Coin! Block #{1} has been added to the chain!
ScoinInnerBlock [index=1, timestamp=Tue Jun 05 14:15:37 CST 2018, data=Hello, I am the S coin! Block 1, previousHash=569f8523cafcaec1b1dbf7000f327dee5ecb7a73b0d2506c6aa229e5f0a07d29, hash=fc3670cead47ea0333f6d02ac499db3d1de19dbef8d0aef67e36efb29eccdbae]
Hash: fc3670cead47ea0333f6d02ac499db3d1de19dbef8d0aef67e36efb29eccdbae
========================================================================
S Coin! Block #{2} has been added to the chain!
ScoinInnerBlock [index=2, timestamp=Tue Jun 05 14:15:37 CST 2018, data=Hello, I am the S coin! Block 2, previousHash=fc3670cead47ea0333f6d02ac499db3d1de19dbef8d0aef67e36efb29eccdbae, hash=5965c78ba7bc96ca42904c5dfa4cc921dde723fd79d0ca38764bd16ec0cf66c0]
Hash: 5965c78ba7bc96ca42904c5dfa4cc921dde723fd79d0ca38764bd16ec0cf66c0
========================================================================
S Coin! Block #{3} has been added to the chain!
ScoinInnerBlock [index=3, timestamp=Tue Jun 05 14:15:37 CST 2018, data=Hello, I am the S coin! Block 3, previousHash=5965c78ba7bc96ca42904c5dfa4cc921dde723fd79d0ca38764bd16ec0cf66c0, hash=aff024d47d2206131eaf2cd5afe5cb5714e9c33f1cd8efc689cbe821d539c094]
Hash: aff024d47d2206131eaf2cd5afe5cb5714e9c33f1cd8efc689cbe821d539c094
========================================================================
S Coin! Block #{4} has been added to the chain!
ScoinInnerBlock [index=4, timestamp=Tue Jun 05 14:15:37 CST 2018, data=Hello, I am the S coin! Block 4, previousHash=aff024d47d2206131eaf2cd5afe5cb5714e9c33f1cd8efc689cbe821d539c094, hash=0315235bc031d4adc14ffab795d19f718a057975f82c2cf54d5c661fc8325307]
Hash: 0315235bc031d4adc14ffab795d19f718a057975f82c2cf54d5c661fc8325307
========================================================================
S Coin! Block #{5} has been added to the chain!
ScoinInnerBlock [index=5, timestamp=Tue Jun 05 14:15:37 CST 2018, data=Hello, I am the S coin! Block 5, previousHash=0315235bc031d4adc14ffab795d19f718a057975f82c2cf54d5c661fc8325307, hash=25e6a78026ac58bee367d416eee161928a92bc61fc82eba7ca31719070acc5b1]
Hash: 25e6a78026ac58bee367d416eee161928a92bc61fc82eba7ca31719070acc5b1
========================================================================
S Coin! Block #{6} has been added to the chain!
ScoinInnerBlock [index=6, timestamp=Tue Jun 05 14:15:37 CST 2018, data=Hello, I am the S coin! Block 6, previousHash=25e6a78026ac58bee367d416eee161928a92bc61fc82eba7ca31719070acc5b1, hash=ac47547ffe54e1dc046a291341a1f6ebeb3017cec22c7fbcfee2ef2babb14997]
Hash: ac47547ffe54e1dc046a291341a1f6ebeb3017cec22c7fbcfee2ef2babb14997
========================================================================
S Coin! Block #{7} has been added to the chain!
ScoinInnerBlock [index=7, timestamp=Tue Jun 05 14:15:37 CST 2018, data=Hello, I am the S coin! Block 7, previousHash=ac47547ffe54e1dc046a291341a1f6ebeb3017cec22c7fbcfee2ef2babb14997, hash=10de0891e7168454add7ea9920e06667de8c296b84716fcb884785a1ac600765]
Hash: 10de0891e7168454add7ea9920e06667de8c296b84716fcb884785a1ac600765
========================================================================
S Coin! Block #{8} has been added to the chain!
ScoinInnerBlock [index=8, timestamp=Tue Jun 05 14:15:37 CST 2018, data=Hello, I am the S coin! Block 8, previousHash=10de0891e7168454add7ea9920e06667de8c296b84716fcb884785a1ac600765, hash=37d046e4cc58590fe7ea5896b6fa9b14250f0e9a2c9ccfda067b6cfc91cbfc93]
Hash: 37d046e4cc58590fe7ea5896b6fa9b14250f0e9a2c9ccfda067b6cfc91cbfc93
========================================================================
S Coin! Block #{9} has been added to the chain!
ScoinInnerBlock [index=9, timestamp=Tue Jun 05 14:15:37 CST 2018, data=Hello, I am the S coin! Block 9, previousHash=37d046e4cc58590fe7ea5896b6fa9b14250f0e9a2c9ccfda067b6cfc91cbfc93, hash=192571b98481619a959488817554bc82abada702d2629c55fc8a734b2cbd7917]
Hash: 192571b98481619a959488817554bc82abada702d2629c55fc8a734b2cbd7917
========================================================================

版权声明

本文标题:33-区块链里面的HelloWorld

文章作者:盛领

发布时间:2017年12月23日 - 00:08:57

原始链接:http://blog.xiaoyuyu.net/post/8b97dde3.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

如您有任何商业合作或者授权方面的协商,请给我留言:sunsetxiao@126.com

盛领 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!