forked from AplaProject/go-apla
-
Notifications
You must be signed in to change notification settings - Fork 35
/
db.go
189 lines (169 loc) · 5.8 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
package block
import (
"bytes"
"encoding/json"
"fmt"
"time"
"github.com/GenesisCommunity/go-genesis/packages/consts"
"github.com/GenesisCommunity/go-genesis/packages/converter"
"github.com/GenesisCommunity/go-genesis/packages/crypto"
"github.com/GenesisCommunity/go-genesis/packages/model"
"github.com/GenesisCommunity/go-genesis/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 := fmt.Sprintf("%d,%x,%s,%d,%d,%d,%d", blockID, block.PrevHeader.Hash, block.MrklRoot,
block.Header.Time, block.Header.EcosystemID, block.Header.KeyID, block.Header.NodePosition)
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
}
// 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
}
rollbackTx := &model.RollbackTx{}
blockRollbackTxs, err := rollbackTx.GetBlockRollbackTransactions(transaction, blockID)
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("getting block rollback txs")
return err
}
buffer := bytes.Buffer{}
for _, rollbackTx := range blockRollbackTxs {
rollbackTxBytes, err := json.Marshal(rollbackTx)
if err != nil {
log.WithFields(log.Fields{"type": consts.JSONMarshallError, "error": err}).Error("marshalling rollback_tx to json")
return err
}
buffer.Write(rollbackTxBytes)
}
rollbackTxsHash, err := crypto.Hash(buffer.Bytes())
if err != nil {
log.WithFields(log.Fields{"type": consts.CryptoError, "error": err}).Error("hashing block rollback_txs")
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: rollbackTxsHash,
Tx: int32(len(block.Transactions)),
}
blockTimeCalculator, err := utils.BuildBlockTimeCalculator(nil)
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("creating block")
return err
}
validBlockTime := true
if blockID > 1 {
validBlockTime, err = blockTimeCalculator.ValidateBlock(b.NodePosition, time.Unix(b.Time, 0))
if err != nil {
log.WithFields(log.Fields{"type": consts.BlockError, "error": err}).Error("block validation")
return err
}
}
if validBlockTime {
err = b.Create(transaction)
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("creating 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, utils.ErrInfo(err)
}
header, err := utils.ParseBlockHeader(bytes.NewBuffer(block.Data), false)
if err != nil {
return nil, utils.ErrInfo(err)
}
BlockData = &header
BlockData.Hash = block.Hash
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
}
return
}