/
pending_cache.go
168 lines (145 loc) · 3.46 KB
/
pending_cache.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
package onroad
import (
"container/list"
"sync"
"github.com/vitelabs/go-vite/v2/common/types"
ledger "github.com/vitelabs/go-vite/v2/interfaces/core"
)
const defaultPullCount uint8 = 5
type callerPendingMap struct {
pmap map[types.Address]*list.List
inferiorList map[types.Address]inferiorState
addrMutex sync.RWMutex
}
func newCallerPendingMap() *callerPendingMap {
return &callerPendingMap{
pmap: make(map[types.Address]*list.List),
inferiorList: make(map[types.Address]inferiorState, 0),
}
}
func (p *callerPendingMap) isPendingMapNotSufficient() bool {
p.addrMutex.RLock()
defer p.addrMutex.RUnlock()
if p.pmap == nil {
return true
}
count := 0
for _, list := range p.pmap {
count += list.Len()
}
if count < int(defaultPullCount) {
return true
}
return false
}
func (p *callerPendingMap) Len() int {
p.addrMutex.RLock()
defer p.addrMutex.RUnlock()
count := 0
for _, list := range p.pmap {
count += list.Len()
}
return count
}
func (p *callerPendingMap) getOnePending() *ledger.AccountBlock {
p.addrMutex.Lock()
defer p.addrMutex.Unlock()
for _, list := range p.pmap {
if ele := list.Front(); ele != nil {
front := ele.Value.(*ledger.AccountBlock)
list.Remove(ele)
return front
}
}
return nil
}
func (p *callerPendingMap) addPendingMap(sendBlock *ledger.AccountBlock) (isExist bool) {
p.addrMutex.Lock()
defer p.addrMutex.Unlock()
caller := sendBlock.AccountAddress
value, ok := p.pmap[caller]
if !ok || value == nil {
l := list.New()
l.PushBack(sendBlock)
p.pmap[caller] = l
return false
}
l := p.pmap[caller]
var ele *list.Element
if ele = l.Back(); ele != nil {
pre := ele.Value.(*ledger.AccountBlock)
if pre.Height == sendBlock.Height {
return true
}
if pre.Height < sendBlock.Height {
l.PushBack(sendBlock)
return false
}
}
l.Init()
l.PushBack(sendBlock)
return false
}
func (p *callerPendingMap) clearPendingMap() {
p.addrMutex.Lock()
defer p.addrMutex.Unlock()
p.pmap = make(map[types.Address]*list.List)
}
func (p *callerPendingMap) addIntoInferiorList(caller types.Address, state inferiorState) {
p.addrMutex.Lock()
defer p.addrMutex.Unlock()
p.inferiorList[caller] = state
delete(p.pmap, caller)
}
func (p *callerPendingMap) releaseCallerByState(state inferiorState) int {
var count int
p.addrMutex.Lock()
defer p.addrMutex.Unlock()
for caller, s := range p.inferiorList {
if s == state {
delete(p.inferiorList, caller)
count++
}
}
return count
}
func (p *callerPendingMap) lenOfCallersByState(state inferiorState) int {
var count int
p.addrMutex.RLock()
defer p.addrMutex.RUnlock()
for _, s := range p.inferiorList {
if s == state {
count++
}
}
return count
}
func (p *callerPendingMap) existInInferiorList(caller types.Address) bool {
p.addrMutex.RLock()
defer p.addrMutex.RUnlock()
if _, ok := p.inferiorList[caller]; ok {
return true
}
return false
}
func (p *callerPendingMap) removeFromInferiorList(caller types.Address) {
p.addrMutex.Lock()
defer p.addrMutex.Unlock()
delete(p.inferiorList, caller)
}
func (p *callerPendingMap) isInferiorStateRetry(caller types.Address) bool {
p.addrMutex.RLock()
defer p.addrMutex.RUnlock()
if state, ok := p.inferiorList[caller]; ok && state == RETRY {
return true
}
return false
}
func (p *callerPendingMap) isInferiorStateOut(caller types.Address) bool {
p.addrMutex.RLock()
defer p.addrMutex.RUnlock()
if state, ok := p.inferiorList[caller]; ok && state == OUT {
return true
}
return false
}