-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
107 lines (95 loc) · 2.63 KB
/
main.go
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
package main
import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"fmt"
"log"
"math/big"
"strconv"
"time"
)
type block struct {
// hash of the previous block
prehash string
//this block hash
hash string
//time stamp
timestamp string
// block content
data string
// block height
height int
//Dig out the address of this block
address string
}
// used to store the blockchain
var blockchain []block
//represent mining node
type node struct {
// number of tokens
tokens int
// pledge time
days int
//node address
address string
}
//mining node
var mineNodesPool []node
//probability node pool
var probabilityNodesPool []node
//initialization
func init() {
// Manually add two nodes
mineNodesPool = append(mineNodesPool, node{1000, 1, "AAAAAAAAAA"})
mineNodesPool = append(mineNodesPool, node{100, 3, "BBBBBBBBBB"})
//Initialize the random node pool (the mining probability is related to the number of tokens and the age of tokens)
for _, v := range mineNodesPool {
for i := 0; i <= v.tokens*v.days; i++ {
probabilityNodesPool = append(probabilityNodesPool, v)
}
}
}
//generate a new block
func generateNewBlock(oldBlock block, data string, address string) block {
newBlock := block{}
newBlock.prehash = oldBlock.hash
newBlock.data = data
newBlock.timestamp = time.Now().Format("2006-01-02 15:04:05")
newBlock.height = oldBlock.height + 1
newBlock.address = getMineNodeAddress()
newBlock.getHash()
return newBlock
}
// hash itself
func (b *block) getHash() {
sumString := b.prehash + b.timestamp + b.data + b.address + strconv.Itoa(b.height)
hash := sha256.Sum256([]byte(sumString))
b.hash = hex.EncodeToString(hash[:])
}
// Randomly get the mining address (the mining probability is related to the number of tokens and the age of the token)
func getMineNodeAddress() string {
bInt := big.NewInt(int64(len(probabilityNodesPool)))
// Get a random number, the maximum does not exceed the size of the random node pool
rInt, err := rand.Int(rand.Reader, bInt)
if err != nil {
log.Panic(err)
}
return probabilityNodesPool[int(rInt.Int64())].address
}
func main() {
//create genesis block
genesisBlock := block{"0000000000000000000000000000000000000000000000000000000000000000", "", time.Now().Format("2006-01-02 15:04:05"), "Created By Sandeep Kumar Sahoo", 1, "0000000000"}
genesisBlock.getHash()
//Add the genesis block to the blockchain
blockchain = append(blockchain, genesisBlock)
fmt.Println(blockchain[0])
i := 0
for {
time.Sleep(time.Second)
newBlock := generateNewBlock(blockchain[i], "Sandeep's POS Blockchain", "00000")
blockchain = append(blockchain, newBlock)
fmt.Println(blockchain[i+1])
i++
}
}