OSDN Git Service

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