OSDN Git Service

Dpos process block (#69)
[bytom/vapor.git] / protocol / txpool.go
1 package protocol
2
3 import (
4         "errors"
5         "sync"
6         "sync/atomic"
7         "time"
8
9         "github.com/golang/groupcache/lru"
10         log "github.com/sirupsen/logrus"
11
12         "github.com/vapor/consensus"
13         "github.com/vapor/event"
14         "github.com/vapor/protocol/bc"
15         "github.com/vapor/protocol/bc/types"
16         "github.com/vapor/protocol/state"
17 )
18
19 // msg type
20 const (
21         MsgNewTx = iota
22         MsgRemoveTx
23         logModule = "protocol"
24 )
25
26 var (
27         maxCachedErrTxs = 1000
28         maxMsgChSize    = 1000
29         maxNewTxNum     = 10000
30         maxOrphanNum    = 2000
31
32         orphanTTL                = 10 * time.Minute
33         orphanExpireScanInterval = 3 * time.Minute
34
35         // ErrTransactionNotExist is the pre-defined error message
36         ErrTransactionNotExist = errors.New("transaction are not existed in the mempool")
37         // ErrPoolIsFull indicates the pool is full
38         ErrPoolIsFull = errors.New("transaction pool reach the max number")
39         // ErrDustTx indicates transaction is dust tx
40         ErrDustTx = errors.New("transaction is dust tx")
41 )
42
43 type TxMsgEvent struct{ TxMsg *TxPoolMsg }
44
45 // TxDesc store tx and related info for mining strategy
46 type TxDesc struct {
47         Tx         *types.Tx `json:"transaction"`
48         Added      time.Time `json:"-"`
49         StatusFail bool      `json:"status_fail"`
50         Height     uint64    `json:"-"`
51         Weight     uint64    `json:"-"`
52         Fee        uint64    `json:"-"`
53 }
54
55 // TxPoolMsg is use for notify pool changes
56 type TxPoolMsg struct {
57         *TxDesc
58         MsgType int
59 }
60
61 type orphanTx struct {
62         *TxDesc
63         expiration time.Time
64 }
65
66 // TxPool is use for store the unconfirmed transaction
67 type TxPool struct {
68         lastUpdated     int64
69         mtx             sync.RWMutex
70         store           Store
71         pool            map[bc.Hash]*TxDesc
72         utxo            map[bc.Hash]*types.Tx
73         orphans         map[bc.Hash]*orphanTx
74         orphansByPrev   map[bc.Hash]map[bc.Hash]*orphanTx
75         errCache        *lru.Cache
76         eventDispatcher *event.Dispatcher
77 }
78
79 // NewTxPool init a new TxPool
80 func NewTxPool(store Store, dispatcher *event.Dispatcher) *TxPool {
81         tp := &TxPool{
82                 lastUpdated:     time.Now().Unix(),
83                 store:           store,
84                 pool:            make(map[bc.Hash]*TxDesc),
85                 utxo:            make(map[bc.Hash]*types.Tx),
86                 orphans:         make(map[bc.Hash]*orphanTx),
87                 orphansByPrev:   make(map[bc.Hash]map[bc.Hash]*orphanTx),
88                 errCache:        lru.New(maxCachedErrTxs),
89                 eventDispatcher: dispatcher,
90         }
91         go tp.orphanExpireWorker()
92         return tp
93 }
94
95 // AddErrCache add a failed transaction record to lru cache
96 func (tp *TxPool) AddErrCache(txHash *bc.Hash, err error) {
97         tp.mtx.Lock()
98         defer tp.mtx.Unlock()
99
100         tp.errCache.Add(txHash, err)
101 }
102
103 // ExpireOrphan expire all the orphans that before the input time range
104 func (tp *TxPool) ExpireOrphan(now time.Time) {
105         tp.mtx.Lock()
106         defer tp.mtx.Unlock()
107
108         for hash, orphan := range tp.orphans {
109                 if orphan.expiration.Before(now) {
110                         tp.removeOrphan(&hash)
111                 }
112         }
113 }
114
115 // GetErrCache return the error of the transaction
116 func (tp *TxPool) GetErrCache(txHash *bc.Hash) error {
117         tp.mtx.Lock()
118         defer tp.mtx.Unlock()
119
120         v, ok := tp.errCache.Get(txHash)
121         if !ok {
122                 return nil
123         }
124         return v.(error)
125 }
126
127 // RemoveTransaction remove a transaction from the pool
128 func (tp *TxPool) RemoveTransaction(txHash *bc.Hash) {
129         tp.mtx.Lock()
130         defer tp.mtx.Unlock()
131
132         txD, ok := tp.pool[*txHash]
133         if !ok {
134                 return
135         }
136
137         for _, output := range txD.Tx.ResultIds {
138                 delete(tp.utxo, *output)
139         }
140         delete(tp.pool, *txHash)
141
142         atomic.StoreInt64(&tp.lastUpdated, time.Now().Unix())
143         tp.eventDispatcher.Post(TxMsgEvent{TxMsg: &TxPoolMsg{TxDesc: txD, MsgType: MsgRemoveTx}})
144         log.WithFields(log.Fields{"module": logModule, "tx_id": txHash}).Debug("remove tx from mempool")
145 }
146
147 // GetTransaction return the TxDesc by hash
148 func (tp *TxPool) GetTransaction(txHash *bc.Hash) (*TxDesc, error) {
149         tp.mtx.RLock()
150         defer tp.mtx.RUnlock()
151
152         if txD, ok := tp.pool[*txHash]; ok {
153                 return txD, nil
154         }
155         return nil, ErrTransactionNotExist
156 }
157
158 // GetTransactions return all the transactions in the pool
159 func (tp *TxPool) GetTransactions() []*TxDesc {
160         tp.mtx.RLock()
161         defer tp.mtx.RUnlock()
162
163         txDs := make([]*TxDesc, len(tp.pool))
164         i := 0
165         for _, desc := range tp.pool {
166                 txDs[i] = desc
167                 i++
168         }
169         return txDs
170 }
171
172 // IsTransactionInPool check wheather a transaction in pool or not
173 func (tp *TxPool) IsTransactionInPool(txHash *bc.Hash) bool {
174         tp.mtx.RLock()
175         defer tp.mtx.RUnlock()
176
177         _, ok := tp.pool[*txHash]
178         return ok
179 }
180
181 // IsTransactionInErrCache check wheather a transaction in errCache or not
182 func (tp *TxPool) IsTransactionInErrCache(txHash *bc.Hash) bool {
183         tp.mtx.RLock()
184         defer tp.mtx.RUnlock()
185
186         _, ok := tp.errCache.Get(txHash)
187         return ok
188 }
189
190 // HaveTransaction IsTransactionInErrCache check is  transaction in errCache or pool
191 func (tp *TxPool) HaveTransaction(txHash *bc.Hash) bool {
192         return tp.IsTransactionInPool(txHash) || tp.IsTransactionInErrCache(txHash)
193 }
194
195 func isTransactionNoBtmInput(tx *types.Tx) bool {
196         for _, input := range tx.TxData.Inputs {
197                 if input.AssetID() == *consensus.BTMAssetID {
198                         return false
199                 }
200         }
201         return true
202 }
203
204 func isTransactionZeroOutput(tx *types.Tx) bool {
205         for _, output := range tx.TxData.Outputs {
206                 value := output.AssetAmount()
207                 if value.Amount == uint64(0) {
208                         return true
209                 }
210         }
211         return false
212 }
213
214 func (tp *TxPool) IsDust(tx *types.Tx) bool {
215         return isTransactionNoBtmInput(tx) || isTransactionZeroOutput(tx)
216 }
217
218 func (tp *TxPool) processTransaction(tx *types.Tx, statusFail bool, height, fee uint64) (bool, error) {
219         tp.mtx.Lock()
220         defer tp.mtx.Unlock()
221
222         txD := &TxDesc{
223                 Tx:         tx,
224                 StatusFail: statusFail,
225                 Weight:     tx.SerializedSize,
226                 Height:     height,
227                 Fee:        fee,
228         }
229         requireParents, err := tp.checkOrphanUtxos(tx)
230         if err != nil {
231                 return false, err
232         }
233
234         if len(requireParents) > 0 {
235                 return true, tp.addOrphan(txD, requireParents)
236         }
237
238         if err := tp.addTransaction(txD); err != nil {
239                 return false, err
240         }
241
242         tp.processOrphans(txD)
243         return false, nil
244 }
245
246 // ProcessTransaction is the main entry for txpool handle new tx, ignore dust tx.
247 func (tp *TxPool) ProcessTransaction(tx *types.Tx, statusFail bool, height, fee uint64) (bool, error) {
248         if tp.IsDust(tx) {
249                 log.WithFields(log.Fields{"module": logModule, "tx_id": tx.ID.String()}).Warn("dust tx")
250                 return false, nil
251         }
252         return tp.processTransaction(tx, statusFail, height, fee)
253 }
254
255 func (tp *TxPool) addOrphan(txD *TxDesc, requireParents []*bc.Hash) error {
256         if len(tp.orphans) >= maxOrphanNum {
257                 return ErrPoolIsFull
258         }
259
260         orphan := &orphanTx{txD, time.Now().Add(orphanTTL)}
261         tp.orphans[txD.Tx.ID] = orphan
262         for _, hash := range requireParents {
263                 if _, ok := tp.orphansByPrev[*hash]; !ok {
264                         tp.orphansByPrev[*hash] = make(map[bc.Hash]*orphanTx)
265                 }
266                 tp.orphansByPrev[*hash][txD.Tx.ID] = orphan
267         }
268         return nil
269 }
270
271 func (tp *TxPool) addTransaction(txD *TxDesc) error {
272         if len(tp.pool) >= maxNewTxNum {
273                 return ErrPoolIsFull
274         }
275
276         tx := txD.Tx
277         txD.Added = time.Now()
278         tp.pool[tx.ID] = txD
279         for _, id := range tx.ResultIds {
280                 var assetID bc.AssetID
281                 outputEntry, err := tx.Entry(*id)
282                 if err != nil {
283                         return err
284                 }
285                 switch output := outputEntry.(type) {
286                 case *bc.IntraChainOutput:
287                         assetID = *output.Source.Value.AssetId
288                 case *bc.VoteOutput:
289                         assetID = *output.Source.Value.AssetId
290                 default:
291                         continue
292                 }
293
294                 if !txD.StatusFail || assetID == *consensus.BTMAssetID {
295                         tp.utxo[*id] = tx
296                 }
297         }
298
299         atomic.StoreInt64(&tp.lastUpdated, time.Now().Unix())
300         tp.eventDispatcher.Post(TxMsgEvent{TxMsg: &TxPoolMsg{TxDesc: txD, MsgType: MsgNewTx}})
301         log.WithFields(log.Fields{"module": logModule, "tx_id": tx.ID.String()}).Debug("Add tx to mempool")
302         return nil
303 }
304
305 func (tp *TxPool) checkOrphanUtxos(tx *types.Tx) ([]*bc.Hash, error) {
306         view := state.NewUtxoViewpoint()
307         if err := tp.store.GetTransactionsUtxo(view, []*bc.Tx{tx.Tx}); err != nil {
308                 return nil, err
309         }
310
311         hashes := []*bc.Hash{}
312         for _, hash := range tx.SpentOutputIDs {
313                 if !view.CanSpend(&hash) && tp.utxo[hash] == nil {
314                         hashes = append(hashes, &hash)
315                 }
316         }
317         return hashes, nil
318 }
319
320 func (tp *TxPool) orphanExpireWorker() {
321         ticker := time.NewTicker(orphanExpireScanInterval)
322         defer ticker.Stop()
323
324         for now := range ticker.C {
325                 tp.ExpireOrphan(now)
326         }
327 }
328
329 func (tp *TxPool) processOrphans(txD *TxDesc) {
330         processOrphans := []*orphanTx{}
331         addRely := func(tx *types.Tx) {
332                 for _, outHash := range tx.ResultIds {
333                         orphans, ok := tp.orphansByPrev[*outHash]
334                         if !ok {
335                                 continue
336                         }
337
338                         for _, orphan := range orphans {
339                                 processOrphans = append(processOrphans, orphan)
340                         }
341                         delete(tp.orphansByPrev, *outHash)
342                 }
343         }
344
345         addRely(txD.Tx)
346         for ; len(processOrphans) > 0; processOrphans = processOrphans[1:] {
347                 processOrphan := processOrphans[0]
348                 requireParents, err := tp.checkOrphanUtxos(processOrphan.Tx)
349                 if err != nil {
350                         log.WithFields(log.Fields{"module": logModule, "err": err}).Error("processOrphans got unexpect error")
351                         continue
352                 }
353
354                 if len(requireParents) == 0 {
355                         addRely(processOrphan.Tx)
356                         tp.removeOrphan(&processOrphan.Tx.ID)
357                         tp.addTransaction(processOrphan.TxDesc)
358                 }
359         }
360 }
361
362 func (tp *TxPool) removeOrphan(hash *bc.Hash) {
363         orphan, ok := tp.orphans[*hash]
364         if !ok {
365                 return
366         }
367
368         for _, spend := range orphan.Tx.SpentOutputIDs {
369                 orphans, ok := tp.orphansByPrev[spend]
370                 if !ok {
371                         continue
372                 }
373
374                 if delete(orphans, *hash); len(orphans) == 0 {
375                         delete(tp.orphansByPrev, spend)
376                 }
377         }
378         delete(tp.orphans, *hash)
379 }