-
Notifications
You must be signed in to change notification settings - Fork 6
/
task.go
217 lines (175 loc) · 5.8 KB
/
task.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
215
216
217
/**
* @file
* @copyright defined in scdo/LICENSE
*/
package miner
import (
"math/big"
"time"
"github.com/scdoproject/go-scdo/common"
"github.com/scdoproject/go-scdo/common/memory"
"github.com/scdoproject/go-scdo/consensus"
"github.com/scdoproject/go-scdo/core"
"github.com/scdoproject/go-scdo/core/state"
"github.com/scdoproject/go-scdo/core/txs"
"github.com/scdoproject/go-scdo/core/types"
"github.com/scdoproject/go-scdo/database"
"github.com/scdoproject/go-scdo/log"
)
// Task is a mining work for engine, containing block header, transactions, and transaction receipts.
type Task struct {
header *types.BlockHeader
txs []*types.Transaction
receipts []*types.Receipt
debts []*types.Debt
coinbase common.Address
debtVerifier types.DebtVerifier
}
// NewTask return Task object
func NewTask(header *types.BlockHeader, coinbase common.Address, verifier types.DebtVerifier) *Task {
return &Task{
header: header,
coinbase: coinbase,
debtVerifier: verifier,
}
}
// applyTransactionsAndDebts TODO need to check more about the transactions, such as gas limit
func (task *Task) applyTransactionsAndDebts(scdo ScdoBackend, statedb *state.Statedb, accountStateDB database.Database, log *log.ScdoLog) error {
now := time.Now()
// entrance
memory.Print(log, "task applyTransactionsAndDebts entrance", now, false)
// choose debts from the pool
size := task.chooseDebts(scdo, statedb, log)
// the reward tx will always be at the first of the block's transactions
reward, err := task.handleMinerRewardTx(statedb)
if err != nil {
return err
}
// choose txs from the pool
task.chooseTransactions(scdo, statedb, log, size)
log.Info("mining block height:%d, reward:%s, transaction number:%d, debt number: %d",
task.header.Height, reward, len(task.txs), len(task.debts))
batch := accountStateDB.NewBatch()
root, err := statedb.Commit(batch)
if err != nil {
return err
}
task.header.StateHash = root
// exit
memory.Print(log, "task applyTransactionsAndDebts exit", now, true)
return nil
}
// chooseDebts choose debts from the debt pool
func (task *Task) chooseDebts(scdo ScdoBackend, statedb *state.Statedb, log *log.ScdoLog) int {
now := time.Now()
// entrance
memory.Print(log, "task chooseDebts entrance", now, false)
size := core.BlockByteLimit
for size > 0 {
debts, _ := scdo.DebtPool().GetProcessableDebts(size)
if len(debts) == 0 {
break
}
canonicalHeadBlock := scdo.BlockChain().CurrentBlock()
preHeader, err := scdo.BlockChain().GetStore().GetBlockHeader(task.header.PreviousBlockHash)
if err != nil {
return size
}
commonAncestor, err := scdo.BlockChain().FindCommonForkAncestor(preHeader, canonicalHeadBlock.Header)
if err != nil {
return size
}
for _, d := range debts {
log.Debug("debt hash: %v", d.Hash)
err := scdo.BlockChain().ApplyDebtWithoutVerify(statedb, d, task.coinbase, preHeader, commonAncestor)
if err != nil {
log.Debug("apply debt error %s", err)
scdo.DebtPool().RemoveDebtByHash(d.Hash)
continue
}
size = size - d.Size()
task.debts = append(task.debts, d)
}
}
// exit
memory.Print(log, "task chooseDebts exit", now, true)
return size
}
// handleMinerRewardTx handles the miner reward transaction.
func (task *Task) handleMinerRewardTx(statedb *state.Statedb) (*big.Int, error) {
reward := consensus.GetReward(task.header.Height)
rewardTx, err := txs.NewRewardTx(task.coinbase, reward, task.header.CreateTimestamp.Uint64())
if err != nil {
return nil, err
}
rewardTxReceipt, err := txs.ApplyRewardTx(rewardTx, statedb)
if err != nil {
return nil, err
}
task.txs = append(task.txs, rewardTx)
// add the receipt of the reward tx
task.receipts = append(task.receipts, rewardTxReceipt)
return reward, nil
}
// chooseTransactions choose transactions from the txpool
func (task *Task) chooseTransactions(scdo ScdoBackend, statedb *state.Statedb, log *log.ScdoLog, size int) {
now := time.Now()
// entrance
memory.Print(log, "task chooseTransactions entrance", now, false)
txIndex := 1 // the first tx is miner reward
for size > 0 {
txs, txsSize := scdo.TxPool().GetProcessableTransactions(size)
if len(txs) == 0 {
break
}
for _, tx := range txs {
if err := tx.Validate(statedb, task.header.Height); err != nil {
scdo.TxPool().RemoveTransaction(tx.Hash)
log.Error("failed to validate tx %s, for %s", tx.Hash.Hex(), err)
txsSize = txsSize - tx.Size()
continue
}
receipt, err := scdo.BlockChain().ApplyTransaction(tx, txIndex, task.coinbase, statedb, task.header)
if err != nil {
scdo.TxPool().RemoveTransaction(tx.Hash)
log.Error("failed to apply tx %s, %s", tx.Hash.Hex(), err)
txsSize = txsSize - tx.Size()
continue
}
task.txs = append(task.txs, tx)
task.receipts = append(task.receipts, receipt)
txIndex++
}
size -= txsSize
}
// exit
memory.Print(log, "task chooseTransactions exit", now, true)
}
// generateBlock builds a block from task
func (task *Task) generateBlock() *types.Block {
return types.NewBlock(task.header, task.txs, task.receipts, task.debts)
}
// Result is the result mined by engine. It contains the raw task and mined block.
type Result struct {
task *Task
block *types.Block // mined block, with good nonce
}
// PrintableOutputTask returns the printable format of task
func PrintableOutputTask(task *Task) map[string]interface{} {
result := map[string]interface{}{
"header": task.header,
"txs": task.txs,
"receipts": task.receipts,
"debts": task.debts,
"coinbase": task.coinbase,
}
return result
}
// PrintableOutputTask returns the printable format of the task header
func PrintableOutputTaskHeader(header *types.BlockHeader, totalDifficulty *big.Int) map[string]interface{} {
result := map[string]interface{}{
"header": header,
"totalDifficulty": totalDifficulty,
}
return result
}