-
Notifications
You must be signed in to change notification settings - Fork 113
/
provider.go
66 lines (58 loc) · 1.24 KB
/
provider.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
package verifier
import (
"github.com/iost-official/go-iost/core/tx"
"github.com/iost-official/go-iost/core/txpool"
)
// ProviderImpl impl of provider
type ProviderImpl struct {
cache []*tx.Tx
pool *txpool.SortedTxMap
iter *txpool.Iterator
droplist map[*tx.Tx]error
}
// NewProvider ...
func NewProvider(pool *txpool.SortedTxMap) *ProviderImpl {
return &ProviderImpl{
cache: make([]*tx.Tx, 0),
droplist: make(map[*tx.Tx]error),
pool: pool,
iter: pool.Iter(),
}
}
// Tx get next tx
func (p *ProviderImpl) Tx() *tx.Tx {
if len(p.cache) != 0 {
t := p.cache[len(p.cache)-1]
p.cache = p.cache[:len(p.cache)-1]
return t
}
t, ok := p.iter.Next()
if !ok {
return nil
}
return t
}
// Return send tx to pool
func (p *ProviderImpl) Return(t *tx.Tx) {
p.cache = append(p.cache, t)
}
// Drop drop bad tx
func (p *ProviderImpl) Drop(t *tx.Tx, err error) {
p.droplist[t] = err
}
// List list tx and errors of drop txs
func (p *ProviderImpl) List() (a []*tx.Tx, b []error) {
a = make([]*tx.Tx, 0)
b = make([]error, 0)
for k, v := range p.droplist {
a = append(a, k)
b = append(b, v)
}
return
}
// Close drop bad tx here
func (p *ProviderImpl) Close() {
for t := range p.droplist {
p.pool.Del(t.Hash())
}
}