forked from AplaProject/go-apla
-
Notifications
You must be signed in to change notification settings - Fork 35
/
block_generator.go
214 lines (182 loc) · 6.56 KB
/
block_generator.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
213
214
// MIT License
//
// Copyright (c) 2016 GenesisCommunity
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package daemons
import (
"bytes"
"context"
"time"
"github.com/GenesisCommunity/go-genesis/packages/block"
"github.com/GenesisCommunity/go-genesis/packages/conf"
"github.com/GenesisCommunity/go-genesis/packages/notificator"
"github.com/GenesisCommunity/go-genesis/packages/service"
"github.com/GenesisCommunity/go-genesis/packages/transaction"
"github.com/GenesisCommunity/go-genesis/packages/conf/syspar"
"github.com/GenesisCommunity/go-genesis/packages/consts"
"github.com/GenesisCommunity/go-genesis/packages/model"
"github.com/GenesisCommunity/go-genesis/packages/utils"
log "github.com/sirupsen/logrus"
)
// BlockGenerator is daemon that generates blocks
func BlockGenerator(ctx context.Context, d *daemon) error {
d.sleepTime = time.Second
if service.IsNodePaused() {
return nil
}
nodePosition, err := syspar.GetNodePositionByKeyID(conf.Config.KeyID)
if err != nil {
// we are not full node and can't generate new blocks
d.sleepTime = 10 * time.Second
d.logger.WithFields(log.Fields{"type": consts.JustWaiting, "error": err}).Debug("we are not full node, sleep for 10 seconds")
return nil
}
QueueParserBlocks(ctx, d)
DBLock()
defer DBUnlock()
// wee need fresh myNodePosition after locking
nodePosition, err = syspar.GetNodePositionByKeyID(conf.Config.KeyID)
if err != nil {
d.logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("getting node position by key id")
return err
}
blockTimeCalculator, err := utils.BuildBlockTimeCalculator(nil)
if err != nil {
d.logger.WithFields(log.Fields{"type": consts.BlockError, "error": err}).Error("building block time calculator")
return err
}
timeToGenerate, err := blockTimeCalculator.SetClock(&utils.ClockWrapper{}).TimeToGenerate(nodePosition)
if err != nil {
d.logger.WithFields(log.Fields{"type": consts.BlockError, "error": err}).Error("calculating block time")
return err
}
if !timeToGenerate {
d.logger.WithFields(log.Fields{"type": consts.JustWaiting}).Debug("not my generation time")
return nil
}
prevBlock := &model.InfoBlock{}
_, err = prevBlock.Get()
if err != nil {
d.logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("getting previous block")
return err
}
NodePrivateKey, NodePublicKey, err := utils.GetNodeKeys()
if err != nil || len(NodePrivateKey) < 1 {
if err == nil {
d.logger.WithFields(log.Fields{"type": consts.EmptyObject}).Error("node private key is empty")
}
return err
}
dtx := DelayedTx{
privateKey: NodePrivateKey,
publicKey: NodePublicKey,
logger: d.logger,
}
dtx.RunForBlockID(prevBlock.BlockID + 1)
trs, err := processTransactions(d.logger)
if err != nil {
return err
}
// Block generation will be started only if we have transactions
if len(trs) == 0 {
return nil
}
header := &utils.BlockData{
BlockID: prevBlock.BlockID + 1,
Time: time.Now().Unix(),
EcosystemID: 0,
KeyID: conf.Config.KeyID,
NodePosition: nodePosition,
Version: consts.BLOCK_VERSION,
}
timeToGenerate, err = blockTimeCalculator.SetClock(&utils.ClockWrapper{}).TimeToGenerate(nodePosition)
if err != nil {
d.logger.WithFields(log.Fields{"type": consts.BlockError, "error": err}).Error("calculating block time")
return err
}
if !timeToGenerate {
d.logger.WithFields(log.Fields{"type": consts.JustWaiting}).Debug("not my generation time")
return nil
}
blockBin, err := generateNextBlock(header, trs, NodePrivateKey, prevBlock.Hash)
if err != nil {
return err
}
err = block.InsertBlockWOForks(blockBin, true, false)
if err != nil {
return err
}
log.WithFields(log.Fields{"Block": header.String(), "type": consts.SyncProcess}).Error("Generated block ID")
go notificator.CheckTokenMovementLimits(nil, conf.Config.TokenMovement, header.BlockID)
return nil
}
func generateNextBlock(blockHeader *utils.BlockData, trs []*model.Transaction, key string, prevBlockHash []byte) ([]byte, error) {
trData := make([][]byte, 0, len(trs))
for _, tr := range trs {
trData = append(trData, tr.Data)
}
return block.MarshallBlock(blockHeader, trData, prevBlockHash, key)
}
func processTransactions(logger *log.Entry) ([]*model.Transaction, error) {
p := new(transaction.Transaction)
// verify transactions
err := transaction.ProcessTransactionsQueue(p.DbTransaction)
if err != nil {
return nil, err
}
trs, err := model.GetAllUnusedTransactions(syspar.GetMaxTxCount())
if err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("getting all unused transactions")
return nil, err
}
limits := block.NewLimits(nil)
// Checks preprocessing count limits
txList := make([]*model.Transaction, 0, len(trs))
for i, txItem := range trs {
bufTransaction := bytes.NewBuffer(txItem.Data)
p, err := transaction.UnmarshallTransaction(bufTransaction)
if err != nil {
if p != nil {
transaction.MarkTransactionBad(p.DbTransaction, p.TxHash, err.Error())
}
continue
}
if err := p.Check(time.Now().Unix(), false); err != nil {
transaction.MarkTransactionBad(p.DbTransaction, p.TxHash, err.Error())
continue
}
if p.TxSmart != nil {
err = limits.CheckLimit(p)
if err == block.ErrLimitStop && i > 0 {
model.IncrementTxAttemptCount(nil, p.TxHash)
break
} else if err != nil {
if err == block.ErrLimitSkip {
model.IncrementTxAttemptCount(nil, p.TxHash)
} else {
transaction.MarkTransactionBad(p.DbTransaction, p.TxHash, err.Error())
}
continue
}
}
txList = append(txList, trs[i])
}
return txList, nil
}