/
block_service_impl.go
163 lines (120 loc) · 3.14 KB
/
block_service_impl.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package service
import (
"it-chain/db/blockchaindb"
"it-chain/domain"
"errors"
)
type Ledger struct {
DB blockchaindb.BlockChainDB
}
func NewLedger(path string) BlockService{
return &Ledger{DB:blockchaindb.CreateNewBlockchainDB(path)}
}
func (l *Ledger) Close() {
l.DB.Close()
}
func (l *Ledger) CreateBlock(txList []*domain.Transaction, createPeerId string) (*domain.Block, error) {
lastBlock, err := l.GetLastBlock()
if err != nil {
return nil, err
}
if lastBlock == nil {
lastBlock, err = l.CreateGenesisBlock()
}
blk := domain.CreateNewBlock(lastBlock, createPeerId)
for _, tx := range txList {
err = blk.PutTranscation(tx)
if err != nil{
return nil, err
}
}
blk.MakeMerkleTree()
err = blk.GenerateBlockHash()
if err != nil{
return nil, err
}
return blk, nil
}
func (l *Ledger) VerifyBlock(blk *domain.Block) (bool, error){
lastBlock, err := l.DB.GetLastBlock()
if lastBlock == nil {
_, err = blk.VerifyBlock()
if err != nil {
return false, err
}
blk.Header.BlockStatus = domain.Status_BLOCK_CONFIRMED
return true, nil
}
if err != nil {
return false, err
}
if lastBlock.Header.BlockHeight + 1 != blk.Header.BlockHeight{
return false, errors.New("Block height misMatched")
}
if lastBlock.Header.BlockHash != blk.Header.PreviousHash{
return false, errors.New("Block hash is different")
}
_, err = blk.VerifyBlock()
if err != nil {
return false, err
}
blk.Header.BlockStatus = domain.Status_BLOCK_CONFIRMED
return true, nil
}
func (l *Ledger) GetLastBlock() (*domain.Block, error) {
blk, err := l.DB.GetLastBlock()
if err != nil{
return nil, err
}
if blk == nil{
return l.CreateGenesisBlock()
}
return blk, err
}
func (l *Ledger) LookUpBlock(arg interface{}) (*domain.Block, error) {
_, err := l.GetLastBlock()
if err != nil{
return nil, errors.New("no block exist")
}
switch arg.(type) {
case int:
arg = uint64(arg.(int))
return l.DB.GetBlockByNumber(arg.(uint64))
case string:
return l.DB.GetBlockByHash(arg.(string))
default:
return nil, errors.New("no matched block")
}
}
func (l *Ledger) AddBlock(blk *domain.Block) (bool, error) {
_, err := l.LookUpBlock(blk.Header.BlockHash)
if err == nil{ return false, errors.New("already this blk exist") }
lastBlock, err := l.GetLastBlock()
if err != nil {
return false, err
}
if blk.Header.BlockStatus != domain.Status_BLOCK_CONFIRMED {
return false, errors.New("unverified block")
}else if lastBlock != nil && lastBlock.Header.BlockHeight > 0 {
if lastBlock.Header.BlockHash != blk.Header.PreviousHash {
return false, errors.New("the hash values of block and last Block are different")
}
}
BlkVarification, _ := blk.VerifyBlock()
if BlkVarification == false{
return false, errors.New("invalid block")
}
l.DB.AddBlock(blk)
return true, nil
}
func (l *Ledger) CreateGenesisBlock() (*domain.Block, error) {
genesisBlock := domain.CreateNewBlock(nil, "0")
genesisBlock.MakeMerkleTree()
genesisBlock.GenerateBlockHash()
BlkVarification, _ := genesisBlock.VerifyBlock()
if BlkVarification == false{
return nil, errors.New("invalid block")
}
l.DB.AddBlock(genesisBlock)
return genesisBlock, nil
}