-
Notifications
You must be signed in to change notification settings - Fork 93
/
db.go
212 lines (186 loc) · 6.41 KB
/
db.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (C) 2017, 2018, 2019 EGAAS S.A.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
package block
import (
"bytes"
"encoding/json"
"fmt"
"time"
"github.com/AplaProject/go-apla/packages/protocols"
"github.com/AplaProject/go-apla/packages/conf/syspar"
"github.com/AplaProject/go-apla/packages/consts"
"github.com/AplaProject/go-apla/packages/converter"
"github.com/AplaProject/go-apla/packages/crypto"
"github.com/AplaProject/go-apla/packages/model"
"github.com/AplaProject/go-apla/packages/utils"
log "github.com/sirupsen/logrus"
)
// UpdBlockInfo updates info_block table
func UpdBlockInfo(dbTransaction *model.DbTransaction, block *Block) error {
blockID := block.Header.BlockID
// for the local tests
forSha := block.Header.ForSha(block.PrevHeader, block.MrklRoot)
hash, err := crypto.DoubleHash([]byte(forSha))
if err != nil {
log.WithFields(log.Fields{"type": consts.CryptoError, "error": err}).Fatal("double hashing block")
}
block.Header.Hash = hash
if block.Header.BlockID == 1 {
ib := &model.InfoBlock{
Hash: hash,
BlockID: blockID,
Time: block.Header.Time,
EcosystemID: block.Header.EcosystemID,
KeyID: block.Header.KeyID,
NodePosition: converter.Int64ToStr(block.Header.NodePosition),
CurrentVersion: fmt.Sprintf("%d", block.Header.Version),
}
err := ib.Create(dbTransaction)
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("creating info block")
return fmt.Errorf("error insert into info_block %s", err)
}
} else {
ibUpdate := &model.InfoBlock{
Hash: hash,
BlockID: blockID,
Time: block.Header.Time,
EcosystemID: block.Header.EcosystemID,
KeyID: block.Header.KeyID,
NodePosition: converter.Int64ToStr(block.Header.NodePosition),
Sent: 0,
}
if err := ibUpdate.Update(dbTransaction); err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("creating info block")
return fmt.Errorf("error while updating info_block: %s", err)
}
}
return nil
}
func GetRollbacksHash(transaction *model.DbTransaction, blockID int64) ([]byte, error) {
r := &model.RollbackTx{}
list, err := r.GetBlockRollbackTransactions(transaction, blockID)
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
enc := json.NewEncoder(buf)
for _, rtx := range list {
if err = enc.Encode(&rtx); err != nil {
return nil, err
}
}
return crypto.Hash(buf.Bytes())
}
// InsertIntoBlockchain inserts a block into the blockchain
func InsertIntoBlockchain(transaction *model.DbTransaction, block *Block) error {
// for local tests
blockID := block.Header.BlockID
// record into the block chain
bl := &model.Block{}
err := bl.DeleteById(transaction, blockID)
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("deleting block by id")
return err
}
rollbacksHash, err := GetRollbacksHash(transaction, blockID)
if err != nil {
log.WithFields(log.Fields{"type": consts.BlockError, "error": err}).Error("getting rollbacks hash")
return err
}
b := &model.Block{
ID: blockID,
Hash: block.Header.Hash,
Data: block.BinData,
EcosystemID: block.Header.EcosystemID,
KeyID: block.Header.KeyID,
NodePosition: block.Header.NodePosition,
Time: block.Header.Time,
RollbacksHash: rollbacksHash,
Tx: int32(len(block.Transactions)),
}
validBlockTime := true
if blockID > 1 {
exists, err := protocols.NewBlockTimeCounter().BlockForTimeExists(time.Unix(b.Time, 0), int(b.NodePosition))
if err != nil {
log.WithFields(log.Fields{"type": consts.BlockError, "error": err}).Error("block validation")
return err
}
validBlockTime = !exists
}
if validBlockTime {
if err = b.Create(transaction); err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("creating block")
return err
}
if err = model.UpdRollbackHash(transaction, rollbacksHash); err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("updating info block")
return err
}
} else {
err := fmt.Errorf("Invalid block time: %d", block.Header.Time)
log.WithFields(log.Fields{"type": consts.BlockError, "error": err}).Error("invalid block time")
return err
}
return nil
}
// GetBlockDataFromBlockChain is retrieving block data from blockchain
func GetBlockDataFromBlockChain(blockID int64) (*utils.BlockData, error) {
BlockData := new(utils.BlockData)
block := &model.Block{}
_, err := block.Get(blockID)
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("Getting block by ID")
return BlockData, err
}
header, _, err := utils.ParseBlockHeader(bytes.NewBuffer(block.Data))
if err != nil {
return nil, err
}
BlockData = &header
BlockData.Hash = block.Hash
BlockData.RollbacksHash = block.RollbacksHash
return BlockData, nil
}
// GetDataFromFirstBlock returns data of first block
func GetDataFromFirstBlock() (data *consts.FirstBlock, ok bool) {
block := &model.Block{}
isFound, err := block.Get(1)
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("getting record of first block")
return
}
if !isFound {
return
}
pb, err := UnmarshallBlock(bytes.NewBuffer(block.Data), true)
if err != nil {
log.WithFields(log.Fields{"type": consts.ParserError, "error": err}).Error("parsing data of first block")
return
}
if len(pb.Transactions) == 0 {
log.WithFields(log.Fields{"type": consts.ParserError}).Error("list of parsers is empty")
return
}
t := pb.Transactions[0]
data, ok = t.TxPtr.(*consts.FirstBlock)
if !ok {
log.WithFields(log.Fields{"type": consts.ParserError}).Error("getting data of first block")
return
}
syspar.SysUpdate(nil)
return
}